1. from django.template import TemplateSyntaxError
    
  2. from django.test import SimpleTestCase
    
  3. 
    
  4. from ..utils import setup
    
  5. 
    
  6. 
    
  7. class WidthRatioTagTests(SimpleTestCase):
    
  8.     libraries = {"custom": "template_tests.templatetags.custom"}
    
  9. 
    
  10.     @setup({"widthratio01": "{% widthratio a b 0 %}"})
    
  11.     def test_widthratio01(self):
    
  12.         output = self.engine.render_to_string("widthratio01", {"a": 50, "b": 100})
    
  13.         self.assertEqual(output, "0")
    
  14. 
    
  15.     @setup({"widthratio02": "{% widthratio a b 100 %}"})
    
  16.     def test_widthratio02(self):
    
  17.         output = self.engine.render_to_string("widthratio02", {"a": 0, "b": 0})
    
  18.         self.assertEqual(output, "0")
    
  19. 
    
  20.     @setup({"widthratio03": "{% widthratio a b 100 %}"})
    
  21.     def test_widthratio03(self):
    
  22.         output = self.engine.render_to_string("widthratio03", {"a": 0, "b": 100})
    
  23.         self.assertEqual(output, "0")
    
  24. 
    
  25.     @setup({"widthratio04": "{% widthratio a b 100 %}"})
    
  26.     def test_widthratio04(self):
    
  27.         output = self.engine.render_to_string("widthratio04", {"a": 50, "b": 100})
    
  28.         self.assertEqual(output, "50")
    
  29. 
    
  30.     @setup({"widthratio05": "{% widthratio a b 100 %}"})
    
  31.     def test_widthratio05(self):
    
  32.         output = self.engine.render_to_string("widthratio05", {"a": 100, "b": 100})
    
  33.         self.assertEqual(output, "100")
    
  34. 
    
  35.     @setup({"widthratio06": "{% widthratio a b 100 %}"})
    
  36.     def test_widthratio06(self):
    
  37.         """
    
  38.         62.5 should round to 62
    
  39.         """
    
  40.         output = self.engine.render_to_string("widthratio06", {"a": 50, "b": 80})
    
  41.         self.assertEqual(output, "62")
    
  42. 
    
  43.     @setup({"widthratio07": "{% widthratio a b 100 %}"})
    
  44.     def test_widthratio07(self):
    
  45.         """
    
  46.         71.4 should round to 71
    
  47.         """
    
  48.         output = self.engine.render_to_string("widthratio07", {"a": 50, "b": 70})
    
  49.         self.assertEqual(output, "71")
    
  50. 
    
  51.     # Raise exception if we don't have 3 args, last one an integer
    
  52.     @setup({"widthratio08": "{% widthratio %}"})
    
  53.     def test_widthratio08(self):
    
  54.         with self.assertRaises(TemplateSyntaxError):
    
  55.             self.engine.get_template("widthratio08")
    
  56. 
    
  57.     @setup({"widthratio09": "{% widthratio a b %}"})
    
  58.     def test_widthratio09(self):
    
  59.         with self.assertRaises(TemplateSyntaxError):
    
  60.             self.engine.render_to_string("widthratio09", {"a": 50, "b": 100})
    
  61. 
    
  62.     @setup({"widthratio10": "{% widthratio a b 100.0 %}"})
    
  63.     def test_widthratio10(self):
    
  64.         output = self.engine.render_to_string("widthratio10", {"a": 50, "b": 100})
    
  65.         self.assertEqual(output, "50")
    
  66. 
    
  67.     @setup({"widthratio11": "{% widthratio a b c %}"})
    
  68.     def test_widthratio11(self):
    
  69.         """
    
  70.         #10043: widthratio should allow max_width to be a variable
    
  71.         """
    
  72.         output = self.engine.render_to_string(
    
  73.             "widthratio11", {"a": 50, "c": 100, "b": 100}
    
  74.         )
    
  75.         self.assertEqual(output, "50")
    
  76. 
    
  77.     # #18739: widthratio should handle None args consistently with
    
  78.     # non-numerics
    
  79.     @setup({"widthratio12a": "{% widthratio a b c %}"})
    
  80.     def test_widthratio12a(self):
    
  81.         output = self.engine.render_to_string(
    
  82.             "widthratio12a", {"a": "a", "c": 100, "b": 100}
    
  83.         )
    
  84.         self.assertEqual(output, "")
    
  85. 
    
  86.     @setup({"widthratio12b": "{% widthratio a b c %}"})
    
  87.     def test_widthratio12b(self):
    
  88.         output = self.engine.render_to_string(
    
  89.             "widthratio12b", {"a": None, "c": 100, "b": 100}
    
  90.         )
    
  91.         self.assertEqual(output, "")
    
  92. 
    
  93.     @setup({"widthratio13a": "{% widthratio a b c %}"})
    
  94.     def test_widthratio13a(self):
    
  95.         output = self.engine.render_to_string(
    
  96.             "widthratio13a", {"a": 0, "c": 100, "b": "b"}
    
  97.         )
    
  98.         self.assertEqual(output, "")
    
  99. 
    
  100.     @setup({"widthratio13b": "{% widthratio a b c %}"})
    
  101.     def test_widthratio13b(self):
    
  102.         output = self.engine.render_to_string(
    
  103.             "widthratio13b", {"a": 0, "c": 100, "b": None}
    
  104.         )
    
  105.         self.assertEqual(output, "")
    
  106. 
    
  107.     @setup({"widthratio14a": "{% widthratio a b c %}"})
    
  108.     def test_widthratio14a(self):
    
  109.         with self.assertRaises(TemplateSyntaxError):
    
  110.             self.engine.render_to_string("widthratio14a", {"a": 0, "c": "c", "b": 100})
    
  111. 
    
  112.     @setup({"widthratio14b": "{% widthratio a b c %}"})
    
  113.     def test_widthratio14b(self):
    
  114.         with self.assertRaises(TemplateSyntaxError):
    
  115.             self.engine.render_to_string("widthratio14b", {"a": 0, "c": None, "b": 100})
    
  116. 
    
  117.     @setup({"widthratio15": '{% load custom %}{% widthratio a|noop:"x y" b 0 %}'})
    
  118.     def test_widthratio15(self):
    
  119.         """
    
  120.         Test whitespace in filter argument
    
  121.         """
    
  122.         output = self.engine.render_to_string("widthratio15", {"a": 50, "b": 100})
    
  123.         self.assertEqual(output, "0")
    
  124. 
    
  125.     # Widthratio with variable assignment
    
  126.     @setup({"widthratio16": "{% widthratio a b 100 as variable %}-{{ variable }}-"})
    
  127.     def test_widthratio16(self):
    
  128.         output = self.engine.render_to_string("widthratio16", {"a": 50, "b": 100})
    
  129.         self.assertEqual(output, "-50-")
    
  130. 
    
  131.     @setup({"widthratio17": "{% widthratio a b 100 as variable %}-{{ variable }}-"})
    
  132.     def test_widthratio17(self):
    
  133.         output = self.engine.render_to_string("widthratio17", {"a": 100, "b": 100})
    
  134.         self.assertEqual(output, "-100-")
    
  135. 
    
  136.     @setup({"widthratio18": "{% widthratio a b 100 as %}"})
    
  137.     def test_widthratio18(self):
    
  138.         with self.assertRaises(TemplateSyntaxError):
    
  139.             self.engine.get_template("widthratio18")
    
  140. 
    
  141.     @setup({"widthratio19": "{% widthratio a b 100 not_as variable %}"})
    
  142.     def test_widthratio19(self):
    
  143.         with self.assertRaises(TemplateSyntaxError):
    
  144.             self.engine.get_template("widthratio19")
    
  145. 
    
  146.     @setup({"widthratio20": "{% widthratio a b 100 %}"})
    
  147.     def test_widthratio20(self):
    
  148.         output = self.engine.render_to_string(
    
  149.             "widthratio20", {"a": float("inf"), "b": float("inf")}
    
  150.         )
    
  151.         self.assertEqual(output, "")
    
  152. 
    
  153.     @setup({"widthratio21": "{% widthratio a b 100 %}"})
    
  154.     def test_widthratio21(self):
    
  155.         output = self.engine.render_to_string(
    
  156.             "widthratio21", {"a": float("inf"), "b": 2}
    
  157.         )
    
  158.         self.assertEqual(output, "")
    
  159. 
    
  160.     @setup({"t": "{% widthratio a b 100 as variable %}-{{ variable }}-"})
    
  161.     def test_zerodivisionerror_as_var(self):
    
  162.         output = self.engine.render_to_string("t", {"a": 0, "b": 0})
    
  163.         self.assertEqual(output, "-0-")
    
  164. 
    
  165.     @setup({"t": "{% widthratio a b c as variable %}-{{ variable }}-"})
    
  166.     def test_typeerror_as_var(self):
    
  167.         output = self.engine.render_to_string("t", {"a": "a", "c": 100, "b": 100})
    
  168.         self.assertEqual(output, "--")