blob: 01246ff3ba45ecdf1036ff3606bb6f8b6d8144e9 [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 Ronacher8ebf1f92007-03-03 11:22:18 +01008 :copyright: 2007 by Armin Ronacher.
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
Armin Ronacher58875382007-02-27 19:40:14 +010013
14
Armin Ronacherab45b842007-03-18 20:47:50 +010015number_re = re.compile(r'^-?\d+(\.\d+)?$')
Armin Ronacher58875382007-02-27 19:40:14 +010016regex_type = type(number_re)
17
18
Armin Ronacher577ad382008-04-16 15:36:49 +020019def test_odd(value):
20 """Return true if the variable is odd."""
21 return value % 2 == 1
Armin Ronacher58875382007-02-27 19:40:14 +010022
23
Armin Ronacher577ad382008-04-16 15:36:49 +020024def test_even(value):
25 """Return true of the variable is even."""
26 return value % 2 == 0
Armin Ronacher58875382007-02-27 19:40:14 +010027
28
Armin Ronacherf59bac22008-04-20 13:11:43 +020029def test_divisibleby(value, num):
30 """Check if a variable is divisible by a number."""
31 return value % num == 0
32
33
Armin Ronacher577ad382008-04-16 15:36:49 +020034def test_defined(value):
35 """Return true if the variable is defined:
Armin Ronacher58875382007-02-27 19:40:14 +010036
Armin Ronacher37a88512007-03-02 20:42:18 +010037 .. sourcecode:: jinja
38
39 {% if variable is defined %}
40 value of variable: {{ variable }}
41 {% else %}
42 variable is not defined
43 {% endif %}
44
Armin Ronacher26c0f512008-04-17 11:16:01 +020045 See the ``default`` filter for a simple way to set undefined
46 variables.
Armin Ronacher58875382007-02-27 19:40:14 +010047 """
Armin Ronacher577ad382008-04-16 15:36:49 +020048 return not isinstance(value, Undefined)
Armin Ronacher58875382007-02-27 19:40:14 +010049
50
Armin Ronacher53042292008-04-26 18:30:19 +020051def test_undefined(value):
52 """Like `defined` but the other way round."""
53 return isinstance(value, Undefined)
54
55
56def test_none(value):
57 """Return true if the variable is none."""
58 return value is None
59
60
Armin Ronacher577ad382008-04-16 15:36:49 +020061def test_lower(value):
Armin Ronacher53042292008-04-26 18:30:19 +020062 """Return true if the variable is lowercased."""
Armin Ronacher577ad382008-04-16 15:36:49 +020063 return unicode(value).islower()
Armin Ronacher58875382007-02-27 19:40:14 +010064
65
Armin Ronacher577ad382008-04-16 15:36:49 +020066def test_upper(value):
Armin Ronacher53042292008-04-26 18:30:19 +020067 """Return true if the variable is uppercased."""
Armin Ronacher577ad382008-04-16 15:36:49 +020068 return unicode(value).isupper()
Armin Ronacher58875382007-02-27 19:40:14 +010069
70
Armin Ronacher53042292008-04-26 18:30:19 +020071def test_string(value):
72 """Return true if the object is a string."""
73 return isinstance(value, basestring)
74
75
76def test_number(value):
77 """Return true if the variable is a number."""
78 return isinstance(value, (int, long, float, complex))
Armin Ronacher58875382007-02-27 19:40:14 +010079
80
Armin Ronacher577ad382008-04-16 15:36:49 +020081def test_sequence(value):
82 """Return true if the variable is a sequence. Sequences are variables
Armin Ronacher37a88512007-03-02 20:42:18 +010083 that are iterable.
Armin Ronacher58875382007-02-27 19:40:14 +010084 """
Armin Ronacher577ad382008-04-16 15:36:49 +020085 try:
86 len(value)
87 value.__getitem__
88 except:
89 return False
90 return True
Armin Ronacher58875382007-02-27 19:40:14 +010091
92
Armin Ronacher577ad382008-04-16 15:36:49 +020093def test_sameas(value, other):
94 """Check if an object points to the same memory address than another
Armin Ronacher69ddc582007-06-24 12:37:13 +020095 object:
96
97 .. sourcecode:: jinja
98
Armin Ronacherf59bac22008-04-20 13:11:43 +020099 {% if foo.attribute is sameas false %}
Armin Ronacher69ddc582007-06-24 12:37:13 +0200100 the foo attribute really is the `False` singleton
101 {% endif %}
Armin Ronacher69ddc582007-06-24 12:37:13 +0200102 """
Armin Ronacher577ad382008-04-16 15:36:49 +0200103 return value is other
Armin Ronacher69ddc582007-06-24 12:37:13 +0200104
105
Armin Ronacher53042292008-04-26 18:30:19 +0200106def test_iterable(value):
107 """Check if it's possible to iterate over an object."""
108 try:
109 iter(value)
110 except TypeError:
111 return False
112 return True
113
114
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200115def test_escaped(value):
116 """Check if the value is escaped."""
117 return hasattr(value, '__html__')
118
119
Armin Ronacher58875382007-02-27 19:40:14 +0100120TESTS = {
121 'odd': test_odd,
122 'even': test_even,
Armin Ronacherf59bac22008-04-20 13:11:43 +0200123 'divisibleby': test_divisibleby,
Armin Ronacher58875382007-02-27 19:40:14 +0100124 'defined': test_defined,
Armin Ronacher53042292008-04-26 18:30:19 +0200125 'undefined': test_undefined,
126 'none': test_none,
Armin Ronacher58875382007-02-27 19:40:14 +0100127 'lower': test_lower,
128 'upper': test_upper,
Armin Ronacher53042292008-04-26 18:30:19 +0200129 'string': test_string,
130 'number': test_number,
Armin Ronacher58875382007-02-27 19:40:14 +0100131 'sequence': test_sequence,
Armin Ronacher53042292008-04-26 18:30:19 +0200132 'iterable': test_iterable,
133 'callable': callable,
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200134 'sameas': test_sameas,
135 'escaped': test_escaped
Armin Ronacher58875382007-02-27 19:40:14 +0100136}