diff options
author | Adrian Moennich <adrian@planetcoding.net> | 2017-02-01 21:05:03 +0100 |
---|---|---|
committer | Adrian Moennich <adrian@planetcoding.net> | 2017-06-24 10:52:06 +0200 |
commit | 9bdb547b0ebc3d2bfd171e2693f3223ff115b4bf (patch) | |
tree | 696ab0d8ffac0494ede61968b4bb3160ff419b83 /tests | |
parent | edac1a8d9fcc4f5f685d916ee8d2101b6e18bbdf (diff) | |
download | jinja2-9bdb547b0ebc3d2bfd171e2693f3223ff115b4bf.tar.gz |
Add previtem/nextitem to loop context
related: #641
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_async.py | 20 | ||||
-rw-r--r-- | tests/test_core_tags.py | 20 |
2 files changed, 40 insertions, 0 deletions
diff --git a/tests/test_async.py b/tests/test_async.py index 279a4bb..83eb937 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -304,6 +304,14 @@ class TestAsyncForLoop(object): output = tmpl.render(seq=list(range(4)), through=('<1>', '<2>')) assert output == '<1><2>' * 4 + def test_lookaround(self, test_env_async): + tmpl = test_env_async.from_string('''{% for item in seq -%} + {{ loop.previtem|default('x') }}-{{ item }}-{{ + loop.nextitem|default('x') }}| + {%- endfor %}''') + output = tmpl.render(seq=list(range(4))) + assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|' + def test_scope(self, test_env_async): tmpl = test_env_async.from_string('{% for item in seq %}{% endfor %}{{ item }}') output = tmpl.render(seq=list(range(10))) @@ -331,6 +339,18 @@ class TestAsyncForLoop(object): dict(a=3, b=[dict(a='a')]) ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]' + def test_recursive_lookaround(self, test_env_async): + tmpl = test_env_async.from_string('''{% for item in seq recursive -%} + [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{ + item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x' + }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] + {%- endfor %}''') + assert tmpl.render(seq=[ + dict(a=1, b=[dict(a=1), dict(a=2)]), + dict(a=2, b=[dict(a=1), dict(a=2)]), + dict(a=3, b=[dict(a='a')]) + ]) == '[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]' + def test_recursive_depth0(self, test_env_async): tmpl = test_env_async.from_string('''{% for item in seq recursive -%} [{{ loop.depth0 }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] diff --git a/tests/test_core_tags.py b/tests/test_core_tags.py index f48d8b4..0fb4be8 100644 --- a/tests/test_core_tags.py +++ b/tests/test_core_tags.py @@ -68,6 +68,14 @@ class TestForLoop(object): output = tmpl.render(seq=list(range(4)), through=('<1>', '<2>')) assert output == '<1><2>' * 4 + def test_lookaround(self, env): + tmpl = env.from_string('''{% for item in seq -%} + {{ loop.previtem|default('x') }}-{{ item }}-{{ + loop.nextitem|default('x') }}| + {%- endfor %}''') + output = tmpl.render(seq=list(range(4))) + assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|' + def test_scope(self, env): tmpl = env.from_string('{% for item in seq %}{% endfor %}{{ item }}') output = tmpl.render(seq=list(range(10))) @@ -95,6 +103,18 @@ class TestForLoop(object): dict(a=3, b=[dict(a='a')]) ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]' + def test_recursive_lookaround(self, env): + tmpl = env.from_string('''{% for item in seq recursive -%} + [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{ + item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x' + }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] + {%- endfor %}''') + assert tmpl.render(seq=[ + dict(a=1, b=[dict(a=1), dict(a=2)]), + dict(a=2, b=[dict(a=1), dict(a=2)]), + dict(a=3, b=[dict(a='a')]) + ]) == '[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]' + def test_recursive_depth0(self, env): tmpl = env.from_string('''{% for item in seq recursive -%} [{{ loop.depth0 }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] |