From 147bd571ad914579b0f40bae53aac33d35ef972e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 7 Jan 2017 15:15:06 +0100 Subject: [PATCH] Change grouping behavior of tests. This fixes #401 --- CHANGES | 5 +++++ jinja2/parser.py | 2 +- tests/test_tests.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index da6774f6e..7ed12dc82 100644 --- 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 ------------- diff --git a/jinja2/parser.py b/jinja2/parser.py index cdb3fe884..00e055807 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -806,7 +806,7 @@ def parse_test(self, node): '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, diff --git a/tests/test_tests.py b/tests/test_tests.py index a606f1ec3..03cd26b25 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -112,3 +112,17 @@ def test_lessthan(self, env): 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)')]