Skip to content
Myr

test_async.py

This file

Path

tests/test_async.py

591 lines · 19.6 KiB · Python

Last changed by

Merge pull request #1181 from bartfeenstra/async-namespaceDavid Lordbefore adoption786867a

History of this file

Take it

The walk stopped after 200 commits: lines older than 793ddd4 point at it.

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 1–2why, one step back
1import asyncio
2

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 3–4why, one step back
3import pytest
4

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

lines 5–11why, one step back
5from jinja2 import DictLoader
6from jinja2 import Environment7from jinja2 import Template8from jinja2.asyncsupport import auto_aiter9from jinja2.exceptions import TemplateNotFound10from jinja2.exceptions import TemplatesNotFound11from jinja2.exceptions import UndefinedError

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 12–19why, one step back
12
1314def run(coro):15 loop = asyncio.get_event_loop()16 return loop.run_until_complete(coro)171819def test_basic_async():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 20–23why, one step back
20 t = Template(
21 "{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True22 )23

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 24–27why, one step back
24 async def func():
25 return await t.render_async()2627 rv = run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 28why, one step back
28 assert rv == "[1][2][3]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 29–31why, one step back
29
3031def test_await_on_calls():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 32why, one step back
32 t = Template("{{ async_func() + normal_func() }}", enable_async=True)

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 33–40why, one step back
33
34 async def async_func():35 return 423637 def normal_func():38 return 233940 async def func():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 41why, one step back
41 return await t.render_async(async_func=async_func, normal_func=normal_func)

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 42–43why, one step back
42
43 rv = run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 44why, one step back
44 assert rv == "65"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 45–47why, one step back
45
4647def test_await_on_calls_normal_render():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 48why, one step back
48 t = Template("{{ async_func() + normal_func() }}", enable_async=True)

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 49–55why, one step back
49
50 async def async_func():51 return 425253 def normal_func():54 return 2355

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 56why, one step back
56 rv = t.render(async_func=async_func, normal_func=normal_func)

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 57why, one step back
57

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 58why, one step back
58 assert rv == "65"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 59–61why, one step back
59
6061def test_await_and_macros():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 62–65why, one step back
62 t = Template(
63 "{% macro foo(x) %}[{{ x }}][{{ async_func() }}]{% endmacro %}{{ foo(42) }}",64 enable_async=True,65 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 66–73why, one step back
66
67 async def async_func():68 return 426970 async def func():71 return await t.render_async(async_func=async_func)7273 rv = run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 74why, one step back
74 assert rv == "[42][42]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 75–77why, one step back
75
7677def test_async_blocks():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 78–83why, one step back
78 t = Template(
79 "{% block foo %}<Test>{% endblock %}{{ self.foo() }}",80 enable_async=True,81 autoescape=True,82 )83

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 84–87why, one step back
84 async def func():
85 return await t.render_async()8687 rv = run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 88why, one step back
88 assert rv == "<Test><Test>"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 89–91why, one step back
89
9091def test_async_generate():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 92why, one step back
92 t = Template("{% for x in [1, 2, 3] %}{{ x }}{% endfor %}", enable_async=True)

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 93why, one step back
93 rv = list(t.generate())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 94why, one step back
94 assert rv == ["1", "2", "3"]

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 95–97why, one step back
95
9697def test_async_iteration_in_templates():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 98–99why, one step back
98 t = Template("{% for x in rng %}{{ x }}{% endfor %}", enable_async=True)
99

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 100–102why, one step back
100 async def async_iterator():
101 for item in [1, 2, 3]:102 yield item

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 103why, one step back
103

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 104why, one step back
104 rv = list(t.generate(rng=async_iterator()))

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 105why, one step back
105 assert rv == ["1", "2", "3"]

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 106–108why, one step back
106
107108def test_async_iteration_in_templates_extended():

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 109–114why, one step back
109 t = Template(
110 "{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}", enable_async=True111 )112 stream = t.generate(rng=auto_aiter(range(1, 4)))113 assert next(stream) == "0"114 assert "".join(stream) == "/11/22/3"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 115–118why, one step back
115
116117@pytest.fixture118def test_env_async():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 119–129why, one step back
119 env = Environment(
120 loader=DictLoader(121 dict(122 module="{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}",123 header="[{{ foo }}|{{ 23 }}]",124 o_printer="({{ o }})",125 )126 ),127 enable_async=True,128 )129 env.globals["bar"] = 23

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 130–135why, one step back
130 return env
131132133class TestAsyncImports(object):134 def test_context_imports(self, test_env_async):135 t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 136why, one step back
136 assert t.render(foo=42) == "[|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 137–139why, one step back
137 t = test_env_async.from_string(
138 '{% import "module" as m without context %}{{ m.test() }}'139 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 140why, one step back
140 assert t.render(foo=42) == "[|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 141–143why, one step back
141 t = test_env_async.from_string(
142 '{% import "module" as m with context %}{{ m.test() }}'143 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 144why, one step back
144 assert t.render(foo=42) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 145why, one step back
145 t = test_env_async.from_string('{% from "module" import test %}{{ test() }}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 146why, one step back
146 assert t.render(foo=42) == "[|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 147–149why, one step back
147 t = test_env_async.from_string(
148 '{% from "module" import test without context %}{{ test() }}'149 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 150why, one step back
150 assert t.render(foo=42) == "[|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 151–153why, one step back
151 t = test_env_async.from_string(
152 '{% from "module" import test with context %}{{ test() }}'153 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 154why, one step back
154 assert t.render(foo=42) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 155–163why, one step back
155
156 def test_trailing_comma(self, test_env_async):157 test_env_async.from_string('{% from "foo" import bar, baz with context %}')158 test_env_async.from_string('{% from "foo" import bar, baz, with context %}')159 test_env_async.from_string('{% from "foo" import bar, with context %}')160 test_env_async.from_string('{% from "foo" import bar, with, context %}')161 test_env_async.from_string('{% from "foo" import bar, with with context %}')162163 def test_exports(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 164–166why, one step back
164 m = run(
165 test_env_async.from_string(166 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 167–172why, one step back
167 {% macro toplevel() %}...{% endmacro %}
168 {% macro __private() %}...{% endmacro %}169 {% set variable = 42 %}170 {% for item in [1] %}171 {% macro notthere() %}{% endmacro %}172 {% endfor %}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 173–177why, one step back
173 """
174 )._get_default_module_async()175 )176 assert run(m.toplevel()) == "..."177 assert not hasattr(m, "__missing")

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 178why, one step back
178 assert m.variable == 42

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 179why, one step back
179 assert not hasattr(m, "notthere")

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 180–184why, one step back
180
181182class TestAsyncIncludes(object):183 def test_context_include(self, test_env_async):184 t = test_env_async.from_string('{% include "header" %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 185why, one step back
185 assert t.render(foo=42) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 186why, one step back
186 t = test_env_async.from_string('{% include "header" with context %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 187why, one step back
187 assert t.render(foo=42) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 188why, one step back
188 t = test_env_async.from_string('{% include "header" without context %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 189why, one step back
189 assert t.render(foo=42) == "[|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 190–192why, one step back
190
191 def test_choice_includes(self, test_env_async):192 t = test_env_async.from_string('{% include ["missing", "header"] %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 193why, one step back
193 assert t.render(foo=42) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 194–197why, one step back
194
195 t = test_env_async.from_string(196 '{% include ["missing", "missing2"] ignore missing %}'197 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 198why, one step back
198 assert t.render(foo=42) == ""

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 199–201why, one step back
199
200 t = test_env_async.from_string('{% include ["missing", "missing2"] %}')201 pytest.raises(TemplateNotFound, t.render)

Merge pull request #1077 from pallets/pytest-cleanupDavid Lordbefore adoption81d526d

line 202why, one step back
202 with pytest.raises(TemplatesNotFound) as e:

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 203why, one step back
203 t.render()

Merge pull request #1077 from pallets/pytest-cleanupDavid Lordbefore adoption81d526d

line 204why, one step back
204

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 205–206why, one step back
205 assert e.value.templates == ["missing", "missing2"]
206 assert e.value.name == "missing2"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 207–208why, one step back
207
208 def test_includes(t, **ctx):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 209–210why, one step back
209 ctx["foo"] = 42
210 assert t.render(ctx) == "[42|23]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 211–213why, one step back
211
212 t = test_env_async.from_string('{% include ["missing", "header"] %}')213 test_includes(t)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 214–215why, one step back
214 t = test_env_async.from_string("{% include x %}")
215 test_includes(t, x=["missing", "header"])

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 216why, one step back
216 t = test_env_async.from_string('{% include [x, "header"] %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 217–223why, one step back
217 test_includes(t, x="missing")
218 t = test_env_async.from_string("{% include x %}")219 test_includes(t, x="header")220 t = test_env_async.from_string("{% include x %}")221 test_includes(t, x="header")222 t = test_env_async.from_string("{% include [x] %}")223 test_includes(t, x="header")

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 224–227why, one step back
224
225 def test_include_ignoring_missing(self, test_env_async):226 t = test_env_async.from_string('{% include "missing" %}')227 pytest.raises(TemplateNotFound, t.render)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 228–232why, one step back
228 for extra in "", "with context", "without context":
229 t = test_env_async.from_string(230 '{% include "missing" ignore missing ' + extra + " %}"231 )232 assert t.render() == ""

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 233–234why, one step back
233
234 def test_context_include_with_overrides(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 235–242why, one step back
235 env = Environment(
236 loader=DictLoader(237 dict(238 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",239 item="{{ item }}",240 )241 )242 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 243–245why, one step back
243 assert env.get_template("main").render() == "123"
244245 def test_unoptimized_scopes(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 246–247why, one step back
246 t = test_env_async.from_string(
247 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 248–254why, one step back
248 {% macro outer(o) %}
249 {% macro inner() %}250 {% include "o_printer" %}251 {% endmacro %}252 {{ inner() }}253 {% endmacro %}254 {{ outer("FOO") }}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 255–257why, one step back
255 """
256 )257 assert t.render().strip() == "(FOO)"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 258why, one step back
258

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption2ed03c6

line 259why, one step back
259 def test_unoptimized_scopes_autoescape(self):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 260–266why, one step back
260 env = Environment(
261 loader=DictLoader(dict(o_printer="({{ o }})",)),262 autoescape=True,263 enable_async=True,264 )265 t = env.from_string(266 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption2ed03c6

lines 267–273why, one step back
267 {% macro outer(o) %}
268 {% macro inner() %}269 {% include "o_printer" %}270 {% endmacro %}271 {{ inner() }}272 {% endmacro %}273 {{ outer("FOO") }}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 274–276why, one step back
274 """
275 )276 assert t.render().strip() == "(FOO)"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption2ed03c6

line 277why, one step back
277

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 278–280why, one step back
278
279class TestAsyncForLoop(object):280 def test_simple(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 281–282why, one step back
281 tmpl = test_env_async.from_string("{% for item in seq %}{{ item }}{% endfor %}")
282 assert tmpl.render(seq=list(range(10))) == "0123456789"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 283–285why, one step back
283
284 def test_else(self, test_env_async):285 tmpl = test_env_async.from_string(

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 286–288why, one step back
286 "{% for item in seq %}XXX{% else %}...{% endfor %}"
287 )288 assert tmpl.render() == "..."

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 289–290why, one step back
289
290 def test_empty_blocks(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 291–294why, one step back
291 tmpl = test_env_async.from_string(
292 "<{% for item in seq %}{% else %}{% endfor %}>"293 )294 assert tmpl.render() == "<>"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 295why, one step back
295

Merge pull request #1101 from pallets/refactor-loop-contextDavid Lordbefore adoption540b260

lines 296–306why, one step back
296 @pytest.mark.parametrize(
297 "transform", [lambda x: x, iter, reversed, lambda x: (i for i in x), auto_aiter]298 )299 def test_context_vars(self, test_env_async, transform):300 t = test_env_async.from_string(301 "{% for item in seq %}{{ loop.index }}|{{ loop.index0 }}"302 "|{{ loop.revindex }}|{{ loop.revindex0 }}|{{ loop.first }}"303 "|{{ loop.last }}|{{ loop.length }}\n{% endfor %}"304 )305 out = t.render(seq=transform([42, 24]))306 assert out == "1|0|2|1|True|False|2\n2|1|1|0|False|True|2\n"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 307–308why, one step back
307
308 def test_cycling(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 309–310why, one step back
309 tmpl = test_env_async.from_string(
310 """{% for item in seq %}{{

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 311why, one step back
311 loop.cycle('<1>', '<2>') }}{% endfor %}{%

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 312–315why, one step back
312 for item in seq %}{{ loop.cycle(*through) }}{% endfor %}"""
313 )314 output = tmpl.render(seq=list(range(4)), through=("<1>", "<2>"))315 assert output == "<1><2>" * 4

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 316why, one step back
316

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

line 317why, one step back
317 def test_lookaround(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 318–319why, one step back
318 tmpl = test_env_async.from_string(
319 """{% for item in seq -%}

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

lines 320–321why, one step back
320 {{ loop.previtem|default('x') }}-{{ item }}-{{
321 loop.nextitem|default('x') }}|

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 322–323why, one step back
322 {%- endfor %}"""
323 )

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

line 324why, one step back
324 output = tmpl.render(seq=list(range(4)))

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 325why, one step back
325 assert output == "x-0-1|0-1-2|1-2-3|2-3-x|"

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

lines 326–327why, one step back
326
327 def test_changed(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 328–329why, one step back
328 tmpl = test_env_async.from_string(
329 """{% for item in seq -%}

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

line 330why, one step back
330 {{ loop.changed(item) }},

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 331–332why, one step back
331 {%- endfor %}"""
332 )

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

line 333why, one step back
333 output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 334why, one step back
334 assert output == "True,False,True,True,False,True,True,False,False,"

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

line 335why, one step back
335

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 336why, one step back
336 def test_scope(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 337why, one step back
337 tmpl = test_env_async.from_string("{% for item in seq %}{% endfor %}{{ item }}")

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 338–344why, one step back
338 output = tmpl.render(seq=list(range(10)))
339 assert not output340341 def test_varlen(self, test_env_async):342 def inner():343 for item in range(5):344 yield item

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 345–348why, one step back
345
346 tmpl = test_env_async.from_string(347 "{% for item in iter %}{{ item }}{% endfor %}"348 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 349why, one step back
349 output = tmpl.render(iter=inner())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 350why, one step back
350 assert output == "01234"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 351–352why, one step back
351
352 def test_noniter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 353why, one step back
353 tmpl = test_env_async.from_string("{% for item in none %}...{% endfor %}")

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 354–356why, one step back
354 pytest.raises(TypeError, tmpl.render)
355356 def test_recursive(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 357–358why, one step back
357 tmpl = test_env_async.from_string(
358 """{% for item in seq recursive -%}

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 359why, one step back
359 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 360–371why, one step back
360 {%- endfor %}"""
361 )362 assert (363 tmpl.render(364 seq=[365 dict(a=1, b=[dict(a=1), dict(a=2)]),366 dict(a=2, b=[dict(a=1), dict(a=2)]),367 dict(a=3, b=[dict(a="a")]),368 ]369 )370 == "[1<[1][2]>][2<[1][2]>][3<[a]>]"371 )

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

lines 372–373why, one step back
372
373 def test_recursive_lookaround(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 374–375why, one step back
374 tmpl = test_env_async.from_string(
375 """{% for item in seq recursive -%}

Merge pull request #676 from ThiefMaster/loop-lookaroundsAdrianbefore adoptionb73cb48

lines 376–378why, one step back
376 [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{
377 item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x'378 }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 379–390why, one step back
379 {%- endfor %}"""
380 )381 assert (382 tmpl.render(383 seq=[384 dict(a=1, b=[dict(a=1), dict(a=2)]),385 dict(a=2, b=[dict(a=1), dict(a=2)]),386 dict(a=3, b=[dict(a="a")]),387 ]388 )389 == "[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]"390 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 391–392why, one step back
391
392 def test_recursive_depth0(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 393–406why, one step back
393 tmpl = test_env_async.from_string(
394 "{% for item in seq recursive %}[{{ loop.depth0 }}:{{ item.a }}"395 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"396 )397 assert (398 tmpl.render(399 seq=[400 dict(a=1, b=[dict(a=1), dict(a=2)]),401 dict(a=2, b=[dict(a=1), dict(a=2)]),402 dict(a=3, b=[dict(a="a")]),403 ]404 )405 == "[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]"406 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 407–408why, one step back
407
408 def test_recursive_depth(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 409–422why, one step back
409 tmpl = test_env_async.from_string(
410 "{% for item in seq recursive %}[{{ loop.depth }}:{{ item.a }}"411 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"412 )413 assert (414 tmpl.render(415 seq=[416 dict(a=1, b=[dict(a=1), dict(a=2)]),417 dict(a=2, b=[dict(a=1), dict(a=2)]),418 dict(a=3, b=[dict(a="a")]),419 ]420 )421 == "[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]"422 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 423–424why, one step back
423
424 def test_looploop(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 425–426why, one step back
425 tmpl = test_env_async.from_string(
426 """{% for row in table %}

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 427–430why, one step back
427 {%- set rowloop = loop -%}
428 {% for cell in row -%}429 [{{ rowloop.index }}|{{ loop.index }}]430 {%- endfor %}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 431–433why, one step back
431 {%- endfor %}"""
432 )433 assert tmpl.render(table=["ab", "cd"]) == "[1|1][1|2][2|1][2|2]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 434–435why, one step back
434
435 def test_reversed_bug(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 436–441why, one step back
436 tmpl = test_env_async.from_string(
437 "{% for i in items %}{{ i }}"438 "{% if not loop.last %}"439 ",{% endif %}{% endfor %}"440 )441 assert tmpl.render(items=reversed([3, 2, 1])) == "1,2,3"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 442–443why, one step back
442
443 def test_loop_errors(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 444–447why, one step back
444 tmpl = test_env_async.from_string(
445 """{% for item in [1] if loop.index446 == 0 %}...{% endfor %}"""447 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 448why, one step back
448 pytest.raises(UndefinedError, tmpl.render)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 449–453why, one step back
449 tmpl = test_env_async.from_string(
450 """{% for item in [] %}...{% else451 %}{{ loop }}{% endfor %}"""452 )453 assert tmpl.render() == ""

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 454–455why, one step back
454
455 def test_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 456–461why, one step back
456 tmpl = test_env_async.from_string(
457 "{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}"458 )459 assert tmpl.render() == "[0][2][4][6][8]"460 tmpl = test_env_async.from_string(461 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 462why, one step back
462 {%- for item in range(10) if item is even %}[{{

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 463–465why, one step back
463 loop.index }}:{{ item }}]{% endfor %}"""
464 )465 assert tmpl.render() == "[1:0][2:2][3:4][4:6][5:8]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 466–468why, one step back
466
467 def test_scoped_special_var(self, test_env_async):468 t = test_env_async.from_string(

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 469–472why, one step back
469 "{% for s in seq %}[{{ loop.first }}{% for c in s %}"
470 "|{{ loop.first }}{% endfor %}]{% endfor %}"471 )472 assert t.render(seq=("ab", "cd")) == "[True|True|False][False|True|False]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 473–474why, one step back
473
474 def test_scoped_loop_var(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 475–484why, one step back
475 t = test_env_async.from_string(
476 "{% for x in seq %}{{ loop.first }}"477 "{% for y in seq %}{% endfor %}{% endfor %}"478 )479 assert t.render(seq="ab") == "TrueFalse"480 t = test_env_async.from_string(481 "{% for x in seq %}{% for y in seq %}"482 "{{ loop.first }}{% endfor %}{% endfor %}"483 )484 assert t.render(seq="ab") == "TrueFalseTrueFalse"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 485–486why, one step back
485
486 def test_recursive_empty_loop_iter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 487–488why, one step back
487 t = test_env_async.from_string(
488 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 489why, one step back
489 {%- for item in foo recursive -%}{%- endfor -%}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 490–492why, one step back
490 """
491 )492 assert t.render(dict(foo=[])) == ""

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 493–494why, one step back
493
494 def test_call_in_loop(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 495–496why, one step back
495 t = test_env_async.from_string(
496 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 497–505why, one step back
497 {%- macro do_something() -%}
498 [{{ caller() }}]499 {%- endmacro %}500501 {%- for i in [1, 2, 3] %}502 {%- call do_something() -%}503 {{ i }}504 {%- endcall %}505 {%- endfor -%}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 506–508why, one step back
506 """
507 )508 assert t.render() == "[1][2][3]"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 509–510why, one step back
509
510 def test_scoping_bug(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 511–512why, one step back
511 t = test_env_async.from_string(
512 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 513–515why, one step back
513 {%- for item in foo %}...{{ item }}...{% endfor %}
514 {%- macro item(a) %}...{{ a }}...{% endmacro %}515 {{- item(2) -}}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 516–518why, one step back
516 """
517 )518 assert t.render(foo=(1,)) == "...1......2..."

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 519–520why, one step back
519
520 def test_unpacking(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 521–524why, one step back
521 tmpl = test_env_async.from_string(
522 "{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}"523 )524 assert tmpl.render() == "1|2|3"

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 525–526why, one step back
525
526 def test_recursive_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 527–528why, one step back
527 t = test_env_async.from_string(
528 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 529–535why, one step back
529 <?xml version="1.0" encoding="UTF-8"?>
530 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">531 {%- for page in [site.root] if page.url != this recursive %}532 <url><loc>{{ page.url }}</loc></url>533 {{- loop(page.children) }}534 {%- endfor %}535 </urlset>

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 536–541why, one step back
536 """
537 )538 sm = t.render(539 this="/foo",540 site={"root": {"url": "/", "children": [{"url": "/foo"}, {"url": "/bar"}]}},541 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 542–545why, one step back
542 lines = [x.strip() for x in sm.splitlines() if x.strip()]
543 assert lines == [544 '<?xml version="1.0" encoding="UTF-8"?>',545 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 546–548why, one step back
546 "<url><loc>/</loc></url>",
547 "<url><loc>/bar</loc></url>",548 "</urlset>",

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 549–551why, one step back
549 ]
550551 def test_nonrecursive_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 552–553why, one step back
552 t = test_env_async.from_string(
553 """

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 554–559why, one step back
554 <?xml version="1.0" encoding="UTF-8"?>
555 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">556 {%- for page in items if page.url != this %}557 <url><loc>{{ page.url }}</loc></url>558 {%- endfor %}559 </urlset>

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 560–564why, one step back
560 """
561 )562 sm = t.render(563 this="/foo", items=[{"url": "/"}, {"url": "/foo"}, {"url": "/bar"}]564 )

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 565–568why, one step back
565 lines = [x.strip() for x in sm.splitlines() if x.strip()]
566 assert lines == [567 '<?xml version="1.0" encoding="UTF-8"?>',568 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 569–571why, one step back
569 "<url><loc>/</loc></url>",
570 "<url><loc>/bar</loc></url>",571 "</urlset>",

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 572why, one step back
572 ]

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption04644ce

lines 573–575why, one step back
573
574 def test_bare_async(self, test_env_async):575 t = test_env_async.from_string('{% extends "header" %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 576why, one step back
576 assert t.render(foo=42) == "[42|23]"

Merge pull request #1143 from pallets/bugfix/attribute-accessDavid Lordbefore adoption3d5bfc6

lines 577–580why, one step back
577
578 def test_awaitable_property_slicing(self, test_env_async):579 t = test_env_async.from_string("{% for x in a.b[:1] %}{{ x }}{% endfor %}")580 assert t.render(a=dict(b=[1, 2, 3])) == "1"

Merge pull request #1181 from bartfeenstra/async-namespaceDavid Lordbefore adoption786867a

lines 581–591why, one step back
581
582583def test_namespace_awaitable(test_env_async):584 async def _test():585 t = test_env_async.from_string(586 '{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'587 )588 actual = await t.render_async()589 assert actual == "Bar"590591 run(_test())