Home | History | Annotate | Download | only in jinja2
      1 # -*- coding: utf-8 -*-
      2 """
      3     jinja2.tests
      4     ~~~~~~~~~~~~
      5 
      6     Jinja test functions. Used with the "is" operator.
      7 
      8     :copyright: (c) 2010 by the Jinja Team.
      9     :license: BSD, see LICENSE for more details.
     10 """
     11 import re
     12 from jinja2.runtime import Undefined
     13 
     14 try:
     15     from collections import Mapping as MappingType
     16 except ImportError:
     17     import UserDict
     18     MappingType = (UserDict.UserDict, UserDict.DictMixin, dict)
     19 
     20 # nose, nothing here to test
     21 __test__ = False
     22 
     23 
     24 number_re = re.compile(r'^-?\d+(\.\d+)?$')
     25 regex_type = type(number_re)
     26 
     27 
     28 try:
     29     test_callable = callable
     30 except NameError:
     31     def test_callable(x):
     32         return hasattr(x, '__call__')
     33 
     34 
     35 def test_odd(value):
     36     """Return true if the variable is odd."""
     37     return value % 2 == 1
     38 
     39 
     40 def test_even(value):
     41     """Return true if the variable is even."""
     42     return value % 2 == 0
     43 
     44 
     45 def test_divisibleby(value, num):
     46     """Check if a variable is divisible by a number."""
     47     return value % num == 0
     48 
     49 
     50 def test_defined(value):
     51     """Return true if the variable is defined:
     52 
     53     .. sourcecode:: jinja
     54 
     55         {% if variable is defined %}
     56             value of variable: {{ variable }}
     57         {% else %}
     58             variable is not defined
     59         {% endif %}
     60 
     61     See the :func:`default` filter for a simple way to set undefined
     62     variables.
     63     """
     64     return not isinstance(value, Undefined)
     65 
     66 
     67 def test_undefined(value):
     68     """Like :func:`defined` but the other way round."""
     69     return isinstance(value, Undefined)
     70 
     71 
     72 def test_none(value):
     73     """Return true if the variable is none."""
     74     return value is None
     75 
     76 
     77 def test_lower(value):
     78     """Return true if the variable is lowercased."""
     79     return unicode(value).islower()
     80 
     81 
     82 def test_upper(value):
     83     """Return true if the variable is uppercased."""
     84     return unicode(value).isupper()
     85 
     86 
     87 def test_string(value):
     88     """Return true if the object is a string."""
     89     return isinstance(value, basestring)
     90 
     91 
     92 def test_mapping(value):
     93     """Return true if the object is a mapping (dict etc.).
     94 
     95     .. versionadded:: 2.6
     96     """
     97     return isinstance(value, MappingType)
     98 
     99 
    100 def test_number(value):
    101     """Return true if the variable is a number."""
    102     return isinstance(value, (int, long, float, complex))
    103 
    104 
    105 def test_sequence(value):
    106     """Return true if the variable is a sequence. Sequences are variables
    107     that are iterable.
    108     """
    109     try:
    110         len(value)
    111         value.__getitem__
    112     except:
    113         return False
    114     return True
    115 
    116 
    117 def test_sameas(value, other):
    118     """Check if an object points to the same memory address than another
    119     object:
    120 
    121     .. sourcecode:: jinja
    122 
    123         {% if foo.attribute is sameas false %}
    124             the foo attribute really is the `False` singleton
    125         {% endif %}
    126     """
    127     return value is other
    128 
    129 
    130 def test_iterable(value):
    131     """Check if it's possible to iterate over an object."""
    132     try:
    133         iter(value)
    134     except TypeError:
    135         return False
    136     return True
    137 
    138 
    139 def test_escaped(value):
    140     """Check if the value is escaped."""
    141     return hasattr(value, '__html__')
    142 
    143 
    144 TESTS = {
    145     'odd':              test_odd,
    146     'even':             test_even,
    147     'divisibleby':      test_divisibleby,
    148     'defined':          test_defined,
    149     'undefined':        test_undefined,
    150     'none':             test_none,
    151     'lower':            test_lower,
    152     'upper':            test_upper,
    153     'string':           test_string,
    154     'mapping':          test_mapping,
    155     'number':           test_number,
    156     'sequence':         test_sequence,
    157     'iterable':         test_iterable,
    158     'callable':         test_callable,
    159     'sameas':           test_sameas,
    160     'escaped':          test_escaped
    161 }
    162