Refs #13408 -- Made unpacking mismatch an exception in {% for %} tag per deprecation timeline.

This commit is contained in:
Tim Graham 2015-08-18 12:25:38 -04:00
parent 1392aff440
commit 3bbebd06ad
2 changed files with 20 additions and 38 deletions

View file

@ -1,6 +1,5 @@
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase, ignore_warnings
from django.utils.deprecation import RemovedInDjango110Warning
from django.test import SimpleTestCase
from ..utils import setup
@ -130,46 +129,31 @@ class ForTagTests(SimpleTestCase):
# These tests raise deprecation warnings and will raise an exception
# in Django 1.10. The existing behavior is silent truncation if the
# length of loopvars differs to the length of each set of items.
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack10': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'})
def test_for_tag_unpack10(self):
output = self.engine.render_to_string(
'for-tag-unpack10',
{'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))},
)
self.assertEqual(output, 'one:1/two:2/')
with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 3.'):
self.engine.render_to_string(
'for-tag-unpack10',
{'items': (('one', 1, 'carrot'), ('two', 2, 'orange'))},
)
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack11': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'})
def test_for_tag_unpack11(self):
output = self.engine.render_to_string(
'for-tag-unpack11',
{'items': (('one', 1), ('two', 2))},
)
with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
self.engine.render_to_string(
'for-tag-unpack11',
{'items': (('one', 1), ('two', 2))},
)
if self.engine.string_if_invalid:
self.assertEqual(output, 'one:1,INVALID/two:2,INVALID/')
else:
self.assertEqual(output, 'one:1,/two:2,/')
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack12': '{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}'})
def test_for_tag_unpack12(self):
output = self.engine.render_to_string(
'for-tag-unpack12',
{'items': (('one', 1, 'carrot'), ('two', 2))}
)
if self.engine.string_if_invalid:
self.assertEqual(output, 'one:1,carrot/two:2,INVALID/')
else:
self.assertEqual(output, 'one:1,carrot/two:2,/')
with self.assertRaisesMessage(ValueError, 'Need 3 values to unpack in for loop; got 2.'):
self.engine.render_to_string(
'for-tag-unpack12',
{'items': (('one', 1, 'carrot'), ('two', 2))}
)
@ignore_warnings(category=RemovedInDjango110Warning)
@setup({'for-tag-unpack14': '{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}'})
def test_for_tag_unpack14(self):
output = self.engine.render_to_string('for-tag-unpack14', {'items': (1, 2)})
if self.engine.string_if_invalid:
self.assertEqual(output, 'INVALID:INVALID/INVALID:INVALID/')
else:
self.assertEqual(output, ':/:/')
with self.assertRaisesMessage(ValueError, 'Need 2 values to unpack in for loop; got 1.'):
self.engine.render_to_string('for-tag-unpack14', {'items': (1, 2)})