2013-07-01 14:22:27 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2014-11-10 21:40:26 +01:00
|
|
|
from django.template import Context, Template
|
|
|
|
from django.template.base import VariableNode
|
2013-12-23 16:01:13 +01:00
|
|
|
from django.test import override_settings
|
2010-03-02 08:42:51 +01:00
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2010-03-02 08:42:51 +01:00
|
|
|
class NodelistTest(TestCase):
|
|
|
|
|
|
|
|
def test_for(self):
|
2014-10-31 11:38:53 +01:00
|
|
|
template = Template('{% for i in 1 %}{{ a }}{% endfor %}')
|
2010-03-02 08:42:51 +01:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_if(self):
|
2014-10-31 11:38:53 +01:00
|
|
|
template = Template('{% if x %}{{ a }}{% endif %}')
|
2010-03-02 08:42:51 +01:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_ifequal(self):
|
2014-10-31 11:38:53 +01:00
|
|
|
template = Template('{% ifequal x y %}{{ a }}{% endifequal %}')
|
2010-03-02 08:42:51 +01:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
|
|
|
|
|
|
|
def test_ifchanged(self):
|
2014-10-31 11:38:53 +01:00
|
|
|
template = Template('{% ifchanged x %}{{ a }}{% endifchanged %}')
|
2010-03-02 08:42:51 +01:00
|
|
|
vars = template.nodelist.get_nodes_by_type(VariableNode)
|
|
|
|
self.assertEqual(len(vars), 1)
|
2011-08-12 16:15:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ErrorIndexTest(TestCase):
|
|
|
|
"""
|
|
|
|
Checks whether index of error is calculated correctly in
|
|
|
|
template debugger in for loops. Refs ticket #5831
|
|
|
|
"""
|
2013-11-02 22:34:05 +01:00
|
|
|
@override_settings(DEBUG=True, TEMPLATE_DEBUG=True)
|
2011-08-12 16:15:17 +02:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
for source, expected_error_source_index in tests:
|
2014-10-31 11:38:53 +01:00
|
|
|
template = Template(source)
|
2011-08-12 16:15:17 +02:00
|
|
|
try:
|
|
|
|
template.render(context)
|
2012-04-28 18:09:37 +02:00
|
|
|
except (RuntimeError, TypeError) as e:
|
2011-09-16 03:16:25 +02:00
|
|
|
error_source_index = e.django_template_source[1]
|
2011-08-12 16:15:17 +02:00
|
|
|
self.assertEqual(error_source_index,
|
|
|
|
expected_error_source_index)
|