blob: bb32349df0b7e67d8fedf388dad3f22cd1bd0ae1 [file] [log] [blame]
[email protected]88a0c562013-05-09 11:33:471# -*- coding: utf-8 -*-
2"""
3 jinja2.tests
4 ~~~~~~~~~~~~
5
6 Jinja test functions. Used with the "is" operator.
7
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
10"""
11import re
yukishiinoa34bd6862016-09-08 04:27:0912from collections import Mapping
[email protected]88a0c562013-05-09 11:33:4713from jinja2.runtime import Undefined
yukishiinoa34bd6862016-09-08 04:27:0914from jinja2._compat import text_type, string_types, integer_types
15import decimal
[email protected]88a0c562013-05-09 11:33:4716
17number_re = re.compile(r'^-?\d+(\.\d+)?$')
18regex_type = type(number_re)
19
20
[email protected]e6e05702013-09-06 09:48:3421test_callable = callable
[email protected]88a0c562013-05-09 11:33:4722
23
24def test_odd(value):
25 """Return true if the variable is odd."""
26 return value % 2 == 1
27
28
29def test_even(value):
30 """Return true if the variable is even."""
31 return value % 2 == 0
32
33
34def test_divisibleby(value, num):
35 """Check if a variable is divisible by a number."""
36 return value % num == 0
37
38
39def test_defined(value):
40 """Return true if the variable is defined:
41
42 .. sourcecode:: jinja
43
44 {% if variable is defined %}
45 value of variable: {{ variable }}
46 {% else %}
47 variable is not defined
48 {% endif %}
49
50 See the :func:`default` filter for a simple way to set undefined
51 variables.
52 """
53 return not isinstance(value, Undefined)
54
55
56def test_undefined(value):
57 """Like :func:`defined` but the other way round."""
58 return isinstance(value, Undefined)
59
60
61def test_none(value):
62 """Return true if the variable is none."""
63 return value is None
64
65
66def test_lower(value):
67 """Return true if the variable is lowercased."""
[email protected]e6e05702013-09-06 09:48:3468 return text_type(value).islower()
[email protected]88a0c562013-05-09 11:33:4769
70
71def test_upper(value):
72 """Return true if the variable is uppercased."""
[email protected]e6e05702013-09-06 09:48:3473 return text_type(value).isupper()
[email protected]88a0c562013-05-09 11:33:4774
75
76def test_string(value):
77 """Return true if the object is a string."""
[email protected]e6e05702013-09-06 09:48:3478 return isinstance(value, string_types)
[email protected]88a0c562013-05-09 11:33:4779
80
81def test_mapping(value):
82 """Return true if the object is a mapping (dict etc.).
83
84 .. versionadded:: 2.6
85 """
yukishiinoa34bd6862016-09-08 04:27:0986 return isinstance(value, Mapping)
[email protected]88a0c562013-05-09 11:33:4787
88
89def test_number(value):
90 """Return true if the variable is a number."""
yukishiinoa34bd6862016-09-08 04:27:0991 return isinstance(value, integer_types + (float, complex, decimal.Decimal))
[email protected]88a0c562013-05-09 11:33:4792
93
94def test_sequence(value):
95 """Return true if the variable is a sequence. Sequences are variables
96 that are iterable.
97 """
98 try:
99 len(value)
100 value.__getitem__
101 except:
102 return False
103 return True
104
105
yukishiinoa34bd6862016-09-08 04:27:09106def 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", "[email protected]") }}
122
123 .. versionadded:: 2.8
124 """
125 return value == other
126
127
[email protected]88a0c562013-05-09 11:33:47128def test_sameas(value, other):
129 """Check if an object points to the same memory address than another
130 object:
131
132 .. sourcecode:: jinja
133
134 {% if foo.attribute is sameas false %}
135 the foo attribute really is the `False` singleton
136 {% endif %}
137 """
138 return value is other
139
140
141def test_iterable(value):
142 """Check if it's possible to iterate over an object."""
143 try:
144 iter(value)
145 except TypeError:
146 return False
147 return True
148
149
150def test_escaped(value):
151 """Check if the value is escaped."""
152 return hasattr(value, '__html__')
153
154
155TESTS = {
156 'odd': test_odd,
157 'even': test_even,
158 'divisibleby': test_divisibleby,
159 'defined': test_defined,
160 'undefined': test_undefined,
161 'none': test_none,
162 'lower': test_lower,
163 'upper': test_upper,
164 'string': test_string,
165 'mapping': test_mapping,
166 'number': test_number,
167 'sequence': test_sequence,
168 'iterable': test_iterable,
169 'callable': test_callable,
170 'sameas': test_sameas,
yukishiinoa34bd6862016-09-08 04:27:09171 'equalto': test_equalto,
[email protected]88a0c562013-05-09 11:33:47172 'escaped': test_escaped
173}