2014-12-07 08:43:10 +00:00
|
|
|
from django.template import TemplateSyntaxError
|
2015-08-17 13:47:55 +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 CycleTagTests(SimpleTestCase):
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle01': '{% cycle a %}'})
|
|
|
|
def test_cycle01(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('cycle01')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle05': '{% cycle %}'})
|
|
|
|
def test_cycle05(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('cycle05')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle06': '{% cycle a %}'})
|
|
|
|
def test_cycle06(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('cycle06')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle07': '{% cycle a,b,c as foo %}{% cycle bar %}'})
|
|
|
|
def test_cycle07(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('cycle07')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle10': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"})
|
|
|
|
def test_cycle10(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle10')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'ab')
|
|
|
|
|
|
|
|
@setup({'cycle11': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"})
|
|
|
|
def test_cycle11(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle11')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'abc')
|
|
|
|
|
|
|
|
@setup({'cycle12': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
|
|
|
|
def test_cycle12(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle12')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'abca')
|
|
|
|
|
|
|
|
@setup({'cycle13': "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"})
|
|
|
|
def test_cycle13(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle13', {'test': list(range(5))})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'a0,b1,a2,b3,a4,')
|
|
|
|
|
|
|
|
@setup({'cycle14': '{% cycle one two as foo %}{% cycle foo %}'})
|
|
|
|
def test_cycle14(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle14', {'one': '1', 'two': '2'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '12')
|
|
|
|
|
|
|
|
@setup({'cycle15': '{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}'})
|
|
|
|
def test_cycle15(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle15', {'test': list(range(5)), 'aye': 'a', 'bee': 'b'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'a0,b1,a2,b3,a4,')
|
|
|
|
|
|
|
|
@setup({'cycle16': '{% cycle one|lower two as foo %}{% cycle foo %}'})
|
|
|
|
def test_cycle16(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle16', {'one': 'A', 'two': '2'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'a2')
|
|
|
|
|
|
|
|
@setup({'cycle17': "{% cycle 'a' 'b' 'c' as abc silent %}"
|
|
|
|
"{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
|
|
|
|
def test_cycle17(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle17')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'cycle18': "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"})
|
|
|
|
def test_cycle18(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('cycle18')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'cycle19': "{% cycle 'a' 'b' as silent %}{% cycle silent %}"})
|
|
|
|
def test_cycle19(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle19')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'ab')
|
|
|
|
|
|
|
|
@setup({'cycle20': '{% cycle one two as foo %} & {% cycle foo %}'})
|
|
|
|
def test_cycle20(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle20', {'two': 'C & D', 'one': 'A & B'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'A & B & C & D')
|
|
|
|
|
|
|
|
@setup({'cycle21': '{% filter force_escape %}'
|
|
|
|
'{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}'})
|
|
|
|
def test_cycle21(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle21', {'two': 'C & D', 'one': 'A & B'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'A & B & C & D')
|
|
|
|
|
|
|
|
@setup({'cycle22': "{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}{% endfor %}"})
|
|
|
|
def test_cycle22(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle22', {'values': [1, 2, 3, 4]})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '1234')
|
|
|
|
|
|
|
|
@setup({'cycle23': "{% for x in values %}"
|
|
|
|
"{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}"})
|
|
|
|
def test_cycle23(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle23', {'values': [1, 2, 3, 4]})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'a1b2c3a4')
|
|
|
|
|
|
|
|
@setup({
|
|
|
|
'cycle24': "{% for x in values %}"
|
|
|
|
"{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}{% endfor %}",
|
|
|
|
'included-cycle': '{{ abc }}',
|
|
|
|
})
|
|
|
|
def test_cycle24(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle24', {'values': [1, 2, 3, 4]})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, 'abca')
|
|
|
|
|
|
|
|
@setup({'cycle25': '{% cycle a as abc %}'})
|
|
|
|
def test_cycle25(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('cycle25', {'a': '<'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '<')
|
|
|
|
|
2015-08-17 12:57:38 +00:00
|
|
|
@setup({'cycle26': '{% cycle a b as ab %}{% cycle ab %}'})
|
2014-11-12 01:32:44 +00:00
|
|
|
def test_cycle26(self):
|
2014-12-21 20:19:05 +00:00
|
|
|
output = self.engine.render_to_string('cycle26', {'a': '<', 'b': '>'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '<>')
|
|
|
|
|
2015-08-17 12:57:38 +00:00
|
|
|
@setup({'cycle27': '{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}{% endautoescape %}'})
|
2014-11-12 01:32:44 +00:00
|
|
|
def test_cycle27(self):
|
2014-12-21 20:19:05 +00:00
|
|
|
output = self.engine.render_to_string('cycle27', {'a': '<', 'b': '>'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '<>')
|
|
|
|
|
2015-08-17 12:57:38 +00:00
|
|
|
@setup({'cycle28': '{% cycle a|safe b as ab %}{% cycle ab %}'})
|
2014-11-12 01:32:44 +00:00
|
|
|
def test_cycle28(self):
|
2014-12-21 20:19:05 +00:00
|
|
|
output = self.engine.render_to_string('cycle28', {'a': '<', 'b': '>'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '<>')
|
2016-07-16 20:54:08 +00:00
|
|
|
|
|
|
|
@setup({
|
|
|
|
'cycle29': "{% cycle 'a' 'b' 'c' as cycler silent %}"
|
|
|
|
"{% for x in values %}"
|
|
|
|
"{% ifchanged x %}"
|
|
|
|
"{% cycle cycler %}{{ cycler }}"
|
|
|
|
"{% else %}"
|
|
|
|
"{{ cycler }}"
|
|
|
|
"{% endifchanged %}"
|
|
|
|
"{% endfor %}"
|
|
|
|
})
|
|
|
|
def test_cycle29(self):
|
|
|
|
"""
|
|
|
|
A named {% cycle %} tag works inside an {% ifchanged %} block and a
|
|
|
|
{% for %} loop.
|
|
|
|
"""
|
|
|
|
output = self.engine.render_to_string('cycle29', {'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]})
|
|
|
|
self.assertEqual(output, 'bcabcabcccaa')
|
2016-07-16 20:55:40 +00:00
|
|
|
|
|
|
|
@setup({
|
|
|
|
'cycle30': "{% cycle 'a' 'b' 'c' as cycler silent %}"
|
|
|
|
"{% for x in values %}"
|
|
|
|
"{% with doesnothing=irrelevant %}"
|
|
|
|
"{% ifchanged x %}"
|
|
|
|
"{% cycle cycler %}{{ cycler }}"
|
|
|
|
"{% else %}"
|
|
|
|
"{{ cycler }}"
|
|
|
|
"{% endifchanged %}"
|
|
|
|
"{% endwith %}"
|
|
|
|
"{% endfor %}"})
|
|
|
|
def test_cycle30(self):
|
|
|
|
"""
|
|
|
|
A {% with %} tag shouldn't reset the {% cycle %} variable.
|
|
|
|
"""
|
|
|
|
output = self.engine.render_to_string(
|
|
|
|
'cycle30', {
|
|
|
|
'irrelevant': 1,
|
|
|
|
'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]
|
|
|
|
})
|
|
|
|
self.assertEqual(output, 'bcabcabcccaa')
|