| 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 collections import Mapping |
| 12 from jinja2.runtime import Undefined | 13 from jinja2.runtime import Undefined |
| 13 from jinja2._compat import text_type, string_types, mapping_types | 14 from jinja2._compat import text_type, string_types, integer_types |
| 14 | 15 import decimal |
| 15 | 16 |
| 16 number_re = re.compile(r'^-?\d+(\.\d+)?$') | 17 number_re = re.compile(r'^-?\d+(\.\d+)?$') |
| 17 regex_type = type(number_re) | 18 regex_type = type(number_re) |
| 18 | 19 |
| 19 | 20 |
| 20 test_callable = callable | 21 test_callable = callable |
| 21 | 22 |
| 22 | 23 |
| 23 def test_odd(value): | 24 def test_odd(value): |
| 24 """Return true if the variable is odd.""" | 25 """Return true if the variable is odd.""" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 def test_string(value): | 76 def test_string(value): |
| 76 """Return true if the object is a string.""" | 77 """Return true if the object is a string.""" |
| 77 return isinstance(value, string_types) | 78 return isinstance(value, string_types) |
| 78 | 79 |
| 79 | 80 |
| 80 def test_mapping(value): | 81 def test_mapping(value): |
| 81 """Return true if the object is a mapping (dict etc.). | 82 """Return true if the object is a mapping (dict etc.). |
| 82 | 83 |
| 83 .. versionadded:: 2.6 | 84 .. versionadded:: 2.6 |
| 84 """ | 85 """ |
| 85 return isinstance(value, mapping_types) | 86 return isinstance(value, Mapping) |
| 86 | 87 |
| 87 | 88 |
| 88 def test_number(value): | 89 def test_number(value): |
| 89 """Return true if the variable is a number.""" | 90 """Return true if the variable is a number.""" |
| 90 return isinstance(value, (int, float, complex)) | 91 return isinstance(value, integer_types + (float, complex, decimal.Decimal)) |
| 91 | 92 |
| 92 | 93 |
| 93 def test_sequence(value): | 94 def test_sequence(value): |
| 94 """Return true if the variable is a sequence. Sequences are variables | 95 """Return true if the variable is a sequence. Sequences are variables |
| 95 that are iterable. | 96 that are iterable. |
| 96 """ | 97 """ |
| 97 try: | 98 try: |
| 98 len(value) | 99 len(value) |
| 99 value.__getitem__ | 100 value.__getitem__ |
| 100 except: | 101 except: |
| 101 return False | 102 return False |
| 102 return True | 103 return True |
| 103 | 104 |
| 104 | 105 |
| 106 def test_equalto(value, other): |
| 107 """Check if an object has the same value as another object: |
| 108 |
| 109 .. sourcecode:: jinja |
| 110 |
| 111 {% if foo.expression is equalto 42 %} |
| 112 the foo attribute evaluates to the constant 42 |
| 113 {% endif %} |
| 114 |
| 115 This appears to be a useless test as it does exactly the same as the |
| 116 ``==`` operator, but it can be useful when used together with the |
| 117 `selectattr` function: |
| 118 |
| 119 .. sourcecode:: jinja |
| 120 |
| 121 {{ users|selectattr("email", "equalto", "foo@bar.invalid") }} |
| 122 |
| 123 .. versionadded:: 2.8 |
| 124 """ |
| 125 return value == other |
| 126 |
| 127 |
| 105 def test_sameas(value, other): | 128 def test_sameas(value, other): |
| 106 """Check if an object points to the same memory address than another | 129 """Check if an object points to the same memory address than another |
| 107 object: | 130 object: |
| 108 | 131 |
| 109 .. sourcecode:: jinja | 132 .. sourcecode:: jinja |
| 110 | 133 |
| 111 {% if foo.attribute is sameas false %} | 134 {% if foo.attribute is sameas false %} |
| 112 the foo attribute really is the `False` singleton | 135 the foo attribute really is the `False` singleton |
| 113 {% endif %} | 136 {% endif %} |
| 114 """ | 137 """ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 138 'none': test_none, | 161 'none': test_none, |
| 139 'lower': test_lower, | 162 'lower': test_lower, |
| 140 'upper': test_upper, | 163 'upper': test_upper, |
| 141 'string': test_string, | 164 'string': test_string, |
| 142 'mapping': test_mapping, | 165 'mapping': test_mapping, |
| 143 'number': test_number, | 166 'number': test_number, |
| 144 'sequence': test_sequence, | 167 'sequence': test_sequence, |
| 145 'iterable': test_iterable, | 168 'iterable': test_iterable, |
| 146 'callable': test_callable, | 169 'callable': test_callable, |
| 147 'sameas': test_sameas, | 170 'sameas': test_sameas, |
| 171 'equalto': test_equalto, |
| 148 'escaped': test_escaped | 172 'escaped': test_escaped |
| 149 } | 173 } |
| OLD | NEW |