blob: 84df5ea7577495ea5c76f29dfcd1416248d2b2e0 [file] [log] [blame]
Armin Ronacher41d6f712010-02-09 16:43:12 +01001# -*- coding: utf-8 -*-
2"""
3 jinja2.testsuite.tests
4 ~~~~~~~~~~~~~~~~~~~~~~
5
6 Who tests the tests?
7
Armin Ronacherbbe0a412017-01-07 16:17:14 +01008 :copyright: (c) 2017 by the Jinja Team.
Armin Ronacher41d6f712010-02-09 16:43:12 +01009 :license: BSD, see LICENSE for more details.
10"""
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053011import pytest
Armin Ronacher41d6f712010-02-09 16:43:12 +010012
13from jinja2 import Markup, Environment
14
Armin Ronacher41d6f712010-02-09 16:43:12 +010015
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053016@pytest.mark.test_tests
Armin Ronacher73e2b512017-01-06 21:00:01 +010017class TestTestsCase(object):
Armin Ronacher41d6f712010-02-09 16:43:12 +010018
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053019 def test_defined(self, env):
20 tmpl = env.from_string('{{ missing is defined }}|'
21 '{{ true is defined }}')
Armin Ronacher41d6f712010-02-09 16:43:12 +010022 assert tmpl.render() == 'False|True'
23
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053024 def test_even(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010025 tmpl = env.from_string('''{{ 1 is even }}|{{ 2 is even }}''')
26 assert tmpl.render() == 'False|True'
27
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053028 def test_odd(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010029 tmpl = env.from_string('''{{ 1 is odd }}|{{ 2 is odd }}''')
30 assert tmpl.render() == 'True|False'
31
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053032 def test_lower(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010033 tmpl = env.from_string('''{{ "foo" is lower }}|{{ "FOO" is lower }}''')
34 assert tmpl.render() == 'True|False'
35
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053036 def test_typechecks(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010037 tmpl = env.from_string('''
38 {{ 42 is undefined }}
39 {{ 42 is defined }}
40 {{ 42 is none }}
41 {{ none is none }}
42 {{ 42 is number }}
43 {{ 42 is string }}
44 {{ "foo" is string }}
45 {{ "foo" is sequence }}
46 {{ [1] is sequence }}
47 {{ range is callable }}
48 {{ 42 is callable }}
49 {{ range(5) is iterable }}
Armin Ronacheree352ec2011-05-24 16:40:23 +020050 {{ {} is mapping }}
51 {{ mydict is mapping }}
52 {{ [] is mapping }}
ThiefMaster9bf5fcb2015-02-06 01:09:13 +010053 {{ 10 is number }}
54 {{ (10 ** 100) is number }}
ThiefMaster1e73c092015-02-06 01:12:09 +010055 {{ 3.14159 is number }}
56 {{ complex is number }}
Armin Ronacher41d6f712010-02-09 16:43:12 +010057 ''')
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053058
Armin Ronacheree352ec2011-05-24 16:40:23 +020059 class MyDict(dict):
60 pass
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053061
ThiefMaster1e73c092015-02-06 01:12:09 +010062 assert tmpl.render(mydict=MyDict(), complex=complex(1, 2)).split() == [
Armin Ronacher41d6f712010-02-09 16:43:12 +010063 'False', 'True', 'False', 'True', 'True', 'False',
Armin Ronacheree352ec2011-05-24 16:40:23 +020064 'True', 'True', 'True', 'True', 'False', 'True',
ThiefMaster1e73c092015-02-06 01:12:09 +010065 'True', 'True', 'False', 'True', 'True', 'True', 'True'
Armin Ronacher41d6f712010-02-09 16:43:12 +010066 ]
67
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053068 def test_sequence(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010069 tmpl = env.from_string(
70 '{{ [1, 2, 3] is sequence }}|'
71 '{{ "foo" is sequence }}|'
72 '{{ 42 is sequence }}'
73 )
74 assert tmpl.render() == 'True|True|False'
75
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053076 def test_upper(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +010077 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')
78 assert tmpl.render() == 'True|False'
79
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053080 def test_equalto(self, env):
David Lordd62f0502017-01-19 20:40:31 -080081 tmpl = env.from_string(
82 '{{ foo is eq 12 }}|'
83 '{{ foo is eq 0 }}|'
84 '{{ foo is eq (3 * 4) }}|'
85 '{{ bar is eq "baz" }}|'
86 '{{ bar is eq "zab" }}|'
87 '{{ bar is eq ("ba" + "z") }}|'
88 '{{ bar is eq bar }}|'
89 '{{ bar is eq foo }}'
90 )
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +053091 assert tmpl.render(foo=12, bar="baz") \
92 == 'True|False|True|True|False|True|True|False'
Thomas van Noort40367c42013-11-22 13:50:51 +010093
David Lordd62f0502017-01-19 20:40:31 -080094 @pytest.mark.parametrize('op,expect', (
95 ('eq 2', True),
96 ('eq 3', False),
97 ('ne 3', True),
98 ('ne 2', False),
99 ('lt 3', True),
100 ('lt 2', False),
101 ('le 2', True),
102 ('le 1', False),
103 ('gt 1', True),
104 ('gt 2', False),
105 ('ge 2', True),
106 ('ge 3', False),
107 ))
108 def test_compare_aliases(self, env, op, expect):
109 t = env.from_string('{{{{ 2 is {op} }}}}'.format(op=op))
110 assert t.render() == str(expect)
111
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +0530112 def test_sameas(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +0100113 tmpl = env.from_string('{{ foo is sameas false }}|'
114 '{{ 0 is sameas false }}')
115 assert tmpl.render(foo=False) == 'True|False'
116
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +0530117 def test_no_paren_for_arg1(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +0100118 tmpl = env.from_string('{{ foo is sameas none }}')
119 assert tmpl.render(foo=None) == 'True'
120
Kartheek Lenkala9d4afa12015-03-22 15:28:54 +0530121 def test_escaped(self, env):
Armin Ronacher41d6f712010-02-09 16:43:12 +0100122 env = Environment(autoescape=True)
123 tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}')
124 assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'
Major Hayden65fbf322017-01-04 10:21:53 -0600125
126 def test_greaterthan(self, env):
127 tmpl = env.from_string('{{ 1 is greaterthan 0 }}|'
128 '{{ 0 is greaterthan 1 }}')
129 assert tmpl.render() == 'True|False'
130
131 def test_lessthan(self, env):
132 tmpl = env.from_string('{{ 0 is lessthan 1 }}|'
133 '{{ 1 is lessthan 0 }}')
134 assert tmpl.render() == 'True|False'
Armin Ronacher147bd572017-01-07 15:15:06 +0100135
136 def test_multiple_tests(self):
137 items = []
138 def matching(x, y):
139 items.append((x, y))
140 return False
141 env = Environment()
142 env.tests['matching'] = matching
143 tmpl = env.from_string("{{ 'us-west-1' is matching "
144 "'(us-east-1|ap-northeast-1)' "
145 "or 'stage' is matching '(dev|stage)' }}")
146 assert tmpl.render() == 'False'
147 assert items == [('us-west-1', '(us-east-1|ap-northeast-1)'),
148 ('stage', '(dev|stage)')]
Dirk D7ab79c62017-01-12 22:03:38 +0100149
150 def test_in(self, env):
151 tmpl = env.from_string('{{ "o" is in "foo" }}|'
152 '{{ "foo" is in "foo" }}|'
153 '{{ "b" is in "foo" }}|'
154 '{{ 1 is in ((1, 2)) }}|'
155 '{{ 3 is in ((1, 2)) }}|'
156 '{{ 1 is in [1, 2] }}|'
157 '{{ 3 is in [1, 2] }}|'
158 '{{ "foo" is in {"foo": 1}}}|'
159 '{{ "baz" is in {"bar": 1}}}')
160 assert tmpl.render() \
161 == 'True|True|False|True|False|True|False|True|False'