f77e60cf36dcb7250cee6883eac38401f072b97f
[jinja2.git] / tests / test_old_bugs.py
1 # -*- coding: utf-8 -*-
2 """
3     Tests for old bugs
4     ~~~~~~~~~~~~~~~~~~
5
6     Unittest that test situations caused by various older bugs.
7
8     :copyright: (c) 2009 by the Jinja Team.
9     :license: BSD.
10 """
11 from py.test import raises
12 from jinja2 import Environment, DictLoader, TemplateSyntaxError
13
14
15 def 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
21
22 def 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))'
32
33
34 def 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>'
37
38
39 def 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() == ''
57
58
59 def test_weird_inline_comment():
60     env = Environment(line_statement_prefix='%')
61     raises(TemplateSyntaxError, env.from_string,
62            '% for item in seq {# missing #}\n...% endfor')