]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
add test and changelog 692/head
authorDavid Lord <davidism@gmail.com>
Sat, 8 Jul 2017 16:04:29 +0000 (09:04 -0700)
committerDavid Lord <davidism@gmail.com>
Sat, 8 Jul 2017 16:04:29 +0000 (09:04 -0700)
use ignore_case function

CHANGES
jinja2/filters.py
tests/test_filters.py

diff --git a/CHANGES b/CHANGES
index 4240a63dc1eab9ef4334ec3f5aa5383e39773425..c7d78b91717b4585d9ef1146c6619517419fa348 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,7 @@ Version 2.10
   ``gt``, ``ge``. (`#665`_)
 - ``import`` statement cannot end with a trailing comma. (`#617`_, `#618`_)
 - ``indent`` filter will not indent blank lines by default. (`#685`_)
+- Add ``reverse`` argument for ``dictsort`` filter. (`#692`_)
 
 .. _#469: https://github.com/pallets/jinja/pull/469
 .. _#475: https://github.com/pallets/jinja/pull/475
@@ -38,6 +39,7 @@ Version 2.10
 .. _#618: https://github.com/pallets/jinja/pull/618
 .. _#665: https://github.com/pallets/jinja/pull/665
 .. _#685: https://github.com/pallets/jinja/pull/685
+.. _#692: https://github.com/pallets/jinja/pull/692
 
 Version 2.9.6
 -------------
index 2496d3aad589f1ed875889557d173a4536e8a936..07e8ccae8d5718d457277dfe1b60bb4cd53cc535 100644 (file)
@@ -227,12 +227,16 @@ def do_dictsort(value, case_sensitive=False, by='key', reverse=False):
     elif by == 'value':
         pos = 1
     else:
-        raise FilterArgumentError('You can only sort by either '
-                                  '"key" or "value"')
+        raise FilterArgumentError(
+            'You can only sort by either "key" or "value"'
+        )
+
     def sort_func(item):
         value = item[pos]
-        if isinstance(value, string_types) and not case_sensitive:
-            value = value.lower()
+
+        if not case_sensitive:
+            value = ignore_case(value)
+
         return value
 
     return sorted(value.items(), key=sort_func, reverse=reverse)
index 2ef3249ea67f8df2ea9c7a332aa781872aaee4d7..f323f761f9bde637f9f374a2792856c45bda930c 100644 (file)
@@ -45,16 +45,16 @@ class TestFilter(object):
         )
         assert tmpl.render(given='yes') == 'no|False|no|yes'
 
-    def test_dictsort(self, env):
-        tmpl = env.from_string(
-            '{{ foo|dictsort }}|'
-            '{{ foo|dictsort(true) }}|'
-            '{{ foo|dictsort(false, "value") }}'
-        )
-        out = tmpl.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})
-        assert out == ("[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]|"
-                       "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]|"
-                       "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]")
+    @pytest.mark.parametrize('args,expect', (
+        ('', "[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]"),
+        ('true',  "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]"),
+        ('by="value"',  "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]"),
+        ('reverse=true', "[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]")
+    ))
+    def test_dictsort(self, env, args, expect):
+        t = env.from_string('{{{{ foo|dictsort({args}) }}}}'.format(args=args))
+        out = t.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})
+        assert out == expect
 
     def test_batch(self, env):
         tmpl = env.from_string("{{ foo|batch(3)|list }}|"