summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2021-08-06 04:09:31 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-08-09 13:37:02 +0200
commitb2be7e12cce8e62eeee512fbea4a32503d048120 (patch)
treee92234149261a71d3a64ab6da3b4ce17880feefe /tests/template_tests
parent921e4ccb77ed2056d5cdfc6756ee82b54831937f (diff)
downloaddjango-b2be7e12cce8e62eeee512fbea4a32503d048120.tar.gz
Refs #33002 -- Made template_tests.tests.py's tests test both Lexer and DebugLexer.
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/tests.py77
1 files changed, 45 insertions, 32 deletions
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index 5ea026723e..8cc86bd44f 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -11,10 +11,12 @@ from django.utils import translation
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):
- template = Engine().from_string('string template')
+ template = self._engine().from_string('string template')
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
self.assertIsNone(template.origin.loader_name)
self.assertEqual(template.source, 'string template')
@@ -25,7 +27,7 @@ class TemplateTests(SimpleTestCase):
#9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
be set.
"""
- t = Engine(debug=True).from_string('{% url will_not_match %}')
+ t = self._engine().from_string('{% url will_not_match %}')
c = Context()
with self.assertRaises(NoReverseMatch):
t.render(c)
@@ -35,7 +37,7 @@ class TemplateTests(SimpleTestCase):
#19827 -- url tag should keep original strack trace when reraising
exception.
"""
- t = Engine().from_string('{% url will_not_match %}')
+ t = self._engine().from_string('{% url will_not_match %}')
c = Context()
try:
t.render(c)
@@ -52,23 +54,24 @@ class TemplateTests(SimpleTestCase):
# 16770 -- The template system doesn't wrap exceptions, but annotates
them.
"""
- engine = Engine(debug=True)
+ engine = self._engine()
c = Context({"coconuts": lambda: 42 / 0})
t = engine.from_string("{{ coconuts }}")
with self.assertRaises(ZeroDivisionError) as e:
t.render(c)
- debug = e.exception.template_debug
- self.assertEqual(debug['start'], 0)
- self.assertEqual(debug['end'], 14)
+ if self.debug_engine:
+ debug = e.exception.template_debug
+ self.assertEqual(debug['start'], 0)
+ self.assertEqual(debug['end'], 14)
def test_invalid_block_suggestion(self):
"""
Error messages should include the unexpected block name and be in all
English.
"""
- engine = Engine()
+ engine = self._engine()
msg = (
"Invalid block tag on line 1: 'endblock', expected 'elif', 'else' "
"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 %}")
def test_unknown_block_tag(self):
- engine = Engine()
+ engine = self._engine()
msg = (
"Invalid block tag on line 1: 'foobar'. Did you forget to "
"register or load this tag?"
@@ -91,69 +94,71 @@ class TemplateTests(SimpleTestCase):
19819 -- Make sure the correct token is highlighted for
FilterExpression errors.
"""
- engine = Engine(debug=True)
+ engine = self._engine()
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
- debug = e.exception.template_debug
- self.assertEqual((debug['start'], debug['end']), (10, 23))
- self.assertEqual((debug['during']), '{{ foo@bar }}')
+ if self.debug_engine:
+ debug = e.exception.template_debug
+ self.assertEqual((debug['start'], debug['end']), (10, 23))
+ self.assertEqual((debug['during']), '{{ foo@bar }}')
def test_compile_tag_error(self):
"""
Errors raised while compiling nodes should include the token
information.
"""
- engine = Engine(
- debug=True,
+ engine = self._engine(
libraries={'bad_tag': 'template_tests.templatetags.bad_tag'},
)
with self.assertRaises(RuntimeError) as e:
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):
- engine = Engine(
+ engine = self._engine(
app_dirs=True,
- debug=True,
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
)
t = engine.get_template('27584_parent.html')
with self.assertRaises(TemplateSyntaxError) as e:
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):
"""Errors in a child of {% extends %} are displayed correctly."""
- engine = Engine(
+ engine = self._engine(
app_dirs=True,
- debug=True,
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
)
t = engine.get_template('27956_child.html')
with self.assertRaises(TemplateSyntaxError) as e:
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):
"""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')
context = Context()
with self.assertRaises(TemplateDoesNotExist) as cm:
template.render(context)
- self.assertEqual(
- cm.exception.template_debug['during'],
- escape('{% include "missing.html" %}'),
- )
+ if self.debug_engine:
+ self.assertEqual(
+ cm.exception.template_debug['during'],
+ escape('{% include "missing.html" %}'),
+ )
def test_super_errors(self):
"""
#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')
with self.assertRaises(NoReverseMatch):
t.render(Context())
@@ -164,7 +169,7 @@ class TemplateTests(SimpleTestCase):
"""
group = Group(name="清風")
c1 = Context({"objs": [group]})
- t1 = Engine().from_string('{% debug %}')
+ t1 = self._engine().from_string('{% debug %}')
self.assertIn("清風", t1.render(c1))
def test_extends_generic_template(self):
@@ -172,7 +177,7 @@ class TemplateTests(SimpleTestCase):
#24338 -- Allow extending django.template.backends.django.Template
objects.
"""
- engine = Engine()
+ engine = self._engine()
parent = engine.from_string('{% block content %}parent{% endblock %}')
child = engine.from_string(
'{% extends parent %}{% block content %}child{% endblock %}')
@@ -183,6 +188,14 @@ class TemplateTests(SimpleTestCase):
#25848 -- Set origin on Node so debugging tools can determine which
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:
self.assertEqual(node.origin, template.origin)
+
+
+class TemplateTests(TemplateTestMixin, SimpleTestCase):
+ debug_engine = False
+
+
+class DebugTemplateTests(TemplateTestMixin, SimpleTestCase):
+ debug_engine = True