test_cycle.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from django.template import TemplateSyntaxError
  2. from django.test import SimpleTestCase
  3. from ..utils import setup
  4. class CycleTagTests(SimpleTestCase):
  5. @setup({'cycle01': '{% cycle a %}'})
  6. def test_cycle01(self):
  7. msg = "No named cycles in template. 'a' is not defined"
  8. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  9. self.engine.get_template('cycle01')
  10. @setup({'cycle05': '{% cycle %}'})
  11. def test_cycle05(self):
  12. msg = "'cycle' tag requires at least two arguments"
  13. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  14. self.engine.get_template('cycle05')
  15. @setup({'cycle07': '{% cycle a,b,c as foo %}{% cycle bar %}'})
  16. def test_cycle07(self):
  17. msg = "Could not parse the remainder: ',b,c' from 'a,b,c'"
  18. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  19. self.engine.get_template('cycle07')
  20. @setup({'cycle10': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"})
  21. def test_cycle10(self):
  22. output = self.engine.render_to_string('cycle10')
  23. self.assertEqual(output, 'ab')
  24. @setup({'cycle11': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"})
  25. def test_cycle11(self):
  26. output = self.engine.render_to_string('cycle11')
  27. self.assertEqual(output, 'abc')
  28. @setup({'cycle12': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
  29. def test_cycle12(self):
  30. output = self.engine.render_to_string('cycle12')
  31. self.assertEqual(output, 'abca')
  32. @setup({'cycle13': "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"})
  33. def test_cycle13(self):
  34. output = self.engine.render_to_string('cycle13', {'test': list(range(5))})
  35. self.assertEqual(output, 'a0,b1,a2,b3,a4,')
  36. @setup({'cycle14': '{% cycle one two as foo %}{% cycle foo %}'})
  37. def test_cycle14(self):
  38. output = self.engine.render_to_string('cycle14', {'one': '1', 'two': '2'})
  39. self.assertEqual(output, '12')
  40. @setup({'cycle15': '{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}'})
  41. def test_cycle15(self):
  42. output = self.engine.render_to_string('cycle15', {'test': list(range(5)), 'aye': 'a', 'bee': 'b'})
  43. self.assertEqual(output, 'a0,b1,a2,b3,a4,')
  44. @setup({'cycle16': '{% cycle one|lower two as foo %}{% cycle foo %}'})
  45. def test_cycle16(self):
  46. output = self.engine.render_to_string('cycle16', {'one': 'A', 'two': '2'})
  47. self.assertEqual(output, 'a2')
  48. @setup({'cycle17': "{% cycle 'a' 'b' 'c' as abc silent %}"
  49. "{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
  50. def test_cycle17(self):
  51. output = self.engine.render_to_string('cycle17')
  52. self.assertEqual(output, '')
  53. @setup({'cycle18': "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"})
  54. def test_cycle18(self):
  55. msg = "Only 'silent' flag is allowed after cycle's name, not 'invalid_flag'."
  56. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  57. self.engine.get_template('cycle18')
  58. @setup({'cycle19': "{% cycle 'a' 'b' as silent %}{% cycle silent %}"})
  59. def test_cycle19(self):
  60. output = self.engine.render_to_string('cycle19')
  61. self.assertEqual(output, 'ab')
  62. @setup({'cycle20': '{% cycle one two as foo %} & {% cycle foo %}'})
  63. def test_cycle20(self):
  64. output = self.engine.render_to_string('cycle20', {'two': 'C & D', 'one': 'A & B'})
  65. self.assertEqual(output, 'A & B & C & D')
  66. @setup({'cycle21': '{% filter force_escape %}'
  67. '{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}'})
  68. def test_cycle21(self):
  69. output = self.engine.render_to_string('cycle21', {'two': 'C & D', 'one': 'A & B'})
  70. self.assertEqual(output, 'A & B & C & D')
  71. @setup({'cycle22': "{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}{% endfor %}"})
  72. def test_cycle22(self):
  73. output = self.engine.render_to_string('cycle22', {'values': [1, 2, 3, 4]})
  74. self.assertEqual(output, '1234')
  75. @setup({'cycle23': "{% for x in values %}"
  76. "{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}"})
  77. def test_cycle23(self):
  78. output = self.engine.render_to_string('cycle23', {'values': [1, 2, 3, 4]})
  79. self.assertEqual(output, 'a1b2c3a4')
  80. @setup({
  81. 'cycle24': "{% for x in values %}"
  82. "{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}{% endfor %}",
  83. 'included-cycle': '{{ abc }}',
  84. })
  85. def test_cycle24(self):
  86. output = self.engine.render_to_string('cycle24', {'values': [1, 2, 3, 4]})
  87. self.assertEqual(output, 'abca')
  88. @setup({'cycle25': '{% cycle a as abc %}'})
  89. def test_cycle25(self):
  90. output = self.engine.render_to_string('cycle25', {'a': '<'})
  91. self.assertEqual(output, '&lt;')
  92. @setup({'cycle26': '{% cycle a b as ab %}{% cycle ab %}'})
  93. def test_cycle26(self):
  94. output = self.engine.render_to_string('cycle26', {'a': '<', 'b': '>'})
  95. self.assertEqual(output, '&lt;&gt;')
  96. @setup({'cycle27': '{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}{% endautoescape %}'})
  97. def test_cycle27(self):
  98. output = self.engine.render_to_string('cycle27', {'a': '<', 'b': '>'})
  99. self.assertEqual(output, '<>')
  100. @setup({'cycle28': '{% cycle a|safe b as ab %}{% cycle ab %}'})
  101. def test_cycle28(self):
  102. output = self.engine.render_to_string('cycle28', {'a': '<', 'b': '>'})
  103. self.assertEqual(output, '<&gt;')
  104. @setup({
  105. 'cycle29': "{% cycle 'a' 'b' 'c' as cycler silent %}"
  106. "{% for x in values %}"
  107. "{% ifchanged x %}"
  108. "{% cycle cycler %}{{ cycler }}"
  109. "{% else %}"
  110. "{{ cycler }}"
  111. "{% endifchanged %}"
  112. "{% endfor %}"
  113. })
  114. def test_cycle29(self):
  115. """
  116. A named {% cycle %} tag works inside an {% ifchanged %} block and a
  117. {% for %} loop.
  118. """
  119. output = self.engine.render_to_string('cycle29', {'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]})
  120. self.assertEqual(output, 'bcabcabcccaa')
  121. @setup({
  122. 'cycle30': "{% cycle 'a' 'b' 'c' as cycler silent %}"
  123. "{% for x in values %}"
  124. "{% with doesnothing=irrelevant %}"
  125. "{% ifchanged x %}"
  126. "{% cycle cycler %}{{ cycler }}"
  127. "{% else %}"
  128. "{{ cycler }}"
  129. "{% endifchanged %}"
  130. "{% endwith %}"
  131. "{% endfor %}"})
  132. def test_cycle30(self):
  133. """
  134. A {% with %} tag shouldn't reset the {% cycle %} variable.
  135. """
  136. output = self.engine.render_to_string(
  137. 'cycle30', {
  138. 'irrelevant': 1,
  139. 'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]
  140. })
  141. self.assertEqual(output, 'bcabcabcccaa')
  142. @setup({
  143. 'undefined_cycle':
  144. "{% cycle 'a' 'b' 'c' as cycler silent %}"
  145. "{% for x in values %}"
  146. "{% cycle undefined %}{{ cycler }}"
  147. "{% endfor %}"
  148. })
  149. def test_cycle_undefined(self):
  150. with self.assertRaisesMessage(TemplateSyntaxError, "Named cycle 'undefined' does not exist"):
  151. self.engine.render_to_string('undefined_cycle')