blob: ee79498112eda5fc3d79a77f91c15fa7c0288a25 [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"""
11import unittest
12
13from jinja2.testsuite import JinjaTestCase, filesystem_loader
14
15from jinja2 import Environment, TemplateSyntaxError
16
17env = Environment(loader=filesystem_loader)
18
19
20class DebugTestCase(JinjaTestCase):
21
22 def test_runtime_error(self):
23 def test():
24 tmpl.render(fail=lambda: 1 / 0)
25 tmpl = env.get_template('broken.html')
26 self.assert_traceback_matches(test, r'''
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010027 File ".*?broken.html", line 2, in (top-level template code|<module>)
Armin Ronacher1fb42692010-02-09 21:14:16 +010028 \{\{ fail\(\) \}\}
29 File ".*?debug.pyc?", line \d+, in <lambda>
30 tmpl\.render\(fail=lambda: 1 / 0\)
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010031ZeroDivisionError: int(eger)? division or modulo by zero
Armin Ronacher1fb42692010-02-09 21:14:16 +010032''')
33
34 def test_syntax_error(self):
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010035 # XXX: the .*? is necessary for python3 which does not hide
36 # some of the stack frames we don't want to show. Not sure
37 # what's up with that, but that is not that critical. Should
38 # be fixed though.
39 self.assert_traceback_matches(lambda: env.get_template('syntaxerror.html'), r'''(?sm)
40 File ".*?syntaxerror.html", line 4, in (template|<module>)
Armin Ronacher1540b2b2010-02-10 02:13:51 +010041 \{% endif %\}.*?
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010042(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 +010043 ''')
44
45 def test_regular_syntax_error(self):
46 def test():
47 raise TemplateSyntaxError('wtf', 42)
48 self.assert_traceback_matches(test, r'''
49 File ".*debug.pyc?", line \d+, in test
50 raise TemplateSyntaxError\('wtf', 42\)
Armin Ronacher6eb17fc2010-02-10 02:02:31 +010051(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
Armin Ronacher1fb42692010-02-09 21:14:16 +010052 line 42''')
53
54
55def suite():
56 suite = unittest.TestSuite()
57 suite.addTest(unittest.makeSuite(DebugTestCase))
58 return suite