Armin Ronacher | ccf284b | 2007-05-21 16:44:26 +0200 | [diff] [blame] | 1 | # -*- 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 | |
| 10 | from jinja import Environment |
| 11 | |
| 12 | NO_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 | |
| 19 | PHP_SYNTAX = '''\ |
| 20 | <!-- I'm a comment, I'm not interesting -->\ |
| 21 | <? for item in seq -?> |
| 22 | <?= item ?> |
| 23 | <?- endfor ?>''' |
| 24 | |
| 25 | ERB_SYNTAX = '''\ |
| 26 | <%# I'm a comment, I'm not interesting %>\ |
| 27 | <% for item in seq -%> |
| 28 | <%= item %> |
| 29 | <%- endfor %>''' |
| 30 | |
| 31 | COMMENT_SYNTAX = '''\ |
| 32 | <!--# I'm a comment, I'm not interesting -->\ |
| 33 | <!-- for item in seq ---> |
| 34 | ${item} |
| 35 | <!--- endfor -->''' |
| 36 | |
| 37 | SMARTY_SYNTAX = '''\ |
| 38 | {* I'm a comment, I'm not interesting *}\ |
| 39 | {for item in seq-} |
| 40 | {item} |
| 41 | {-endfor}''' |
| 42 | |
| 43 | |
| 44 | def 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 | |
| 55 | def test_php_syntax(): |
| 56 | env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->') |
| 57 | tmpl = env.from_string(PHP_SYNTAX) |
| 58 | assert tmpl.render(seq=range(5)) == '01234' |
| 59 | |
| 60 | |
| 61 | def test_erb_syntax(): |
| 62 | env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>') |
| 63 | tmpl = env.from_string(ERB_SYNTAX) |
| 64 | assert tmpl.render(seq=range(5)) == '01234' |
| 65 | |
| 66 | |
| 67 | def test_comment_syntax(): |
| 68 | env = Environment('<!--', '-->', '${', '}', '<!--#', '-->') |
| 69 | tmpl = env.from_string(COMMENT_SYNTAX) |
| 70 | assert tmpl.render(seq=range(5)) == '01234' |
| 71 | |
| 72 | |
| 73 | def test_smarty_syntax(): |
| 74 | env = Environment('{', '}', '{', '}', '{*', '*}') |
| 75 | tmpl = env.from_string(SMARTY_SYNTAX) |
| 76 | assert tmpl.render(seq=range(5)) == '01234' |