Skip to content
Myr

add test and changelog for constant comparison folding

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 48036fb · Browse files · 3 files+39 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+39 added−8 removed

  • tests:+31 added−8 removed
  • other:+8 added−0 removed

3 files+39 added−8 removed

modified:jinja2/nodes.py+4 added
766 unmodified lines
767768 def as_const(self, eval_ctx=None):769 eval_ctx = get_eval_context(self, eval_ctx)770 result = value = self.expr.as_const(eval_ctx)771772 try:773 for op in self.ops:774 new_value = op.expr.as_const(eval_ctx)775 result = _cmpop_to_func[op.op](value, new_value)776777 if not result:778 return False779780 value = new_value781 except Exception:782 raise Impossible()783784 return result785786787class Operand(Helper):
219 unmodified lines
modified:tests/test_lexnparse.py+31 added-8 removed
321 unmodified lines
322 def test_concat(self, env):323 tmpl = env.from_string("{{ [1, 2] ~ 'foo' }}")324 assert tmpl.render() == '[1, 2]foo'325326 def test_compare(self, env):327 tmpl = env.from_string('{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|'328 '{{ 2 == 2 }}|{{ 1 <= 1 }}')329 assert tmpl.render() == 'True|True|True|True|True'326 @pytest.mark.parametrize(327 ("a", "op", "b"),328 [329 (1, ">", 0),330 (1, ">=", 1),331 (2, "<", 3),332 (3, "<=", 4),333 (4, "==", 4),334 (4, "!=", 5),335 ],336 )337 def test_compare(self, env, a, op, b):338 t = env.from_string("{{ %d %s %d }}" % (a, op, b))339 assert t.render() == "True"340341 def test_compare_parens(self, env):332 tmpl = env.from_string(333 "{{ i*(j<5) }}"334 )335 assert tmpl.render(i=2, j=3) == '2'342 t = env.from_string("{{ i * (j < 5) }}")343 assert t.render(i=2, j=3) == "2"344345 @pytest.mark.parametrize(346 ("src", "expect"),347 [348 ("{{ 4 < 2 < 3 }}", "False"),349 ("{{ a < b < c }}", "False"),350 ("{{ 4 > 2 > 3 }}", "False"),351 ("{{ a > b > c }}", "False"),352 ("{{ 4 > 2 < 3 }}", "True"),353 ("{{ a > b < c }}", "True"),354 ],355 )356 def test_compare_compound(self, env, src, expect):357 t = env.from_string(src)358 assert t.render(a=4, b=2, c=3) == expect359360 def test_inop(self, env):361 tmpl = env.from_string('{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}')362 assert tmpl.render() == 'True|False'363
349 unmodified lines
modified:CHANGES.rst+4 added
75 unmodified lines
76- Add ``break_on_hyphens`` parameter to ``|wordwrap`` filter.77 :issue:`550`78- Use :func:`callable` to inject context at runtime for compatibility79 with Cython compiled functions. :pr:`1108`80- When chained comparisons of constants are evaluated at compile time,81 the result follows Python's behavior of returning ``False`` if any82 comparison returns ``False``, rather than only the last one.83 :issue:`1102`848586Version 2.10.387--------------
629 unmodified lines