2013-07-01 12:22:27 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2015-04-11 23:41:45 +00:00
|
|
|
from django.template import Context, Engine
|
2015-06-20 11:37:43 +00:00
|
|
|
from django.template.base import TextNode, VariableNode
|
|
|
|
from django.utils import six
|
2010-03-02 07:42:51 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-03-02 07:42:51 +00:00
|
|
|
class NodelistTest(TestCase):
|
|
|
|
|
2015-04-11 23:41:45 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.engine = Engine()
|
|
|
|
super(NodelistTest, cls).setUpClass()
|
|
|
|
|
2010-03-02 07:42:51 +00:00
|
|
|
def test_for(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
template = self.engine.from_string('{% for i in 1 %}{{ a }}{% endfor %}')
|
2010-03-02 07:42:51 +00:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_if(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
template = self.engine.from_string('{% if x %}{{ a }}{% endif %}')
|
2010-03-02 07:42:51 +00:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_ifequal(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
template = self.engine.from_string('{% ifequal x y %}{{ a }}{% endifequal %}')
|
2010-03-02 07:42:51 +00:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_ifchanged(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
template = self.engine.from_string('{% ifchanged x %}{{ a }}{% endifchanged %}')
|
2010-03-02 07:42:51 +00:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
2011-08-12 14:15:17 +00:00
|
|
|
|
|
|
|
|
2015-06-20 11:37:43 +00:00
|
|
|
class TextNodeTest(TestCase):
|
|
|
|
|
|
|
|
def test_textnode_repr(self):
|
|
|
|
engine = Engine()
|
|
|
|
for temptext, reprtext in [
|
|
|
|
("Hello, world!", "<TextNode: u'Hello, world!'>"),
|
|
|
|
("One\ntwo.", "<TextNode: u'One\\ntwo.'>"),
|
|
|
|
]:
|
|
|
|
template = engine.from_string(temptext)
|
|
|
|
texts = template.nodelist.get_nodes_by_type(TextNode)
|
|
|
|
if six.PY3:
|
|
|
|
reprtext = reprtext.replace("u'", "'")
|
|
|
|
self.assertEqual(repr(texts[0]), reprtext)
|
|
|
|
|
|
|
|
|
2011-08-12 14:15:17 +00:00
|
|
|
class ErrorIndexTest(TestCase):
|
|
|
|
"""
|
|
|
|
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)),
|
|
|
|
('{% 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)),
|
|
|
|
('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', (18, 37)),
|
|
|
|
]
|
|
|
|
context = Context({
|
|
|
|
'range': range(5),
|
|
|
|
'five': 5,
|
|
|
|
})
|
2015-05-08 20:10:36 +00:00
|
|
|
engine = Engine(debug=True, libraries={'bad_tag': 'template_tests.templatetags.bad_tag'})
|
2011-08-12 14:15:17 +00:00
|
|
|
for source, expected_error_source_index in tests:
|
2015-04-11 23:41:45 +00:00
|
|
|
template = engine.from_string(source)
|
2011-08-12 14:15:17 +00:00
|
|
|
try:
|
|
|
|
template.render(context)
|
2012-04-28 16:09:37 +00:00
|
|
|
except (RuntimeError, TypeError) as e:
|
2015-03-06 15:53:25 +00:00
|
|
|
debug = e.template_debug
|
|
|
|
self.assertEqual((debug['start'], debug['end']), expected_error_source_index)
|