Skip to content
Myr

test_parser.py

This file

Path

tests/test_parser.py

93 lines · 2.2 KiB · Python

Last changed by

converted unit tests, started rewriting filtersChristoph Hackbefore adoptione9e43bb

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    unit test for the parser4    ~~~~~~~~~~~~~~~~~~~~~~~~56    :copyright: 2007 by Armin Ronacher.7    :license: BSD, see LICENSE for more details.8"""910from jinja2 import Environment1112NO_VARIABLE_BLOCK = '''\13{# i'm a freaking comment #}\14{% if foo %}{% foo %}{% endif %}15{% for item in seq %}{% item %}{% endfor %}16{% trans foo %}foo is {% foo %}{% endtrans %}17{% trans foo %}one foo{% pluralize %}{% foo %} foos{% endtrans %}'''1819PHP_SYNTAX = '''\20<!-- I'm a comment, I'm not interesting -->\21<? for item in seq -?>22    <?= item ?>23<?- endfor ?>'''2425ERB_SYNTAX = '''\26<%# I'm a comment, I'm not interesting %>\27<% for item in seq -%>28    <%= item %>29<%- endfor %>'''3031COMMENT_SYNTAX = '''\32<!--# I'm a comment, I'm not interesting -->\33<!-- for item in seq --->34    ${item}35<!--- endfor -->'''3637SMARTY_SYNTAX = '''\38{* I'm a comment, I'm not interesting *}\39{for item in seq-}40    {item}41{-endfor}'''4243BALANCING = '''{{{'foo':'bar'}.foo}}'''4445STARTCOMMENT = '''{# foo comment46and bar comment #}47{% macro blub() %}foo{% endmacro %}48{{ blub() }}'''495051def test_no_variable_block():52    env = Environment('{%', '%}', None, None)53    tmpl = env.from_string(NO_VARIABLE_BLOCK)54    assert tmpl.render(foo=42, seq=range(2)).splitlines() == [55        '42',56        '01',57        'foo is 42',58        '42 foos'59    ]606162def test_php_syntax():63    env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')64    tmpl = env.from_string(PHP_SYNTAX)65    assert tmpl.render(seq=range(5)) == '01234'666768def test_erb_syntax():69    env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')70    tmpl = env.from_string(ERB_SYNTAX)71    assert tmpl.render(seq=range(5)) == '01234'727374def test_comment_syntax():75    env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')76    tmpl = env.from_string(COMMENT_SYNTAX)77    assert tmpl.render(seq=range(5)) == '01234'787980def test_smarty_syntax():81    env = Environment('{', '}', '{', '}', '{*', '*}')82    tmpl = env.from_string(SMARTY_SYNTAX)83    assert tmpl.render(seq=range(5)) == '01234'848586def test_balancing(env):87    tmpl = env.from_string(BALANCING)88    assert tmpl.render() == 'bar'899091def test_start_comment(env):92    tmpl = env.from_string(STARTCOMMENT)93    assert tmpl.render().strip() == 'foo'