summaryrefslogtreecommitdiff
path: root/test/test_template.py
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/test_template.py
parentf33e3cf9ec62456dae60703a373057fe3221b956 (diff)
downloadmako-c8598ab628a0da0d55857196627753dad9849a39.tar.gz
- 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/test_template.py')
-rw-r--r--test/test_template.py50
1 files changed, 48 insertions, 2 deletions
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("""