test_jinja2.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Since this package contains a "jinja2" directory, this is required to
  2. # silence an ImportWarning warning on Python 2.
  3. from __future__ import absolute_import
  4. from unittest import skipIf
  5. from django.template import TemplateSyntaxError
  6. from .test_dummy import TemplateStringsTests
  7. try:
  8. import jinja2
  9. except ImportError:
  10. jinja2 = None
  11. Jinja2 = None
  12. else:
  13. from django.template.backends.jinja2 import Jinja2
  14. @skipIf(jinja2 is None, "this test requires jinja2")
  15. class Jinja2Tests(TemplateStringsTests):
  16. engine_class = Jinja2
  17. backend_name = 'jinja2'
  18. options = {'keep_trailing_newline': True}
  19. def test_origin(self):
  20. template = self.engine.get_template('template_backends/hello.html')
  21. self.assertTrue(template.origin.name.endswith('hello.html'))
  22. self.assertEqual(template.origin.template_name, 'template_backends/hello.html')
  23. def test_origin_from_string(self):
  24. template = self.engine.from_string('Hello!\n')
  25. self.assertEqual(template.origin.name, '<template>')
  26. self.assertIsNone(template.origin.template_name)
  27. def test_self_context(self):
  28. """
  29. Using 'self' in the context should not throw errors (#24538).
  30. """
  31. # self will be overridden to be a TemplateReference, so the self
  32. # variable will not come through. Attempting to use one though should
  33. # not throw an error.
  34. template = self.engine.from_string('hello {{ foo }}!')
  35. content = template.render(context={'self': 'self', 'foo': 'world'})
  36. self.assertEqual(content, 'hello world!')
  37. def test_exception_debug_info_min_context(self):
  38. with self.assertRaises(TemplateSyntaxError) as e:
  39. self.engine.get_template('template_backends/syntax_error.html')
  40. debug = e.exception.template_debug
  41. self.assertEqual(debug['after'], '')
  42. self.assertEqual(debug['before'], '')
  43. self.assertEqual(debug['during'], '{% block %}')
  44. self.assertEqual(debug['bottom'], 1)
  45. self.assertEqual(debug['top'], 0)
  46. self.assertEqual(debug['line'], 1)
  47. self.assertEqual(debug['total'], 1)
  48. self.assertEqual(len(debug['source_lines']), 1)
  49. self.assertTrue(debug['name'].endswith('syntax_error.html'))
  50. self.assertIn('message', debug)
  51. def test_exception_debug_info_max_context(self):
  52. with self.assertRaises(TemplateSyntaxError) as e:
  53. self.engine.get_template('template_backends/syntax_error2.html')
  54. debug = e.exception.template_debug
  55. self.assertEqual(debug['after'], '')
  56. self.assertEqual(debug['before'], '')
  57. self.assertEqual(debug['during'], '{% block %}')
  58. self.assertEqual(debug['bottom'], 26)
  59. self.assertEqual(debug['top'], 5)
  60. self.assertEqual(debug['line'], 16)
  61. self.assertEqual(debug['total'], 31)
  62. self.assertEqual(len(debug['source_lines']), 21)
  63. self.assertTrue(debug['name'].endswith('syntax_error2.html'))
  64. self.assertIn('message', debug)