Permalink
Browse files

Added test for mappings. This fixes #35

  • Loading branch information...
1 parent 067879e commit ee352ec21fdf753eff85e5d02631567d8c45a6d0 @mitsuhiko mitsuhiko committed May 24, 2011
Showing with 25 additions and 2 deletions.
  1. +2 −0 CHANGES
  2. +15 −0 jinja2/tests.py
  3. +8 −2 jinja2/testsuite/tests.py
View
@@ -32,6 +32,8 @@ Version 2.6
- like sum and sort, join now also is able to join attributes of objects
as string.
- the internal eval context now has a reference to the environment.
+- added a mapping test to see if an object is a dict or an object with
+ a similar interface.
Version 2.5.5
-------------
View
@@ -11,6 +11,12 @@
import re
from jinja2.runtime import Undefined
+try:
+ from collections import Mapping as MappingType
+except ImportError:
+ import UserDict
+ MappingType = (UserDict.UserDict, UserDict.DictMixin, dict)
+
# nose, nothing here to test
__test__ = False
@@ -83,6 +89,14 @@ def test_string(value):
return isinstance(value, basestring)
+def test_mapping(value):
+ """Return true if the object is a mapping (dict etc.).
+
+ .. versionadded:: 2.6
+ """
+ return isinstance(value, MappingType)
+
+
def test_number(value):
"""Return true if the variable is a number."""
return isinstance(value, (int, long, float, complex))
@@ -137,6 +151,7 @@ def test_escaped(value):
'lower': test_lower,
'upper': test_upper,
'string': test_string,
+ 'mapping': test_mapping,
'number': test_number,
'sequence': test_sequence,
'iterable': test_iterable,
@@ -48,10 +48,16 @@ def test_typechecks(self):
{{ range is callable }}
{{ 42 is callable }}
{{ range(5) is iterable }}
+ {{ {} is mapping }}
+ {{ mydict is mapping }}
+ {{ [] is mapping }}
''')
- assert tmpl.render().split() == [
+ class MyDict(dict):
+ pass
+ assert tmpl.render(mydict=MyDict()).split() == [
'False', 'True', 'False', 'True', 'True', 'False',
- 'True', 'True', 'True', 'True', 'False', 'True'
+ 'True', 'True', 'True', 'True', 'False', 'True',
+ 'True', 'True', 'False'
]
def test_sequence(self):

0 comments on commit ee352ec

Please sign in to comment.