from jinja2.sandbox import SandboxedEnvironment
for env in Environment(), SandboxedEnvironment():
- tmpl = env.from_string('{{ foo.items() }}')
+ # the |list is necessary for python3
+ tmpl = env.from_string('{{ foo.items()|list }}')
assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
- tmpl = env.from_string('{{ foo|attr("items")() }}')
+ tmpl = env.from_string('{{ foo|attr("items")()|list }}')
assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
tmpl = env.from_string('{{ foo["items"] }}')
assert tmpl.render(foo={'items': 42}) == '42'
tmpl.render(fail=lambda: 1 / 0)
tmpl = env.get_template('broken.html')
self.assert_traceback_matches(test, r'''
- File ".*?broken.html", line 2, in top-level template code
+ File ".*?broken.html", line 2, in (top-level template code|<module>)
\{\{ fail\(\) \}\}
File ".*?debug.pyc?", line \d+, in <lambda>
tmpl\.render\(fail=lambda: 1 / 0\)
-ZeroDivisionError: integer division or modulo by zero
+ZeroDivisionError: int(eger)? division or modulo by zero
''')
def test_syntax_error(self):
- self.assert_traceback_matches(lambda: env.get_template('syntaxerror.html'), r'''
- File ".*?syntaxerror.html", line 4, in template
+ # XXX: the .*? is necessary for python3 which does not hide
+ # some of the stack frames we don't want to show. Not sure
+ # what's up with that, but that is not that critical. Should
+ # be fixed though.
+ self.assert_traceback_matches(lambda: env.get_template('syntaxerror.html'), r'''(?sm)
+ File ".*?syntaxerror.html", line 4, in (template|<module>)
\{% endif %\}
-TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
+ .*?
+(jinja2\.exceptions\.)?TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
''')
def test_regular_syntax_error(self):
self.assert_traceback_matches(test, r'''
File ".*debug.pyc?", line \d+, in test
raise TemplateSyntaxError\('wtf', 42\)
-TemplateSyntaxError: wtf
+(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
line 42''')
assert tmpl.render() == 'raboof|[3, 2, 1]'
def test_string(self):
- tmpl = env.from_string('''{{ range(10)|string }}''')
- assert tmpl.render(foo=range(10)) == unicode(xrange(10))
+ x = [1, 2, 3, 4, 5]
+ tmpl = env.from_string('''{{ obj|string }}''')
+ assert tmpl.render(obj=x) == unicode(x)
def test_title(self):
tmpl = env.from_string('''{{ "foo bar"|title }}''')
:license: BSD, see LICENSE for more details.
"""
import os
+import sys
import time
import tempfile
import unittest
env = Environment()
+# how does a string look like in jinja syntax?
+if sys.version_info < (3, 0):
+ def jinja_string_repr(string):
+ return repr(string)[1:]
+else:
+ jinja_string_repr = repr
+
+
class LexerTestCase(JinjaTestCase):
def test_raw1(self):
def test_string_escapes(self):
for char in u'\0', u'\u2668', u'\xe4', u'\t', u'\r', u'\n':
- tmpl = env.from_string('{{ %s }}' % repr(char)[1:])
+ tmpl = env.from_string('{{ %s }}' % jinja_string_repr(char))
assert tmpl.render() == char
assert env.from_string('{{ "\N{HOT SPRINGS}" }}').render() == u'\u2668'
def test_bytefallback(self):
+ from pprint import pformat
tmpl = env.from_string(u'''{{ 'foo'|pprint }}|{{ 'bär'|pprint }}''')
- assert tmpl.render() == u"'foo'|u'b\\xe4r'"
+ assert tmpl.render() == pformat('foo') + '|' + pformat(u'bär')
def test_operators(self):
from jinja2.lexer import operators