2014-12-07 16:43:10 +08:00
|
|
|
from django.template import TemplateSyntaxError
|
2017-02-04 18:43:14 +08:00
|
|
|
from django.template.defaulttags import WithNode
|
2014-12-04 04:36:17 +08:00
|
|
|
from django.test import SimpleTestCase
|
2014-11-12 09:32:44 +08:00
|
|
|
|
2014-12-07 16:43:10 +08:00
|
|
|
from ..utils import setup
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
2014-12-04 04:36:17 +08:00
|
|
|
class WithTagTests(SimpleTestCase):
|
2017-05-29 03:37:21 +08:00
|
|
|
at_least_with_one_msg = "'with' expected at least one variable assignment"
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
@setup({'with01': '{% with key=dict.key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('with01', {'dict': {'key': 50}})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '50')
|
|
|
|
|
|
|
|
@setup({'legacywith01': '{% with dict.key as key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_legacywith01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('legacywith01', {'dict': {'key': 50}})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '50')
|
|
|
|
|
|
|
|
@setup({'with02': '{{ key }}{% with key=dict.key %}'
|
|
|
|
'{{ key }}-{{ dict.key }}-{{ key }}'
|
|
|
|
'{% endwith %}{{ key }}'})
|
|
|
|
def test_with02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('with02', {'dict': {'key': 50}})
|
|
|
|
if self.engine.string_if_invalid:
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'INVALID50-50-50INVALID')
|
|
|
|
else:
|
|
|
|
self.assertEqual(output, '50-50-50')
|
|
|
|
|
|
|
|
@setup({'legacywith02': '{{ key }}{% with dict.key as key %}'
|
|
|
|
'{{ key }}-{{ dict.key }}-{{ key }}'
|
|
|
|
'{% endwith %}{{ key }}'})
|
|
|
|
def test_legacywith02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('legacywith02', {'dict': {'key': 50}})
|
|
|
|
if self.engine.string_if_invalid:
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'INVALID50-50-50INVALID')
|
|
|
|
else:
|
|
|
|
self.assertEqual(output, '50-50-50')
|
|
|
|
|
|
|
|
@setup({'with03': '{% with a=alpha b=beta %}{{ a }}{{ b }}{% endwith %}'})
|
|
|
|
def test_with03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('with03', {'alpha': 'A', 'beta': 'B'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'AB')
|
|
|
|
|
|
|
|
@setup({'with-error01': '{% with dict.key xx key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with_error01(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, self.at_least_with_one_msg):
|
2014-12-07 16:43:10 +08:00
|
|
|
self.engine.render_to_string('with-error01', {'dict': {'key': 50}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
@setup({'with-error02': '{% with dict.key as %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with_error02(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, self.at_least_with_one_msg):
|
2014-12-07 16:43:10 +08:00
|
|
|
self.engine.render_to_string('with-error02', {'dict': {'key': 50}})
|
2017-02-04 18:43:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class WithNodeTests(SimpleTestCase):
|
|
|
|
def test_repr(self):
|
|
|
|
node = WithNode(nodelist=[], name='a', var='dict.key')
|
|
|
|
self.assertEqual(repr(node), '<WithNode>')
|