blob: 7552dec38932657f126c844fce56ccd263ff1a82 [file] [log] [blame]
Armin Ronacher1fb42692010-02-09 21:14:16 +01001# -*- coding: utf-8 -*-
2"""
3 jinja2.testsuite.debug
4 ~~~~~~~~~~~~~~~~~~~~~~
5
6 Tests the debug system.
7
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
10"""
Armin Ronacher821a4232010-02-17 07:59:38 +010011import sys
Armin Ronacher1fb42692010-02-09 21:14:16 +010012import unittest
13
14from jinja2.testsuite import JinjaTestCase, filesystem_loader
15
16from jinja2 import Environment, TemplateSyntaxError
17
18env = Environment(loader=filesystem_loader)
19
20
21class DebugTestCase(JinjaTestCase):
22
Armin Ronacher821a4232010-02-17 07:59:38 +010023 if sys.version_info[:2] != (2, 4):
24 def test_runtime_error(self):
25 def test():
26 tmpl.render(fail=lambda: 1 / 0)
27 tmpl = env.get_template('broken.html')
28 self.assert_traceback_matches(test, r'''
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010029 File ".*?broken.html", line 2, in (top-level template code|<module>)
Armin Ronacher1fb42692010-02-09 21:14:16 +010030 \{\{ fail\(\) \}\}
31 File ".*?debug.pyc?", line \d+, in <lambda>
32 tmpl\.render\(fail=lambda: 1 / 0\)
Georg Brandl17f54472010-02-21 17:49:04 +010033ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero
Armin Ronacher1fb42692010-02-09 21:14:16 +010034''')
35
36 def test_syntax_error(self):
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010037 # XXX: the .*? is necessary for python3 which does not hide
38 # some of the stack frames we don't want to show. Not sure
39 # what's up with that, but that is not that critical. Should
40 # be fixed though.
41 self.assert_traceback_matches(lambda: env.get_template('syntaxerror.html'), r'''(?sm)
42 File ".*?syntaxerror.html", line 4, in (template|<module>)
Armin Ronacher1540b2b2010-02-10 02:13:51 +010043 \{% endif %\}.*?
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010044(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'.
Armin Ronacher1fb42692010-02-09 21:14:16 +010045 ''')
46
47 def test_regular_syntax_error(self):
48 def test():
49 raise TemplateSyntaxError('wtf', 42)
50 self.assert_traceback_matches(test, r'''
51 File ".*debug.pyc?", line \d+, in test
52 raise TemplateSyntaxError\('wtf', 42\)
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010053(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
Armin Ronacher1fb42692010-02-09 21:14:16 +010054 line 42''')
55
56
57def suite():
58 suite = unittest.TestSuite()
59 suite.addTest(unittest.makeSuite(DebugTestCase))
60 return suite