blob: 37082ba2f03e786708725243b7b0cf871b9d1a19 [file] [log] [blame]
Armin Ronacher93e14c22007-03-20 16:17:48 +01001# -*- coding: utf-8 -*-
2"""
3 unit test for various things
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.exceptions import TemplateSyntaxError
Armin Ronacher93e14c22007-03-20 16:17:48 +010010
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020011
Armin Ronacher93e14c22007-03-20 16:17:48 +010012UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010013RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020014CONST = '''{{ true }}|{{ false }}|{{ none }}|\
15{{ none is defined }}|{{ missing is defined }}'''
16LOCALSET = '''{% foo = 0 %}\
17{% for item in [1, 2] %}{% foo = 1 %}{% endfor %}\
Armin Ronacher1cc232c2007-09-07 17:52:41 +020018{{ foo }}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020019CONSTASS1 = '''{% true = 42 %}'''
20CONSTASS2 = '''{% for none in seq %}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010021
22
23def test_unpacking(env):
24 tmpl = env.from_string(UNPACKING)
25 assert tmpl.render() == '1|2|3'
26
27
28def test_raw(env):
29 tmpl = env.from_string(RAW)
30 assert tmpl.render() == '{{ FOO }} and {% BAR %}'
31
32
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020033def test_lru_cache():
34 from jinja2.utils import LRUCache
35 d = LRUCache(3)
Armin Ronacher93e14c22007-03-20 16:17:48 +010036 d["a"] = 1
37 d["b"] = 2
38 d["c"] = 3
39 d["a"]
40 d["d"] = 4
41 assert len(d) == 3
42 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d
Armin Ronacheree2c18e2007-04-20 22:39:04 +020043
44
Armin Ronacherecc051b2007-06-01 18:25:28 +020045def test_const(env):
46 tmpl = env.from_string(CONST)
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020047 assert tmpl.render() == 'True|False|None|True|False'
Armin Ronacherecc051b2007-06-01 18:25:28 +020048
49
50def test_const_assign(env):
51 for tmpl in CONSTASS1, CONSTASS2:
52 try:
53 env.from_string(tmpl)
54 except TemplateSyntaxError:
55 pass
56 else:
57 raise AssertionError('expected syntax error')
Armin Ronacher1cc232c2007-09-07 17:52:41 +020058
59
60def test_localset(env):
61 tmpl = env.from_string(LOCALSET)
62 assert tmpl.render() == '0'