File: test_parser.py

package info (click to toggle)
jinja 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,408 kB
  • ctags: 1,171
  • sloc: python: 6,438; ansic: 397; makefile: 74
file content (93 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
    unit test for the parser
    ~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2007 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""

from jinja import Environment

NO_VARIABLE_BLOCK = '''\
{# i'm a freaking comment #}\
{% if foo %}{% foo %}{% endif %}
{% for item in seq %}{% item %}{% endfor %}
{% trans foo %}foo is {% foo %}{% endtrans %}
{% trans foo %}one foo{% pluralize %}{% foo %} foos{% endtrans %}'''

PHP_SYNTAX = '''\
<!-- I'm a comment, I'm not interesting -->\
<? for item in seq -?>
    <?= item ?>
<?- endfor ?>'''

ERB_SYNTAX = '''\
<%# I'm a comment, I'm not interesting %>\
<% for item in seq -%>
    <%= item %>
<%- endfor %>'''

COMMENT_SYNTAX = '''\
<!--# I'm a comment, I'm not interesting -->\
<!-- for item in seq --->
    ${item}
<!--- endfor -->'''

SMARTY_SYNTAX = '''\
{* I'm a comment, I'm not interesting *}\
{for item in seq-}
    {item}
{-endfor}'''

BALANCING = '''{{{'foo':'bar'}.foo}}'''

STARTCOMMENT = '''{# foo comment
and bar comment #}
{% macro blub() %}foo{% endmacro %}
{{ blub() }}'''


def test_no_variable_block():
    env = Environment('{%', '%}', None, None)
    tmpl = env.from_string(NO_VARIABLE_BLOCK)
    assert tmpl.render(foo=42, seq=range(2)).splitlines() == [
        '42',
        '01',
        'foo is 42',
        '42 foos'
    ]


def test_php_syntax():
    env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
    tmpl = env.from_string(PHP_SYNTAX)
    assert tmpl.render(seq=range(5)) == '01234'


def test_erb_syntax():
    env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
    tmpl = env.from_string(ERB_SYNTAX)
    assert tmpl.render(seq=range(5)) == '01234'


def test_comment_syntax():
    env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
    tmpl = env.from_string(COMMENT_SYNTAX)
    assert tmpl.render(seq=range(5)) == '01234'


def test_smarty_syntax():
    env = Environment('{', '}', '{', '}', '{*', '*}')
    tmpl = env.from_string(SMARTY_SYNTAX)
    assert tmpl.render(seq=range(5)) == '01234'


def test_balancing(env):
    tmpl = env.from_string(BALANCING)
    assert tmpl.render() == 'bar'


def test_start_comment(env):
    tmpl = env.from_string(STARTCOMMENT)
    assert tmpl.render().strip() == 'foo'