add test and changelog
Before adoption: this commit came in with the history the project adopted, so no landing records it.
use ignore_case function
authored by David Lord · committed by David Lord · parent 10fc8af · Browse files · 3 files·+20 added−14 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+20 added−14 removed
- tests:+10 added−10 removed
- other:+10 added−4 removed
Provenance
- Commit
34ca7f1- Tree
cabbc36- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
10fc8af
3 files changed+20 added−14 removed
modified:jinja2/filters.py+8 added-4 removed
225 unmodified lines226 pos = 0227 elif by == 'value':228 pos = 1229 else:230- raise FilterArgumentError('You can only sort by either '231- '"key" or "value"')230+ raise FilterArgumentError(231+ 'You can only sort by either "key" or "value"'232+ )233+234 def sort_func(item):235 value = item[pos]234- if isinstance(value, string_types) and not case_sensitive:235- value = value.lower()236+237+ if not case_sensitive:238+ value = ignore_case(value)239+240 return value241 242 return sorted(value.items(), key=sort_func, reverse=reverse)243 947 unmodified linesmodified:tests/test_filters.py+10 added-10 removed
43 unmodified lines44 "{{ false|default('no', true) }}|{{ given|default('no') }}"45 )46 assert tmpl.render(given='yes') == 'no|False|no|yes'47 48- def test_dictsort(self, env):49- tmpl = env.from_string(50- '{{ foo|dictsort }}|'51- '{{ foo|dictsort(true) }}|'52- '{{ foo|dictsort(false, "value") }}'53- )54- out = tmpl.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})55- assert out == ("[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]|"56- "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]|"57- "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]")48+ @pytest.mark.parametrize('args,expect', (49+ ('', "[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]"),50+ ('true', "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]"),51+ ('by="value"', "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]"),52+ ('reverse=true', "[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]")53+ ))54+ def test_dictsort(self, env, args, expect):55+ t = env.from_string('{{{{ foo|dictsort({args}) }}}}'.format(args=args))56+ out = t.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})57+ assert out == expect58 59 def test_batch(self, env):60 tmpl = env.from_string("{{ foo|batch(3)|list }}|"61 "{{ foo|batch(3, 'X')|list }}")590 unmodified linesmodified:CHANGES+2 added
28 unmodified lines29 - Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,30 ``gt``, ``ge``. (`#665`_)31 - ``import`` statement cannot end with a trailing comma. (`#617`_, `#618`_)32 - ``indent`` filter will not indent blank lines by default. (`#685`_)33+- Add ``reverse`` argument for ``dictsort`` filter. (`#692`_)34 35 .. _#469: https://github.com/pallets/jinja/pull/46936 .. _#475: https://github.com/pallets/jinja/pull/47537 .. _#478: https://github.com/pallets/jinja/pull/47838 .. _#617: https://github.com/pallets/jinja/pull/61739 .. _#618: https://github.com/pallets/jinja/pull/61840 .. _#665: https://github.com/pallets/jinja/pull/66541 .. _#685: https://github.com/pallets/jinja/pull/68542+.. _#692: https://github.com/pallets/jinja/pull/69243 44 Version 2.9.645 -------------46 (bugfix release, released on April 3rd 2017)504 unmodified lines