Skip to content
Myr

allow using comparison operator symbols as tests

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

add tests and aliases for all comparison operators adjust docs to prefer short names for compare tests closes #664

authored by David Lord · committed by David Lord · parent 05b55d5 · 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

4 files+64 added−48 removed

modified:docs/jinjaext.py+16 added-4 removed
10 unmodified lines
11import collections12import os13import re14import inspect1516import logging1718import jinja219from itertools import islice20from types import BuiltinFunctionType21from docutils import nodes
88 unmodified lines
110 return result111112113def 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 = {}119120 for name, func in mapping.items():121 reverse_mapping.setdefault(func, []).append(name)122123 filters = []124 compare_ops = set(('lt', 'le', 'eq', 'ne', 'ge', 'gt'))125126 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()131132 filters.sort()133 result = ViewList()134135 for name, aliases, func in filters:136 for item in format_function(name, aliases, func):137 result.append(item, '<jinjaext>')138139 node = nodes.paragraph()140 state.nested_parse(result, content_offset, node)141 return node.children142143 return directive144145146from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS
60 unmodified lines
modified:jinja2/tests.py+17 added-36 removed
6 unmodified lines
78 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import operator12import re13from collections import Mapping14from jinja2.runtime import Undefined15from jinja2._compat import text_type, string_types, integer_types
87 unmodified lines
103 return False104 return True105106106def test_equalto(value, other):107 """Check if an object has the same value as another object:108109 .. sourcecode:: jinja110111 {% if foo.expression is equalto 42 %}112 the foo attribute evaluates to the constant 42113 {% endif %}114115 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:118119 .. sourcecode:: jinja120121 {{ users|selectattr("email", "equalto", "foo@bar.invalid") }}122123 .. versionadded:: 2.8124 """125 return value == other126127107def test_sameas(value, other):108 """Check if an object points to the same memory address than another109 object:110
19 unmodified lines
130 """Check if the value is escaped."""131 return hasattr(value, '__html__')132133155def test_greaterthan(value, other):156 """Check if value is greater than other."""157 return value > other158159160def test_lessthan(value, other):161 """Check if value is less than other."""162 return value < other163164134def test_in(value, seq):135 """Check if value is in seq.136137 .. versionadded:: 2.10
16 unmodified lines
154 '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 lines
77 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')78 assert tmpl.render() == 'True|False'7980 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'9394 @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)111112 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 lines
modified:CHANGES+3 added
24 unmodified lines
25- 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`_)3132.. _#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/6653637Version 2.9.638-------------39(bugfix release, released on April 3rd 2017)
504 unmodified lines