| Armin Ronacher | 1fb4269 | 2010-02-09 21:14:16 +0100 | [diff] [blame] | 1 | # -*- 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 | """ |
| 11 | import unittest |
| 12 | |
| 13 | from jinja2.testsuite import JinjaTestCase, filesystem_loader |
| 14 | |
| 15 | from jinja2 import Environment, TemplateSyntaxError |
| 16 | |
| 17 | env = Environment(loader=filesystem_loader) |
| 18 | |
| 19 | |
| 20 | class 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 Ronacher | 6eb17fc | 2010-02-10 02:02:31 +0100 | [diff] [blame] | 27 | File ".*?broken.html", line 2, in (top-level template code|<module>) |
| Armin Ronacher | 1fb4269 | 2010-02-09 21:14:16 +0100 | [diff] [blame] | 28 | \{\{ fail\(\) \}\} |
| 29 | File ".*?debug.pyc?", line \d+, in <lambda> |
| 30 | tmpl\.render\(fail=lambda: 1 / 0\) |
| Armin Ronacher | 6eb17fc | 2010-02-10 02:02:31 +0100 | [diff] [blame] | 31 | ZeroDivisionError: int(eger)? division or modulo by zero |
| Armin Ronacher | 1fb4269 | 2010-02-09 21:14:16 +0100 | [diff] [blame] | 32 | ''') |
| 33 | |
| 34 | def test_syntax_error(self): |
| Armin Ronacher | 6eb17fc | 2010-02-10 02:02:31 +0100 | [diff] [blame] | 35 | # 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 Ronacher | 1540b2b | 2010-02-10 02:13:51 +0100 | [diff] [blame] | 41 | \{% endif %\}.*? |
| Armin Ronacher | 6eb17fc | 2010-02-10 02:02:31 +0100 | [diff] [blame] | 42 | (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 Ronacher | 1fb4269 | 2010-02-09 21:14:16 +0100 | [diff] [blame] | 43 | ''') |
| 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 Ronacher | 6eb17fc | 2010-02-10 02:02:31 +0100 | [diff] [blame] | 51 | (jinja2\.exceptions\.)?TemplateSyntaxError: wtf |
| Armin Ronacher | 1fb4269 | 2010-02-09 21:14:16 +0100 | [diff] [blame] | 52 | line 42''') |
| 53 | |
| 54 | |
| 55 | def suite(): |
| 56 | suite = unittest.TestSuite() |
| 57 | suite.addTest(unittest.makeSuite(DebugTestCase)) |
| 58 | return suite |