blob: 4cce6a23c38b37f7101f247b27621626b7b8d574 [file] [log] [blame]
Armin Ronacherecc051b2007-06-01 18:25:28 +02001# -*- coding: utf-8 -*-
2"""
3 unit test for expression syntax
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 :copyright: 2007 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8"""
Christoph Hacke9e43bb2008-04-13 23:35:48 +02009from jinja2 import Environment, DictLoader
10from jinja2.exceptions import TemplateSyntaxError
Armin Ronacher1cc232c2007-09-07 17:52:41 +020011
Armin Ronacherecc051b2007-06-01 18:25:28 +020012
13CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''
14SLICING = '''{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}'''
15ATTR = '''{{ foo.bar }}|{{ foo['bar'] }}'''
16SUBSCRIPT = '''{{ foo[0] }}|{{ foo[-1] }}'''
17KEYATTR = '''{{ {'items': 'foo'}.items }}|{{ {}.items() }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020018TUPLE = '''{{ () }}|{{ (1,) }}|{{ (1, 2) }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020019MATH = '''{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}'''
20DIV = '''{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}'''
21UNARY = '''{{ +3 }}|{{ -3 }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020022CONCAT = '''{{ [1, 2] ~ 'foo' }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020023COMPARE = '''{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020024INOP = '''{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}'''
Armin Ronacher203bfcb2008-04-24 21:54:44 +020025LITERALS = '''{{ [] }}|{{ {} }}|{{ () }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020026BOOL = '''{{ true and false }}|{{ false or true }}|{{ not false }}'''
Armin Ronacherdb69d0a2007-06-02 01:35:53 +020027GROUPING = '''{{ (true and false) or (false and true) and not false }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020028CONDEXPR = '''{{ 0 if true else 1 }}'''
29DJANGOATTR = '''{{ [1, 2, 3].0 }}'''
30FILTERPRIORITY = '''{{ "foo"|upper + "bar"|upper }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020031TUPLETEMPLATES = [
32 '{{ () }}',
33 '{{ (1, 2) }}',
34 '{{ (1, 2,) }}',
35 '{{ 1, }}',
36 '{{ 1, 2 }}',
37 '{% for foo, bar in seq %}...{% endfor %}',
38 '{% for x in foo, bar %}...{% endfor %}',
39 '{% for x in foo, %}...{% endfor %}',
40 '{% for x in foo, recursive %}...{% endfor %}',
41 '{% for x in foo, bar recursive %}...{% endfor %}',
42 '{% for x, in foo, recursive %}...{% endfor %}'
43]
Armin Ronacher203bfcb2008-04-24 21:54:44 +020044TRAILINGCOMMA = '''{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020045
46
47def test_call():
Christoph Hacke9e43bb2008-04-13 23:35:48 +020048 from jinja2 import Environment
Armin Ronacherecc051b2007-06-01 18:25:28 +020049 env = Environment()
50 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
51 tmpl = env.from_string(CALL)
52 assert tmpl.render() == 'abdfh'
53
54
55def test_slicing(env):
56 tmpl = env.from_string(SLICING)
57 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
58
59
60def test_attr(env):
61 tmpl = env.from_string(ATTR)
62 assert tmpl.render(foo={'bar': 42}) == '42|42'
63
64
65def test_subscript(env):
66 tmpl = env.from_string(SUBSCRIPT)
67 assert tmpl.render(foo=[0, 1, 2]) == '0|2'
68
69
70def test_keyattr(env):
71 tmpl = env.from_string(KEYATTR)
72 assert tmpl.render() == 'foo|[]'
73
74
75def test_tuple(env):
76 tmpl = env.from_string(TUPLE)
Armin Ronacher1cc232c2007-09-07 17:52:41 +020077 assert tmpl.render() == '()|(1,)|(1, 2)'
Armin Ronacherecc051b2007-06-01 18:25:28 +020078
79
80def test_math(env):
81 tmpl = env.from_string(MATH)
82 assert tmpl.render() == '1.5|8'
83
84
85def test_div(env):
86 tmpl = env.from_string(DIV)
87 assert tmpl.render() == '1|1.5|1'
88
89
90def test_unary(env):
91 tmpl = env.from_string(UNARY)
92 assert tmpl.render() == '3|-3'
93
94
Armin Ronacher1cc232c2007-09-07 17:52:41 +020095def test_concat(env):
96 tmpl = env.from_string(CONCAT)
97 assert tmpl.render() == '[1, 2]foo'
98
99
Armin Ronacherecc051b2007-06-01 18:25:28 +0200100def test_compare(env):
101 tmpl = env.from_string(COMPARE)
102 assert tmpl.render() == 'True|True|True|True|True'
103
104
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200105def test_inop(env):
106 tmpl = env.from_string(INOP)
107 assert tmpl.render() == 'True|False'
108
109
Armin Ronacherecc051b2007-06-01 18:25:28 +0200110def test_literals(env):
111 tmpl = env.from_string(LITERALS)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200112 assert tmpl.render().lower() == '[]|{}|()'
Armin Ronacherecc051b2007-06-01 18:25:28 +0200113
114
115def test_bool(env):
116 tmpl = env.from_string(BOOL)
117 assert tmpl.render() == 'False|True|True'
Armin Ronacherdb69d0a2007-06-02 01:35:53 +0200118
119
120def test_grouping(env):
121 tmpl = env.from_string(GROUPING)
122 assert tmpl.render() == 'False'
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200123
124
125def test_django_attr(env):
126 tmpl = env.from_string(DJANGOATTR)
127 assert tmpl.render() == '1'
128
129
130def test_conditional_expression(env):
131 tmpl = env.from_string(CONDEXPR)
132 assert tmpl.render() == '0'
133
134
135def test_filter_priority(env):
136 tmpl = env.from_string(FILTERPRIORITY)
137 assert tmpl.render() == 'FOOBAR'
138
139
140def test_function_calls(env):
141 tests = [
142 (True, '*foo, bar'),
143 (True, '*foo, *bar'),
144 (True, '*foo, bar=42'),
145 (True, '**foo, *bar'),
146 (True, '**foo, bar'),
147 (False, 'foo, bar'),
148 (False, 'foo, bar=42'),
149 (False, 'foo, bar=23, *args'),
150 (False, 'a, b=c, *d, **e'),
151 (False, '*foo, **bar')
152 ]
153 for should_fail, sig in tests:
154 if should_fail:
155 try:
156 print env.from_string('{{ foo(%s) }}' % sig)
157 except TemplateSyntaxError:
158 continue
159 assert False, 'expected syntax error'
160 else:
161 env.from_string('foo(%s)' % sig)
162
163
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200164def test_tuple_expr(env):
165 for tmpl in TUPLETEMPLATES:
166 assert env.from_string(tmpl)
167
168
169def test_trailing_comma(env):
170 tmpl = env.from_string(TRAILINGCOMMA)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200171 assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'