diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-10 12:09:30 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-10 12:09:30 -0500 |
commit | ca21a5353a4c88f3477a53f2f45cd9ca280b0479 (patch) | |
tree | 45649d02b5b83ce56ff1a6ad702c955823ebbc6b /test/test_template.py | |
parent | c8598ab628a0da0d55857196627753dad9849a39 (diff) | |
download | mako-ca21a5353a4c88f3477a53f2f45cd9ca280b0479.tar.gz |
- The range of Python identifiers that
are considered "undefined", meaning they
are pulled from the context, has been
trimmed back to not include variables
declared inside of expressions (i.e. from
list comprehensions), as well as
in the argument list of lambdas. This
to better support the strict_undefined
feature.
Diffstat (limited to 'test/test_template.py')
-rw-r--r-- | test/test_template.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test/test_template.py b/test/test_template.py index ddf746a..dbfd068 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -591,6 +591,104 @@ class UndefinedVarsTest(TemplateTest): t.render, y=12 ) + def test_expression_declared(self): + t = Template(""" + ${",".join([t for t in ("a", "b", "c")])} + """, strict_undefined=True) + + eq_(result_lines(t.render()), ['a,b,c']) + + t = Template(""" + <%self:foo value="${[(val, n) for val, n in [(1, 2)]]}"/> + + <%def name="foo(value)"> + ${value} + </%def> + + """, strict_undefined=True) + + eq_(result_lines(t.render()), ['[(1, 2)]']) + + t = Template(""" + <%call expr="foo(value=[(val, n) for val, n in [(1, 2)]])" /> + + <%def name="foo(value)"> + ${value} + </%def> + + """, strict_undefined=True) + + eq_(result_lines(t.render()), ['[(1, 2)]']) + + l = TemplateLookup(strict_undefined=True) + l.put_string("i", "hi, ${pageargs['y']}") + l.put_string("t", """ + <%include file="i" args="y=[x for x in range(3)]" /> + """) + eq_( + result_lines(l.get_template("t").render()), ['hi, [0, 1, 2]'] + ) + + l.put_string('q', """ + <%namespace name="i" file="${(str([x for x in range(3)][2]) + 'i')[-1]}" /> + ${i.body(y='x')} + """) + eq_( + result_lines(l.get_template("q").render()), ['hi, x'] + ) + + t = Template(""" + <% + y = lambda q: str(q) + %> + ${y('hi')} + """, strict_undefined=True) + eq_( + result_lines(t.render()), ["hi"] + ) + + def test_list_comprehensions_plus_undeclared_nonstrict(self): + # traditional behavior. variable inside a list comprehension + # is treated as an "undefined", so is pulled from the context. + t = Template(""" + t is: ${t} + + ${",".join([t for t in ("a", "b", "c")])} + """) + + eq_( + result_lines(t.render(t="T")), + ['t is: T', 'a,b,c'] + ) + + def test_traditional_assignment_plus_undeclared(self): + t = Template(""" + t is: ${t} + + <% + t = 12 + %> + """) + assert_raises( + UnboundLocalError, + t.render, t="T" + ) + + def test_list_comprehensions_plus_undeclared_strict(self): + # with strict, a list comprehension now behaves + # like the undeclared case above. + t = Template(""" + t is: ${t} + + ${",".join([t for t in ("a", "b", "c")])} + """, strict_undefined=True) + + eq_( + result_lines(t.render(t="T")), + ['t is: T', 'a,b,c'] + ) + + class ControlTest(TemplateTest): def test_control(self): t = Template(""" |