blob: 92ac6b8a8caccdc36cce00abc60bd5cb77e5b8b5 [file] [log] [blame]
Armin Ronacher93e14c22007-03-20 16:17:48 +01001# -*- coding: utf-8 -*-
2"""
3 unit test for various things
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
Armin Ronacher62ccd1b2009-01-04 14:26:19 +01006 :copyright: (c) 2009 by the Jinja Team.
Armin Ronacher93e14c22007-03-20 16:17:48 +01007 :license: BSD, see LICENSE for more details.
8"""
Armin Ronacher9f258ff2008-05-24 22:28:52 +02009import gc
Armin Ronacher42979eb2009-07-26 11:08:50 +020010from jinja2 import escape, is_undefined, Environment
Armin Ronacherccae0552008-10-05 23:08:58 +020011from jinja2.utils import Cycler
Christoph Hacke9e43bb2008-04-13 23:35:48 +020012from jinja2.exceptions import TemplateSyntaxError
Armin Ronacher93e14c22007-03-20 16:17:48 +010013
Armin Ronacher42979eb2009-07-26 11:08:50 +020014from nose.tools import assert_raises
15
16env = Environment()
Rene Leonhardtc7e6c6d2009-04-20 23:08:53 +020017
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020018
Armin Ronacher93e14c22007-03-20 16:17:48 +010019UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010020RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020021CONST = '''{{ true }}|{{ false }}|{{ none }}|\
22{{ none is defined }}|{{ missing is defined }}'''
Armin Ronacher0a2ac692008-05-13 01:03:08 +020023LOCALSET = '''{% set foo = 0 %}\
24{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\
Armin Ronacher1cc232c2007-09-07 17:52:41 +020025{{ foo }}'''
Armin Ronacher0a2ac692008-05-13 01:03:08 +020026CONSTASS1 = '''{% set true = 42 %}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020027CONSTASS2 = '''{% for none in seq %}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010028
29
Armin Ronacher42979eb2009-07-26 11:08:50 +020030def test_unpacking():
Armin Ronacher93e14c22007-03-20 16:17:48 +010031 tmpl = env.from_string(UNPACKING)
32 assert tmpl.render() == '1|2|3'
33
34
Armin Ronacher42979eb2009-07-26 11:08:50 +020035def test_raw():
Armin Ronacher93e14c22007-03-20 16:17:48 +010036 tmpl = env.from_string(RAW)
37 assert tmpl.render() == '{{ FOO }} and {% BAR %}'
38
39
Armin Ronacher42979eb2009-07-26 11:08:50 +020040def test_const():
Armin Ronacherecc051b2007-06-01 18:25:28 +020041 tmpl = env.from_string(CONST)
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020042 assert tmpl.render() == 'True|False|None|True|False'
Armin Ronacherecc051b2007-06-01 18:25:28 +020043
44
Armin Ronacher42979eb2009-07-26 11:08:50 +020045def test_const_assign():
Armin Ronacherecc051b2007-06-01 18:25:28 +020046 for tmpl in CONSTASS1, CONSTASS2:
Armin Ronacher42979eb2009-07-26 11:08:50 +020047 assert_raises(TemplateSyntaxError, env.from_string, tmpl)
Armin Ronacher1cc232c2007-09-07 17:52:41 +020048
49
Armin Ronacher42979eb2009-07-26 11:08:50 +020050def test_localset():
Armin Ronacher1cc232c2007-09-07 17:52:41 +020051 tmpl = env.from_string(LOCALSET)
52 assert tmpl.render() == '0'
Armin Ronacher9f258ff2008-05-24 22:28:52 +020053
54
55def test_markup_leaks():
56 counts = set()
57 for count in xrange(20):
58 for item in xrange(1000):
59 escape("foo")
60 escape("<foo>")
61 escape(u"foo")
62 escape(u"<foo>")
63 counts.add(len(gc.get_objects()))
64 assert len(counts) == 1, 'ouch, c extension seems to leak objects'
Armin Ronacherf15f5f72008-05-26 12:21:45 +020065
66
Armin Ronacher6dc6f292008-06-12 08:50:07 +020067def test_item_and_attribute():
Armin Ronacherf15f5f72008-05-26 12:21:45 +020068 from jinja2.sandbox import SandboxedEnvironment
69
70 for env in Environment(), SandboxedEnvironment():
71 tmpl = env.from_string('{{ foo.items() }}')
Armin Ronacher6dc6f292008-06-12 08:50:07 +020072 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
Armin Ronachere3290ea2008-06-12 10:30:01 +020073 tmpl = env.from_string('{{ foo|attr("items")() }}')
Armin Ronacher6dc6f292008-06-12 08:50:07 +020074 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
75 tmpl = env.from_string('{{ foo["items"] }}')
76 assert tmpl.render(foo={'items': 42}) == '42'
Armin Ronacher665bfb82008-07-14 13:41:46 +020077
78
79def test_finalizer():
Armin Ronacher665bfb82008-07-14 13:41:46 +020080 def finalize_none_empty(value):
81 if value is None:
82 value = u''
83 return value
84 env = Environment(finalize=finalize_none_empty)
85 tmpl = env.from_string('{% for item in seq %}|{{ item }}{% endfor %}')
86 assert tmpl.render(seq=(None, 1, "foo")) == '||1|foo'
87 tmpl = env.from_string('<{{ none }}>')
88 assert tmpl.render() == '<>'
Armin Ronacherccae0552008-10-05 23:08:58 +020089
90
91def test_cycler():
92 items = 1, 2, 3
93 c = Cycler(*items)
94 for item in items + items:
95 assert c.current == item
96 assert c.next() == item
97 c.next()
98 assert c.current == 2
99 c.reset()
100 assert c.current == 1
Armin Ronacherba6e25a2008-11-02 15:58:14 +0100101
102
Armin Ronacher42979eb2009-07-26 11:08:50 +0200103def test_expressions():
Armin Ronacherba6e25a2008-11-02 15:58:14 +0100104 expr = env.compile_expression("foo")
105 assert expr() is None
106 assert expr(foo=42) == 42
107 expr2 = env.compile_expression("foo", undefined_to_none=False)
108 assert is_undefined(expr2())
109
110 expr = env.compile_expression("42 + foo")
111 assert expr(foo=42) == 84