aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-11-09 19:44:52 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2010-11-09 19:44:52 -0500
commitc8598ab628a0da0d55857196627753dad9849a39 (patch)
tree70530c81a36bc5011602c92be4dcd86380d7bb77 /test
parentf33e3cf9ec62456dae60703a373057fe3221b956 (diff)
downloadexternal_python_mako-c8598ab628a0da0d55857196627753dad9849a39.tar.gz
external_python_mako-c8598ab628a0da0d55857196627753dad9849a39.tar.bz2
external_python_mako-c8598ab628a0da0d55857196627753dad9849a39.zip
- New flag on Template, TemplateLookup -
strict_undefined=True, will cause variables not found in the context to raise a NameError immediately, instead of defaulting to the UNDEFINED value.
Diffstat (limited to 'test')
-rw-r--r--test/__init__.py9
-rw-r--r--test/test_template.py50
2 files changed, 57 insertions, 2 deletions
diff --git a/test/__init__.py b/test/__init__.py
index 77eae86..3280216 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -57,6 +57,15 @@ def teardown():
import shutil
shutil.rmtree(module_base, True)
+def assert_raises(except_cls, callable_, *args, **kw):
+ try:
+ callable_(*args, **kw)
+ success = False
+ except except_cls, e:
+ success = True
+
+ # assert outside the block so it works for AssertionError too !
+ assert success, "Callable did not raise an exception"
def skip_if(predicate, reason=None):
"""Skip a test if predicate is true."""
diff --git a/test/test_template.py b/test/test_template.py
index b713f36..ddf746a 100644
--- a/test/test_template.py
+++ b/test/test_template.py
@@ -7,8 +7,7 @@ from mako import exceptions, util
import re, os
from util import flatten_result, result_lines
import codecs
-
-from test import TemplateTest, eq_, template_base, module_base, skip_if
+from test import TemplateTest, eq_, template_base, module_base, skip_if, assert_raises
class EncodingTest(TemplateTest):
def test_unicode(self):
@@ -544,7 +543,54 @@ class IncludeTest(TemplateTest):
</%b:bar>
""")
assert flatten_result(lookup.get_template("c").render()) == "bar: calling bar this is a"
+
+class UndefinedVarsTest(TemplateTest):
+ def test_undefined(self):
+ t = Template("""
+ % if x is UNDEFINED:
+ undefined
+ % else:
+ x: ${x}
+ % endif
+ """)
+
+ assert result_lines(t.render(x=12)) == ["x: 12"]
+ assert result_lines(t.render(y=12)) == ["undefined"]
+
+ def test_strict(self):
+ t = Template("""
+ % if x is UNDEFINED:
+ undefined
+ % else:
+ x: ${x}
+ % endif
+ """, strict_undefined=True)
+ assert result_lines(t.render(x=12)) == ['x: 12']
+
+ assert_raises(
+ NameError,
+ t.render, y=12
+ )
+
+ l = TemplateLookup(strict_undefined=True)
+ l.put_string("a", "some template")
+ l.put_string("b", """
+ <%namespace name='a' file='a' import='*'/>
+ % if x is UNDEFINED:
+ undefined
+ % else:
+ x: ${x}
+ % endif
+ """)
+
+ assert result_lines(t.render(x=12)) == ['x: 12']
+
+ assert_raises(
+ NameError,
+ t.render, y=12
+ )
+
class ControlTest(TemplateTest):
def test_control(self):
t = Template("""