1. from django.template import TemplateSyntaxError
    
  2. from django.test import SimpleTestCase
    
  3. 
    
  4. from ..utils import setup
    
  5. 
    
  6. 
    
  7. class FirstOfTagTests(SimpleTestCase):
    
  8.     @setup({"firstof01": "{% firstof a b c %}"})
    
  9.     def test_firstof01(self):
    
  10.         output = self.engine.render_to_string("firstof01", {"a": 0, "c": 0, "b": 0})
    
  11.         self.assertEqual(output, "")
    
  12. 
    
  13.     @setup({"firstof02": "{% firstof a b c %}"})
    
  14.     def test_firstof02(self):
    
  15.         output = self.engine.render_to_string("firstof02", {"a": 1, "c": 0, "b": 0})
    
  16.         self.assertEqual(output, "1")
    
  17. 
    
  18.     @setup({"firstof03": "{% firstof a b c %}"})
    
  19.     def test_firstof03(self):
    
  20.         output = self.engine.render_to_string("firstof03", {"a": 0, "c": 0, "b": 2})
    
  21.         self.assertEqual(output, "2")
    
  22. 
    
  23.     @setup({"firstof04": "{% firstof a b c %}"})
    
  24.     def test_firstof04(self):
    
  25.         output = self.engine.render_to_string("firstof04", {"a": 0, "c": 3, "b": 0})
    
  26.         self.assertEqual(output, "3")
    
  27. 
    
  28.     @setup({"firstof05": "{% firstof a b c %}"})
    
  29.     def test_firstof05(self):
    
  30.         output = self.engine.render_to_string("firstof05", {"a": 1, "c": 3, "b": 2})
    
  31.         self.assertEqual(output, "1")
    
  32. 
    
  33.     @setup({"firstof06": "{% firstof a b c %}"})
    
  34.     def test_firstof06(self):
    
  35.         output = self.engine.render_to_string("firstof06", {"c": 3, "b": 0})
    
  36.         self.assertEqual(output, "3")
    
  37. 
    
  38.     @setup({"firstof07": '{% firstof a b "c" %}'})
    
  39.     def test_firstof07(self):
    
  40.         output = self.engine.render_to_string("firstof07", {"a": 0})
    
  41.         self.assertEqual(output, "c")
    
  42. 
    
  43.     @setup({"firstof08": '{% firstof a b "c and d" %}'})
    
  44.     def test_firstof08(self):
    
  45.         output = self.engine.render_to_string("firstof08", {"a": 0, "b": 0})
    
  46.         self.assertEqual(output, "c and d")
    
  47. 
    
  48.     @setup({"firstof09": "{% firstof %}"})
    
  49.     def test_firstof09(self):
    
  50.         with self.assertRaises(TemplateSyntaxError):
    
  51.             self.engine.get_template("firstof09")
    
  52. 
    
  53.     @setup({"firstof10": "{% firstof a %}"})
    
  54.     def test_firstof10(self):
    
  55.         output = self.engine.render_to_string("firstof10", {"a": "<"})
    
  56.         self.assertEqual(output, "&lt;")
    
  57. 
    
  58.     @setup({"firstof11": "{% firstof a b %}"})
    
  59.     def test_firstof11(self):
    
  60.         output = self.engine.render_to_string("firstof11", {"a": "<", "b": ">"})
    
  61.         self.assertEqual(output, "&lt;")
    
  62. 
    
  63.     @setup({"firstof12": "{% firstof a b %}"})
    
  64.     def test_firstof12(self):
    
  65.         output = self.engine.render_to_string("firstof12", {"a": "", "b": ">"})
    
  66.         self.assertEqual(output, "&gt;")
    
  67. 
    
  68.     @setup({"firstof13": "{% autoescape off %}{% firstof a %}{% endautoescape %}"})
    
  69.     def test_firstof13(self):
    
  70.         output = self.engine.render_to_string("firstof13", {"a": "<"})
    
  71.         self.assertEqual(output, "<")
    
  72. 
    
  73.     @setup({"firstof14": "{% firstof a|safe b %}"})
    
  74.     def test_firstof14(self):
    
  75.         output = self.engine.render_to_string("firstof14", {"a": "<"})
    
  76.         self.assertEqual(output, "<")
    
  77. 
    
  78.     @setup({"firstof15": "{% firstof a b c as myvar %}"})
    
  79.     def test_firstof15(self):
    
  80.         ctx = {"a": 0, "b": 2, "c": 3}
    
  81.         output = self.engine.render_to_string("firstof15", ctx)
    
  82.         self.assertEqual(ctx["myvar"], "2")
    
  83.         self.assertEqual(output, "")
    
  84. 
    
  85.     @setup({"firstof16": "{% firstof a b c as myvar %}"})
    
  86.     def test_all_false_arguments_asvar(self):
    
  87.         ctx = {"a": 0, "b": 0, "c": 0}
    
  88.         output = self.engine.render_to_string("firstof16", ctx)
    
  89.         self.assertEqual(ctx["myvar"], "")
    
  90.         self.assertEqual(output, "")