Skip to content
Myr

test_tests.py

This file

Path

tests/test_tests.py

233 lines · 7.7 KiB · Python

Last changed by

Merge pull request #1391 from pallets/markupsafe-importsDavid Lordbefore adoptiona9b06f4

History of this file

Take it

The walk stopped after 200 commits: lines older than c580dd0 point at it.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 1why, one step back
1import pytest

Merge pull request #1391 from pallets/markupsafe-importsDavid Lordbefore adoptiona9b06f4

line 2why, one step back
2from markupsafe import Markup

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 3–4why, one step back
3
4from jinja2 import Environment

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 5–6why, one step back
5from jinja2 import TemplateAssertionError
6from jinja2 import TemplateRuntimeError

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 7–210why, one step back
7
89class MyDict(dict):10 pass111213class TestTestsCase:14 def test_defined(self, env):15 tmpl = env.from_string("{{ missing is defined }}|{{ true is defined }}")16 assert tmpl.render() == "False|True"1718 def test_even(self, env):19 tmpl = env.from_string("""{{ 1 is even }}|{{ 2 is even }}""")20 assert tmpl.render() == "False|True"2122 def test_odd(self, env):23 tmpl = env.from_string("""{{ 1 is odd }}|{{ 2 is odd }}""")24 assert tmpl.render() == "True|False"2526 def test_lower(self, env):27 tmpl = env.from_string("""{{ "foo" is lower }}|{{ "FOO" is lower }}""")28 assert tmpl.render() == "True|False"2930 # Test type checks31 @pytest.mark.parametrize(32 "op,expect",33 (34 ("none is none", True),35 ("false is none", False),36 ("true is none", False),37 ("42 is none", False),38 ("none is true", False),39 ("false is true", False),40 ("true is true", True),41 ("0 is true", False),42 ("1 is true", False),43 ("42 is true", False),44 ("none is false", False),45 ("false is false", True),46 ("true is false", False),47 ("0 is false", False),48 ("1 is false", False),49 ("42 is false", False),50 ("none is boolean", False),51 ("false is boolean", True),52 ("true is boolean", True),53 ("0 is boolean", False),54 ("1 is boolean", False),55 ("42 is boolean", False),56 ("0.0 is boolean", False),57 ("1.0 is boolean", False),58 ("3.14159 is boolean", False),59 ("none is integer", False),60 ("false is integer", False),61 ("true is integer", False),62 ("42 is integer", True),63 ("3.14159 is integer", False),64 ("(10 ** 100) is integer", True),65 ("none is float", False),66 ("false is float", False),67 ("true is float", False),68 ("42 is float", False),69 ("4.2 is float", True),70 ("(10 ** 100) is float", False),71 ("none is number", False),72 ("false is number", True),73 ("true is number", True),74 ("42 is number", True),75 ("3.14159 is number", True),76 ("complex is number", True),77 ("(10 ** 100) is number", True),78 ("none is string", False),79 ("false is string", False),80 ("true is string", False),81 ("42 is string", False),82 ('"foo" is string', True),83 ("none is sequence", False),84 ("false is sequence", False),85 ("42 is sequence", False),86 ('"foo" is sequence', True),87 ("[] is sequence", True),88 ("[1, 2, 3] is sequence", True),89 ("{} is sequence", True),90 ("none is mapping", False),91 ("false is mapping", False),92 ("42 is mapping", False),93 ('"foo" is mapping', False),94 ("[] is mapping", False),95 ("{} is mapping", True),96 ("mydict is mapping", True),97 ("none is iterable", False),98 ("false is iterable", False),99 ("42 is iterable", False),100 ('"foo" is iterable', True),101 ("[] is iterable", True),102 ("{} is iterable", True),103 ("range(5) is iterable", True),104 ("none is callable", False),105 ("false is callable", False),106 ("42 is callable", False),107 ('"foo" is callable', False),108 ("[] is callable", False),109 ("{} is callable", False),110 ("range is callable", True),111 ),112 )113 def test_types(self, env, op, expect):114 t = env.from_string(f"{{{{ {op} }}}}")115 assert t.render(mydict=MyDict(), complex=complex(1, 2)) == str(expect)116117 def test_upper(self, env):118 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')119 assert tmpl.render() == "True|False"120121 def test_equalto(self, env):122 tmpl = env.from_string(123 "{{ foo is eq 12 }}|"124 "{{ foo is eq 0 }}|"125 "{{ foo is eq (3 * 4) }}|"126 '{{ bar is eq "baz" }}|'127 '{{ bar is eq "zab" }}|'128 '{{ bar is eq ("ba" + "z") }}|'129 "{{ bar is eq bar }}|"130 "{{ bar is eq foo }}"131 )132 assert (133 tmpl.render(foo=12, bar="baz")134 == "True|False|True|True|False|True|True|False"135 )136137 @pytest.mark.parametrize(138 "op,expect",139 (140 ("eq 2", True),141 ("eq 3", False),142 ("ne 3", True),143 ("ne 2", False),144 ("lt 3", True),145 ("lt 2", False),146 ("le 2", True),147 ("le 1", False),148 ("gt 1", True),149 ("gt 2", False),150 ("ge 2", True),151 ("ge 3", False),152 ),153 )154 def test_compare_aliases(self, env, op, expect):155 t = env.from_string(f"{{{{ 2 is {op} }}}}")156 assert t.render() == str(expect)157158 def test_sameas(self, env):159 tmpl = env.from_string("{{ foo is sameas false }}|{{ 0 is sameas false }}")160 assert tmpl.render(foo=False) == "True|False"161162 def test_no_paren_for_arg1(self, env):163 tmpl = env.from_string("{{ foo is sameas none }}")164 assert tmpl.render(foo=None) == "True"165166 def test_escaped(self, env):167 env = Environment(autoescape=True)168 tmpl = env.from_string("{{ x is escaped }}|{{ y is escaped }}")169 assert tmpl.render(x="foo", y=Markup("foo")) == "False|True"170171 def test_greaterthan(self, env):172 tmpl = env.from_string("{{ 1 is greaterthan 0 }}|{{ 0 is greaterthan 1 }}")173 assert tmpl.render() == "True|False"174175 def test_lessthan(self, env):176 tmpl = env.from_string("{{ 0 is lessthan 1 }}|{{ 1 is lessthan 0 }}")177 assert tmpl.render() == "True|False"178179 def test_multiple_tests(self):180 items = []181182 def matching(x, y):183 items.append((x, y))184 return False185186 env = Environment()187 env.tests["matching"] = matching188 tmpl = env.from_string(189 "{{ 'us-west-1' is matching '(us-east-1|ap-northeast-1)'"190 " or 'stage' is matching '(dev|stage)' }}"191 )192 assert tmpl.render() == "False"193 assert items == [194 ("us-west-1", "(us-east-1|ap-northeast-1)"),195 ("stage", "(dev|stage)"),196 ]197198 def test_in(self, env):199 tmpl = env.from_string(200 '{{ "o" is in "foo" }}|'201 '{{ "foo" is in "foo" }}|'202 '{{ "b" is in "foo" }}|'203 "{{ 1 is in ((1, 2)) }}|"204 "{{ 3 is in ((1, 2)) }}|"205 "{{ 1 is in [1, 2] }}|"206 "{{ 3 is in [1, 2] }}|"207 '{{ "foo" is in {"foo": 1}}}|'208 '{{ "baz" is in {"bar": 1}}}'209 )210 assert tmpl.render() == "True|True|False|True|False|True|False|True|False"

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 211–233why, one step back
211
212213def test_name_undefined(env):214 with pytest.raises(TemplateAssertionError, match="No test named 'f'"):215 env.from_string("{{ x is f }}")216217218def test_name_undefined_in_if(env):219 t = env.from_string("{% if x is defined %}{{ x is f }}{% endif %}")220 assert t.render() == ""221222 with pytest.raises(TemplateRuntimeError, match="No test named 'f'"):223 t.render(x=1)224225226def test_is_filter(env):227 assert env.call_test("filter", "title")228 assert not env.call_test("filter", "bad-name")229230231def test_is_test(env):232 assert env.call_test("test", "number")233 assert not env.call_test("test", "bad-name")