]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Pass context when using select 1762/head
authorRens Groothuijsen <l.groothuijsen@alumni.maastrichtuniversity.nl>
Wed, 16 Nov 2022 00:25:49 +0000 (01:25 +0100)
committerDavid Lord <davidism@gmail.com>
Fri, 20 Dec 2024 04:47:24 +0000 (20:47 -0800)
CHANGES.rst
src/jinja2/filters.py
tests/test_regression.py

index e31de857d3f6e939f9eb56d038efe159b633eab7..521f5a08a3984b6e14b2c5d03f177f1d778bbf6a 100644 (file)
@@ -41,6 +41,8 @@ Unreleased
     contain the templates directory. :issue:`1705`
 -   Improve annotations for methods returning copies. :pr:`1880`
 -   ``urlize`` does not add ``mailto:`` to values like `@a@b`. :pr:`1870`
+-   Tests decorated with `@pass_context`` can be used with the ``|select``
+    filter. :issue:`1624`
 
 
 Version 3.1.4
index a92832a34063c9ae13845432e407e7db3bf8e261..e5b5a00c5e8cb31f69c07c807594db1c492fd116 100644 (file)
@@ -1780,7 +1780,7 @@ def prepare_select_or_reject(
         args = args[1 + off :]
 
         def func(item: t.Any) -> t.Any:
-            return context.environment.call_test(name, item, args, kwargs)
+            return context.environment.call_test(name, item, args, kwargs, context)
 
     except LookupError:
         func = bool  # type: ignore
index 7bd4d15649af566ab5a715609eac8398b25013dc..10df2d1bd0b72e001729497bbeb37e4683c3aea1 100644 (file)
@@ -737,6 +737,18 @@ End"""
         )
         assert tmpl.render() == "hellohellohello"
 
+    def test_pass_context_with_select(self, env):
+        @pass_context
+        def is_foo(ctx, s):
+            assert ctx is not None
+            return s == "foo"
+
+        env.tests["foo"] = is_foo
+        tmpl = env.from_string(
+            "{% for x in ['one', 'foo'] | select('foo') %}{{ x }}{% endfor %}"
+        )
+        assert tmpl.render() == "foo"
+
 
 @pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])
 def test_unicode_whitespace(env, unicode_char):