diff options
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(""" |
