diff options
Diffstat (limited to 'tests/test_async.py')
-rw-r--r-- | tests/test_async.py | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/tests/test_async.py b/tests/test_async.py index 92ac2a3..5f331a5 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -2,6 +2,7 @@ import pytest import asyncio from jinja2 import Template, Environment, DictLoader +from jinja2.asyncsupport import auto_aiter from jinja2.exceptions import TemplateNotFound, TemplatesNotFound, \ UndefinedError @@ -274,26 +275,17 @@ class TestAsyncForLoop(object): tmpl = test_env_async.from_string('<{% for item in seq %}{% else %}{% endfor %}>') assert tmpl.render() == '<>' - def test_context_vars(self, test_env_async): - slist = [42, 24] - for seq in [slist, iter(slist), reversed(slist), (_ for _ in slist)]: - tmpl = test_env_async.from_string('''{% for item in seq -%} - {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{ - loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{ - loop.length }}###{% endfor %}''') - one, two, _ = tmpl.render(seq=seq).split('###') - (one_index, one_index0, one_revindex, one_revindex0, one_first, - one_last, one_length) = one.split('|') - (two_index, two_index0, two_revindex, two_revindex0, two_first, - two_last, two_length) = two.split('|') - - assert int(one_index) == 1 and int(two_index) == 2 - assert int(one_index0) == 0 and int(two_index0) == 1 - assert int(one_revindex) == 2 and int(two_revindex) == 1 - assert int(one_revindex0) == 1 and int(two_revindex0) == 0 - assert one_first == 'True' and two_first == 'False' - assert one_last == 'False' and two_last == 'True' - assert one_length == two_length == '2' + @pytest.mark.parametrize( + "transform", [lambda x: x, iter, reversed, lambda x: (i for i in x), auto_aiter] + ) + def test_context_vars(self, test_env_async, transform): + t = test_env_async.from_string( + "{% for item in seq %}{{ loop.index }}|{{ loop.index0 }}" + "|{{ loop.revindex }}|{{ loop.revindex0 }}|{{ loop.first }}" + "|{{ loop.last }}|{{ loop.length }}\n{% endfor %}" + ) + out = t.render(seq=transform([42, 24])) + assert out == "1|0|2|1|True|False|2\n2|1|1|0|False|True|2\n" def test_cycling(self, test_env_async): tmpl = test_env_async.from_string('''{% for item in seq %}{{ |