]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Added test for the scope node
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 8 Jan 2017 13:37:57 +0000 (14:37 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 8 Jan 2017 13:38:00 +0000 (14:38 +0100)
tests/test_ext.py

index 9f8b9c3a65bce4e87c581cde6060728622b51c82..9ec5ac35a126e096e1ad620e2b321f4b92463187 100644 (file)
@@ -303,6 +303,39 @@ class TestInternationalization(object):
         ]
 
 
+@pytest.mark.ext
+class TestScope(object):
+
+    def test_basic_scope_behavior(self):
+        # This is what the old with statement compiled down to
+        class ScopeExt(Extension):
+            tags = set(['scope'])
+
+            def parse(self, parser):
+                node = nodes.Scope(lineno=next(parser.stream).lineno)
+                assignments = []
+                while parser.stream.current.type != 'block_end':
+                    lineno = parser.stream.current.lineno
+                    if assignments:
+                        parser.stream.expect('comma')
+                    target = parser.parse_assign_target()
+                    parser.stream.expect('assign')
+                    expr = parser.parse_expression()
+                    assignments.append(nodes.Assign(target, expr, lineno=lineno))
+                node.body = assignments + \
+                    list(parser.parse_statements(('name:endscope',),
+                                                 drop_needle=True))
+                return node
+
+        env = Environment(extensions=[ScopeExt])
+        tmpl = env.from_string('''\
+        {%- with a=1, b=2, c=b, d=e, e=5 -%}
+            {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}
+        {%- endwith -%}
+        ''')
+        assert tmpl.render(b=3, e=4) == '1|2|2|4|5'
+
+
 @pytest.mark.ext
 class TestNewstyleInternationalization(object):