Merge 848e9de8ca into 5ef70112a1
This commit is contained in:
commit
1df1f3d360
@ -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,
|
||||
|
||||
@ -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'"):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user