diff options
| author | David Lord <davidism@gmail.com> | 2019-11-20 12:38:16 -0800 |
|---|---|---|
| committer | David Lord <davidism@gmail.com> | 2019-11-20 14:09:44 -0800 |
| commit | 3487c8e087599962d09ed30e6e80782b95a43cbb (patch) | |
| tree | 27ef51bb35b5d1f224f58be8e519c67f6a12f3fa /tests | |
| parent | 5d33f673ce261220b0a39b1c7a07e0f3bc6f8c64 (diff) | |
| download | jinja2-3487c8e087599962d09ed30e6e80782b95a43cbb.tar.gz | |
refactor visit_Output
* `finalize` is generated once and cached for all nodes.
* Extract common behavior for native env.
Removed the compiler behavior where groups of nodes would generate a
format string. Instead, individual nodes are always yielded. This made
rendering 30% faster in the examples, and simplifies the code. It also
removes the issue where Python would report either the first or last
line of the multi-line format expression, messing up the traceback line
number mapping.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_api.py | 30 | ||||
| -rw-r--r-- | tests/test_async.py | 13 |
2 files changed, 21 insertions, 22 deletions
diff --git a/tests/test_api.py b/tests/test_api.py index ec93db2..2eb9ce7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -11,6 +11,7 @@ import os import tempfile import shutil +from io import StringIO import pytest from jinja2 import Environment, Undefined, ChainableUndefined, \ @@ -206,25 +207,24 @@ class TestMeta(object): @pytest.mark.api @pytest.mark.streaming class TestStreaming(object): - def test_basic_streaming(self, env): - tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index " - "}} - {{ item }}</li>{%- endfor %}</ul>") - stream = tmpl.stream(seq=list(range(4))) - assert next(stream) == '<ul>' - assert next(stream) == '<li>1 - 0</li>' - assert next(stream) == '<li>2 - 1</li>' - assert next(stream) == '<li>3 - 2</li>' - assert next(stream) == '<li>4 - 3</li>' - assert next(stream) == '</ul>' + t = env.from_string( + "<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>" + "{%- endfor %}</ul>" + ) + stream = t.stream(seq=list(range(3))) + assert next(stream) == "<ul>" + assert "".join(stream) == "<li>1 - 0</li><li>2 - 1</li><li>3 - 2</li></ul>" def test_buffered_streaming(self, env): - tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index " - "}} - {{ item }}</li>{%- endfor %}</ul>") - stream = tmpl.stream(seq=list(range(4))) + tmpl = env.from_string( + "<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>" + "{%- endfor %}</ul>" + ) + stream = tmpl.stream(seq=list(range(3))) stream.enable_buffering(size=3) - assert next(stream) == u'<ul><li>1 - 0</li><li>2 - 1</li>' - assert next(stream) == u'<li>3 - 2</li><li>4 - 3</li></ul>' + assert next(stream) == u'<ul><li>1' + assert next(stream) == u' - 0</li>' def test_streaming_behavior(self, env): tmpl = env.from_string("") diff --git a/tests/test_async.py b/tests/test_async.py index 5f331a5..b71f094 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -102,13 +102,12 @@ def test_async_iteration_in_templates(): def test_async_iteration_in_templates_extended(): - t = Template('{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}', - enable_async=True) - async def async_iterator(): - for item in [1, 2, 3]: - yield item - rv = list(t.generate(rng=async_iterator())) - assert rv == ['0/1', '1/2', '2/3'] + t = Template( + "{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}", enable_async=True + ) + stream = t.generate(rng=auto_aiter(range(1, 4))) + assert next(stream) == "0" + assert "".join(stream) == "/11/22/3" @pytest.fixture |
