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