1 # -*- coding: utf-8 -*-
3 unit test for the parser
4 ~~~~~~~~~~~~~~~~~~~~~~~~
6 :copyright: (c) 2009 by the Jinja Team.
7 :license: BSD, see LICENSE for more details.
9 from jinja2 import Environment
15 <!-- I'm a comment, I'm not interesting -->\
16 <? for item in seq -?>
21 <%# I'm a comment, I'm not interesting %>\
22 <% for item in seq -%>
27 <!--# I'm a comment, I'm not interesting -->\
28 <!-- for item in seq --->
33 <%# regular comment %>
38 MAKO_SYNTAX_LINECOMMENTS = '''\
39 <%# regular comment %>
41 ${item} ## the rest of the stuff
44 BALANCING = '''{{{'foo':'bar'}.foo}}'''
46 STARTCOMMENT = '''{# foo comment
48 {% macro blub() %}foo{% endmacro %}
51 LINE_SYNTAX_PRIORITY1 = '''\
53 I'm a multiline comment */
55 * ${item} # this is just extra stuff
59 LINE_SYNTAX_PRIORITY2 = '''\
61 I'm a multiline comment */
63 * ${item} ## this is just extra stuff
64 ## extra stuff i just want to ignore
69 def test_php_syntax():
70 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
71 tmpl = env.from_string(PHP_SYNTAX)
72 assert tmpl.render(seq=range(5)) == '01234'
75 def test_erb_syntax():
76 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
77 tmpl = env.from_string(ERB_SYNTAX)
78 assert tmpl.render(seq=range(5)) == '01234'
81 def test_comment_syntax():
82 env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
83 tmpl = env.from_string(COMMENT_SYNTAX)
84 assert tmpl.render(seq=range(5)) == '01234'
88 tmpl = env.from_string(BALANCING)
89 assert tmpl.render() == 'bar'
92 def test_start_comment():
93 tmpl = env.from_string(STARTCOMMENT)
94 assert tmpl.render().strip() == 'foo'
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()] == \
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()] == \
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'