1 # -*- coding: utf-8 -*-
6 Jinja test functions. Used with the "is" operator.
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
12 from jinja2.runtime import Undefined
14 # nose, nothing here to test
18 number_re = re.compile(r'^-?\d+(\.\d+)?$')
19 regex_type = type(number_re)
23 test_callable = callable
26 return hasattr(x, '__call__')
30 """Return true if the variable is odd."""
35 """Return true if the variable is even."""
39 def test_divisibleby(value, num):
40 """Check if a variable is divisible by a number."""
41 return value % num == 0
44 def test_defined(value):
45 """Return true if the variable is defined:
49 {% if variable is defined %}
50 value of variable: {{ variable }}
52 variable is not defined
55 See the :func:`default` filter for a simple way to set undefined
58 return not isinstance(value, Undefined)
61 def test_undefined(value):
62 """Like :func:`defined` but the other way round."""
63 return isinstance(value, Undefined)
67 """Return true if the variable is none."""
71 def test_lower(value):
72 """Return true if the variable is lowercased."""
73 return unicode(value).islower()
76 def test_upper(value):
77 """Return true if the variable is uppercased."""
78 return unicode(value).isupper()
81 def test_string(value):
82 """Return true if the object is a string."""
83 return isinstance(value, basestring)
86 def test_number(value):
87 """Return true if the variable is a number."""
88 return isinstance(value, (int, long, float, complex))
91 def test_sequence(value):
92 """Return true if the variable is a sequence. Sequences are variables
103 def test_sameas(value, other):
104 """Check if an object points to the same memory address than another
107 .. sourcecode:: jinja
109 {% if foo.attribute is sameas false %}
110 the foo attribute really is the `False` singleton
113 return value is other
116 def test_iterable(value):
117 """Check if it's possible to iterate over an object."""
125 def test_escaped(value):
126 """Check if the value is escaped."""
127 return hasattr(value, '__html__')
133 'divisibleby': test_divisibleby,
134 'defined': test_defined,
135 'undefined': test_undefined,
139 'string': test_string,
140 'number': test_number,
141 'sequence': test_sequence,
142 'iterable': test_iterable,
143 'callable': test_callable,
144 'sameas': test_sameas,
145 'escaped': test_escaped