11cb0ef41Sopenharmony_ci# -*- coding: utf-8 -*-
21cb0ef41Sopenharmony_ci"""
31cb0ef41Sopenharmony_ci    jinja2.tests
41cb0ef41Sopenharmony_ci    ~~~~~~~~~~~~
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci    Jinja test functions. Used with the "is" operator.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci    :copyright: (c) 2017 by the Jinja Team.
91cb0ef41Sopenharmony_ci    :license: BSD, see LICENSE for more details.
101cb0ef41Sopenharmony_ci"""
111cb0ef41Sopenharmony_ciimport operator
121cb0ef41Sopenharmony_ciimport re
131cb0ef41Sopenharmony_cifrom collections.abc import Mapping
141cb0ef41Sopenharmony_cifrom jinja2.runtime import Undefined
151cb0ef41Sopenharmony_cifrom jinja2._compat import text_type, string_types, integer_types
161cb0ef41Sopenharmony_ciimport decimal
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cinumber_re = re.compile(r'^-?\d+(\.\d+)?$')
191cb0ef41Sopenharmony_ciregex_type = type(number_re)
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_citest_callable = callable
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cidef test_odd(value):
261cb0ef41Sopenharmony_ci    """Return true if the variable is odd."""
271cb0ef41Sopenharmony_ci    return value % 2 == 1
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cidef test_even(value):
311cb0ef41Sopenharmony_ci    """Return true if the variable is even."""
321cb0ef41Sopenharmony_ci    return value % 2 == 0
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cidef test_divisibleby(value, num):
361cb0ef41Sopenharmony_ci    """Check if a variable is divisible by a number."""
371cb0ef41Sopenharmony_ci    return value % num == 0
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cidef test_defined(value):
411cb0ef41Sopenharmony_ci    """Return true if the variable is defined:
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    .. sourcecode:: jinja
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci        {% if variable is defined %}
461cb0ef41Sopenharmony_ci            value of variable: {{ variable }}
471cb0ef41Sopenharmony_ci        {% else %}
481cb0ef41Sopenharmony_ci            variable is not defined
491cb0ef41Sopenharmony_ci        {% endif %}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    See the :func:`default` filter for a simple way to set undefined
521cb0ef41Sopenharmony_ci    variables.
531cb0ef41Sopenharmony_ci    """
541cb0ef41Sopenharmony_ci    return not isinstance(value, Undefined)
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cidef test_undefined(value):
581cb0ef41Sopenharmony_ci    """Like :func:`defined` but the other way round."""
591cb0ef41Sopenharmony_ci    return isinstance(value, Undefined)
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cidef test_none(value):
631cb0ef41Sopenharmony_ci    """Return true if the variable is none."""
641cb0ef41Sopenharmony_ci    return value is None
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cidef test_lower(value):
681cb0ef41Sopenharmony_ci    """Return true if the variable is lowercased."""
691cb0ef41Sopenharmony_ci    return text_type(value).islower()
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cidef test_upper(value):
731cb0ef41Sopenharmony_ci    """Return true if the variable is uppercased."""
741cb0ef41Sopenharmony_ci    return text_type(value).isupper()
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cidef test_string(value):
781cb0ef41Sopenharmony_ci    """Return true if the object is a string."""
791cb0ef41Sopenharmony_ci    return isinstance(value, string_types)
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cidef test_mapping(value):
831cb0ef41Sopenharmony_ci    """Return true if the object is a mapping (dict etc.).
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    .. versionadded:: 2.6
861cb0ef41Sopenharmony_ci    """
871cb0ef41Sopenharmony_ci    return isinstance(value, Mapping)
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_cidef test_number(value):
911cb0ef41Sopenharmony_ci    """Return true if the variable is a number."""
921cb0ef41Sopenharmony_ci    return isinstance(value, integer_types + (float, complex, decimal.Decimal))
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_cidef test_sequence(value):
961cb0ef41Sopenharmony_ci    """Return true if the variable is a sequence. Sequences are variables
971cb0ef41Sopenharmony_ci    that are iterable.
981cb0ef41Sopenharmony_ci    """
991cb0ef41Sopenharmony_ci    try:
1001cb0ef41Sopenharmony_ci        len(value)
1011cb0ef41Sopenharmony_ci        value.__getitem__
1021cb0ef41Sopenharmony_ci    except:
1031cb0ef41Sopenharmony_ci        return False
1041cb0ef41Sopenharmony_ci    return True
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_cidef test_sameas(value, other):
1081cb0ef41Sopenharmony_ci    """Check if an object points to the same memory address than another
1091cb0ef41Sopenharmony_ci    object:
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    .. sourcecode:: jinja
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci        {% if foo.attribute is sameas false %}
1141cb0ef41Sopenharmony_ci            the foo attribute really is the `False` singleton
1151cb0ef41Sopenharmony_ci        {% endif %}
1161cb0ef41Sopenharmony_ci    """
1171cb0ef41Sopenharmony_ci    return value is other
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_cidef test_iterable(value):
1211cb0ef41Sopenharmony_ci    """Check if it's possible to iterate over an object."""
1221cb0ef41Sopenharmony_ci    try:
1231cb0ef41Sopenharmony_ci        iter(value)
1241cb0ef41Sopenharmony_ci    except TypeError:
1251cb0ef41Sopenharmony_ci        return False
1261cb0ef41Sopenharmony_ci    return True
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_cidef test_escaped(value):
1301cb0ef41Sopenharmony_ci    """Check if the value is escaped."""
1311cb0ef41Sopenharmony_ci    return hasattr(value, '__html__')
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_cidef test_in(value, seq):
1351cb0ef41Sopenharmony_ci    """Check if value is in seq.
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci    .. versionadded:: 2.10
1381cb0ef41Sopenharmony_ci    """
1391cb0ef41Sopenharmony_ci    return value in seq
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciTESTS = {
1431cb0ef41Sopenharmony_ci    'odd':              test_odd,
1441cb0ef41Sopenharmony_ci    'even':             test_even,
1451cb0ef41Sopenharmony_ci    'divisibleby':      test_divisibleby,
1461cb0ef41Sopenharmony_ci    'defined':          test_defined,
1471cb0ef41Sopenharmony_ci    'undefined':        test_undefined,
1481cb0ef41Sopenharmony_ci    'none':             test_none,
1491cb0ef41Sopenharmony_ci    'lower':            test_lower,
1501cb0ef41Sopenharmony_ci    'upper':            test_upper,
1511cb0ef41Sopenharmony_ci    'string':           test_string,
1521cb0ef41Sopenharmony_ci    'mapping':          test_mapping,
1531cb0ef41Sopenharmony_ci    'number':           test_number,
1541cb0ef41Sopenharmony_ci    'sequence':         test_sequence,
1551cb0ef41Sopenharmony_ci    'iterable':         test_iterable,
1561cb0ef41Sopenharmony_ci    'callable':         test_callable,
1571cb0ef41Sopenharmony_ci    'sameas':           test_sameas,
1581cb0ef41Sopenharmony_ci    'escaped':          test_escaped,
1591cb0ef41Sopenharmony_ci    'in':               test_in,
1601cb0ef41Sopenharmony_ci    '==':               operator.eq,
1611cb0ef41Sopenharmony_ci    'eq':               operator.eq,
1621cb0ef41Sopenharmony_ci    'equalto':          operator.eq,
1631cb0ef41Sopenharmony_ci    '!=':               operator.ne,
1641cb0ef41Sopenharmony_ci    'ne':               operator.ne,
1651cb0ef41Sopenharmony_ci    '>':                operator.gt,
1661cb0ef41Sopenharmony_ci    'gt':               operator.gt,
1671cb0ef41Sopenharmony_ci    'greaterthan':      operator.gt,
1681cb0ef41Sopenharmony_ci    'ge':               operator.ge,
1691cb0ef41Sopenharmony_ci    '>=':               operator.ge,
1701cb0ef41Sopenharmony_ci    '<':                operator.lt,
1711cb0ef41Sopenharmony_ci    'lt':               operator.lt,
1721cb0ef41Sopenharmony_ci    'lessthan':         operator.lt,
1731cb0ef41Sopenharmony_ci    '<=':               operator.le,
1741cb0ef41Sopenharmony_ci    'le':               operator.le,
1751cb0ef41Sopenharmony_ci}
176