blob: dc91990e1c6c26baf78567eeb751f08f0dcabb16 [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 %}',
Armin Ronacherb5124e62008-04-25 00:36:14 +020039 '{% for x in foo, %}...{% endfor %}'
Armin Ronacher1cc232c2007-09-07 17:52:41 +020040]
Armin Ronacher203bfcb2008-04-24 21:54:44 +020041TRAILINGCOMMA = '''{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020042
43
44def test_call():
Christoph Hacke9e43bb2008-04-13 23:35:48 +020045 from jinja2 import Environment
Armin Ronacherecc051b2007-06-01 18:25:28 +020046 env = Environment()
47 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
48 tmpl = env.from_string(CALL)
49 assert tmpl.render() == 'abdfh'
50
51
52def test_slicing(env):
53 tmpl = env.from_string(SLICING)
54 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
55
56
57def test_attr(env):
58 tmpl = env.from_string(ATTR)
59 assert tmpl.render(foo={'bar': 42}) == '42|42'
60
61
62def test_subscript(env):
63 tmpl = env.from_string(SUBSCRIPT)
64 assert tmpl.render(foo=[0, 1, 2]) == '0|2'
65
66
67def test_keyattr(env):
68 tmpl = env.from_string(KEYATTR)
69 assert tmpl.render() == 'foo|[]'
70
71
72def test_tuple(env):
73 tmpl = env.from_string(TUPLE)
Armin Ronacher1cc232c2007-09-07 17:52:41 +020074 assert tmpl.render() == '()|(1,)|(1, 2)'
Armin Ronacherecc051b2007-06-01 18:25:28 +020075
76
77def test_math(env):
78 tmpl = env.from_string(MATH)
79 assert tmpl.render() == '1.5|8'
80
81
82def test_div(env):
83 tmpl = env.from_string(DIV)
84 assert tmpl.render() == '1|1.5|1'
85
86
87def test_unary(env):
88 tmpl = env.from_string(UNARY)
89 assert tmpl.render() == '3|-3'
90
91
Armin Ronacher1cc232c2007-09-07 17:52:41 +020092def test_concat(env):
93 tmpl = env.from_string(CONCAT)
94 assert tmpl.render() == '[1, 2]foo'
95
96
Armin Ronacherecc051b2007-06-01 18:25:28 +020097def test_compare(env):
98 tmpl = env.from_string(COMPARE)
99 assert tmpl.render() == 'True|True|True|True|True'
100
101
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200102def test_inop(env):
103 tmpl = env.from_string(INOP)
104 assert tmpl.render() == 'True|False'
105
106
Armin Ronacherecc051b2007-06-01 18:25:28 +0200107def test_literals(env):
108 tmpl = env.from_string(LITERALS)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200109 assert tmpl.render().lower() == '[]|{}|()'
Armin Ronacherecc051b2007-06-01 18:25:28 +0200110
111
112def test_bool(env):
113 tmpl = env.from_string(BOOL)
114 assert tmpl.render() == 'False|True|True'
Armin Ronacherdb69d0a2007-06-02 01:35:53 +0200115
116
117def test_grouping(env):
118 tmpl = env.from_string(GROUPING)
119 assert tmpl.render() == 'False'
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200120
121
122def test_django_attr(env):
123 tmpl = env.from_string(DJANGOATTR)
124 assert tmpl.render() == '1'
125
126
127def test_conditional_expression(env):
128 tmpl = env.from_string(CONDEXPR)
129 assert tmpl.render() == '0'
130
131
132def test_filter_priority(env):
133 tmpl = env.from_string(FILTERPRIORITY)
134 assert tmpl.render() == 'FOOBAR'
135
136
137def test_function_calls(env):
138 tests = [
139 (True, '*foo, bar'),
140 (True, '*foo, *bar'),
141 (True, '*foo, bar=42'),
142 (True, '**foo, *bar'),
143 (True, '**foo, bar'),
144 (False, 'foo, bar'),
145 (False, 'foo, bar=42'),
146 (False, 'foo, bar=23, *args'),
147 (False, 'a, b=c, *d, **e'),
148 (False, '*foo, **bar')
149 ]
150 for should_fail, sig in tests:
151 if should_fail:
152 try:
153 print env.from_string('{{ foo(%s) }}' % sig)
154 except TemplateSyntaxError:
155 continue
156 assert False, 'expected syntax error'
157 else:
158 env.from_string('foo(%s)' % sig)
159
160
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200161def test_tuple_expr(env):
162 for tmpl in TUPLETEMPLATES:
163 assert env.from_string(tmpl)
164
165
166def test_trailing_comma(env):
167 tmpl = env.from_string(TRAILINGCOMMA)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200168 assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'