Skip to content

Commit

Permalink
Added test for mappings. This fixes pallets#35
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed May 24, 2011
1 parent 067879e commit ee352ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
-------------
Expand Down
15 changes: 15 additions & 0 deletions jinja2/tests.py
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions jinja2/testsuite/tests.py
Expand Up @@ -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):
Expand Down

0 comments on commit ee352ec

Please sign in to comment.