blob: d9d75c0034afdb51ea7c9f92008ee3c5fc1d833c [file] [log] [blame]
Armin Ronacherccf284b2007-05-21 16:44:26 +02001# -*- coding: utf-8 -*-
2"""
3 unit test for the parser
4 ~~~~~~~~~~~~~~~~~~~~~~~~
5
6 :copyright: 2007 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8"""
9
10from jinja import Environment
11
12NO_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 %}'''
18
19PHP_SYNTAX = '''\
20<!-- I'm a comment, I'm not interesting -->\
21<? for item in seq -?>
22 <?= item ?>
23<?- endfor ?>'''
24
25ERB_SYNTAX = '''\
26<%# I'm a comment, I'm not interesting %>\
27<% for item in seq -%>
28 <%= item %>
29<%- endfor %>'''
30
31COMMENT_SYNTAX = '''\
32<!--# I'm a comment, I'm not interesting -->\
33<!-- for item in seq --->
34 ${item}
35<!--- endfor -->'''
36
37SMARTY_SYNTAX = '''\
38{* I'm a comment, I'm not interesting *}\
39{for item in seq-}
40 {item}
41{-endfor}'''
42
43
44def test_no_variable_block():
45 env = Environment('{%', '%}', None, None)
46 tmpl = env.from_string(NO_VARIABLE_BLOCK)
47 assert tmpl.render(foo=42, seq=range(2)).splitlines() == [
48 '42',
49 '01',
50 'foo is 42',
51 '42 foos'
52 ]
53
54
55def test_php_syntax():
56 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
57 tmpl = env.from_string(PHP_SYNTAX)
58 assert tmpl.render(seq=range(5)) == '01234'
59
60
61def test_erb_syntax():
62 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
63 tmpl = env.from_string(ERB_SYNTAX)
64 assert tmpl.render(seq=range(5)) == '01234'
65
66
67def test_comment_syntax():
68 env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
69 tmpl = env.from_string(COMMENT_SYNTAX)
70 assert tmpl.render(seq=range(5)) == '01234'
71
72
73def test_smarty_syntax():
74 env = Environment('{', '}', '{', '}', '{*', '*}')
75 tmpl = env.from_string(SMARTY_SYNTAX)
76 assert tmpl.render(seq=range(5)) == '01234'