test_width_ratio.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from django.template import TemplateSyntaxError
  2. from django.test import SimpleTestCase
  3. from django.utils import six
  4. from ..utils import setup
  5. class WidthRatioTagTests(SimpleTestCase):
  6. libraries = {'custom': 'template_tests.templatetags.custom'}
  7. @setup({'widthratio01': '{% widthratio a b 0 %}'})
  8. def test_widthratio01(self):
  9. output = self.engine.render_to_string('widthratio01', {'a': 50, 'b': 100})
  10. self.assertEqual(output, '0')
  11. @setup({'widthratio02': '{% widthratio a b 100 %}'})
  12. def test_widthratio02(self):
  13. output = self.engine.render_to_string('widthratio02', {'a': 0, 'b': 0})
  14. self.assertEqual(output, '0')
  15. @setup({'widthratio03': '{% widthratio a b 100 %}'})
  16. def test_widthratio03(self):
  17. output = self.engine.render_to_string('widthratio03', {'a': 0, 'b': 100})
  18. self.assertEqual(output, '0')
  19. @setup({'widthratio04': '{% widthratio a b 100 %}'})
  20. def test_widthratio04(self):
  21. output = self.engine.render_to_string('widthratio04', {'a': 50, 'b': 100})
  22. self.assertEqual(output, '50')
  23. @setup({'widthratio05': '{% widthratio a b 100 %}'})
  24. def test_widthratio05(self):
  25. output = self.engine.render_to_string('widthratio05', {'a': 100, 'b': 100})
  26. self.assertEqual(output, '100')
  27. @setup({'widthratio06': '{% widthratio a b 100 %}'})
  28. def test_widthratio06(self):
  29. """
  30. 62.5 should round to 63 on Python 2 and 62 on Python 3
  31. See http://docs.python.org/py3k/whatsnew/3.0.html
  32. """
  33. output = self.engine.render_to_string('widthratio06', {'a': 50, 'b': 80})
  34. self.assertEqual(output, '62' if six.PY3 else '63')
  35. @setup({'widthratio07': '{% widthratio a b 100 %}'})
  36. def test_widthratio07(self):
  37. """
  38. 71.4 should round to 71
  39. """
  40. output = self.engine.render_to_string('widthratio07', {'a': 50, 'b': 70})
  41. self.assertEqual(output, '71')
  42. # Raise exception if we don't have 3 args, last one an integer
  43. @setup({'widthratio08': '{% widthratio %}'})
  44. def test_widthratio08(self):
  45. with self.assertRaises(TemplateSyntaxError):
  46. self.engine.get_template('widthratio08')
  47. @setup({'widthratio09': '{% widthratio a b %}'})
  48. def test_widthratio09(self):
  49. with self.assertRaises(TemplateSyntaxError):
  50. self.engine.render_to_string('widthratio09', {'a': 50, 'b': 100})
  51. @setup({'widthratio10': '{% widthratio a b 100.0 %}'})
  52. def test_widthratio10(self):
  53. output = self.engine.render_to_string('widthratio10', {'a': 50, 'b': 100})
  54. self.assertEqual(output, '50')
  55. @setup({'widthratio11': '{% widthratio a b c %}'})
  56. def test_widthratio11(self):
  57. """
  58. #10043: widthratio should allow max_width to be a variable
  59. """
  60. output = self.engine.render_to_string('widthratio11', {'a': 50, 'c': 100, 'b': 100})
  61. self.assertEqual(output, '50')
  62. # #18739: widthratio should handle None args consistently with
  63. # non-numerics
  64. @setup({'widthratio12a': '{% widthratio a b c %}'})
  65. def test_widthratio12a(self):
  66. output = self.engine.render_to_string('widthratio12a', {'a': 'a', 'c': 100, 'b': 100})
  67. self.assertEqual(output, '')
  68. @setup({'widthratio12b': '{% widthratio a b c %}'})
  69. def test_widthratio12b(self):
  70. output = self.engine.render_to_string('widthratio12b', {'a': None, 'c': 100, 'b': 100})
  71. self.assertEqual(output, '')
  72. @setup({'widthratio13a': '{% widthratio a b c %}'})
  73. def test_widthratio13a(self):
  74. output = self.engine.render_to_string('widthratio13a', {'a': 0, 'c': 100, 'b': 'b'})
  75. self.assertEqual(output, '')
  76. @setup({'widthratio13b': '{% widthratio a b c %}'})
  77. def test_widthratio13b(self):
  78. output = self.engine.render_to_string('widthratio13b', {'a': 0, 'c': 100, 'b': None})
  79. self.assertEqual(output, '')
  80. @setup({'widthratio14a': '{% widthratio a b c %}'})
  81. def test_widthratio14a(self):
  82. with self.assertRaises(TemplateSyntaxError):
  83. self.engine.render_to_string('widthratio14a', {'a': 0, 'c': 'c', 'b': 100})
  84. @setup({'widthratio14b': '{% widthratio a b c %}'})
  85. def test_widthratio14b(self):
  86. with self.assertRaises(TemplateSyntaxError):
  87. self.engine.render_to_string('widthratio14b', {'a': 0, 'c': None, 'b': 100})
  88. @setup({'widthratio15': '{% load custom %}{% widthratio a|noop:"x y" b 0 %}'})
  89. def test_widthratio15(self):
  90. """
  91. Test whitespace in filter argument
  92. """
  93. output = self.engine.render_to_string('widthratio15', {'a': 50, 'b': 100})
  94. self.assertEqual(output, '0')
  95. # Widthratio with variable assignment
  96. @setup({'widthratio16': '{% widthratio a b 100 as variable %}-{{ variable }}-'})
  97. def test_widthratio16(self):
  98. output = self.engine.render_to_string('widthratio16', {'a': 50, 'b': 100})
  99. self.assertEqual(output, '-50-')
  100. @setup({'widthratio17': '{% widthratio a b 100 as variable %}-{{ variable }}-'})
  101. def test_widthratio17(self):
  102. output = self.engine.render_to_string('widthratio17', {'a': 100, 'b': 100})
  103. self.assertEqual(output, '-100-')
  104. @setup({'widthratio18': '{% widthratio a b 100 as %}'})
  105. def test_widthratio18(self):
  106. with self.assertRaises(TemplateSyntaxError):
  107. self.engine.get_template('widthratio18')
  108. @setup({'widthratio19': '{% widthratio a b 100 not_as variable %}'})
  109. def test_widthratio19(self):
  110. with self.assertRaises(TemplateSyntaxError):
  111. self.engine.get_template('widthratio19')
  112. @setup({'widthratio20': '{% widthratio a b 100 %}'})
  113. def test_widthratio20(self):
  114. output = self.engine.render_to_string('widthratio20', {'a': float('inf'), 'b': float('inf')})
  115. self.assertEqual(output, '')
  116. @setup({'widthratio21': '{% widthratio a b 100 %}'})
  117. def test_widthratio21(self):
  118. output = self.engine.render_to_string('widthratio21', {'a': float('inf'), 'b': 2})
  119. self.assertEqual(output, '')