This commit is contained in:
DCAuto 2025-07-16 17:48:51 -07:00 committed by GitHub
commit 1df1f3d360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -213,6 +213,15 @@ def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool:
return value in seq
def test_contains(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:: 3.1.5
"""
return seq in value
TESTS = {
"odd": test_odd,
"even": test_even,
@ -238,6 +247,7 @@ TESTS = {
"sameas": test_sameas,
"escaped": test_escaped,
"in": test_in,
"contains": test_contains,
"==": operator.eq,
"eq": operator.eq,
"equalto": operator.eq,

View File

@ -209,6 +209,22 @@ class TestTestsCase:
)
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'"):