test_parser.py
This file
Path
tests/test_parser.py
113 lines · 2.8 KiB · Python
Last changed by
Fixed bug in line-based comments with priority.Armin Ronacherbefore adoptiondb7985d
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 Environment101112PHP_SYNTAX = '''\13<!-- I'm a comment, I'm not interesting -->\14<? for item in seq -?>15 <?= item ?>16<?- endfor ?>'''1718ERB_SYNTAX = '''\19<%# I'm a comment, I'm not interesting %>\20<% for item in seq -%>21 <%= item %>22<%- endfor %>'''2324COMMENT_SYNTAX = '''\25<!--# I'm a comment, I'm not interesting -->\26<!-- for item in seq --->27 ${item}28<!--- endfor -->'''2930MAKO_SYNTAX = '''\31<%# regular comment %>32% for item in seq:33 ${item}34% endfor'''3536MAKO_SYNTAX_LINECOMMENTS = '''\37<%# regular comment %>38% for item in seq:39 ${item} ## the rest of the stuff40% endfor'''4142BALANCING = '''{{{'foo':'bar'}.foo}}'''4344STARTCOMMENT = '''{# foo comment45and bar comment #}46{% macro blub() %}foo{% endmacro %}47{{ blub() }}'''4849LINE_SYNTAX_PRIORITY1 = '''\50/* ignore me.51 I'm a multiline comment */52## for item in seq:53* ${item} # this is just extra stuff54## endfor55'''5657LINE_SYNTAX_PRIORITY2 = '''\58/* ignore me.59 I'm a multiline comment */60# for item in seq:61* ${item} ## this is just extra stuff62# endfor63'''646566def test_php_syntax():67 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')68 tmpl = env.from_string(PHP_SYNTAX)69 assert tmpl.render(seq=range(5)) == '01234'707172def test_erb_syntax():73 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')74 tmpl = env.from_string(ERB_SYNTAX)75 assert tmpl.render(seq=range(5)) == '01234'767778def test_comment_syntax():79 env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')80 tmpl = env.from_string(COMMENT_SYNTAX)81 assert tmpl.render(seq=range(5)) == '01234'828384def test_balancing(env):85 tmpl = env.from_string(BALANCING)86 assert tmpl.render() == 'bar'878889def test_start_comment(env):90 tmpl = env.from_string(STARTCOMMENT)91 assert tmpl.render().strip() == 'foo'929394def test_line_syntax():95 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')96 tmpl = env.from_string(MAKO_SYNTAX)97 assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \98 range(5)99100 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')101 tmpl = env.from_string(MAKO_SYNTAX_LINECOMMENTS)102 assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \103 range(5)104105106def test_line_syntax_priority():107 # XXX: why is the whitespace there in front of the newline?108 env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')109 tmpl = env.from_string(LINE_SYNTAX_PRIORITY1)110 assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'111 env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')112 tmpl = env.from_string(LINE_SYNTAX_PRIORITY2)113 assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'