debug.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.testsuite.debug
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Tests the debug system.
  6. :copyright: (c) 2010 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import unittest
  10. from jinja2.testsuite import JinjaTestCase, filesystem_loader
  11. from jinja2 import Environment, TemplateSyntaxError
  12. env = Environment(loader=filesystem_loader)
  13. class DebugTestCase(JinjaTestCase):
  14. def test_runtime_error(self):
  15. def test():
  16. tmpl.render(fail=lambda: 1 / 0)
  17. tmpl = env.get_template('broken.html')
  18. self.assert_traceback_matches(test, r'''
  19. File ".*?broken.html", line 2, in (top-level template code|<module>)
  20. \{\{ fail\(\) \}\}
  21. File ".*?debug.pyc?", line \d+, in <lambda>
  22. tmpl\.render\(fail=lambda: 1 / 0\)
  23. ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero
  24. ''')
  25. def test_syntax_error(self):
  26. # XXX: the .*? is necessary for python3 which does not hide
  27. # some of the stack frames we don't want to show. Not sure
  28. # what's up with that, but that is not that critical. Should
  29. # be fixed though.
  30. self.assert_traceback_matches(lambda: env.get_template('syntaxerror.html'), r'''(?sm)
  31. File ".*?syntaxerror.html", line 4, in (template|<module>)
  32. \{% endif %\}.*?
  33. (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'.
  34. ''')
  35. def test_regular_syntax_error(self):
  36. def test():
  37. raise TemplateSyntaxError('wtf', 42)
  38. self.assert_traceback_matches(test, r'''
  39. File ".*debug.pyc?", line \d+, in test
  40. raise TemplateSyntaxError\('wtf', 42\)
  41. (jinja2\.exceptions\.)?TemplateSyntaxError: wtf
  42. line 42''')
  43. def suite():
  44. suite = unittest.TestSuite()
  45. suite.addTest(unittest.makeSuite(DebugTestCase))
  46. return suite