2014-12-15 04:22:57 +08:00
|
|
|
from unittest import skipIf
|
|
|
|
|
2015-04-25 03:33:03 +08:00
|
|
|
from django.template import TemplateSyntaxError
|
2016-06-27 21:56:21 +08:00
|
|
|
from django.test import RequestFactory
|
2015-04-25 03:33:03 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .test_dummy import TemplateStringsTests
|
|
|
|
|
2014-11-24 21:34:02 +08:00
|
|
|
try:
|
|
|
|
import jinja2
|
|
|
|
except ImportError:
|
2014-12-15 04:22:57 +08:00
|
|
|
jinja2 = None
|
|
|
|
Jinja2 = None
|
2014-11-24 21:34:02 +08:00
|
|
|
else:
|
|
|
|
from django.template.backends.jinja2 import Jinja2
|
2014-12-15 04:22:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(jinja2 is None, "this test requires jinja2")
|
|
|
|
class Jinja2Tests(TemplateStringsTests):
|
|
|
|
|
|
|
|
engine_class = Jinja2
|
|
|
|
backend_name = 'jinja2'
|
2016-06-27 21:56:21 +08:00
|
|
|
options = {
|
|
|
|
'keep_trailing_newline': True,
|
|
|
|
'context_processors': [
|
|
|
|
'django.template.context_processors.static',
|
|
|
|
],
|
|
|
|
}
|
2015-03-31 12:37:17 +08:00
|
|
|
|
2015-04-25 03:33:03 +08:00
|
|
|
def test_origin(self):
|
|
|
|
template = self.engine.get_template('template_backends/hello.html')
|
|
|
|
self.assertTrue(template.origin.name.endswith('hello.html'))
|
|
|
|
self.assertEqual(template.origin.template_name, 'template_backends/hello.html')
|
|
|
|
|
|
|
|
def test_origin_from_string(self):
|
|
|
|
template = self.engine.from_string('Hello!\n')
|
|
|
|
self.assertEqual(template.origin.name, '<template>')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(template.origin.template_name)
|
2015-04-25 03:33:03 +08:00
|
|
|
|
2015-03-31 12:37:17 +08:00
|
|
|
def test_self_context(self):
|
|
|
|
"""
|
2015-04-02 01:08:43 +08:00
|
|
|
Using 'self' in the context should not throw errors (#24538).
|
2015-03-31 12:37:17 +08:00
|
|
|
"""
|
|
|
|
# self will be overridden to be a TemplateReference, so the self
|
|
|
|
# variable will not come through. Attempting to use one though should
|
|
|
|
# not throw an error.
|
2015-04-02 01:08:43 +08:00
|
|
|
template = self.engine.from_string('hello {{ foo }}!')
|
2015-03-31 12:37:17 +08:00
|
|
|
content = template.render(context={'self': 'self', 'foo': 'world'})
|
|
|
|
self.assertEqual(content, 'hello world!')
|
2015-04-25 03:33:03 +08:00
|
|
|
|
|
|
|
def test_exception_debug_info_min_context(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
self.engine.get_template('template_backends/syntax_error.html')
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual(debug['after'], '')
|
|
|
|
self.assertEqual(debug['before'], '')
|
|
|
|
self.assertEqual(debug['during'], '{% block %}')
|
|
|
|
self.assertEqual(debug['bottom'], 1)
|
|
|
|
self.assertEqual(debug['top'], 0)
|
|
|
|
self.assertEqual(debug['line'], 1)
|
|
|
|
self.assertEqual(debug['total'], 1)
|
|
|
|
self.assertEqual(len(debug['source_lines']), 1)
|
|
|
|
self.assertTrue(debug['name'].endswith('syntax_error.html'))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIn('message', debug)
|
2015-04-25 03:33:03 +08:00
|
|
|
|
|
|
|
def test_exception_debug_info_max_context(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
self.engine.get_template('template_backends/syntax_error2.html')
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual(debug['after'], '')
|
|
|
|
self.assertEqual(debug['before'], '')
|
|
|
|
self.assertEqual(debug['during'], '{% block %}')
|
|
|
|
self.assertEqual(debug['bottom'], 26)
|
|
|
|
self.assertEqual(debug['top'], 5)
|
|
|
|
self.assertEqual(debug['line'], 16)
|
|
|
|
self.assertEqual(debug['total'], 31)
|
|
|
|
self.assertEqual(len(debug['source_lines']), 21)
|
|
|
|
self.assertTrue(debug['name'].endswith('syntax_error2.html'))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIn('message', debug)
|
2016-06-27 21:56:21 +08:00
|
|
|
|
|
|
|
def test_context_processors(self):
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
template = self.engine.from_string('Static URL: {{ STATIC_URL }}')
|
|
|
|
content = template.render(request=request)
|
|
|
|
self.assertEqual(content, 'Static URL: /static/')
|
|
|
|
with self.settings(STATIC_URL='/s/'):
|
|
|
|
content = template.render(request=request)
|
|
|
|
self.assertEqual(content, 'Static URL: /s/')
|