1. from django.template import TemplateSyntaxError
    
  2. from django.test import SimpleTestCase
    
  3. 
    
  4. from ..utils import setup
    
  5. 
    
  6. 
    
  7. class ResetCycleTagTests(SimpleTestCase):
    
  8.     @setup({"resetcycle01": "{% resetcycle %}"})
    
  9.     def test_resetcycle01(self):
    
  10.         with self.assertRaisesMessage(TemplateSyntaxError, "No cycles in template."):
    
  11.             self.engine.get_template("resetcycle01")
    
  12. 
    
  13.     @setup({"resetcycle02": "{% resetcycle undefinedcycle %}"})
    
  14.     def test_resetcycle02(self):
    
  15.         with self.assertRaisesMessage(
    
  16.             TemplateSyntaxError, "Named cycle 'undefinedcycle' does not exist."
    
  17.         ):
    
  18.             self.engine.get_template("resetcycle02")
    
  19. 
    
  20.     @setup({"resetcycle03": "{% cycle 'a' 'b' %}{% resetcycle undefinedcycle %}"})
    
  21.     def test_resetcycle03(self):
    
  22.         with self.assertRaisesMessage(
    
  23.             TemplateSyntaxError, "Named cycle 'undefinedcycle' does not exist."
    
  24.         ):
    
  25.             self.engine.get_template("resetcycle03")
    
  26. 
    
  27.     @setup({"resetcycle04": "{% cycle 'a' 'b' as ab %}{% resetcycle undefinedcycle %}"})
    
  28.     def test_resetcycle04(self):
    
  29.         with self.assertRaisesMessage(
    
  30.             TemplateSyntaxError, "Named cycle 'undefinedcycle' does not exist."
    
  31.         ):
    
  32.             self.engine.get_template("resetcycle04")
    
  33. 
    
  34.     @setup(
    
  35.         {
    
  36.             "resetcycle05": (
    
  37.                 "{% for i in test %}{% cycle 'a' 'b' %}{% resetcycle %}{% endfor %}"
    
  38.             )
    
  39.         }
    
  40.     )
    
  41.     def test_resetcycle05(self):
    
  42.         output = self.engine.render_to_string("resetcycle05", {"test": list(range(5))})
    
  43.         self.assertEqual(output, "aaaaa")
    
  44. 
    
  45.     @setup(
    
  46.         {
    
  47.             "resetcycle06": "{% cycle 'a' 'b' 'c' as abc %}"
    
  48.             "{% for i in test %}"
    
  49.             "{% cycle abc %}"
    
  50.             "{% cycle '-' '+' %}"
    
  51.             "{% resetcycle %}"
    
  52.             "{% endfor %}"
    
  53.         }
    
  54.     )
    
  55.     def test_resetcycle06(self):
    
  56.         output = self.engine.render_to_string("resetcycle06", {"test": list(range(5))})
    
  57.         self.assertEqual(output, "ab-c-a-b-c-")
    
  58. 
    
  59.     @setup(
    
  60.         {
    
  61.             "resetcycle07": "{% cycle 'a' 'b' 'c' as abc %}"
    
  62.             "{% for i in test %}"
    
  63.             "{% resetcycle abc %}"
    
  64.             "{% cycle abc %}"
    
  65.             "{% cycle '-' '+' %}"
    
  66.             "{% endfor %}"
    
  67.         }
    
  68.     )
    
  69.     def test_resetcycle07(self):
    
  70.         output = self.engine.render_to_string("resetcycle07", {"test": list(range(5))})
    
  71.         self.assertEqual(output, "aa-a+a-a+a-")
    
  72. 
    
  73.     @setup(
    
  74.         {
    
  75.             "resetcycle08": "{% for i in outer %}"
    
  76.             "{% for j in inner %}"
    
  77.             "{% cycle 'a' 'b' %}"
    
  78.             "{% endfor %}"
    
  79.             "{% resetcycle %}"
    
  80.             "{% endfor %}"
    
  81.         }
    
  82.     )
    
  83.     def test_resetcycle08(self):
    
  84.         output = self.engine.render_to_string(
    
  85.             "resetcycle08", {"outer": list(range(2)), "inner": list(range(3))}
    
  86.         )
    
  87.         self.assertEqual(output, "abaaba")
    
  88. 
    
  89.     @setup(
    
  90.         {
    
  91.             "resetcycle09": "{% for i in outer %}"
    
  92.             "{% cycle 'a' 'b' %}"
    
  93.             "{% for j in inner %}"
    
  94.             "{% cycle 'X' 'Y' %}"
    
  95.             "{% endfor %}"
    
  96.             "{% resetcycle %}"
    
  97.             "{% endfor %}"
    
  98.         }
    
  99.     )
    
  100.     def test_resetcycle09(self):
    
  101.         output = self.engine.render_to_string(
    
  102.             "resetcycle09", {"outer": list(range(2)), "inner": list(range(3))}
    
  103.         )
    
  104.         self.assertEqual(output, "aXYXbXYX")
    
  105. 
    
  106.     @setup(
    
  107.         {
    
  108.             "resetcycle10": "{% for i in test %}"
    
  109.             "{% cycle 'X' 'Y' 'Z' as XYZ %}"
    
  110.             "{% cycle 'a' 'b' 'c' as abc %}"
    
  111.             "{% if i == 1 %}"
    
  112.             "{% resetcycle abc %}"
    
  113.             "{% endif %}"
    
  114.             "{% endfor %}"
    
  115.         }
    
  116.     )
    
  117.     def test_resetcycle10(self):
    
  118.         output = self.engine.render_to_string("resetcycle10", {"test": list(range(5))})
    
  119.         self.assertEqual(output, "XaYbZaXbYc")
    
  120. 
    
  121.     @setup(
    
  122.         {
    
  123.             "resetcycle11": "{% for i in test %}"
    
  124.             "{% cycle 'X' 'Y' 'Z' as XYZ %}"
    
  125.             "{% cycle 'a' 'b' 'c' as abc %}"
    
  126.             "{% if i == 1 %}"
    
  127.             "{% resetcycle XYZ %}"
    
  128.             "{% endif %}"
    
  129.             "{% endfor %}"
    
  130.         }
    
  131.     )
    
  132.     def test_resetcycle11(self):
    
  133.         output = self.engine.render_to_string("resetcycle11", {"test": list(range(5))})
    
  134.         self.assertEqual(output, "XaYbXcYaZb")