test_parser.py
This file
Path
tests/test_parser.py
116 lines · 2.9 KiB · Python
Last changed by
Got rid of py.testisms.Armin Ronacherbefore adoption42979eb
History of this fileTake it
1# -*- coding: utf-8 -*-2"""3 unit test for the parser4 ~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: (c) 2009 by the Jinja Team.7 :license: BSD, see LICENSE for more details.8"""9from jinja2 import Environment1011env = Environment()121314PHP_SYNTAX = '''\15<!-- I'm a comment, I'm not interesting -->\16<? for item in seq -?>17 <?= item ?>18<?- endfor ?>'''1920ERB_SYNTAX = '''\21<%# I'm a comment, I'm not interesting %>\22<% for item in seq -%>23 <%= item %>24<%- endfor %>'''2526COMMENT_SYNTAX = '''\27<!--# I'm a comment, I'm not interesting -->\28<!-- for item in seq --->29 ${item}30<!--- endfor -->'''3132MAKO_SYNTAX = '''\33<%# regular comment %>34% for item in seq:35 ${item}36% endfor'''3738MAKO_SYNTAX_LINECOMMENTS = '''\39<%# regular comment %>40% for item in seq:41 ${item} ## the rest of the stuff42% endfor'''4344BALANCING = '''{{{'foo':'bar'}.foo}}'''4546STARTCOMMENT = '''{# foo comment47and bar comment #}48{% macro blub() %}foo{% endmacro %}49{{ blub() }}'''5051LINE_SYNTAX_PRIORITY1 = '''\52/* ignore me.53 I'm a multiline comment */54## for item in seq:55* ${item} # this is just extra stuff56## endfor57'''5859LINE_SYNTAX_PRIORITY2 = '''\60/* ignore me.61 I'm a multiline comment */62# for item in seq:63* ${item} ## this is just extra stuff64 ## extra stuff i just want to ignore65# endfor66'''676869def test_php_syntax():70 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')71 tmpl = env.from_string(PHP_SYNTAX)72 assert tmpl.render(seq=range(5)) == '01234'737475def test_erb_syntax():76 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')77 tmpl = env.from_string(ERB_SYNTAX)78 assert tmpl.render(seq=range(5)) == '01234'798081def test_comment_syntax():82 env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')83 tmpl = env.from_string(COMMENT_SYNTAX)84 assert tmpl.render(seq=range(5)) == '01234'858687def test_balancing():88 tmpl = env.from_string(BALANCING)89 assert tmpl.render() == 'bar'909192def test_start_comment():93 tmpl = env.from_string(STARTCOMMENT)94 assert tmpl.render().strip() == 'foo'959697def 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)102103 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)107108109def 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'