Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add equalto test as value-level analogue of reference-level sameas test. #283

Merged
merged 2 commits into from

3 participants

@thomasvnoort

I was missing a test for value-level equality, analogue to the sameas test that compares pointers. For instance, to filter a hash like {{ users|selectattr("email", "equalto", "jinja@pocoo.org") }}.

@berkerpeksag

It would be good to add a test to jinja2/testsuite/tests.py.

@thomasvnoort

I don't mean to be impatient, but is somebody looking into merging this pull request? To me it seems like a simple yet powerful addition.

@mitsuhiko
Owner

That sounds like a good idea for the new filter support.

@mitsuhiko mitsuhiko merged commit 40367c4 into mitsuhiko:master

1 check passed

Details default The Travis CI build passed
@mitsuhiko
Owner

Sorry for not handling this for so long. I'm trying to fix this. For the time being, send me an email if you have any more requests that need proper review to jump ahead of the github notification list.

@thomasvnoort

Wonderful, thanks for your quick reply!

@thomasvnoort thomasvnoort deleted the silkapp:equalto-test branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 22, 2013
  1. Define tests for equalto.

    Thomas van Noort authored
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 0 deletions.
  1. +11 −0 jinja2/tests.py
  2. +11 −0 jinja2/testsuite/tests.py
View
11 jinja2/tests.py
@@ -102,6 +102,16 @@ def test_sequence(value):
return False
return True
+def test_equalto(value, other):
+ """Check if an object has the same value as another object:
+
+ .. sourcecode:: jinja
+
+ {% if foo.expression is equalto 42 %}
+ the foo attribute evaluates to the constant 42
+ {% endif %}
+ """
+ return value == other
def test_sameas(value, other):
"""Check if an object points to the same memory address than another
@@ -146,5 +156,6 @@ def test_escaped(value):
'iterable': test_iterable,
'callable': test_callable,
'sameas': test_sameas,
+ 'equalto': test_equalto,
'escaped': test_escaped
}
View
11 jinja2/testsuite/tests.py
@@ -72,6 +72,17 @@ def test_upper(self):
tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')
assert tmpl.render() == 'True|False'
+ def test_equalto(self):
+ tmpl = env.from_string('{{ foo is equalto 12 }}|'
+ '{{ foo is equalto 0 }}|'
+ '{{ foo is equalto (3 * 4) }}|'
+ '{{ bar is equalto "baz" }}|'
+ '{{ bar is equalto "zab" }}|'
+ '{{ bar is equalto ("ba" + "z") }}|'
+ '{{ bar is equalto bar }}|'
+ '{{ bar is equalto foo }}')
+ assert tmpl.render(foo=12, bar="baz") == 'True|False|True|True|False|True|True|False'
+
def test_sameas(self):
tmpl = env.from_string('{{ foo is sameas false }}|'
'{{ 0 is sameas false }}')
Something went wrong with that request. Please try again.