tests.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.tests
  4. ~~~~~~~~~~~~
  5. Jinja test functions. Used with the "is" operator.
  6. :copyright: (c) 2010 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import re
  10. from collections import Mapping
  11. from jinja2.runtime import Undefined
  12. from jinja2._compat import text_type, string_types, integer_types
  13. import decimal
  14. number_re = re.compile(r'^-?\d+(\.\d+)?$')
  15. regex_type = type(number_re)
  16. test_callable = callable
  17. def test_odd(value):
  18. """Return true if the variable is odd."""
  19. return value % 2 == 1
  20. def test_even(value):
  21. """Return true if the variable is even."""
  22. return value % 2 == 0
  23. def test_divisibleby(value, num):
  24. """Check if a variable is divisible by a number."""
  25. return value % num == 0
  26. def test_defined(value):
  27. """Return true if the variable is defined:
  28. .. sourcecode:: jinja
  29. {% if variable is defined %}
  30. value of variable: {{ variable }}
  31. {% else %}
  32. variable is not defined
  33. {% endif %}
  34. See the :func:`default` filter for a simple way to set undefined
  35. variables.
  36. """
  37. return not isinstance(value, Undefined)
  38. def test_undefined(value):
  39. """Like :func:`defined` but the other way round."""
  40. return isinstance(value, Undefined)
  41. def test_none(value):
  42. """Return true if the variable is none."""
  43. return value is None
  44. def test_lower(value):
  45. """Return true if the variable is lowercased."""
  46. return text_type(value).islower()
  47. def test_upper(value):
  48. """Return true if the variable is uppercased."""
  49. return text_type(value).isupper()
  50. def test_string(value):
  51. """Return true if the object is a string."""
  52. return isinstance(value, string_types)
  53. def test_mapping(value):
  54. """Return true if the object is a mapping (dict etc.).
  55. .. versionadded:: 2.6
  56. """
  57. return isinstance(value, Mapping)
  58. def test_number(value):
  59. """Return true if the variable is a number."""
  60. return isinstance(value, integer_types + (float, complex, decimal.Decimal))
  61. def test_sequence(value):
  62. """Return true if the variable is a sequence. Sequences are variables
  63. that are iterable.
  64. """
  65. try:
  66. len(value)
  67. value.__getitem__
  68. except:
  69. return False
  70. return True
  71. def test_equalto(value, other):
  72. """Check if an object has the same value as another object:
  73. .. sourcecode:: jinja
  74. {% if foo.expression is equalto 42 %}
  75. the foo attribute evaluates to the constant 42
  76. {% endif %}
  77. This appears to be a useless test as it does exactly the same as the
  78. ``==`` operator, but it can be useful when used together with the
  79. `selectattr` function:
  80. .. sourcecode:: jinja
  81. {{ users|selectattr("email", "equalto", "foo@bar.invalid") }}
  82. .. versionadded:: 2.8
  83. """
  84. return value == other
  85. def test_sameas(value, other):
  86. """Check if an object points to the same memory address than another
  87. object:
  88. .. sourcecode:: jinja
  89. {% if foo.attribute is sameas false %}
  90. the foo attribute really is the `False` singleton
  91. {% endif %}
  92. """
  93. return value is other
  94. def test_iterable(value):
  95. """Check if it's possible to iterate over an object."""
  96. try:
  97. iter(value)
  98. except TypeError:
  99. return False
  100. return True
  101. def test_escaped(value):
  102. """Check if the value is escaped."""
  103. return hasattr(value, '__html__')
  104. TESTS = {
  105. 'odd': test_odd,
  106. 'even': test_even,
  107. 'divisibleby': test_divisibleby,
  108. 'defined': test_defined,
  109. 'undefined': test_undefined,
  110. 'none': test_none,
  111. 'lower': test_lower,
  112. 'upper': test_upper,
  113. 'string': test_string,
  114. 'mapping': test_mapping,
  115. 'number': test_number,
  116. 'sequence': test_sequence,
  117. 'iterable': test_iterable,
  118. 'callable': test_callable,
  119. 'sameas': test_sameas,
  120. 'equalto': test_equalto,
  121. 'escaped': test_escaped
  122. }