blob: 140b5bf8276f43c35da5b77a54ddeddc9f099d73 [file] [log] [blame]
Armin Ronacher58875382007-02-27 19:40:14 +01001# -*- coding: utf-8 -*-
2"""
Armin Ronacher26c0f512008-04-17 11:16:01 +02003 jinja2.tests
4 ~~~~~~~~~~~~
Armin Ronacher58875382007-02-27 19:40:14 +01005
6 Jinja test functions. Used with the "is" operator.
7
Armin Ronacher55494e42010-01-22 09:41:48 +01008 :copyright: (c) 2010 by the Jinja Team.
Armin Ronacher58875382007-02-27 19:40:14 +01009 :license: BSD, see LICENSE for more details.
10"""
11import re
Armin Ronacher577ad382008-04-16 15:36:49 +020012from jinja2.runtime import Undefined
Thomas Waldmanne0003552013-05-17 23:52:14 +020013import six
Armin Ronacher58875382007-02-27 19:40:14 +010014
Armin Ronacheree352ec2011-05-24 16:40:23 +020015try:
16 from collections import Mapping as MappingType
17except ImportError:
18 import UserDict
19 MappingType = (UserDict.UserDict, UserDict.DictMixin, dict)
20
Armin Ronacher8e64adf2010-02-07 02:00:11 +010021# nose, nothing here to test
22__test__ = False
23
Armin Ronacher58875382007-02-27 19:40:14 +010024
Armin Ronacherab45b842007-03-18 20:47:50 +010025number_re = re.compile(r'^-?\d+(\.\d+)?$')
Armin Ronacher58875382007-02-27 19:40:14 +010026regex_type = type(number_re)
27
28
Armin Ronacher42a19882009-08-05 18:45:39 +020029try:
30 test_callable = callable
31except NameError:
32 def test_callable(x):
33 return hasattr(x, '__call__')
34
35
Armin Ronacher577ad382008-04-16 15:36:49 +020036def test_odd(value):
37 """Return true if the variable is odd."""
38 return value % 2 == 1
Armin Ronacher58875382007-02-27 19:40:14 +010039
40
Armin Ronacher577ad382008-04-16 15:36:49 +020041def test_even(value):
Lukas Meuserad48a2e2008-05-01 18:19:57 +020042 """Return true if the variable is even."""
Armin Ronacher577ad382008-04-16 15:36:49 +020043 return value % 2 == 0
Armin Ronacher58875382007-02-27 19:40:14 +010044
45
Armin Ronacherf59bac22008-04-20 13:11:43 +020046def test_divisibleby(value, num):
47 """Check if a variable is divisible by a number."""
48 return value % num == 0
49
50
Armin Ronacher577ad382008-04-16 15:36:49 +020051def test_defined(value):
52 """Return true if the variable is defined:
Armin Ronacher58875382007-02-27 19:40:14 +010053
Armin Ronacher37a88512007-03-02 20:42:18 +010054 .. sourcecode:: jinja
55
56 {% if variable is defined %}
57 value of variable: {{ variable }}
58 {% else %}
59 variable is not defined
60 {% endif %}
61
Armin Ronacher9f258ff2008-05-24 22:28:52 +020062 See the :func:`default` filter for a simple way to set undefined
Armin Ronacher26c0f512008-04-17 11:16:01 +020063 variables.
Armin Ronacher58875382007-02-27 19:40:14 +010064 """
Armin Ronacher577ad382008-04-16 15:36:49 +020065 return not isinstance(value, Undefined)
Armin Ronacher58875382007-02-27 19:40:14 +010066
67
Armin Ronacher53042292008-04-26 18:30:19 +020068def test_undefined(value):
Armin Ronacher9f258ff2008-05-24 22:28:52 +020069 """Like :func:`defined` but the other way round."""
Armin Ronacher53042292008-04-26 18:30:19 +020070 return isinstance(value, Undefined)
71
72
73def test_none(value):
74 """Return true if the variable is none."""
75 return value is None
76
77
Armin Ronacher577ad382008-04-16 15:36:49 +020078def test_lower(value):
Armin Ronacher53042292008-04-26 18:30:19 +020079 """Return true if the variable is lowercased."""
Thomas Waldmanne0003552013-05-17 23:52:14 +020080 return six.text_type(value).islower()
Armin Ronacher58875382007-02-27 19:40:14 +010081
82
Armin Ronacher577ad382008-04-16 15:36:49 +020083def test_upper(value):
Armin Ronacher53042292008-04-26 18:30:19 +020084 """Return true if the variable is uppercased."""
Thomas Waldmanne0003552013-05-17 23:52:14 +020085 return six.text_type(value).isupper()
Armin Ronacher58875382007-02-27 19:40:14 +010086
87
Armin Ronacher53042292008-04-26 18:30:19 +020088def test_string(value):
89 """Return true if the object is a string."""
Thomas Waldmann7d295622013-05-18 00:06:22 +020090 return isinstance(value, six.string_types)
Armin Ronacher53042292008-04-26 18:30:19 +020091
92
Armin Ronacheree352ec2011-05-24 16:40:23 +020093def test_mapping(value):
94 """Return true if the object is a mapping (dict etc.).
95
96 .. versionadded:: 2.6
97 """
98 return isinstance(value, MappingType)
99
100
Armin Ronacher53042292008-04-26 18:30:19 +0200101def test_number(value):
102 """Return true if the variable is a number."""
Thomas Waldmann7d295622013-05-18 00:06:22 +0200103 return isinstance(value, (int, float, complex))
Armin Ronacher58875382007-02-27 19:40:14 +0100104
105
Armin Ronacher577ad382008-04-16 15:36:49 +0200106def test_sequence(value):
107 """Return true if the variable is a sequence. Sequences are variables
Armin Ronacher37a88512007-03-02 20:42:18 +0100108 that are iterable.
Armin Ronacher58875382007-02-27 19:40:14 +0100109 """
Armin Ronacher577ad382008-04-16 15:36:49 +0200110 try:
111 len(value)
112 value.__getitem__
113 except:
114 return False
115 return True
Armin Ronacher58875382007-02-27 19:40:14 +0100116
117
Armin Ronacher577ad382008-04-16 15:36:49 +0200118def test_sameas(value, other):
119 """Check if an object points to the same memory address than another
Armin Ronacher69ddc582007-06-24 12:37:13 +0200120 object:
121
122 .. sourcecode:: jinja
123
Armin Ronacherf59bac22008-04-20 13:11:43 +0200124 {% if foo.attribute is sameas false %}
Armin Ronacher69ddc582007-06-24 12:37:13 +0200125 the foo attribute really is the `False` singleton
126 {% endif %}
Armin Ronacher69ddc582007-06-24 12:37:13 +0200127 """
Armin Ronacher577ad382008-04-16 15:36:49 +0200128 return value is other
Armin Ronacher69ddc582007-06-24 12:37:13 +0200129
130
Armin Ronacher53042292008-04-26 18:30:19 +0200131def test_iterable(value):
132 """Check if it's possible to iterate over an object."""
133 try:
134 iter(value)
135 except TypeError:
136 return False
137 return True
138
139
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200140def test_escaped(value):
141 """Check if the value is escaped."""
142 return hasattr(value, '__html__')
143
144
Armin Ronacher58875382007-02-27 19:40:14 +0100145TESTS = {
146 'odd': test_odd,
147 'even': test_even,
Armin Ronacherf59bac22008-04-20 13:11:43 +0200148 'divisibleby': test_divisibleby,
Armin Ronacher58875382007-02-27 19:40:14 +0100149 'defined': test_defined,
Armin Ronacher53042292008-04-26 18:30:19 +0200150 'undefined': test_undefined,
151 'none': test_none,
Armin Ronacher58875382007-02-27 19:40:14 +0100152 'lower': test_lower,
153 'upper': test_upper,
Armin Ronacher53042292008-04-26 18:30:19 +0200154 'string': test_string,
Armin Ronacheree352ec2011-05-24 16:40:23 +0200155 'mapping': test_mapping,
Armin Ronacher53042292008-04-26 18:30:19 +0200156 'number': test_number,
Armin Ronacher58875382007-02-27 19:40:14 +0100157 'sequence': test_sequence,
Armin Ronacher53042292008-04-26 18:30:19 +0200158 'iterable': test_iterable,
Armin Ronacher42a19882009-08-05 18:45:39 +0200159 'callable': test_callable,
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200160 'sameas': test_sameas,
161 'escaped': test_escaped
Armin Ronacher58875382007-02-27 19:40:14 +0100162}