]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Add test for greater/less than (#624)
authorMajor Hayden <major@mhtx.net>
Wed, 4 Jan 2017 16:21:53 +0000 (10:21 -0600)
committerDavid Lord <davidism@gmail.com>
Wed, 4 Jan 2017 16:21:53 +0000 (08:21 -0800)
It can be helpful to have a test for values that are greater or less than
another value, especially when looping over lists and using `select()` or
`selectattr()` filters. This patch adds tests that allow a developer to
check if a value is greater or less than another value.

For example, if I have a list of vehicles and I want to find the motorcycles:

    vehicles | selectattr('wheels', 'lessthan', 3)

Or, if I want to find large trucks:

    vehicles | selectattr('wheels', 'greaterthan', 4)

Tests are included.

jinja2/tests.py
tests/test_tests.py

index bb32349df0b7e67d8fedf388dad3f22cd1bd0ae1..999540e5d2b098572ef7f82699394ad1feddedee 100644 (file)
@@ -152,6 +152,16 @@ def test_escaped(value):
     return hasattr(value, '__html__')
 
 
+def test_greaterthan(value, other):
+    """Check if value is greater than other."""
+    return value > other
+
+
+def test_lessthan(value, other):
+    """Check if value is less than other."""
+    return value < other
+
+
 TESTS = {
     'odd':              test_odd,
     'even':             test_even,
@@ -169,5 +179,7 @@ TESTS = {
     'callable':         test_callable,
     'sameas':           test_sameas,
     'equalto':          test_equalto,
-    'escaped':          test_escaped
+    'escaped':          test_escaped,
+    'greaterthan':      test_greaterthan,
+    'lessthan':         test_lessthan
 }
index 9e54038c9a2dc18bbdc4069484389e852dfd3cf9..54d87bb4213f1d5dcaf77ec9a7abf42bf82d9480 100644 (file)
@@ -102,3 +102,13 @@ class TestTestsCase():
         env = Environment(autoescape=True)
         tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}')
         assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'
+
+    def test_greaterthan(self, env):
+        tmpl = env.from_string('{{ 1 is greaterthan 0 }}|'
+                               '{{ 0 is greaterthan 1 }}')
+        assert tmpl.render() == 'True|False'
+
+    def test_lessthan(self, env):
+        tmpl = env.from_string('{{ 0 is lessthan 1 }}|'
+                               '{{ 1 is lessthan 0 }}')
+        assert tmpl.render() == 'True|False'