]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Add "is in" containment test (#658)
authorDirk D <bluthund23@gmail.com>
Thu, 12 Jan 2017 21:03:38 +0000 (22:03 +0100)
committerDavid Lord <davidism@gmail.com>
Thu, 12 Jan 2017 21:03:38 +0000 (13:03 -0800)
jinja2/tests.py
tests/test_tests.py

index bd843b77bcf52b34bd169d83c7dc16cd6dae9ee1..e4129658ad6d79f9c05dc3abd439eb130eff7353 100644 (file)
@@ -162,6 +162,11 @@ def test_lessthan(value, other):
     return value < other
 
 
+def test_in(value, seq):
+    """Check if value is in seq."""
+    return value in seq
+
+
 TESTS = {
     'odd':              test_odd,
     'even':             test_even,
@@ -181,5 +186,6 @@ TESTS = {
     'equalto':          test_equalto,
     'escaped':          test_escaped,
     'greaterthan':      test_greaterthan,
-    'lessthan':         test_lessthan
+    'lessthan':         test_lessthan,
+    'in':               test_in
 }
index 0dead3bfadba27d77a82645046dd47a09126a08f..3539a1fd37e1aa547dbf47359deeeec99cdae854 100644 (file)
@@ -126,3 +126,16 @@ class TestTestsCase(object):
         assert tmpl.render() == 'False'
         assert items == [('us-west-1', '(us-east-1|ap-northeast-1)'),
                          ('stage', '(dev|stage)')]
+
+    def test_in(self, env):
+        tmpl = env.from_string('{{ "o" is in "foo" }}|'
+                               '{{ "foo" is in "foo" }}|'
+                               '{{ "b" is in "foo" }}|'
+                               '{{ 1 is in ((1, 2)) }}|'
+                               '{{ 3 is in ((1, 2)) }}|'
+                               '{{ 1 is in [1, 2] }}|'
+                               '{{ 3 is in [1, 2] }}|'
+                               '{{ "foo" is in {"foo": 1}}}|'
+                               '{{ "baz" is in {"bar": 1}}}')
+        assert tmpl.render() \
+            == 'True|True|False|True|False|True|False|True|False'