blob: e6a8714e1731b6a5c56a73725c1d9024574dfceb [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"""
9
10CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''
11SLICING = '''{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}'''
12ATTR = '''{{ foo.bar }}|{{ foo['bar'] }}'''
13SUBSCRIPT = '''{{ foo[0] }}|{{ foo[-1] }}'''
14KEYATTR = '''{{ {'items': 'foo'}.items }}|{{ {}.items() }}'''
15TUPLE = '''{{ () }}'''
16MATH = '''{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}'''
17DIV = '''{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}'''
18UNARY = '''{{ +3 }}|{{ -3 }}'''
19COMPARE = '''{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}'''
20LITERALS = '''{{ [] }}|{{ {} }}|{{ '' }}'''
21BOOL = '''{{ true and false }}|{{ false or true }}|{{ not false }}'''
Armin Ronacherdb69d0a2007-06-02 01:35:53 +020022GROUPING = '''{{ (true and false) or (false and true) and not false }}'''
Armin Ronacherecc051b2007-06-01 18:25:28 +020023
24
25def test_call():
26 from jinja import Environment
27 env = Environment()
28 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
29 tmpl = env.from_string(CALL)
30 assert tmpl.render() == 'abdfh'
31
32
33def test_slicing(env):
34 tmpl = env.from_string(SLICING)
35 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
36
37
38def test_attr(env):
39 tmpl = env.from_string(ATTR)
40 assert tmpl.render(foo={'bar': 42}) == '42|42'
41
42
43def test_subscript(env):
44 tmpl = env.from_string(SUBSCRIPT)
45 assert tmpl.render(foo=[0, 1, 2]) == '0|2'
46
47
48def test_keyattr(env):
49 tmpl = env.from_string(KEYATTR)
50 assert tmpl.render() == 'foo|[]'
51
52
53def test_tuple(env):
54 tmpl = env.from_string(TUPLE)
55 assert tmpl.render() == '[]'
56
57
58def test_math(env):
59 tmpl = env.from_string(MATH)
60 assert tmpl.render() == '1.5|8'
61
62
63def test_div(env):
64 tmpl = env.from_string(DIV)
65 assert tmpl.render() == '1|1.5|1'
66
67
68def test_unary(env):
69 tmpl = env.from_string(UNARY)
70 assert tmpl.render() == '3|-3'
71
72
73def test_compare(env):
74 tmpl = env.from_string(COMPARE)
75 assert tmpl.render() == 'True|True|True|True|True'
76
77
78def test_literals(env):
79 tmpl = env.from_string(LITERALS)
80 assert tmpl.render() == '[]|{}|'
81
82
83def test_bool(env):
84 tmpl = env.from_string(BOOL)
85 assert tmpl.render() == 'False|True|True'
Armin Ronacherdb69d0a2007-06-02 01:35:53 +020086
87
88def test_grouping(env):
89 tmpl = env.from_string(GROUPING)
90 assert tmpl.render() == 'False'