2015-04-12 07:41:45 +08:00
|
|
|
from django.template import Context, Engine
|
2015-06-20 19:37:43 +08:00
|
|
|
from django.template.base import TextNode, VariableNode
|
2021-01-14 17:14:57 +08:00
|
|
|
from django.test import SimpleTestCase
|
2010-03-02 15:42:51 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2020-05-03 22:55:05 +08:00
|
|
|
class NodelistTest(SimpleTestCase):
|
2010-03-02 15:42:51 +08:00
|
|
|
|
2015-04-12 07:41:45 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.engine = Engine()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2015-04-12 07:41:45 +08:00
|
|
|
|
2010-03-02 15:42:51 +08:00
|
|
|
def test_for(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
template = self.engine.from_string('{% for i in 1 %}{{ a }}{% endfor %}')
|
2010-03-02 15:42:51 +08:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_if(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
template = self.engine.from_string('{% if x %}{{ a }}{% endif %}')
|
2010-03-02 15:42:51 +08:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_ifchanged(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
template = self.engine.from_string('{% ifchanged x %}{{ a }}{% endifchanged %}')
|
2010-03-02 15:42:51 +08:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
2011-08-12 22:15:17 +08:00
|
|
|
|
|
|
|
|
2020-05-03 22:55:05 +08:00
|
|
|
class TextNodeTest(SimpleTestCase):
|
2015-06-20 19:37:43 +08:00
|
|
|
|
|
|
|
def test_textnode_repr(self):
|
|
|
|
engine = Engine()
|
|
|
|
for temptext, reprtext in [
|
2016-12-01 18:38:01 +08:00
|
|
|
("Hello, world!", "<TextNode: 'Hello, world!'>"),
|
|
|
|
("One\ntwo.", "<TextNode: 'One\\ntwo.'>"),
|
2015-06-20 19:37:43 +08:00
|
|
|
]:
|
|
|
|
template = engine.from_string(temptext)
|
|
|
|
texts = template.nodelist.get_nodes_by_type(TextNode)
|
|
|
|
self.assertEqual(repr(texts[0]), reprtext)
|
|
|
|
|
|
|
|
|
2020-05-03 22:55:05 +08:00
|
|
|
class ErrorIndexTest(SimpleTestCase):
|
2011-08-12 22:15:17 +08:00
|
|
|
"""
|
|
|
|
Checks whether index of error is calculated correctly in
|
|
|
|
template debugger in for loops. Refs ticket #5831
|
|
|
|
"""
|
|
|
|
def test_correct_exception_index(self):
|
|
|
|
tests = [
|
|
|
|
('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}', (38, 56)),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
'{% load bad_tag %}{% for i in range %}{% for j in range %}'
|
|
|
|
'{% badsimpletag %}{% endfor %}{% endfor %}',
|
|
|
|
(58, 76)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'{% load bad_tag %}{% for i in range %}{% badsimpletag %}'
|
|
|
|
'{% for j in range %}Hello{% endfor %}{% endfor %}',
|
|
|
|
(38, 56)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'{% load bad_tag %}{% for i in range %}{% for j in five %}'
|
|
|
|
'{% badsimpletag %}{% endfor %}{% endfor %}',
|
|
|
|
(38, 57)
|
|
|
|
),
|
2011-08-12 22:15:17 +08:00
|
|
|
('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', (18, 37)),
|
|
|
|
]
|
|
|
|
context = Context({
|
|
|
|
'range': range(5),
|
|
|
|
'five': 5,
|
|
|
|
})
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(debug=True, libraries={'bad_tag': 'template_tests.templatetags.bad_tag'})
|
2011-08-12 22:15:17 +08:00
|
|
|
for source, expected_error_source_index in tests:
|
2015-04-12 07:41:45 +08:00
|
|
|
template = engine.from_string(source)
|
2011-08-12 22:15:17 +08:00
|
|
|
try:
|
|
|
|
template.render(context)
|
2012-04-29 00:09:37 +08:00
|
|
|
except (RuntimeError, TypeError) as e:
|
2015-03-06 23:53:25 +08:00
|
|
|
debug = e.template_debug
|
|
|
|
self.assertEqual((debug['start'], debug['end']), expected_error_source_index)
|