Merge pull request #665 from pallets/operator-tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Allow using comparison operator symbols as tests
authored by David Lord · committed by GitHub · parents 05b55d5, d62f050 · Browse files · 4 files·+64 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+64 added−48 removed
- tests:+28 added−8 removed
- other:+36 added−40 removed
Provenance
- Commit
ca1abd4- Tree
f384cf4- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
05b55d5- Parent 2
d62f050
4 files changed+64 added−48 removed
modified:docs/jinjaext.py+16 added-4 removed
10 unmodified lines11 import collections12 import os13 import re14 import inspect15+16+import logging17+18 import jinja219 from itertools import islice20 from types import BuiltinFunctionType21 from docutils import nodes88 unmodified lines110 return result111 112 113 def dump_functions(mapping):111- def directive(dirname, arguments, options, content, lineno,112- content_offset, block_text, state, state_machine):114+ def directive(115+ dirname, arguments, options, content, lineno, content_offset,116+ block_text, state, state_machine117+ ):118 reverse_mapping = {}119+120 for name, func in mapping.items():121 reverse_mapping.setdefault(func, []).append(name)122+123 filters = []124+ compare_ops = set(('lt', 'le', 'eq', 'ne', 'ge', 'gt'))125+126 for func, names in reverse_mapping.items():118- aliases = sorted(names, key=lambda x: len(x))127+ aliases = sorted(names, key=len)128+ aliases = sorted(aliases, key=lambda x: x in compare_ops)129 name = aliases.pop()130 filters.append((name, aliases, func))121- filters.sort()131 132+ filters.sort()133 result = ViewList()134+135 for name, aliases, func in filters:136 for item in format_function(name, aliases, func):137 result.append(item, '<jinjaext>')138 139 node = nodes.paragraph()140 state.nested_parse(result, content_offset, node)141 return node.children142+143 return directive144 145 146 from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS60 unmodified linesmodified:jinja2/tests.py+17 added-36 removed
6 unmodified lines7 8 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10 """11+import operator12 import re13 from collections import Mapping14 from jinja2.runtime import Undefined15 from jinja2._compat import text_type, string_types, integer_types87 unmodified lines103 return False104 return True105 106 106-def test_equalto(value, other):107- """Check if an object has the same value as another object:108-109- .. sourcecode:: jinja110-111- {% if foo.expression is equalto 42 %}112- the foo attribute evaluates to the constant 42113- {% endif %}114-115- This appears to be a useless test as it does exactly the same as the116- ``==`` operator, but it can be useful when used together with the117- `selectattr` function:118-119- .. sourcecode:: jinja120-121- {{ users|selectattr("email", "equalto", "foo@bar.invalid") }}122-123- .. versionadded:: 2.8124- """125- return value == other126-127-107 def test_sameas(value, other):108 """Check if an object points to the same memory address than another109 object:110 19 unmodified lines130 """Check if the value is escaped."""131 return hasattr(value, '__html__')132 133 155-def test_greaterthan(value, other):156- """Check if value is greater than other."""157- return value > other158-159-160-def test_lessthan(value, other):161- """Check if value is less than other."""162- return value < other163-164-134 def test_in(value, seq):135 """Check if value is in seq.136 137 .. versionadded:: 2.1016 unmodified lines154 'sequence': test_sequence,155 'iterable': test_iterable,156 'callable': test_callable,157 'sameas': test_sameas,189- 'equalto': test_equalto,158 'escaped': test_escaped,191- 'greaterthan': test_greaterthan,192- 'lessthan': test_lessthan,193- 'in': test_in159+ 'in': test_in,160+ '==': operator.eq,161+ 'eq': operator.eq,162+ 'equalto': operator.eq,163+ '!=': operator.ne,164+ 'ne': operator.ne,165+ '>': operator.gt,166+ 'gt': operator.gt,167+ 'greaterthan': operator.gt,168+ 'ge': operator.ge,169+ '>=': operator.ge,170+ '<': operator.lt,171+ 'lt': operator.lt,172+ 'lessthan': operator.lt,173+ '<=': operator.le,174+ 'le': operator.le,175 }modified:tests/test_tests.py+28 added-8 removed
76 unmodified lines77 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')78 assert tmpl.render() == 'True|False'79 80 def test_equalto(self, env):81- tmpl = env.from_string('{{ foo is equalto 12 }}|'82- '{{ foo is equalto 0 }}|'83- '{{ foo is equalto (3 * 4) }}|'84- '{{ bar is equalto "baz" }}|'85- '{{ bar is equalto "zab" }}|'86- '{{ bar is equalto ("ba" + "z") }}|'87- '{{ bar is equalto bar }}|'88- '{{ bar is equalto foo }}')81+ tmpl = env.from_string(82+ '{{ foo is eq 12 }}|'83+ '{{ foo is eq 0 }}|'84+ '{{ foo is eq (3 * 4) }}|'85+ '{{ bar is eq "baz" }}|'86+ '{{ bar is eq "zab" }}|'87+ '{{ bar is eq ("ba" + "z") }}|'88+ '{{ bar is eq bar }}|'89+ '{{ bar is eq foo }}'90+ )91 assert tmpl.render(foo=12, bar="baz") \92 == 'True|False|True|True|False|True|True|False'93 94+ @pytest.mark.parametrize('op,expect', (95+ ('eq 2', True),96+ ('eq 3', False),97+ ('ne 3', True),98+ ('ne 2', False),99+ ('lt 3', True),100+ ('lt 2', False),101+ ('le 2', True),102+ ('le 1', False),103+ ('gt 1', True),104+ ('gt 2', False),105+ ('ge 2', True),106+ ('ge 3', False),107+ ))108+ def test_compare_aliases(self, env, op, expect):109+ t = env.from_string('{{{{ 2 is {op} }}}}'.format(op=op))110+ assert t.render() == str(expect)111+112 def test_sameas(self, env):113 tmpl = env.from_string('{{ foo is sameas false }}|'114 '{{ 0 is sameas false }}')115 assert tmpl.render(foo=False) == 'True|False'46 unmodified linesmodified:CHANGES+3 added
24 unmodified lines25 - The ``random`` filter is no longer incorrectly constant folded and will26 produce a new random choice each time the template is rendered. (`#478`_)27 - Add a ``unique`` filter. (`#469`_)28 - Add ``min`` and ``max`` filters. (`#475`_)29+- Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,30+ ``gt``, ``ge``. (`#665`_)31 32 .. _#469: https://github.com/pallets/jinja/pull/46933 .. _#475: https://github.com/pallets/jinja/pull/47534 .. _#478: https://github.com/pallets/jinja/pull/47835+.. _#665: https://github.com/pallets/jinja/pull/66536 37 Version 2.9.638 -------------39 (bugfix release, released on April 3rd 2017)504 unmodified lines