allow history to work in webkit browsers
[gae-samples.git] / python27 / guestbook / jinja2 / tests.py
blob50510b0d353791c5dd3c3fd213cfc1545a9576cc
1 # -*- coding: utf-8 -*-
2 """
3 jinja2.tests
4 ~~~~~~~~~~~~
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.
10 """
11 import re
12 from jinja2.runtime import Undefined
14 try:
15 from collections import Mapping as MappingType
16 except ImportError:
17 import UserDict
18 MappingType = (UserDict.UserDict, UserDict.DictMixin, dict)
20 # nose, nothing here to test
21 __test__ = False
24 number_re = re.compile(r'^-?\d+(\.\d+)?$')
25 regex_type = type(number_re)
28 try:
29 test_callable = callable
30 except NameError:
31 def test_callable(x):
32 return hasattr(x, '__call__')
35 def test_odd(value):
36 """Return true if the variable is odd."""
37 return value % 2 == 1
40 def test_even(value):
41 """Return true if the variable is even."""
42 return value % 2 == 0
45 def test_divisibleby(value, num):
46 """Check if a variable is divisible by a number."""
47 return value % num == 0
50 def test_defined(value):
51 """Return true if the variable is defined:
53 .. sourcecode:: jinja
55 {% if variable is defined %}
56 value of variable: {{ variable }}
57 {% else %}
58 variable is not defined
59 {% endif %}
61 See the :func:`default` filter for a simple way to set undefined
62 variables.
63 """
64 return not isinstance(value, Undefined)
67 def test_undefined(value):
68 """Like :func:`defined` but the other way round."""
69 return isinstance(value, Undefined)
72 def test_none(value):
73 """Return true if the variable is none."""
74 return value is None
77 def test_lower(value):
78 """Return true if the variable is lowercased."""
79 return unicode(value).islower()
82 def test_upper(value):
83 """Return true if the variable is uppercased."""
84 return unicode(value).isupper()
87 def test_string(value):
88 """Return true if the object is a string."""
89 return isinstance(value, basestring)
92 def test_mapping(value):
93 """Return true if the object is a mapping (dict etc.).
95 .. versionadded:: 2.6
96 """
97 return isinstance(value, MappingType)
100 def test_number(value):
101 """Return true if the variable is a number."""
102 return isinstance(value, (int, long, float, complex))
105 def test_sequence(value):
106 """Return true if the variable is a sequence. Sequences are variables
107 that are iterable.
109 try:
110 len(value)
111 value.__getitem__
112 except:
113 return False
114 return True
117 def test_sameas(value, other):
118 """Check if an object points to the same memory address than another
119 object:
121 .. sourcecode:: jinja
123 {% if foo.attribute is sameas false %}
124 the foo attribute really is the `False` singleton
125 {% endif %}
127 return value is other
130 def test_iterable(value):
131 """Check if it's possible to iterate over an object."""
132 try:
133 iter(value)
134 except TypeError:
135 return False
136 return True
139 def test_escaped(value):
140 """Check if the value is escaped."""
141 return hasattr(value, '__html__')
144 TESTS = {
145 'odd': test_odd,
146 'even': test_even,
147 'divisibleby': test_divisibleby,
148 'defined': test_defined,
149 'undefined': test_undefined,
150 'none': test_none,
151 'lower': test_lower,
152 'upper': test_upper,
153 'string': test_string,
154 'mapping': test_mapping,
155 'number': test_number,
156 'sequence': test_sequence,
157 'iterable': test_iterable,
158 'callable': test_callable,
159 'sameas': test_sameas,
160 'escaped': test_escaped