| 1 |
from django.template import TemplateSyntaxError |
| 2 |
from django.test import SimpleTestCase |
| 3 |
|
| 4 |
from ..utils import setup |
| 5 |
|
| 6 |
|
| 7 |
class CycleTagTests(SimpleTestCase): |
| 8 |
@setup({"cycle01": "{% cycle a %}"}) |
| 9 |
def test_cycle01(self): |
| 10 |
msg = "No named cycles in template. 'a' is not defined" |
| 11 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 12 |
self.engine.get_template("cycle01") |
| 13 |
|
| 14 |
@setup({"cycle05": "{% cycle %}"}) |
| 15 |
def test_cycle05(self): |
| 16 |
msg = "'cycle' tag requires at least two arguments" |
| 17 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 18 |
self.engine.get_template("cycle05") |
| 19 |
|
| 20 |
@setup({"cycle07": "{% cycle a,b,c as foo %}{% cycle bar %}"}) |
| 21 |
def test_cycle07(self): |
| 22 |
msg = "Could not parse the remainder: ',b,c' from 'a,b,c'" |
| 23 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 24 |
self.engine.get_template("cycle07") |
| 25 |
|
| 26 |
@setup({"cycle10": "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"}) |
| 27 |
def test_cycle10(self): |
| 28 |
output = self.engine.render_to_string("cycle10") |
| 29 |
self.assertEqual(output, "ab") |
| 30 |
|
| 31 |
@setup({"cycle11": "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"}) |
| 32 |
def test_cycle11(self): |
| 33 |
output = self.engine.render_to_string("cycle11") |
| 34 |
self.assertEqual(output, "abc") |
| 35 |
|
| 36 |
@setup( |
| 37 |
{ |
| 38 |
"cycle12": ( |
| 39 |
"{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}" |
| 40 |
"{% cycle abc %}" |
| 41 |
) |
| 42 |
} |
| 43 |
) |
| 44 |
def test_cycle12(self): |
| 45 |
output = self.engine.render_to_string("cycle12") |
| 46 |
self.assertEqual(output, "abca") |
| 47 |
|
| 48 |
@setup({"cycle13": "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"}) |
| 49 |
def test_cycle13(self): |
| 50 |
output = self.engine.render_to_string("cycle13", {"test": list(range(5))}) |
| 51 |
self.assertEqual(output, "a0,b1,a2,b3,a4,") |
| 52 |
|
| 53 |
@setup({"cycle14": "{% cycle one two as foo %}{% cycle foo %}"}) |
| 54 |
def test_cycle14(self): |
| 55 |
output = self.engine.render_to_string("cycle14", {"one": "1", "two": "2"}) |
| 56 |
self.assertEqual(output, "12") |
| 57 |
|
| 58 |
@setup({"cycle15": "{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}"}) |
| 59 |
def test_cycle15(self): |
| 60 |
output = self.engine.render_to_string( |
| 61 |
"cycle15", {"test": list(range(5)), "aye": "a", "bee": "b"} |
| 62 |
) |
| 63 |
self.assertEqual(output, "a0,b1,a2,b3,a4,") |
| 64 |
|
| 65 |
@setup({"cycle16": "{% cycle one|lower two as foo %}{% cycle foo %}"}) |
| 66 |
def test_cycle16(self): |
| 67 |
output = self.engine.render_to_string("cycle16", {"one": "A", "two": "2"}) |
| 68 |
self.assertEqual(output, "a2") |
| 69 |
|
| 70 |
@setup( |
| 71 |
{ |
| 72 |
"cycle17": "{% cycle 'a' 'b' 'c' as abc silent %}" |
| 73 |
"{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}" |
| 74 |
} |
| 75 |
) |
| 76 |
def test_cycle17(self): |
| 77 |
output = self.engine.render_to_string("cycle17") |
| 78 |
self.assertEqual(output, "") |
| 79 |
|
| 80 |
@setup({"cycle18": "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"}) |
| 81 |
def test_cycle18(self): |
| 82 |
msg = "Only 'silent' flag is allowed after cycle's name, not 'invalid_flag'." |
| 83 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 84 |
self.engine.get_template("cycle18") |
| 85 |
|
| 86 |
@setup({"cycle19": "{% cycle 'a' 'b' as silent %}{% cycle silent %}"}) |
| 87 |
def test_cycle19(self): |
| 88 |
output = self.engine.render_to_string("cycle19") |
| 89 |
self.assertEqual(output, "ab") |
| 90 |
|
| 91 |
@setup({"cycle20": "{% cycle one two as foo %} & {% cycle foo %}"}) |
| 92 |
def test_cycle20(self): |
| 93 |
output = self.engine.render_to_string( |
| 94 |
"cycle20", {"two": "C & D", "one": "A & B"} |
| 95 |
) |
| 96 |
self.assertEqual(output, "A & B & C & D") |
| 97 |
|
| 98 |
@setup( |
| 99 |
{ |
| 100 |
"cycle21": "{% filter force_escape %}" |
| 101 |
"{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}" |
| 102 |
} |
| 103 |
) |
| 104 |
def test_cycle21(self): |
| 105 |
output = self.engine.render_to_string( |
| 106 |
"cycle21", {"two": "C & D", "one": "A & B"} |
| 107 |
) |
| 108 |
self.assertEqual(output, "A & B & C & D") |
| 109 |
|
| 110 |
@setup( |
| 111 |
{ |
| 112 |
"cycle22": ( |
| 113 |
"{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}" |
| 114 |
"{% endfor %}" |
| 115 |
) |
| 116 |
} |
| 117 |
) |
| 118 |
def test_cycle22(self): |
| 119 |
output = self.engine.render_to_string("cycle22", {"values": [1, 2, 3, 4]}) |
| 120 |
self.assertEqual(output, "1234") |
| 121 |
|
| 122 |
@setup( |
| 123 |
{ |
| 124 |
"cycle23": "{% for x in values %}" |
| 125 |
"{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}" |
| 126 |
} |
| 127 |
) |
| 128 |
def test_cycle23(self): |
| 129 |
output = self.engine.render_to_string("cycle23", {"values": [1, 2, 3, 4]}) |
| 130 |
self.assertEqual(output, "a1b2c3a4") |
| 131 |
|
| 132 |
@setup( |
| 133 |
{ |
| 134 |
"cycle24": ( |
| 135 |
"{% for x in values %}" |
| 136 |
"{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}" |
| 137 |
"{% endfor %}" |
| 138 |
), |
| 139 |
"included-cycle": "{{ abc }}", |
| 140 |
} |
| 141 |
) |
| 142 |
def test_cycle24(self): |
| 143 |
output = self.engine.render_to_string("cycle24", {"values": [1, 2, 3, 4]}) |
| 144 |
self.assertEqual(output, "abca") |
| 145 |
|
| 146 |
@setup({"cycle25": "{% cycle a as abc %}"}) |
| 147 |
def test_cycle25(self): |
| 148 |
output = self.engine.render_to_string("cycle25", {"a": "<"}) |
| 149 |
self.assertEqual(output, "<") |
| 150 |
|
| 151 |
@setup({"cycle26": "{% cycle a b as ab %}{% cycle ab %}"}) |
| 152 |
def test_cycle26(self): |
| 153 |
output = self.engine.render_to_string("cycle26", {"a": "<", "b": ">"}) |
| 154 |
self.assertEqual(output, "<>") |
| 155 |
|
| 156 |
@setup( |
| 157 |
{ |
| 158 |
"cycle27": ( |
| 159 |
"{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}" |
| 160 |
"{% endautoescape %}" |
| 161 |
) |
| 162 |
} |
| 163 |
) |
| 164 |
def test_cycle27(self): |
| 165 |
output = self.engine.render_to_string("cycle27", {"a": "<", "b": ">"}) |
| 166 |
self.assertEqual(output, "<>") |
| 167 |
|
| 168 |
@setup({"cycle28": "{% cycle a|safe b as ab %}{% cycle ab %}"}) |
| 169 |
def test_cycle28(self): |
| 170 |
output = self.engine.render_to_string("cycle28", {"a": "<", "b": ">"}) |
| 171 |
self.assertEqual(output, "<>") |
| 172 |
|
| 173 |
@setup( |
| 174 |
{ |
| 175 |
"cycle29": "{% cycle 'a' 'b' 'c' as cycler silent %}" |
| 176 |
"{% for x in values %}" |
| 177 |
"{% ifchanged x %}" |
| 178 |
"{% cycle cycler %}{{ cycler }}" |
| 179 |
"{% else %}" |
| 180 |
"{{ cycler }}" |
| 181 |
"{% endifchanged %}" |
| 182 |
"{% endfor %}" |
| 183 |
} |
| 184 |
) |
| 185 |
def test_cycle29(self): |
| 186 |
""" |
| 187 |
A named {% cycle %} tag works inside an {% ifchanged %} block and a |
| 188 |
{% for %} loop. |
| 189 |
""" |
| 190 |
output = self.engine.render_to_string( |
| 191 |
"cycle29", {"values": [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]} |
| 192 |
) |
| 193 |
self.assertEqual(output, "bcabcabcccaa") |
| 194 |
|
| 195 |
@setup( |
| 196 |
{ |
| 197 |
"cycle30": "{% cycle 'a' 'b' 'c' as cycler silent %}" |
| 198 |
"{% for x in values %}" |
| 199 |
"{% with doesnothing=irrelevant %}" |
| 200 |
"{% ifchanged x %}" |
| 201 |
"{% cycle cycler %}{{ cycler }}" |
| 202 |
"{% else %}" |
| 203 |
"{{ cycler }}" |
| 204 |
"{% endifchanged %}" |
| 205 |
"{% endwith %}" |
| 206 |
"{% endfor %}" |
| 207 |
} |
| 208 |
) |
| 209 |
def test_cycle30(self): |
| 210 |
""" |
| 211 |
A {% with %} tag shouldn't reset the {% cycle %} variable. |
| 212 |
""" |
| 213 |
output = self.engine.render_to_string( |
| 214 |
"cycle30", {"irrelevant": 1, "values": [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]} |
| 215 |
) |
| 216 |
self.assertEqual(output, "bcabcabcccaa") |
| 217 |
|
| 218 |
@setup( |
| 219 |
{ |
| 220 |
"undefined_cycle": "{% cycle 'a' 'b' 'c' as cycler silent %}" |
| 221 |
"{% for x in values %}" |
| 222 |
"{% cycle undefined %}{{ cycler }}" |
| 223 |
"{% endfor %}" |
| 224 |
} |
| 225 |
) |
| 226 |
def test_cycle_undefined(self): |
| 227 |
with self.assertRaisesMessage( |
| 228 |
TemplateSyntaxError, "Named cycle 'undefined' does not exist" |
| 229 |
): |
| 230 |
self.engine.render_to_string("undefined_cycle") |