test_tests.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import pytest
  2. from markupsafe import Markup
  3. from jinja2 import Environment
  4. from jinja2 import TemplateAssertionError
  5. from jinja2 import TemplateRuntimeError
  6. class MyDict(dict):
  7. pass
  8. class TestTestsCase:
  9. def test_defined(self, env):
  10. tmpl = env.from_string("{{ missing is defined }}|{{ true is defined }}")
  11. assert tmpl.render() == "False|True"
  12. def test_even(self, env):
  13. tmpl = env.from_string("""{{ 1 is even }}|{{ 2 is even }}""")
  14. assert tmpl.render() == "False|True"
  15. def test_odd(self, env):
  16. tmpl = env.from_string("""{{ 1 is odd }}|{{ 2 is odd }}""")
  17. assert tmpl.render() == "True|False"
  18. def test_lower(self, env):
  19. tmpl = env.from_string("""{{ "foo" is lower }}|{{ "FOO" is lower }}""")
  20. assert tmpl.render() == "True|False"
  21. # Test type checks
  22. @pytest.mark.parametrize(
  23. "op,expect",
  24. (
  25. ("none is none", True),
  26. ("false is none", False),
  27. ("true is none", False),
  28. ("42 is none", False),
  29. ("none is true", False),
  30. ("false is true", False),
  31. ("true is true", True),
  32. ("0 is true", False),
  33. ("1 is true", False),
  34. ("42 is true", False),
  35. ("none is false", False),
  36. ("false is false", True),
  37. ("true is false", False),
  38. ("0 is false", False),
  39. ("1 is false", False),
  40. ("42 is false", False),
  41. ("none is boolean", False),
  42. ("false is boolean", True),
  43. ("true is boolean", True),
  44. ("0 is boolean", False),
  45. ("1 is boolean", False),
  46. ("42 is boolean", False),
  47. ("0.0 is boolean", False),
  48. ("1.0 is boolean", False),
  49. ("3.14159 is boolean", False),
  50. ("none is integer", False),
  51. ("false is integer", False),
  52. ("true is integer", False),
  53. ("42 is integer", True),
  54. ("3.14159 is integer", False),
  55. ("(10 ** 100) is integer", True),
  56. ("none is float", False),
  57. ("false is float", False),
  58. ("true is float", False),
  59. ("42 is float", False),
  60. ("4.2 is float", True),
  61. ("(10 ** 100) is float", False),
  62. ("none is number", False),
  63. ("false is number", True),
  64. ("true is number", True),
  65. ("42 is number", True),
  66. ("3.14159 is number", True),
  67. ("complex is number", True),
  68. ("(10 ** 100) is number", True),
  69. ("none is string", False),
  70. ("false is string", False),
  71. ("true is string", False),
  72. ("42 is string", False),
  73. ('"foo" is string', True),
  74. ("none is sequence", False),
  75. ("false is sequence", False),
  76. ("42 is sequence", False),
  77. ('"foo" is sequence', True),
  78. ("[] is sequence", True),
  79. ("[1, 2, 3] is sequence", True),
  80. ("{} is sequence", True),
  81. ("none is mapping", False),
  82. ("false is mapping", False),
  83. ("42 is mapping", False),
  84. ('"foo" is mapping', False),
  85. ("[] is mapping", False),
  86. ("{} is mapping", True),
  87. ("mydict is mapping", True),
  88. ("none is iterable", False),
  89. ("false is iterable", False),
  90. ("42 is iterable", False),
  91. ('"foo" is iterable', True),
  92. ("[] is iterable", True),
  93. ("{} is iterable", True),
  94. ("range(5) is iterable", True),
  95. ("none is callable", False),
  96. ("false is callable", False),
  97. ("42 is callable", False),
  98. ('"foo" is callable', False),
  99. ("[] is callable", False),
  100. ("{} is callable", False),
  101. ("range is callable", True),
  102. ),
  103. )
  104. def test_types(self, env, op, expect):
  105. t = env.from_string(f"{{{{ {op} }}}}")
  106. assert t.render(mydict=MyDict(), complex=complex(1, 2)) == str(expect)
  107. def test_upper(self, env):
  108. tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')
  109. assert tmpl.render() == "True|False"
  110. def test_equalto(self, env):
  111. tmpl = env.from_string(
  112. "{{ foo is eq 12 }}|"
  113. "{{ foo is eq 0 }}|"
  114. "{{ foo is eq (3 * 4) }}|"
  115. '{{ bar is eq "baz" }}|'
  116. '{{ bar is eq "zab" }}|'
  117. '{{ bar is eq ("ba" + "z") }}|'
  118. "{{ bar is eq bar }}|"
  119. "{{ bar is eq foo }}"
  120. )
  121. assert (
  122. tmpl.render(foo=12, bar="baz")
  123. == "True|False|True|True|False|True|True|False"
  124. )
  125. @pytest.mark.parametrize(
  126. "op,expect",
  127. (
  128. ("eq 2", True),
  129. ("eq 3", False),
  130. ("ne 3", True),
  131. ("ne 2", False),
  132. ("lt 3", True),
  133. ("lt 2", False),
  134. ("le 2", True),
  135. ("le 1", False),
  136. ("gt 1", True),
  137. ("gt 2", False),
  138. ("ge 2", True),
  139. ("ge 3", False),
  140. ),
  141. )
  142. def test_compare_aliases(self, env, op, expect):
  143. t = env.from_string(f"{{{{ 2 is {op} }}}}")
  144. assert t.render() == str(expect)
  145. def test_sameas(self, env):
  146. tmpl = env.from_string("{{ foo is sameas false }}|{{ 0 is sameas false }}")
  147. assert tmpl.render(foo=False) == "True|False"
  148. def test_no_paren_for_arg1(self, env):
  149. tmpl = env.from_string("{{ foo is sameas none }}")
  150. assert tmpl.render(foo=None) == "True"
  151. def test_escaped(self, env):
  152. env = Environment(autoescape=True)
  153. tmpl = env.from_string("{{ x is escaped }}|{{ y is escaped }}")
  154. assert tmpl.render(x="foo", y=Markup("foo")) == "False|True"
  155. def test_greaterthan(self, env):
  156. tmpl = env.from_string("{{ 1 is greaterthan 0 }}|{{ 0 is greaterthan 1 }}")
  157. assert tmpl.render() == "True|False"
  158. def test_lessthan(self, env):
  159. tmpl = env.from_string("{{ 0 is lessthan 1 }}|{{ 1 is lessthan 0 }}")
  160. assert tmpl.render() == "True|False"
  161. def test_multiple_tests(self):
  162. items = []
  163. def matching(x, y):
  164. items.append((x, y))
  165. return False
  166. env = Environment()
  167. env.tests["matching"] = matching
  168. tmpl = env.from_string(
  169. "{{ 'us-west-1' is matching '(us-east-1|ap-northeast-1)'"
  170. " or 'stage' is matching '(dev|stage)' }}"
  171. )
  172. assert tmpl.render() == "False"
  173. assert items == [
  174. ("us-west-1", "(us-east-1|ap-northeast-1)"),
  175. ("stage", "(dev|stage)"),
  176. ]
  177. def test_in(self, env):
  178. tmpl = env.from_string(
  179. '{{ "o" is in "foo" }}|'
  180. '{{ "foo" is in "foo" }}|'
  181. '{{ "b" is in "foo" }}|'
  182. "{{ 1 is in ((1, 2)) }}|"
  183. "{{ 3 is in ((1, 2)) }}|"
  184. "{{ 1 is in [1, 2] }}|"
  185. "{{ 3 is in [1, 2] }}|"
  186. '{{ "foo" is in {"foo": 1}}}|'
  187. '{{ "baz" is in {"bar": 1}}}'
  188. )
  189. assert tmpl.render() == "True|True|False|True|False|True|False|True|False"
  190. def test_name_undefined(env):
  191. with pytest.raises(TemplateAssertionError, match="No test named 'f'"):
  192. env.from_string("{{ x is f }}")
  193. def test_name_undefined_in_if(env):
  194. t = env.from_string("{% if x is defined %}{{ x is f }}{% endif %}")
  195. assert t.render() == ""
  196. with pytest.raises(TemplateRuntimeError, match="No test named 'f'"):
  197. t.render(x=1)
  198. def test_is_filter(env):
  199. assert env.call_test("filter", "title")
  200. assert not env.call_test("filter", "bad-name")
  201. def test_is_test(env):
  202. assert env.call_test("test", "number")
  203. assert not env.call_test("test", "bad-name")