support for local common symbols (whatever that is good for); introduce ir_visibility...
[libfirm] / scripts / jinja2 / tests.py
1 # -*- coding: utf-8 -*-
2 """
3     jinja2.tests
4     ~~~~~~~~~~~~
5
6     Jinja test functions. Used with the "is" operator.
7
8     :copyright: 2007 by Armin Ronacher.
9     :license: BSD, see LICENSE for more details.
10 """
11 import re
12 from jinja2.runtime import Undefined
13
14
15 number_re = re.compile(r'^-?\d+(\.\d+)?$')
16 regex_type = type(number_re)
17
18
19 def test_odd(value):
20     """Return true if the variable is odd."""
21     return value % 2 == 1
22
23
24 def test_even(value):
25     """Return true if the variable is even."""
26     return value % 2 == 0
27
28
29 def test_divisibleby(value, num):
30     """Check if a variable is divisible by a number."""
31     return value % num == 0
32
33
34 def test_defined(value):
35     """Return true if the variable is defined:
36
37     .. sourcecode:: jinja
38
39         {% if variable is defined %}
40             value of variable: {{ variable }}
41         {% else %}
42             variable is not defined
43         {% endif %}
44
45     See the :func:`default` filter for a simple way to set undefined
46     variables.
47     """
48     return not isinstance(value, Undefined)
49
50
51 def test_undefined(value):
52     """Like :func:`defined` but the other way round."""
53     return isinstance(value, Undefined)
54
55
56 def test_none(value):
57     """Return true if the variable is none."""
58     return value is None
59
60
61 def test_lower(value):
62     """Return true if the variable is lowercased."""
63     return unicode(value).islower()
64
65
66 def test_upper(value):
67     """Return true if the variable is uppercased."""
68     return unicode(value).isupper()
69
70
71 def test_string(value):
72     """Return true if the object is a string."""
73     return isinstance(value, basestring)
74
75
76 def test_number(value):
77     """Return true if the variable is a number."""
78     return isinstance(value, (int, long, float, complex))
79
80
81 def test_sequence(value):
82     """Return true if the variable is a sequence. Sequences are variables
83     that are iterable.
84     """
85     try:
86         len(value)
87         value.__getitem__
88     except:
89         return False
90     return True
91
92
93 def test_sameas(value, other):
94     """Check if an object points to the same memory address than another
95     object:
96
97     .. sourcecode:: jinja
98
99         {% if foo.attribute is sameas false %}
100             the foo attribute really is the `False` singleton
101         {% endif %}
102     """
103     return value is other
104
105
106 def 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
115 def test_escaped(value):
116     """Check if the value is escaped."""
117     return hasattr(value, '__html__')
118
119
120 TESTS = {
121     'odd':              test_odd,
122     'even':             test_even,
123     'divisibleby':      test_divisibleby,
124     'defined':          test_defined,
125     'undefined':        test_undefined,
126     'none':             test_none,
127     'lower':            test_lower,
128     'upper':            test_upper,
129     'string':           test_string,
130     'number':           test_number,
131     'sequence':         test_sequence,
132     'iterable':         test_iterable,
133     'callable':         callable,
134     'sameas':           test_sameas,
135     'escaped':          test_escaped
136 }