Halftime, lexer and parser tests added.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 10f4bcc · Browse files · 2 files·+194 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+194 added−1 removed
- other:+194 added−1 removed
Provenance
- Commit
c608fb8- Tree
e982051- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
10f4bcc
2 files changed+194 added−1 removed
modified:jinja2/testsuite/__init__.py+2 added-1 removed
43 unmodified lines44 45 46 def suite():47 from jinja2.testsuite import ext, filters, tests, core_tags, \48- loader, inheritance, imports48+ loader, inheritance, imports, lexnparse49 suite = unittest.TestSuite()50 suite.addTest(ext.suite())51 suite.addTest(filters.suite())52 suite.addTest(tests.suite())53 suite.addTest(core_tags.suite())54 suite.addTest(loader.suite())55 suite.addTest(inheritance.suite())56 suite.addTest(imports.suite())57+ suite.addTest(lexnparse.suite())58 return suiteadded:jinja2/testsuite/lexnparse.py+192 added
1+# -*- coding: utf-8 -*-2+"""3+ jinja2.testsuite.lexnparse4+ ~~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ All the unittests regarding lexing, parsing and syntax.7+8+ :copyright: (c) 2010 by the Jinja Team.9+ :license: BSD, see LICENSE for more details.10+"""11+import os12+import time13+import tempfile14+import unittest15+16+from jinja2.testsuite import JinjaTestCase17+18+from jinja2 import Environment, Template, TemplateSyntaxError19+20+env = Environment()21+22+23+class LexerTestCase(JinjaTestCase):24+25+ def test_raw(self):26+ tmpl = env.from_string('{% raw %}foo{% endraw %}|'27+ '{%raw%}{{ bar }}|{% baz %}{% endraw %}')28+ assert tmpl.render() == 'foo|{{ bar }}|{% baz %}'29+30+ def test_balancing(self):31+ env = Environment('{%', '%}', '${', '}')32+ tmpl = env.from_string('''{% for item in seq33+ %}${{'foo': item}|upper}{% endfor %}''')34+ assert tmpl.render(seq=range(3)) == "{'FOO': 0}{'FOO': 1}{'FOO': 2}"35+36+ def test_comments(self):37+ env = Environment('<!--', '-->', '{', '}')38+ tmpl = env.from_string('''\39+<ul>40+<!--- for item in seq -->41+ <li>{item}</li>42+<!--- endfor -->43+</ul>''')44+ assert tmpl.render(seq=range(3)) == ("<ul>\n <li>0</li>\n "45+ "<li>1</li>\n <li>2</li>\n</ul>")46+47+ def test_string_escapes(self):48+ for char in u'\0', u'\u2668', u'\xe4', u'\t', u'\r', u'\n':49+ tmpl = env.from_string('{{ %s }}' % repr(char)[1:])50+ assert tmpl.render() == char51+ assert env.from_string('{{ "\N{HOT SPRINGS}" }}').render() == u'\u2668'52+53+ def test_bytefallback(self):54+ tmpl = env.from_string(u'''{{ 'foo'|pprint }}|{{ 'bär'|pprint }}''')55+ assert tmpl.render() == u"'foo'|u'b\\xe4r'"56+57+ def test_operators(self):58+ from jinja2.lexer import operators59+ for test, expect in operators.iteritems():60+ if test in '([{}])':61+ continue62+ stream = env.lexer.tokenize('{{ %s }}' % test)63+ stream.next()64+ assert stream.current.type == expect65+66+ def test_normalizing(self):67+ for seq in '\r', '\r\n', '\n':68+ env = Environment(newline_sequence=seq)69+ tmpl = env.from_string('1\n2\r\n3\n4\n')70+ result = tmpl.render()71+ assert result.replace(seq, 'X') == '1X2X3X4'72+73+74+class ParserTestCase(JinjaTestCase):75+76+ def test_php_syntax(self):77+ env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')78+ tmpl = env.from_string('''\79+<!-- I'm a comment, I'm not interesting -->\80+<? for item in seq -?>81+ <?= item ?>82+<?- endfor ?>''')83+ assert tmpl.render(seq=range(5)) == '01234'84+85+ def test_erb_syntax(self):86+ env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')87+ tmpl = env.from_string('''\88+<%# I'm a comment, I'm not interesting %>\89+<% for item in seq -%>90+ <%= item %>91+<%- endfor %>''')92+ assert tmpl.render(seq=range(5)) == '01234'93+94+ def test_comment_syntax(self):95+ env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')96+ tmpl = env.from_string('''\97+<!--# I'm a comment, I'm not interesting -->\98+<!-- for item in seq --->99+ ${item}100+<!--- endfor -->''')101+ assert tmpl.render(seq=range(5)) == '01234'102+103+ def test_balancing(self):104+ tmpl = env.from_string('''{{{'foo':'bar'}.foo}}''')105+ assert tmpl.render() == 'bar'106+107+ def test_start_comment(self):108+ tmpl = env.from_string('''{# foo comment109+and bar comment #}110+{% macro blub() %}foo{% endmacro %}111+{{ blub() }}''')112+ assert tmpl.render().strip() == 'foo'113+114+ def test_line_syntax(self):115+ env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')116+ tmpl = env.from_string('''\117+<%# regular comment %>118+% for item in seq:119+ ${item}120+% endfor''')121+ assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \122+ range(5)123+124+ env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')125+ tmpl = env.from_string('''\126+<%# regular comment %>127+% for item in seq:128+ ${item} ## the rest of the stuff129+% endfor''')130+ assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \131+ range(5)132+133+ def test_line_syntax_priority(self):134+ # XXX: why is the whitespace there in front of the newline?135+ env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')136+ tmpl = env.from_string('''\137+/* ignore me.138+ I'm a multiline comment */139+## for item in seq:140+* ${item} # this is just extra stuff141+## endfor''')142+ assert tmpl.render(seq=[1, 2]).strip() == '* 1\n* 2'143+ env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')144+ tmpl = env.from_string('''\145+/* ignore me.146+ I'm a multiline comment */147+# for item in seq:148+* ${item} ## this is just extra stuff149+ ## extra stuff i just want to ignore150+# endfor''')151+ assert tmpl.render(seq=[1, 2]).strip() == '* 1\n\n* 2'152+153+ def test_error_messages(self):154+ def assert_error(code, expected):155+ try:156+ Template(code)157+ except TemplateSyntaxError, e:158+ assert str(e) == expected, 'unexpected error message'159+ else:160+ assert False, 'that was suposed to be an error'161+162+ assert_error('{% for item in seq %}...{% endif %}',163+ "Encountered unknown tag 'endif'. Jinja was looking "164+ "for the following tags: 'endfor' or 'else'. The "165+ "innermost block that needs to be closed is 'for'.")166+ assert_error('{% if foo %}{% for item in seq %}...{% endfor %}{% endfor %}',167+ "Encountered unknown tag 'endfor'. Jinja was looking for "168+ "the following tags: 'elif' or 'else' or 'endif'. The "169+ "innermost block that needs to be closed is 'if'.")170+ assert_error('{% if foo %}',171+ "Unexpected end of template. Jinja was looking for the "172+ "following tags: 'elif' or 'else' or 'endif'. The "173+ "innermost block that needs to be closed is 'if'.")174+ assert_error('{% for item in seq %}',175+ "Unexpected end of template. Jinja was looking for the "176+ "following tags: 'endfor' or 'else'. The innermost block "177+ "that needs to be closed is 'for'.")178+ assert_error('{% block foo-bar-baz %}',179+ "Block names in Jinja have to be valid Python identifiers "180+ "and may not contain hypens, use an underscore instead.")181+182+183+class SyntaxTestCase(JinjaTestCase):184+ pass185+186+187+def suite():188+ suite = unittest.TestSuite()189+ suite.addTest(unittest.makeSuite(LexerTestCase))190+ suite.addTest(unittest.makeSuite(ParserTestCase))191+ suite.addTest(unittest.makeSuite(SyntaxTestCase))192+ return suite