test_cycle.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from django.template import TemplateSyntaxError
  2. from django.test import SimpleTestCase, ignore_warnings
  3. from django.utils.deprecation import RemovedInDjango110Warning
  4. from ..utils import setup
  5. class CycleTagTests(SimpleTestCase):
  6. @setup({'cycle01': '{% cycle a %}'})
  7. def test_cycle01(self):
  8. with self.assertRaises(TemplateSyntaxError):
  9. self.engine.get_template('cycle01')
  10. @ignore_warnings(category=RemovedInDjango110Warning)
  11. @setup({'cycle02': '{% cycle a,b,c as abc %}{% cycle abc %}'})
  12. def test_cycle02(self):
  13. output = self.engine.render_to_string('cycle02')
  14. self.assertEqual(output, 'ab')
  15. @ignore_warnings(category=RemovedInDjango110Warning)
  16. @setup({'cycle03': '{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}'})
  17. def test_cycle03(self):
  18. output = self.engine.render_to_string('cycle03')
  19. self.assertEqual(output, 'abc')
  20. @ignore_warnings(category=RemovedInDjango110Warning)
  21. @setup({'cycle04': '{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}'})
  22. def test_cycle04(self):
  23. output = self.engine.render_to_string('cycle04')
  24. self.assertEqual(output, 'abca')
  25. @setup({'cycle05': '{% cycle %}'})
  26. def test_cycle05(self):
  27. with self.assertRaises(TemplateSyntaxError):
  28. self.engine.get_template('cycle05')
  29. @setup({'cycle06': '{% cycle a %}'})
  30. def test_cycle06(self):
  31. with self.assertRaises(TemplateSyntaxError):
  32. self.engine.get_template('cycle06')
  33. @ignore_warnings(category=RemovedInDjango110Warning)
  34. @setup({'cycle07': '{% cycle a,b,c as foo %}{% cycle bar %}'})
  35. def test_cycle07(self):
  36. with self.assertRaises(TemplateSyntaxError):
  37. self.engine.get_template('cycle07')
  38. @ignore_warnings(category=RemovedInDjango110Warning)
  39. @setup({'cycle08': '{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}'})
  40. def test_cycle08(self):
  41. output = self.engine.render_to_string('cycle08')
  42. self.assertEqual(output, 'abbbcc')
  43. @ignore_warnings(category=RemovedInDjango110Warning)
  44. @setup({'cycle09': '{% for i in test %}{% cycle a,b %}{{ i }},{% endfor %}'})
  45. def test_cycle09(self):
  46. output = self.engine.render_to_string('cycle09', {'test': list(range(5))})
  47. self.assertEqual(output, 'a0,b1,a2,b3,a4,')
  48. @setup({'cycle10': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"})
  49. def test_cycle10(self):
  50. output = self.engine.render_to_string('cycle10')
  51. self.assertEqual(output, 'ab')
  52. @setup({'cycle11': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"})
  53. def test_cycle11(self):
  54. output = self.engine.render_to_string('cycle11')
  55. self.assertEqual(output, 'abc')
  56. @setup({'cycle12': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
  57. def test_cycle12(self):
  58. output = self.engine.render_to_string('cycle12')
  59. self.assertEqual(output, 'abca')
  60. @setup({'cycle13': "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"})
  61. def test_cycle13(self):
  62. output = self.engine.render_to_string('cycle13', {'test': list(range(5))})
  63. self.assertEqual(output, 'a0,b1,a2,b3,a4,')
  64. @setup({'cycle14': '{% cycle one two as foo %}{% cycle foo %}'})
  65. def test_cycle14(self):
  66. output = self.engine.render_to_string('cycle14', {'one': '1', 'two': '2'})
  67. self.assertEqual(output, '12')
  68. @setup({'cycle15': '{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}'})
  69. def test_cycle15(self):
  70. output = self.engine.render_to_string('cycle15', {'test': list(range(5)), 'aye': 'a', 'bee': 'b'})
  71. self.assertEqual(output, 'a0,b1,a2,b3,a4,')
  72. @setup({'cycle16': '{% cycle one|lower two as foo %}{% cycle foo %}'})
  73. def test_cycle16(self):
  74. output = self.engine.render_to_string('cycle16', {'one': 'A', 'two': '2'})
  75. self.assertEqual(output, 'a2')
  76. @setup({'cycle17': "{% cycle 'a' 'b' 'c' as abc silent %}"
  77. "{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"})
  78. def test_cycle17(self):
  79. output = self.engine.render_to_string('cycle17')
  80. self.assertEqual(output, '')
  81. @setup({'cycle18': "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"})
  82. def test_cycle18(self):
  83. with self.assertRaises(TemplateSyntaxError):
  84. self.engine.get_template('cycle18')
  85. @setup({'cycle19': "{% cycle 'a' 'b' as silent %}{% cycle silent %}"})
  86. def test_cycle19(self):
  87. output = self.engine.render_to_string('cycle19')
  88. self.assertEqual(output, 'ab')
  89. @setup({'cycle20': '{% cycle one two as foo %} & {% cycle foo %}'})
  90. def test_cycle20(self):
  91. output = self.engine.render_to_string('cycle20', {'two': 'C & D', 'one': 'A & B'})
  92. self.assertEqual(output, 'A & B & C & D')
  93. @setup({'cycle21': '{% filter force_escape %}'
  94. '{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}'})
  95. def test_cycle21(self):
  96. output = self.engine.render_to_string('cycle21', {'two': 'C & D', 'one': 'A & B'})
  97. self.assertEqual(output, 'A & B & C & D')
  98. @setup({'cycle22': "{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}{% endfor %}"})
  99. def test_cycle22(self):
  100. output = self.engine.render_to_string('cycle22', {'values': [1, 2, 3, 4]})
  101. self.assertEqual(output, '1234')
  102. @setup({'cycle23': "{% for x in values %}"
  103. "{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}"})
  104. def test_cycle23(self):
  105. output = self.engine.render_to_string('cycle23', {'values': [1, 2, 3, 4]})
  106. self.assertEqual(output, 'a1b2c3a4')
  107. @setup({
  108. 'cycle24': "{% for x in values %}"
  109. "{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}{% endfor %}",
  110. 'included-cycle': '{{ abc }}',
  111. })
  112. def test_cycle24(self):
  113. output = self.engine.render_to_string('cycle24', {'values': [1, 2, 3, 4]})
  114. self.assertEqual(output, 'abca')
  115. @setup({'cycle25': '{% cycle a as abc %}'})
  116. def test_cycle25(self):
  117. output = self.engine.render_to_string('cycle25', {'a': '<'})
  118. self.assertEqual(output, '&lt;')
  119. @setup({'cycle26': '{% cycle a b as ab %}{% cycle ab %}'})
  120. def test_cycle26(self):
  121. output = self.engine.render_to_string('cycle26', {'a': '<', 'b': '>'})
  122. self.assertEqual(output, '&lt;&gt;')
  123. @setup({'cycle27': '{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}{% endautoescape %}'})
  124. def test_cycle27(self):
  125. output = self.engine.render_to_string('cycle27', {'a': '<', 'b': '>'})
  126. self.assertEqual(output, '<>')
  127. @setup({'cycle28': '{% cycle a|safe b as ab %}{% cycle ab %}'})
  128. def test_cycle28(self):
  129. output = self.engine.render_to_string('cycle28', {'a': '<', 'b': '>'})
  130. self.assertEqual(output, '<&gt;')