tests.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 jinja2.runtime import Undefined
  11. from jinja2._compat import text_type, string_types, mapping_types
  12. number_re = re.compile(r'^-?\d+(\.\d+)?$')
  13. regex_type = type(number_re)
  14. test_callable = callable
  15. def test_odd(value):
  16. """Return true if the variable is odd."""
  17. return value % 2 == 1
  18. def test_even(value):
  19. """Return true if the variable is even."""
  20. return value % 2 == 0
  21. def test_divisibleby(value, num):
  22. """Check if a variable is divisible by a number."""
  23. return value % num == 0
  24. def test_defined(value):
  25. """Return true if the variable is defined:
  26. .. sourcecode:: jinja
  27. {% if variable is defined %}
  28. value of variable: {{ variable }}
  29. {% else %}
  30. variable is not defined
  31. {% endif %}
  32. See the :func:`default` filter for a simple way to set undefined
  33. variables.
  34. """
  35. return not isinstance(value, Undefined)
  36. def test_undefined(value):
  37. """Like :func:`defined` but the other way round."""
  38. return isinstance(value, Undefined)
  39. def test_none(value):
  40. """Return true if the variable is none."""
  41. return value is None
  42. def test_lower(value):
  43. """Return true if the variable is lowercased."""
  44. return text_type(value).islower()
  45. def test_upper(value):
  46. """Return true if the variable is uppercased."""
  47. return text_type(value).isupper()
  48. def test_string(value):
  49. """Return true if the object is a string."""
  50. return isinstance(value, string_types)
  51. def test_mapping(value):
  52. """Return true if the object is a mapping (dict etc.).
  53. .. versionadded:: 2.6
  54. """
  55. return isinstance(value, mapping_types)
  56. def test_number(value):
  57. """Return true if the variable is a number."""
  58. return isinstance(value, (int, float, complex))
  59. def test_sequence(value):
  60. """Return true if the variable is a sequence. Sequences are variables
  61. that are iterable.
  62. """
  63. try:
  64. len(value)
  65. value.__getitem__
  66. except:
  67. return False
  68. return True
  69. def test_sameas(value, other):
  70. """Check if an object points to the same memory address than another
  71. object:
  72. .. sourcecode:: jinja
  73. {% if foo.attribute is sameas false %}
  74. the foo attribute really is the `False` singleton
  75. {% endif %}
  76. """
  77. return value is other
  78. def test_iterable(value):
  79. """Check if it's possible to iterate over an object."""
  80. try:
  81. iter(value)
  82. except TypeError:
  83. return False
  84. return True
  85. def test_escaped(value):
  86. """Check if the value is escaped."""
  87. return hasattr(value, '__html__')
  88. TESTS = {
  89. 'odd': test_odd,
  90. 'even': test_even,
  91. 'divisibleby': test_divisibleby,
  92. 'defined': test_defined,
  93. 'undefined': test_undefined,
  94. 'none': test_none,
  95. 'lower': test_lower,
  96. 'upper': test_upper,
  97. 'string': test_string,
  98. 'mapping': test_mapping,
  99. 'number': test_number,
  100. 'sequence': test_sequence,
  101. 'iterable': test_iterable,
  102. 'callable': test_callable,
  103. 'sameas': test_sameas,
  104. 'escaped': test_escaped
  105. }