xref: /aosp_15_r20/external/libchrome/third_party/jinja2/tests.py (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
2*635a8641SAndroid Build Coastguard Worker"""
3*635a8641SAndroid Build Coastguard Worker    jinja2.tests
4*635a8641SAndroid Build Coastguard Worker    ~~~~~~~~~~~~
5*635a8641SAndroid Build Coastguard Worker
6*635a8641SAndroid Build Coastguard Worker    Jinja test functions. Used with the "is" operator.
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker    :copyright: (c) 2017 by the Jinja Team.
9*635a8641SAndroid Build Coastguard Worker    :license: BSD, see LICENSE for more details.
10*635a8641SAndroid Build Coastguard Worker"""
11*635a8641SAndroid Build Coastguard Workerimport operator
12*635a8641SAndroid Build Coastguard Workerimport re
13*635a8641SAndroid Build Coastguard Workerfrom jinja2.runtime import Undefined
14*635a8641SAndroid Build Coastguard Workerfrom jinja2._compat import text_type, string_types, integer_types
15*635a8641SAndroid Build Coastguard Workerimport decimal
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Workernumber_re = re.compile(r'^-?\d+(\.\d+)?$')
18*635a8641SAndroid Build Coastguard Workerregex_type = type(number_re)
19*635a8641SAndroid Build Coastguard Worker
20*635a8641SAndroid Build Coastguard Worker
21*635a8641SAndroid Build Coastguard Workertest_callable = callable
22*635a8641SAndroid Build Coastguard Worker
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Workerdef test_odd(value):
25*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is odd."""
26*635a8641SAndroid Build Coastguard Worker    return value % 2 == 1
27*635a8641SAndroid Build Coastguard Worker
28*635a8641SAndroid Build Coastguard Worker
29*635a8641SAndroid Build Coastguard Workerdef test_even(value):
30*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is even."""
31*635a8641SAndroid Build Coastguard Worker    return value % 2 == 0
32*635a8641SAndroid Build Coastguard Worker
33*635a8641SAndroid Build Coastguard Worker
34*635a8641SAndroid Build Coastguard Workerdef test_divisibleby(value, num):
35*635a8641SAndroid Build Coastguard Worker    """Check if a variable is divisible by a number."""
36*635a8641SAndroid Build Coastguard Worker    return value % num == 0
37*635a8641SAndroid Build Coastguard Worker
38*635a8641SAndroid Build Coastguard Worker
39*635a8641SAndroid Build Coastguard Workerdef test_defined(value):
40*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is defined:
41*635a8641SAndroid Build Coastguard Worker
42*635a8641SAndroid Build Coastguard Worker    .. sourcecode:: jinja
43*635a8641SAndroid Build Coastguard Worker
44*635a8641SAndroid Build Coastguard Worker        {% if variable is defined %}
45*635a8641SAndroid Build Coastguard Worker            value of variable: {{ variable }}
46*635a8641SAndroid Build Coastguard Worker        {% else %}
47*635a8641SAndroid Build Coastguard Worker            variable is not defined
48*635a8641SAndroid Build Coastguard Worker        {% endif %}
49*635a8641SAndroid Build Coastguard Worker
50*635a8641SAndroid Build Coastguard Worker    See the :func:`default` filter for a simple way to set undefined
51*635a8641SAndroid Build Coastguard Worker    variables.
52*635a8641SAndroid Build Coastguard Worker    """
53*635a8641SAndroid Build Coastguard Worker    return not isinstance(value, Undefined)
54*635a8641SAndroid Build Coastguard Worker
55*635a8641SAndroid Build Coastguard Worker
56*635a8641SAndroid Build Coastguard Workerdef test_undefined(value):
57*635a8641SAndroid Build Coastguard Worker    """Like :func:`defined` but the other way round."""
58*635a8641SAndroid Build Coastguard Worker    return isinstance(value, Undefined)
59*635a8641SAndroid Build Coastguard Worker
60*635a8641SAndroid Build Coastguard Worker
61*635a8641SAndroid Build Coastguard Workerdef test_none(value):
62*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is none."""
63*635a8641SAndroid Build Coastguard Worker    return value is None
64*635a8641SAndroid Build Coastguard Worker
65*635a8641SAndroid Build Coastguard Worker
66*635a8641SAndroid Build Coastguard Workerdef test_lower(value):
67*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is lowercased."""
68*635a8641SAndroid Build Coastguard Worker    return text_type(value).islower()
69*635a8641SAndroid Build Coastguard Worker
70*635a8641SAndroid Build Coastguard Worker
71*635a8641SAndroid Build Coastguard Workerdef test_upper(value):
72*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is uppercased."""
73*635a8641SAndroid Build Coastguard Worker    return text_type(value).isupper()
74*635a8641SAndroid Build Coastguard Worker
75*635a8641SAndroid Build Coastguard Worker
76*635a8641SAndroid Build Coastguard Workerdef test_string(value):
77*635a8641SAndroid Build Coastguard Worker    """Return true if the object is a string."""
78*635a8641SAndroid Build Coastguard Worker    return isinstance(value, string_types)
79*635a8641SAndroid Build Coastguard Worker
80*635a8641SAndroid Build Coastguard Worker
81*635a8641SAndroid Build Coastguard Workerdef test_number(value):
82*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is a number."""
83*635a8641SAndroid Build Coastguard Worker    return isinstance(value, integer_types + (float, complex, decimal.Decimal))
84*635a8641SAndroid Build Coastguard Worker
85*635a8641SAndroid Build Coastguard Worker
86*635a8641SAndroid Build Coastguard Workerdef test_sequence(value):
87*635a8641SAndroid Build Coastguard Worker    """Return true if the variable is a sequence. Sequences are variables
88*635a8641SAndroid Build Coastguard Worker    that are iterable.
89*635a8641SAndroid Build Coastguard Worker    """
90*635a8641SAndroid Build Coastguard Worker    try:
91*635a8641SAndroid Build Coastguard Worker        len(value)
92*635a8641SAndroid Build Coastguard Worker        value.__getitem__
93*635a8641SAndroid Build Coastguard Worker    except:
94*635a8641SAndroid Build Coastguard Worker        return False
95*635a8641SAndroid Build Coastguard Worker    return True
96*635a8641SAndroid Build Coastguard Worker
97*635a8641SAndroid Build Coastguard Worker
98*635a8641SAndroid Build Coastguard Workerdef test_sameas(value, other):
99*635a8641SAndroid Build Coastguard Worker    """Check if an object points to the same memory address than another
100*635a8641SAndroid Build Coastguard Worker    object:
101*635a8641SAndroid Build Coastguard Worker
102*635a8641SAndroid Build Coastguard Worker    .. sourcecode:: jinja
103*635a8641SAndroid Build Coastguard Worker
104*635a8641SAndroid Build Coastguard Worker        {% if foo.attribute is sameas false %}
105*635a8641SAndroid Build Coastguard Worker            the foo attribute really is the `False` singleton
106*635a8641SAndroid Build Coastguard Worker        {% endif %}
107*635a8641SAndroid Build Coastguard Worker    """
108*635a8641SAndroid Build Coastguard Worker    return value is other
109*635a8641SAndroid Build Coastguard Worker
110*635a8641SAndroid Build Coastguard Worker
111*635a8641SAndroid Build Coastguard Workerdef test_iterable(value):
112*635a8641SAndroid Build Coastguard Worker    """Check if it's possible to iterate over an object."""
113*635a8641SAndroid Build Coastguard Worker    try:
114*635a8641SAndroid Build Coastguard Worker        iter(value)
115*635a8641SAndroid Build Coastguard Worker    except TypeError:
116*635a8641SAndroid Build Coastguard Worker        return False
117*635a8641SAndroid Build Coastguard Worker    return True
118*635a8641SAndroid Build Coastguard Worker
119*635a8641SAndroid Build Coastguard Worker
120*635a8641SAndroid Build Coastguard Workerdef test_escaped(value):
121*635a8641SAndroid Build Coastguard Worker    """Check if the value is escaped."""
122*635a8641SAndroid Build Coastguard Worker    return hasattr(value, '__html__')
123*635a8641SAndroid Build Coastguard Worker
124*635a8641SAndroid Build Coastguard Worker
125*635a8641SAndroid Build Coastguard Workerdef test_in(value, seq):
126*635a8641SAndroid Build Coastguard Worker    """Check if value is in seq.
127*635a8641SAndroid Build Coastguard Worker
128*635a8641SAndroid Build Coastguard Worker    .. versionadded:: 2.10
129*635a8641SAndroid Build Coastguard Worker    """
130*635a8641SAndroid Build Coastguard Worker    return value in seq
131*635a8641SAndroid Build Coastguard Worker
132*635a8641SAndroid Build Coastguard Worker
133*635a8641SAndroid Build Coastguard WorkerTESTS = {
134*635a8641SAndroid Build Coastguard Worker    'odd':              test_odd,
135*635a8641SAndroid Build Coastguard Worker    'even':             test_even,
136*635a8641SAndroid Build Coastguard Worker    'divisibleby':      test_divisibleby,
137*635a8641SAndroid Build Coastguard Worker    'defined':          test_defined,
138*635a8641SAndroid Build Coastguard Worker    'undefined':        test_undefined,
139*635a8641SAndroid Build Coastguard Worker    'none':             test_none,
140*635a8641SAndroid Build Coastguard Worker    'lower':            test_lower,
141*635a8641SAndroid Build Coastguard Worker    'upper':            test_upper,
142*635a8641SAndroid Build Coastguard Worker    'string':           test_string,
143*635a8641SAndroid Build Coastguard Worker    'number':           test_number,
144*635a8641SAndroid Build Coastguard Worker    'sequence':         test_sequence,
145*635a8641SAndroid Build Coastguard Worker    'iterable':         test_iterable,
146*635a8641SAndroid Build Coastguard Worker    'callable':         test_callable,
147*635a8641SAndroid Build Coastguard Worker    'sameas':           test_sameas,
148*635a8641SAndroid Build Coastguard Worker    'escaped':          test_escaped,
149*635a8641SAndroid Build Coastguard Worker    'in':               test_in,
150*635a8641SAndroid Build Coastguard Worker    '==':               operator.eq,
151*635a8641SAndroid Build Coastguard Worker    'eq':               operator.eq,
152*635a8641SAndroid Build Coastguard Worker    'equalto':          operator.eq,
153*635a8641SAndroid Build Coastguard Worker    '!=':               operator.ne,
154*635a8641SAndroid Build Coastguard Worker    'ne':               operator.ne,
155*635a8641SAndroid Build Coastguard Worker    '>':                operator.gt,
156*635a8641SAndroid Build Coastguard Worker    'gt':               operator.gt,
157*635a8641SAndroid Build Coastguard Worker    'greaterthan':      operator.gt,
158*635a8641SAndroid Build Coastguard Worker    'ge':               operator.ge,
159*635a8641SAndroid Build Coastguard Worker    '>=':               operator.ge,
160*635a8641SAndroid Build Coastguard Worker    '<':                operator.lt,
161*635a8641SAndroid Build Coastguard Worker    'lt':               operator.lt,
162*635a8641SAndroid Build Coastguard Worker    'lessthan':         operator.lt,
163*635a8641SAndroid Build Coastguard Worker    '<=':               operator.le,
164*635a8641SAndroid Build Coastguard Worker    'le':               operator.le,
165*635a8641SAndroid Build Coastguard Worker}
166