1c8851d58a2d0478771f57488a55ea176107b041
[jinja2.git] / tests / test_parser.py
1 # -*- coding: utf-8 -*-
2 """
3     unit test for the parser
4     ~~~~~~~~~~~~~~~~~~~~~~~~
5
6     :copyright: 2007 by Armin Ronacher.
7     :license: BSD, see LICENSE for more details.
8 """
9
10 from jinja import Environment
11
12 NO_VARIABLE_BLOCK = '''\
13 {# i'm a freaking comment #}\
14 {% if foo %}{% foo %}{% endif %}
15 {% for item in seq %}{% item %}{% endfor %}
16 {% trans foo %}foo is {% foo %}{% endtrans %}
17 {% trans foo %}one foo{% pluralize %}{% foo %} foos{% endtrans %}'''
18
19 PHP_SYNTAX = '''\
20 <!-- I'm a comment, I'm not interesting -->\
21 <? for item in seq -?>
22     <?= item ?>
23 <?- endfor ?>'''
24
25 ERB_SYNTAX = '''\
26 <%# I'm a comment, I'm not interesting %>\
27 <% for item in seq -%>
28     <%= item %>
29 <%- endfor %>'''
30
31 COMMENT_SYNTAX = '''\
32 <!--# I'm a comment, I'm not interesting -->\
33 <!-- for item in seq --->
34     ${item}
35 <!--- endfor -->'''
36
37 SMARTY_SYNTAX = '''\
38 {* I'm a comment, I'm not interesting *}\
39 {for item in seq-}
40     {item}
41 {-endfor}'''
42
43 BALANCING = '''{{{'foo':'bar'}.foo}}'''
44
45
46 def test_no_variable_block():
47     env = Environment('{%', '%}', None, None)
48     tmpl = env.from_string(NO_VARIABLE_BLOCK)
49     assert tmpl.render(foo=42, seq=range(2)).splitlines() == [
50         '42',
51         '01',
52         'foo is 42',
53         '42 foos'
54     ]
55
56
57 def test_php_syntax():
58     env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
59     tmpl = env.from_string(PHP_SYNTAX)
60     assert tmpl.render(seq=range(5)) == '01234'
61
62
63 def test_erb_syntax():
64     env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
65     tmpl = env.from_string(ERB_SYNTAX)
66     assert tmpl.render(seq=range(5)) == '01234'
67
68
69 def test_comment_syntax():
70     env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
71     tmpl = env.from_string(COMMENT_SYNTAX)
72     assert tmpl.render(seq=range(5)) == '01234'
73
74
75 def test_smarty_syntax():
76     env = Environment('{', '}', '{', '}', '{*', '*}')
77     tmpl = env.from_string(SMARTY_SYNTAX)
78     assert tmpl.render(seq=range(5)) == '01234'
79
80
81 def test_balancing(env):
82     tmpl = env.from_string(BALANCING)
83     assert tmpl.render() == 'bar'