| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 """ | 2 """ |
| 3 jinja2.tests | 3 jinja2.tests |
| 4 ~~~~~~~~~~~~ | 4 ~~~~~~~~~~~~ |
| 5 | 5 |
| 6 Jinja test functions. Used with the "is" operator. | 6 Jinja test functions. Used with the "is" operator. |
| 7 | 7 |
| 8 :copyright: (c) 2010 by the Jinja Team. | 8 :copyright: (c) 2010 by the Jinja Team. |
| 9 :license: BSD, see LICENSE for more details. | 9 :license: BSD, see LICENSE for more details. |
| 10 """ | 10 """ |
| 11 import re | 11 import re |
| 12 from jinja2.runtime import Undefined | 12 from jinja2.runtime import Undefined |
| 13 | 13 from jinja2._compat import text_type, string_types, mapping_types |
| 14 try: | |
| 15 from collections import Mapping as MappingType | |
| 16 except ImportError: | |
| 17 import UserDict | |
| 18 MappingType = (UserDict.UserDict, UserDict.DictMixin, dict) | |
| 19 | |
| 20 # nose, nothing here to test | |
| 21 __test__ = False | |
| 22 | 14 |
| 23 | 15 |
| 24 number_re = re.compile(r'^-?\d+(\.\d+)?$') | 16 number_re = re.compile(r'^-?\d+(\.\d+)?$') |
| 25 regex_type = type(number_re) | 17 regex_type = type(number_re) |
| 26 | 18 |
| 27 | 19 |
| 28 try: | 20 test_callable = callable |
| 29 test_callable = callable | |
| 30 except NameError: | |
| 31 def test_callable(x): | |
| 32 return hasattr(x, '__call__') | |
| 33 | 21 |
| 34 | 22 |
| 35 def test_odd(value): | 23 def test_odd(value): |
| 36 """Return true if the variable is odd.""" | 24 """Return true if the variable is odd.""" |
| 37 return value % 2 == 1 | 25 return value % 2 == 1 |
| 38 | 26 |
| 39 | 27 |
| 40 def test_even(value): | 28 def test_even(value): |
| 41 """Return true if the variable is even.""" | 29 """Return true if the variable is even.""" |
| 42 return value % 2 == 0 | 30 return value % 2 == 0 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 69 return isinstance(value, Undefined) | 57 return isinstance(value, Undefined) |
| 70 | 58 |
| 71 | 59 |
| 72 def test_none(value): | 60 def test_none(value): |
| 73 """Return true if the variable is none.""" | 61 """Return true if the variable is none.""" |
| 74 return value is None | 62 return value is None |
| 75 | 63 |
| 76 | 64 |
| 77 def test_lower(value): | 65 def test_lower(value): |
| 78 """Return true if the variable is lowercased.""" | 66 """Return true if the variable is lowercased.""" |
| 79 return unicode(value).islower() | 67 return text_type(value).islower() |
| 80 | 68 |
| 81 | 69 |
| 82 def test_upper(value): | 70 def test_upper(value): |
| 83 """Return true if the variable is uppercased.""" | 71 """Return true if the variable is uppercased.""" |
| 84 return unicode(value).isupper() | 72 return text_type(value).isupper() |
| 85 | 73 |
| 86 | 74 |
| 87 def test_string(value): | 75 def test_string(value): |
| 88 """Return true if the object is a string.""" | 76 """Return true if the object is a string.""" |
| 89 return isinstance(value, basestring) | 77 return isinstance(value, string_types) |
| 90 | 78 |
| 91 | 79 |
| 92 def test_mapping(value): | 80 def test_mapping(value): |
| 93 """Return true if the object is a mapping (dict etc.). | 81 """Return true if the object is a mapping (dict etc.). |
| 94 | 82 |
| 95 .. versionadded:: 2.6 | 83 .. versionadded:: 2.6 |
| 96 """ | 84 """ |
| 97 return isinstance(value, MappingType) | 85 return isinstance(value, mapping_types) |
| 98 | 86 |
| 99 | 87 |
| 100 def test_number(value): | 88 def test_number(value): |
| 101 """Return true if the variable is a number.""" | 89 """Return true if the variable is a number.""" |
| 102 return isinstance(value, (int, long, float, complex)) | 90 return isinstance(value, (int, float, complex)) |
| 103 | 91 |
| 104 | 92 |
| 105 def test_sequence(value): | 93 def test_sequence(value): |
| 106 """Return true if the variable is a sequence. Sequences are variables | 94 """Return true if the variable is a sequence. Sequences are variables |
| 107 that are iterable. | 95 that are iterable. |
| 108 """ | 96 """ |
| 109 try: | 97 try: |
| 110 len(value) | 98 len(value) |
| 111 value.__getitem__ | 99 value.__getitem__ |
| 112 except: | 100 except: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 'upper': test_upper, | 140 'upper': test_upper, |
| 153 'string': test_string, | 141 'string': test_string, |
| 154 'mapping': test_mapping, | 142 'mapping': test_mapping, |
| 155 'number': test_number, | 143 'number': test_number, |
| 156 'sequence': test_sequence, | 144 'sequence': test_sequence, |
| 157 'iterable': test_iterable, | 145 'iterable': test_iterable, |
| 158 'callable': test_callable, | 146 'callable': test_callable, |
| 159 'sameas': test_sameas, | 147 'sameas': test_sameas, |
| 160 'escaped': test_escaped | 148 'escaped': test_escaped |
| 161 } | 149 } |
| OLD | NEW |