More Python 3 support.
[jinja2.git] / tests / test_parser.py
1 # -*- coding: utf-8 -*-
2 """
3     unit test for the parser
4     ~~~~~~~~~~~~~~~~~~~~~~~~
5
6     :copyright: (c) 2009 by the Jinja Team.
7     :license: BSD, see LICENSE for more details.
8 """
9 from jinja2 import Environment
10
11 env = Environment()
12
13
14 PHP_SYNTAX = '''\
15 <!-- I'm a comment, I'm not interesting -->\
16 <? for item in seq -?>
17     <?= item ?>
18 <?- endfor ?>'''
19
20 ERB_SYNTAX = '''\
21 <%# I'm a comment, I'm not interesting %>\
22 <% for item in seq -%>
23     <%= item %>
24 <%- endfor %>'''
25
26 COMMENT_SYNTAX = '''\
27 <!--# I'm a comment, I'm not interesting -->\
28 <!-- for item in seq --->
29     ${item}
30 <!--- endfor -->'''
31
32 MAKO_SYNTAX = '''\
33 <%# regular comment %>
34 % for item in seq:
35     ${item}
36 % endfor'''
37
38 MAKO_SYNTAX_LINECOMMENTS = '''\
39 <%# regular comment %>
40 % for item in seq:
41     ${item} ## the rest of the stuff
42 % endfor'''
43
44 BALANCING = '''{{{'foo':'bar'}.foo}}'''
45
46 STARTCOMMENT = '''{# foo comment
47 and bar comment #}
48 {% macro blub() %}foo{% endmacro %}
49 {{ blub() }}'''
50
51 LINE_SYNTAX_PRIORITY1 = '''\
52 /* ignore me.
53    I'm a multiline comment */
54 ## for item in seq:
55 * ${item}          # this is just extra stuff
56 ## endfor
57 '''
58
59 LINE_SYNTAX_PRIORITY2 = '''\
60 /* ignore me.
61    I'm a multiline comment */
62 # for item in seq:
63 * ${item}          ## this is just extra stuff
64     ## extra stuff i just want to ignore
65 # endfor
66 '''
67
68
69 def test_php_syntax():
70     env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
71     tmpl = env.from_string(PHP_SYNTAX)
72     assert tmpl.render(seq=range(5)) == '01234'
73
74
75 def test_erb_syntax():
76     env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
77     tmpl = env.from_string(ERB_SYNTAX)
78     assert tmpl.render(seq=range(5)) == '01234'
79
80
81 def test_comment_syntax():
82     env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
83     tmpl = env.from_string(COMMENT_SYNTAX)
84     assert tmpl.render(seq=range(5)) == '01234'
85
86
87 def test_balancing():
88     tmpl = env.from_string(BALANCING)
89     assert tmpl.render() == 'bar'
90
91
92 def test_start_comment():
93     tmpl = env.from_string(STARTCOMMENT)
94     assert tmpl.render().strip() == 'foo'
95
96
97 def test_line_syntax():
98     env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')
99     tmpl = env.from_string(MAKO_SYNTAX)
100     assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
101            range(5)
102
103     env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')
104     tmpl = env.from_string(MAKO_SYNTAX_LINECOMMENTS)
105     assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
106             range(5)
107
108
109 def test_line_syntax_priority():
110     # XXX: why is the whitespace there in front of the newline?
111     env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')
112     tmpl = env.from_string(LINE_SYNTAX_PRIORITY1)
113     assert tmpl.render(seq=[1, 2]).strip() == '* 1\n* 2'
114     env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')
115     tmpl = env.from_string(LINE_SYNTAX_PRIORITY2)
116     assert tmpl.render(seq=[1, 2]).strip() == '* 1\n\n* 2'