]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Additional test coverage for async rendering of native type templates (#1807)
authorJames Addison <55152140+jayaddison@users.noreply.github.com>
Wed, 10 Jul 2024 16:14:52 +0000 (17:14 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Jul 2024 16:14:52 +0000 (09:14 -0700)
tests/conftest.py
tests/test_async.py
tests/test_async_filters.py
tests/test_nativetypes.py

index e225ab907c02e48e5eee523a3a595a92fb03b985..2fda63a82522c6204c7972da862aab75bbe95c90 100644 (file)
@@ -1,11 +1,22 @@
+import asyncio
 from pathlib import Path
 
 import pytest
+import trio
 
 from jinja2 import loaders
 from jinja2.environment import Environment
 
 
+def _asyncio_run(async_fn, *args):
+    return asyncio.run(async_fn(*args))
+
+
+@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
+def run_async_fn(request):
+    return request.param
+
+
 @pytest.fixture
 def env():
     """returns a new environment."""
index 4edced9dd9fe0c284ba5002e52f711131e9490ff..9bd9fda17cf044801ffaf3c5c94ec0e56aa6b173 100644 (file)
@@ -1,7 +1,4 @@
-import asyncio
-
 import pytest
-import trio
 
 from jinja2 import ChainableUndefined
 from jinja2 import DictLoader
@@ -14,15 +11,6 @@ from jinja2.exceptions import UndefinedError
 from jinja2.nativetypes import NativeEnvironment
 
 
-def _asyncio_run(async_fn, *args):
-    return asyncio.run(async_fn(*args))
-
-
-@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
-def run_async_fn(request):
-    return request.param
-
-
 def test_basic_async(run_async_fn):
     t = Template(
         "{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True
index e8cc350d5070d8a1b96a2c2e0946a168b5b07e2d..88ae5f41eee15d9fc00169370279c239b5eae5e9 100644 (file)
@@ -1,9 +1,7 @@
-import asyncio
 import contextlib
 from collections import namedtuple
 
 import pytest
-import trio
 from markupsafe import Markup
 
 from jinja2 import Environment
@@ -29,15 +27,6 @@ def env_async():
     return Environment(enable_async=True)
 
 
-def _asyncio_run(async_fn, *args):
-    return asyncio.run(async_fn(*args))
-
-
-@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
-def run_async_fn(request):
-    return request.param
-
-
 @contextlib.asynccontextmanager
 async def closing_factory():
     async with contextlib.AsyncExitStack() as stack:
index 8c85252518d390fa752cc75a26842a83b196ca9b..355799cc72e888cbae2192882993b4f3426d0098 100644 (file)
@@ -13,6 +13,11 @@ def env():
     return NativeEnvironment()
 
 
+@pytest.fixture
+def async_native_env():
+    return NativeEnvironment(enable_async=True)
+
+
 def test_is_defined_native_return(env):
     t = env.from_string("{{ missing is defined }}")
     assert not t.render()
@@ -122,6 +127,18 @@ def test_string_top_level(env):
     assert result == "Jinja"
 
 
+def test_string_concatenation(async_native_env, run_async_fn):
+    async def async_render():
+        t = async_native_env.from_string(
+            "{%- macro x(y) -%}{{ y }}{%- endmacro -%}{{- x('not') }} {{ x('bad') -}}"
+        )
+        result = await t.render_async()
+        assert isinstance(result, str)
+        assert result == "not bad"
+
+    run_async_fn(async_render)
+
+
 def test_tuple_of_variable_strings(env):
     t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")
     result = t.render(a=1, b=2, c="bytes")