Refs #33002 -- Made template_tests.tests.py's tests test both Lexer and DebugLexer.
This commit is contained in:
parent
921e4ccb77
commit
b2be7e12cc
|
@ -11,10 +11,12 @@ from django.utils import translation
|
||||||
from django.utils.html import escape
|
from django.utils.html import escape
|
||||||
|
|
||||||
|
|
||||||
class TemplateTests(SimpleTestCase):
|
class TemplateTestMixin:
|
||||||
|
def _engine(self, **kwargs):
|
||||||
|
return Engine(debug=self.debug_engine, **kwargs)
|
||||||
|
|
||||||
def test_string_origin(self):
|
def test_string_origin(self):
|
||||||
template = Engine().from_string('string template')
|
template = self._engine().from_string('string template')
|
||||||
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
|
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
|
||||||
self.assertIsNone(template.origin.loader_name)
|
self.assertIsNone(template.origin.loader_name)
|
||||||
self.assertEqual(template.source, 'string template')
|
self.assertEqual(template.source, 'string template')
|
||||||
|
@ -25,7 +27,7 @@ class TemplateTests(SimpleTestCase):
|
||||||
#9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
|
#9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
|
||||||
be set.
|
be set.
|
||||||
"""
|
"""
|
||||||
t = Engine(debug=True).from_string('{% url will_not_match %}')
|
t = self._engine().from_string('{% url will_not_match %}')
|
||||||
c = Context()
|
c = Context()
|
||||||
with self.assertRaises(NoReverseMatch):
|
with self.assertRaises(NoReverseMatch):
|
||||||
t.render(c)
|
t.render(c)
|
||||||
|
@ -35,7 +37,7 @@ class TemplateTests(SimpleTestCase):
|
||||||
#19827 -- url tag should keep original strack trace when reraising
|
#19827 -- url tag should keep original strack trace when reraising
|
||||||
exception.
|
exception.
|
||||||
"""
|
"""
|
||||||
t = Engine().from_string('{% url will_not_match %}')
|
t = self._engine().from_string('{% url will_not_match %}')
|
||||||
c = Context()
|
c = Context()
|
||||||
try:
|
try:
|
||||||
t.render(c)
|
t.render(c)
|
||||||
|
@ -52,23 +54,24 @@ class TemplateTests(SimpleTestCase):
|
||||||
# 16770 -- The template system doesn't wrap exceptions, but annotates
|
# 16770 -- The template system doesn't wrap exceptions, but annotates
|
||||||
them.
|
them.
|
||||||
"""
|
"""
|
||||||
engine = Engine(debug=True)
|
engine = self._engine()
|
||||||
c = Context({"coconuts": lambda: 42 / 0})
|
c = Context({"coconuts": lambda: 42 / 0})
|
||||||
t = engine.from_string("{{ coconuts }}")
|
t = engine.from_string("{{ coconuts }}")
|
||||||
|
|
||||||
with self.assertRaises(ZeroDivisionError) as e:
|
with self.assertRaises(ZeroDivisionError) as e:
|
||||||
t.render(c)
|
t.render(c)
|
||||||
|
|
||||||
debug = e.exception.template_debug
|
if self.debug_engine:
|
||||||
self.assertEqual(debug['start'], 0)
|
debug = e.exception.template_debug
|
||||||
self.assertEqual(debug['end'], 14)
|
self.assertEqual(debug['start'], 0)
|
||||||
|
self.assertEqual(debug['end'], 14)
|
||||||
|
|
||||||
def test_invalid_block_suggestion(self):
|
def test_invalid_block_suggestion(self):
|
||||||
"""
|
"""
|
||||||
Error messages should include the unexpected block name and be in all
|
Error messages should include the unexpected block name and be in all
|
||||||
English.
|
English.
|
||||||
"""
|
"""
|
||||||
engine = Engine()
|
engine = self._engine()
|
||||||
msg = (
|
msg = (
|
||||||
"Invalid block tag on line 1: 'endblock', expected 'elif', 'else' "
|
"Invalid block tag on line 1: 'endblock', expected 'elif', 'else' "
|
||||||
"or 'endif'. Did you forget to register or load this tag?"
|
"or 'endif'. Did you forget to register or load this tag?"
|
||||||
|
@ -78,7 +81,7 @@ class TemplateTests(SimpleTestCase):
|
||||||
engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
|
engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
|
||||||
|
|
||||||
def test_unknown_block_tag(self):
|
def test_unknown_block_tag(self):
|
||||||
engine = Engine()
|
engine = self._engine()
|
||||||
msg = (
|
msg = (
|
||||||
"Invalid block tag on line 1: 'foobar'. Did you forget to "
|
"Invalid block tag on line 1: 'foobar'. Did you forget to "
|
||||||
"register or load this tag?"
|
"register or load this tag?"
|
||||||
|
@ -91,69 +94,71 @@ class TemplateTests(SimpleTestCase):
|
||||||
19819 -- Make sure the correct token is highlighted for
|
19819 -- Make sure the correct token is highlighted for
|
||||||
FilterExpression errors.
|
FilterExpression errors.
|
||||||
"""
|
"""
|
||||||
engine = Engine(debug=True)
|
engine = self._engine()
|
||||||
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
|
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
|
||||||
|
|
||||||
with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
|
with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
|
||||||
engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
|
engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
|
||||||
|
|
||||||
debug = e.exception.template_debug
|
if self.debug_engine:
|
||||||
self.assertEqual((debug['start'], debug['end']), (10, 23))
|
debug = e.exception.template_debug
|
||||||
self.assertEqual((debug['during']), '{{ foo@bar }}')
|
self.assertEqual((debug['start'], debug['end']), (10, 23))
|
||||||
|
self.assertEqual((debug['during']), '{{ foo@bar }}')
|
||||||
|
|
||||||
def test_compile_tag_error(self):
|
def test_compile_tag_error(self):
|
||||||
"""
|
"""
|
||||||
Errors raised while compiling nodes should include the token
|
Errors raised while compiling nodes should include the token
|
||||||
information.
|
information.
|
||||||
"""
|
"""
|
||||||
engine = Engine(
|
engine = self._engine(
|
||||||
debug=True,
|
|
||||||
libraries={'bad_tag': 'template_tests.templatetags.bad_tag'},
|
libraries={'bad_tag': 'template_tests.templatetags.bad_tag'},
|
||||||
)
|
)
|
||||||
with self.assertRaises(RuntimeError) as e:
|
with self.assertRaises(RuntimeError) as e:
|
||||||
engine.from_string("{% load bad_tag %}{% badtag %}")
|
engine.from_string("{% load bad_tag %}{% badtag %}")
|
||||||
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
if self.debug_engine:
|
||||||
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
||||||
|
|
||||||
def test_compile_tag_error_27584(self):
|
def test_compile_tag_error_27584(self):
|
||||||
engine = Engine(
|
engine = self._engine(
|
||||||
app_dirs=True,
|
app_dirs=True,
|
||||||
debug=True,
|
|
||||||
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
|
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
|
||||||
)
|
)
|
||||||
t = engine.get_template('27584_parent.html')
|
t = engine.get_template('27584_parent.html')
|
||||||
with self.assertRaises(TemplateSyntaxError) as e:
|
with self.assertRaises(TemplateSyntaxError) as e:
|
||||||
t.render(Context())
|
t.render(Context())
|
||||||
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
if self.debug_engine:
|
||||||
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
||||||
|
|
||||||
def test_compile_tag_error_27956(self):
|
def test_compile_tag_error_27956(self):
|
||||||
"""Errors in a child of {% extends %} are displayed correctly."""
|
"""Errors in a child of {% extends %} are displayed correctly."""
|
||||||
engine = Engine(
|
engine = self._engine(
|
||||||
app_dirs=True,
|
app_dirs=True,
|
||||||
debug=True,
|
|
||||||
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
|
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
|
||||||
)
|
)
|
||||||
t = engine.get_template('27956_child.html')
|
t = engine.get_template('27956_child.html')
|
||||||
with self.assertRaises(TemplateSyntaxError) as e:
|
with self.assertRaises(TemplateSyntaxError) as e:
|
||||||
t.render(Context())
|
t.render(Context())
|
||||||
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
if self.debug_engine:
|
||||||
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
||||||
|
|
||||||
def test_render_tag_error_in_extended_block(self):
|
def test_render_tag_error_in_extended_block(self):
|
||||||
"""Errors in extended block are displayed correctly."""
|
"""Errors in extended block are displayed correctly."""
|
||||||
e = Engine(app_dirs=True, debug=True)
|
e = self._engine(app_dirs=True)
|
||||||
template = e.get_template('test_extends_block_error.html')
|
template = e.get_template('test_extends_block_error.html')
|
||||||
context = Context()
|
context = Context()
|
||||||
with self.assertRaises(TemplateDoesNotExist) as cm:
|
with self.assertRaises(TemplateDoesNotExist) as cm:
|
||||||
template.render(context)
|
template.render(context)
|
||||||
self.assertEqual(
|
if self.debug_engine:
|
||||||
cm.exception.template_debug['during'],
|
self.assertEqual(
|
||||||
escape('{% include "missing.html" %}'),
|
cm.exception.template_debug['during'],
|
||||||
)
|
escape('{% include "missing.html" %}'),
|
||||||
|
)
|
||||||
|
|
||||||
def test_super_errors(self):
|
def test_super_errors(self):
|
||||||
"""
|
"""
|
||||||
#18169 -- NoReverseMatch should not be silence in block.super.
|
#18169 -- NoReverseMatch should not be silence in block.super.
|
||||||
"""
|
"""
|
||||||
engine = Engine(app_dirs=True)
|
engine = self._engine(app_dirs=True)
|
||||||
t = engine.get_template('included_content.html')
|
t = engine.get_template('included_content.html')
|
||||||
with self.assertRaises(NoReverseMatch):
|
with self.assertRaises(NoReverseMatch):
|
||||||
t.render(Context())
|
t.render(Context())
|
||||||
|
@ -164,7 +169,7 @@ class TemplateTests(SimpleTestCase):
|
||||||
"""
|
"""
|
||||||
group = Group(name="清風")
|
group = Group(name="清風")
|
||||||
c1 = Context({"objs": [group]})
|
c1 = Context({"objs": [group]})
|
||||||
t1 = Engine().from_string('{% debug %}')
|
t1 = self._engine().from_string('{% debug %}')
|
||||||
self.assertIn("清風", t1.render(c1))
|
self.assertIn("清風", t1.render(c1))
|
||||||
|
|
||||||
def test_extends_generic_template(self):
|
def test_extends_generic_template(self):
|
||||||
|
@ -172,7 +177,7 @@ class TemplateTests(SimpleTestCase):
|
||||||
#24338 -- Allow extending django.template.backends.django.Template
|
#24338 -- Allow extending django.template.backends.django.Template
|
||||||
objects.
|
objects.
|
||||||
"""
|
"""
|
||||||
engine = Engine()
|
engine = self._engine()
|
||||||
parent = engine.from_string('{% block content %}parent{% endblock %}')
|
parent = engine.from_string('{% block content %}parent{% endblock %}')
|
||||||
child = engine.from_string(
|
child = engine.from_string(
|
||||||
'{% extends parent %}{% block content %}child{% endblock %}')
|
'{% extends parent %}{% block content %}child{% endblock %}')
|
||||||
|
@ -183,6 +188,14 @@ class TemplateTests(SimpleTestCase):
|
||||||
#25848 -- Set origin on Node so debugging tools can determine which
|
#25848 -- Set origin on Node so debugging tools can determine which
|
||||||
template the node came from even if extending or including templates.
|
template the node came from even if extending or including templates.
|
||||||
"""
|
"""
|
||||||
template = Engine().from_string('content')
|
template = self._engine().from_string('content')
|
||||||
for node in template.nodelist:
|
for node in template.nodelist:
|
||||||
self.assertEqual(node.origin, template.origin)
|
self.assertEqual(node.origin, template.origin)
|
||||||
|
|
||||||
|
|
||||||
|
class TemplateTests(TemplateTestMixin, SimpleTestCase):
|
||||||
|
debug_engine = False
|
||||||
|
|
||||||
|
|
||||||
|
class DebugTemplateTests(TemplateTestMixin, SimpleTestCase):
|
||||||
|
debug_engine = True
|
||||||
|
|
Loading…
Reference in New Issue