2014-12-07 08:43:10 +00:00
|
|
|
from django.template import TemplateSyntaxError
|
2014-12-03 20:36:17 +00:00
|
|
|
from django.test import SimpleTestCase
|
2014-11-12 01:32:44 +00:00
|
|
|
|
2014-12-07 08:43:10 +00:00
|
|
|
from ..utils import setup
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class WithTagTests(SimpleTestCase):
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'with01': '{% with key=dict.key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with01(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('with01', {'dict': {'key': 50}})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '50')
|
|
|
|
|
|
|
|
@setup({'legacywith01': '{% with dict.key as key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_legacywith01(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('legacywith01', {'dict': {'key': 50}})
|
2014-11-12 01:32:44 +00: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 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('with02', {'dict': {'key': 50}})
|
|
|
|
if self.engine.string_if_invalid:
|
2014-11-12 01:32:44 +00: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 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('legacywith02', {'dict': {'key': 50}})
|
|
|
|
if self.engine.string_if_invalid:
|
2014-11-12 01:32:44 +00: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 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('with03', {'alpha': 'A', 'beta': 'B'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'AB')
|
|
|
|
|
|
|
|
@setup({'with-error01': '{% with dict.key xx key %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with_error01(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.render_to_string('with-error01', {'dict': {'key': 50}})
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'with-error02': '{% with dict.key as %}{{ key }}{% endwith %}'})
|
|
|
|
def test_with_error02(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.render_to_string('with-error02', {'dict': {'key': 50}})
|