Skip to content
Myr

test_parser.py

This file

Path

tests/test_parser.py

75 lines · 1.7 KiB · Python

Last changed by

Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b

History of this file

Take 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% for item in seq:32    ${item}33% endfor'''3435BALANCING = '''{{{'foo':'bar'}.foo}}'''3637STARTCOMMENT = '''{# foo comment38and bar comment #}39{% macro blub() %}foo{% endmacro %}40{{ blub() }}'''414243def test_php_syntax():44    env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')45    tmpl = env.from_string(PHP_SYNTAX)46    assert tmpl.render(seq=range(5)) == '01234'474849def test_erb_syntax():50    env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')51    tmpl = env.from_string(ERB_SYNTAX)52    assert tmpl.render(seq=range(5)) == '01234'535455def test_comment_syntax():56    env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')57    tmpl = env.from_string(COMMENT_SYNTAX)58    assert tmpl.render(seq=range(5)) == '01234'596061def test_balancing(env):62    tmpl = env.from_string(BALANCING)63    assert tmpl.render() == 'bar'646566def test_start_comment(env):67    tmpl = env.from_string(STARTCOMMENT)68    assert tmpl.render().strip() == 'foo'697071def test_line_syntax():72    env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')73    tmpl = env.from_string(MAKO_SYNTAX)74    assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \75           range(5)