blob: 535b97c80455cb03746d9219a95c22349d27024a [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"""
Armin Ronacher9f258ff2008-05-24 22:28:52 +02009import gc
10from py.test import raises
11from jinja2 import escape
Christoph Hacke9e43bb2008-04-13 23:35:48 +020012from jinja2.exceptions import TemplateSyntaxError
Armin Ronacher93e14c22007-03-20 16:17:48 +010013
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020014
Armin Ronacher93e14c22007-03-20 16:17:48 +010015UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010016RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020017CONST = '''{{ true }}|{{ false }}|{{ none }}|\
18{{ none is defined }}|{{ missing is defined }}'''
Armin Ronacher0a2ac692008-05-13 01:03:08 +020019LOCALSET = '''{% set foo = 0 %}\
20{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\
Armin Ronacher1cc232c2007-09-07 17:52:41 +020021{{ foo }}'''
Armin Ronacher0a2ac692008-05-13 01:03:08 +020022CONSTASS1 = '''{% set true = 42 %}'''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020023CONSTASS2 = '''{% for none in seq %}{% endfor %}'''
Armin Ronacher93e14c22007-03-20 16:17:48 +010024
25
26def test_unpacking(env):
27 tmpl = env.from_string(UNPACKING)
28 assert tmpl.render() == '1|2|3'
29
30
31def test_raw(env):
32 tmpl = env.from_string(RAW)
33 assert tmpl.render() == '{{ FOO }} and {% BAR %}'
34
35
Armin Ronacherecc051b2007-06-01 18:25:28 +020036def test_const(env):
37 tmpl = env.from_string(CONST)
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020038 assert tmpl.render() == 'True|False|None|True|False'
Armin Ronacherecc051b2007-06-01 18:25:28 +020039
40
41def test_const_assign(env):
42 for tmpl in CONSTASS1, CONSTASS2:
Armin Ronacher9f258ff2008-05-24 22:28:52 +020043 raises(TemplateSyntaxError, env.from_string, tmpl)
Armin Ronacher1cc232c2007-09-07 17:52:41 +020044
45
46def test_localset(env):
47 tmpl = env.from_string(LOCALSET)
48 assert tmpl.render() == '0'
Armin Ronacher9f258ff2008-05-24 22:28:52 +020049
50
51def test_markup_leaks():
52 counts = set()
53 for count in xrange(20):
54 for item in xrange(1000):
55 escape("foo")
56 escape("<foo>")
57 escape(u"foo")
58 escape(u"<foo>")
59 counts.add(len(gc.get_objects()))
60 assert len(counts) == 1, 'ouch, c extension seems to leak objects'
Armin Ronacherf15f5f72008-05-26 12:21:45 +020061
62
Armin Ronacher6dc6f292008-06-12 08:50:07 +020063def test_item_and_attribute():
Armin Ronacherf15f5f72008-05-26 12:21:45 +020064 from jinja2 import Environment
65 from jinja2.sandbox import SandboxedEnvironment
66
67 for env in Environment(), SandboxedEnvironment():
68 tmpl = env.from_string('{{ foo.items() }}')
Armin Ronacher6dc6f292008-06-12 08:50:07 +020069 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
70 tmpl = env.from_string('{{ foo|attr("items") }}')
71 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
72 tmpl = env.from_string('{{ foo["items"] }}')
73 assert tmpl.render(foo={'items': 42}) == '42'