From d109d664a0f3df926b612ad3ca26b79287a62c8b Mon Sep 17 00:00:00 2001 From: Austin Lewis Date: Thu, 24 Oct 2024 08:26:01 -0400 Subject: [PATCH 1/4] Add contains test --- src/jinja2/tests.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/jinja2/tests.py b/src/jinja2/tests.py index 1a59e3703..d13091bfd 100644 --- a/src/jinja2/tests.py +++ b/src/jinja2/tests.py @@ -207,12 +207,21 @@ def test_escaped(value: t.Any) -> bool: def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: """Check if value is in seq. + Opposite of the 'in' test, allowing use as a test in filters like 'selectattr' - .. versionadded:: 2.10 + .. versionadded:: ?? """ return value in seq +def test_contains(value: t.Any, seq: t.Container[t.Any]) -> bool: + """Check if seq is in value. + + .. versionadded:: 2.10 + """ + return seq in value + + TESTS = { "odd": test_odd, "even": test_even, @@ -238,6 +247,7 @@ def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: "sameas": test_sameas, "escaped": test_escaped, "in": test_in, + "contains": test_contains, "==": operator.eq, "eq": operator.eq, "equalto": operator.eq, From 3837417a07b1b59727e5f13201e24eb5f4232678 Mon Sep 17 00:00:00 2001 From: Austin Lewis Date: Thu, 24 Oct 2024 08:40:21 -0400 Subject: [PATCH 2/4] Add test for contains --- tests/test_tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_tests.py b/tests/test_tests.py index 75178d6ad..482aa872b 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -209,6 +209,22 @@ def test_in(self, env): ) assert tmpl.render() == "True|True|False|True|False|True|False|True|False" + def test_contains(self, env): + tmpl = env.from_string( + '{{ "foo" is contains "o" }}|' + '{{ "foo" is contains "foo" }}|' + '{{ "foo" is contains "b" }}|' + "{{ ((1, 2)) is contains 1 }}|" + "{{ ((1, 2)) is contains 3 }}|" + "{{ [1, 2] is contains 1 }}|" + "{{ [1, 2] is contains 3 }}|" + '{{ {"foo": 1} is contains "foo" }}|' + '{{ {"bar": 1} is contains "baz" }}|' + '{{ [{"foo": "bar"}, {"foo": "fighter"}] | ' + 'selectattr("foo", "contains", "ba") | list | length }}' + ) + assert tmpl.render() == "True|True|False|True|False|True|False|True|False|1" + def test_name_undefined(env): with pytest.raises(TemplateAssertionError, match="No test named 'f'"): From b041a57396e4a44f47215b6942cfe204e37aba00 Mon Sep 17 00:00:00 2001 From: Austin Lewis Date: Thu, 24 Oct 2024 08:55:24 -0400 Subject: [PATCH 3/4] update version number for test --- src/jinja2/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jinja2/tests.py b/src/jinja2/tests.py index d13091bfd..8282df7e7 100644 --- a/src/jinja2/tests.py +++ b/src/jinja2/tests.py @@ -209,7 +209,7 @@ def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: """Check if value is in seq. Opposite of the 'in' test, allowing use as a test in filters like 'selectattr' - .. versionadded:: ?? + .. versionadded:: 2.10 """ return value in seq @@ -217,7 +217,7 @@ def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: def test_contains(value: t.Any, seq: t.Container[t.Any]) -> bool: """Check if seq is in value. - .. versionadded:: 2.10 + .. versionadded:: 3.1.5 """ return seq in value From 848e9de8cae25f0f972d850d2b8bf2ffe096e2dd Mon Sep 17 00:00:00 2001 From: Austin Lewis Date: Thu, 24 Oct 2024 09:39:09 -0400 Subject: [PATCH 4/4] fixed updates on wrong line --- src/jinja2/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jinja2/tests.py b/src/jinja2/tests.py index 8282df7e7..d83b1c7ba 100644 --- a/src/jinja2/tests.py +++ b/src/jinja2/tests.py @@ -207,7 +207,6 @@ def test_escaped(value: t.Any) -> bool: def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: """Check if value is in seq. - Opposite of the 'in' test, allowing use as a test in filters like 'selectattr' .. versionadded:: 2.10 """ @@ -215,7 +214,8 @@ def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool: def test_contains(value: t.Any, seq: t.Container[t.Any]) -> bool: - """Check if seq is in value. + """Check if value is in seq. + Opposite of the 'in' test, allowing use as a test in filters like 'selectattr' .. versionadded:: 3.1.5 """