From 69ddc5854d8a61dcbfb0bae1121ff855cb8bcf75 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 24 Jun 2007 12:37:13 +0200 Subject: [PATCH] [svn] added sameas test function --HG-- branch : trunk --- CHANGES | 2 ++ jinja/filters.py | 2 ++ jinja/tests.py | 19 ++++++++++++++++++- tests/test_tests.py | 6 ++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 6bf0278..b30fa9b 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,8 @@ Version 1.2 - added `groupby` filter. +- added `sameas` test function. + Version 1.1 ----------- diff --git a/jinja/filters.py b/jinja/filters.py index 3f621a7..f253d37 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -872,6 +872,8 @@ def do_groupby(attribute): As you can see the item we're grouping by is stored in the `grouper` attribute and the `list` contains all the objects that have this grouper in common. + + *New in Jinja 1.2* """ def wrapped(env, context, value): expr = lambda x: env.get_attribute(x, attribute) diff --git a/jinja/tests.py b/jinja/tests.py index cdef3a3..4cbe65a 100644 --- a/jinja/tests.py +++ b/jinja/tests.py @@ -111,6 +111,22 @@ def test_matching(regex): return wrapped +def test_sameas(other): + """ + Check if an object points to the same memory address than another + object: + + .. sourcecode:: jinja + + {% if foo.attribute is sameas(false) %} + the foo attribute really is the `False` singleton + {% endif %} + + *New in Jinja 1.2* + """ + return lambda e, c, v: v is other + + TESTS = { 'odd': test_odd, 'even': test_even, @@ -119,5 +135,6 @@ TESTS = { 'upper': test_upper, 'numeric': test_numeric, 'sequence': test_sequence, - 'matching': test_matching + 'matching': test_matching, + 'sameas': test_sameas } diff --git a/tests/test_tests.py b/tests/test_tests.py index 387f3fb..c28cd90 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -19,6 +19,7 @@ SEQUENCE = '''{{ [1, 2, 3] is sequence }}|\ {{ "foo" is sequence }}|\ {{ 42 is sequence }}''' UPPER = '''{{ "FOO" is upper }}|{{ "foo" is upper }}''' +SAMEAS = '''{{ foo is sameas(false) }}|{{ 0 is sameas(false) }}''' def test_defined(env): @@ -59,3 +60,8 @@ def test_sequence(env): def test_upper(env): tmpl = env.from_string(UPPER) assert tmpl.render() == 'True|False' + + +def test_sameas(env): + tmpl = env.from_string(SAMEAS) + assert tmpl.render(foo=False) == 'True|False' -- 2.26.2