blob: 59d6037e6c668842eccbd98bb973ef83f2aca6ce [file] [log] [blame]
Armin Ronacher335b87a2008-09-21 17:08:48 +02001# -*- coding: utf-8 -*-
2"""
3 Tests for old bugs
4 ~~~~~~~~~~~~~~~~~~
5
6 Unittest that test situations caused by various older bugs.
7
Armin Ronacher62ccd1b2009-01-04 14:26:19 +01008 :copyright: (c) 2009 by the Jinja Team.
Armin Ronacher335b87a2008-09-21 17:08:48 +02009 :license: BSD.
10"""
Armin Ronacher1dcafe52009-03-30 18:50:49 +020011from py.test import raises
12from jinja2 import Environment, DictLoader, TemplateSyntaxError
Armin Ronacher79668952008-09-23 22:52:46 +020013
Armin Ronacher335b87a2008-09-21 17:08:48 +020014
15def test_keyword_folding():
16 env = Environment()
17 env.filters['testing'] = lambda value, some: value + some
18 assert env.from_string("{{ 'test'|testing(some='stuff') }}") \
19 .render() == 'teststuff'
20
Armin Ronacher79668952008-09-23 22:52:46 +020021
22def test_extends_output_bugs():
23 env = Environment(loader=DictLoader({
24 'parent.html': '(({% block title %}{% endblock %}))'
25 }))
26
27 t = env.from_string('{% if expr %}{% extends "parent.html" %}{% endif %}'
28 '[[{% block title %}title{% endblock %}]]'
29 '{% for item in [1, 2, 3] %}({{ item }}){% endfor %}')
30 assert t.render(expr=False) == '[[title]](1)(2)(3)'
31 assert t.render(expr=True) == '((title))'
Armin Ronacherd9342dc2008-11-17 00:35:30 +010032
33
34def test_urlize_filter_escaping(env):
35 tmpl = env.from_string('{{ "http://www.example.org/<foo"|urlize }}')
36 assert tmpl.render() == '<a href="http://www.example.org/&lt;foo">http://www.example.org/&lt;foo</a>'
Armin Ronacher02b42a82009-03-18 00:59:32 +010037
38
39def test_loop_call_loop(env):
40 tmpl = env.from_string('''
41
42 {% macro test() %}
43 {{ caller() }}
44 {% endmacro %}
45
46 {% for num1 in range(5) %}
47 {% call test() %}
48 {% for num2 in range(10) %}
49 {{ loop.index }}
50 {% endfor %}
51 {% endcall %}
52 {% endfor %}
53
54 ''')
55
56 assert tmpl.render() == ''
Armin Ronacher1dcafe52009-03-30 18:50:49 +020057
58
59def test_weird_inline_comment():
60 env = Environment(line_statement_prefix='%')
61 raises(TemplateSyntaxError, env.from_string,
62 '% for item in seq {# missing #}\n...% endfor')
Armin Ronacher59b6bd52009-03-30 21:00:16 +020063
64
65def test_old_macro_loop_scoping_bug(env):
66 tmpl = env.from_string('{% for i in (1, 2) %}{{ i }}{% endfor %}'
67 '{% macro i() %}3{% endmacro %}{{ i() }}')
68 assert tmpl.render() == '123'