blob: b76fe5f7dbc11706b66a0808b44e3c48fb8e5fb2 [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] }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020017TUPLE = '''{{ () }}|{{ (1,) }}|{{ (1, 2) }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020018MATH = '''{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}'''
19DIV = '''{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}'''
20UNARY = '''{{ +3 }}|{{ -3 }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020021CONCAT = '''{{ [1, 2] ~ 'foo' }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020022COMPARE = '''{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020023INOP = '''{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}'''
Armin Ronacher203bfcb2008-04-24 21:54:44 +020024LITERALS = '''{{ [] }}|{{ {} }}|{{ () }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020025BOOL = '''{{ true and false }}|{{ false or true }}|{{ not false }}'''
Armin Ronacherdb69d0a2007-06-02 01:35:53 +020026GROUPING = '''{{ (true and false) or (false and true) and not false }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020027CONDEXPR = '''{{ 0 if true else 1 }}'''
28DJANGOATTR = '''{{ [1, 2, 3].0 }}'''
29FILTERPRIORITY = '''{{ "foo"|upper + "bar"|upper }}'''
Armin Ronacher1cc232c2007-09-07 17:52:41 +020030TUPLETEMPLATES = [
31 '{{ () }}',
32 '{{ (1, 2) }}',
33 '{{ (1, 2,) }}',
34 '{{ 1, }}',
35 '{{ 1, 2 }}',
36 '{% for foo, bar in seq %}...{% endfor %}',
37 '{% for x in foo, bar %}...{% endfor %}',
Armin Ronacherb5124e62008-04-25 00:36:14 +020038 '{% for x in foo, %}...{% endfor %}'
Armin Ronacher1cc232c2007-09-07 17:52:41 +020039]
Armin Ronacher203bfcb2008-04-24 21:54:44 +020040TRAILINGCOMMA = '''{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020041
42
43def test_call():
Christoph Hacke9e43bb2008-04-13 23:35:48 +020044 from jinja2 import Environment
Armin Ronacherecc051b2007-06-01 18:25:28 +020045 env = Environment()
46 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
47 tmpl = env.from_string(CALL)
48 assert tmpl.render() == 'abdfh'
49
50
51def test_slicing(env):
52 tmpl = env.from_string(SLICING)
53 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
54
55
56def test_attr(env):
57 tmpl = env.from_string(ATTR)
58 assert tmpl.render(foo={'bar': 42}) == '42|42'
59
60
61def test_subscript(env):
62 tmpl = env.from_string(SUBSCRIPT)
63 assert tmpl.render(foo=[0, 1, 2]) == '0|2'
64
65
Armin Ronacherecc051b2007-06-01 18:25:28 +020066def test_tuple(env):
67 tmpl = env.from_string(TUPLE)
Armin Ronacher1cc232c2007-09-07 17:52:41 +020068 assert tmpl.render() == '()|(1,)|(1, 2)'
Armin Ronacherecc051b2007-06-01 18:25:28 +020069
70
71def test_math(env):
72 tmpl = env.from_string(MATH)
73 assert tmpl.render() == '1.5|8'
74
75
76def test_div(env):
77 tmpl = env.from_string(DIV)
78 assert tmpl.render() == '1|1.5|1'
79
80
81def test_unary(env):
82 tmpl = env.from_string(UNARY)
83 assert tmpl.render() == '3|-3'
84
85
Armin Ronacher1cc232c2007-09-07 17:52:41 +020086def test_concat(env):
87 tmpl = env.from_string(CONCAT)
88 assert tmpl.render() == '[1, 2]foo'
89
90
Armin Ronacherecc051b2007-06-01 18:25:28 +020091def test_compare(env):
92 tmpl = env.from_string(COMPARE)
93 assert tmpl.render() == 'True|True|True|True|True'
94
95
Armin Ronacher1cc232c2007-09-07 17:52:41 +020096def test_inop(env):
97 tmpl = env.from_string(INOP)
98 assert tmpl.render() == 'True|False'
99
100
Armin Ronacherecc051b2007-06-01 18:25:28 +0200101def test_literals(env):
102 tmpl = env.from_string(LITERALS)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200103 assert tmpl.render().lower() == '[]|{}|()'
Armin Ronacherecc051b2007-06-01 18:25:28 +0200104
105
106def test_bool(env):
107 tmpl = env.from_string(BOOL)
108 assert tmpl.render() == 'False|True|True'
Armin Ronacherdb69d0a2007-06-02 01:35:53 +0200109
110
111def test_grouping(env):
112 tmpl = env.from_string(GROUPING)
113 assert tmpl.render() == 'False'
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200114
115
116def test_django_attr(env):
117 tmpl = env.from_string(DJANGOATTR)
118 assert tmpl.render() == '1'
119
120
121def test_conditional_expression(env):
122 tmpl = env.from_string(CONDEXPR)
123 assert tmpl.render() == '0'
124
125
126def test_filter_priority(env):
127 tmpl = env.from_string(FILTERPRIORITY)
128 assert tmpl.render() == 'FOOBAR'
129
130
131def test_function_calls(env):
132 tests = [
133 (True, '*foo, bar'),
134 (True, '*foo, *bar'),
135 (True, '*foo, bar=42'),
136 (True, '**foo, *bar'),
137 (True, '**foo, bar'),
138 (False, 'foo, bar'),
139 (False, 'foo, bar=42'),
140 (False, 'foo, bar=23, *args'),
141 (False, 'a, b=c, *d, **e'),
142 (False, '*foo, **bar')
143 ]
144 for should_fail, sig in tests:
145 if should_fail:
146 try:
147 print env.from_string('{{ foo(%s) }}' % sig)
148 except TemplateSyntaxError:
149 continue
150 assert False, 'expected syntax error'
151 else:
152 env.from_string('foo(%s)' % sig)
153
154
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200155def test_tuple_expr(env):
156 for tmpl in TUPLETEMPLATES:
Armin Ronacher115de2e2008-05-01 22:20:05 +0200157 print tmpl
Armin Ronacher1cc232c2007-09-07 17:52:41 +0200158 assert env.from_string(tmpl)
159
160
161def test_trailing_comma(env):
162 tmpl = env.from_string(TRAILINGCOMMA)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200163 assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'