]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Change grouping behavior of tests. This fixes #401
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 7 Jan 2017 14:15:06 +0000 (15:15 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 7 Jan 2017 14:15:06 +0000 (15:15 +0100)
CHANGES
jinja2/parser.py
tests/test_tests.py

diff --git a/CHANGES b/CHANGES
index da6774f6e1afa5677e45518e994fc7212606a668..7ed12dc82765e6aa7641805def2ed44c5627463a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,11 @@ Version 2.9
 - Block sets are now marked `safe` by default.
 - On Python 2 the asciification of ASCII strings can now be disabled with
   the `compiler.ascii_str` policy.
+- Tests now no longer accept an arbitrary expression as first argument but
+  a restricted one.  This means that you can now properly use multiple
+  tests in one expression without extra parentheses.  In particular you can
+  now write ``foo is divisibleby 2 or foo is divisibleby 3``
+  as you would expect.
 
 Version 2.8.2
 -------------
index cdb3fe88468e38acd143a195e483e2614b34c57d..00e05580719b6d977cd8e2995d6023278ce0af57 100644 (file)
@@ -806,7 +806,7 @@ class Parser(object):
                                            'name:and')):
             if self.stream.current.test('name:is'):
                 self.fail('You cannot chain multiple tests with is')
-            args = [self.parse_expression()]
+            args = [self.parse_primary()]
         else:
             args = []
         node = nodes.Test(node, name, args, kwargs, dyn_args,
index a606f1ec3ef331bbf977d48a290eb2b358b44b83..03cd26b2536b7f6d770a5beb3c13af4d0b284168 100644 (file)
@@ -112,3 +112,17 @@ class TestTestsCase(object):
         tmpl = env.from_string('{{ 0 is lessthan 1 }}|'
                                '{{ 1 is lessthan 0 }}')
         assert tmpl.render() == 'True|False'
+
+    def test_multiple_tests(self):
+        items = []
+        def matching(x, y):
+            items.append((x, y))
+            return False
+        env = Environment()
+        env.tests['matching'] = matching
+        tmpl = env.from_string("{{ 'us-west-1' is matching "
+                               "'(us-east-1|ap-northeast-1)' "
+                               "or 'stage' is matching '(dev|stage)' }}")
+        assert tmpl.render() == 'False'
+        assert items == [('us-west-1', '(us-east-1|ap-northeast-1)'),
+                         ('stage', '(dev|stage)')]