2018-03-25 04:38:20 +08:00
|
|
|
from django.template import TemplateSyntaxError
|
2017-02-04 18:43:14 +08:00
|
|
|
from django.template.defaulttags import IfEqualNode
|
2020-05-03 09:04:51 +08:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings
|
|
|
|
from django.utils.deprecation import RemovedInDjango40Warning
|
2014-11-12 09:32:44 +08:00
|
|
|
|
2014-12-07 16:43:10 +08:00
|
|
|
from ..utils import setup
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
2020-05-03 09:04:51 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango40Warning)
|
2014-12-04 04:36:17 +08:00
|
|
|
class IfEqualTagTests(SimpleTestCase):
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
@setup({'ifequal01': '{% ifequal a b %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal01', {'a': 1, 'b': 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifequal02': '{% ifequal a b %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal02', {'a': 1, 'b': 1})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal03': '{% ifequal a b %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal03', {'a': 1, 'b': 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal04': '{% ifequal a b %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal04', {'a': 1, 'b': 1})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal05': '{% ifequal a \'test\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal05', {'a': 'test'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal06': '{% ifequal a \'test\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal06(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal06', {'a': 'no'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal07': '{% ifequal a "test" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal07(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal07', {'a': 'test'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal08': '{% ifequal a "test" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal08(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal08', {'a': 'no'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal09': '{% ifequal a "test" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal09(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal09')
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal10': '{% ifequal a b %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal10(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal10')
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
# SMART SPLITTING
|
|
|
|
@setup({'ifequal-split01': '{% ifequal a "test man" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split01')
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal-split02': '{% ifequal a "test man" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split02', {'a': 'foo'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal-split03': '{% ifequal a "test man" %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split03', {'a': 'test man'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-split04': '{% ifequal a \'test man\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split04', {'a': 'test man'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-split05': '{% ifequal a \'i "love" you\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split05', {'a': ''})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal-split06': '{% ifequal a \'i "love" you\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split06(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split06', {'a': 'i "love" you'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-split07': '{% ifequal a \'i "love" you\' %}yes{% else %}no{% endifequal %}'})
|
|
|
|
def test_ifequal_split07(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split07', {'a': 'i love you'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
@setup({'ifequal-split08': r"{% ifequal a 'I\'m happy' %}yes{% else %}no{% endifequal %}"})
|
|
|
|
def test_ifequal_split08(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split08', {'a': "I'm happy"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-split09': r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}"})
|
|
|
|
def test_ifequal_split09(self):
|
2016-09-17 00:15:00 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split09', {'a': r'slash\man'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-split10': r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}"})
|
|
|
|
def test_ifequal_split10(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-split10', {'a': 'slashman'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
|
|
|
|
|
|
|
# NUMERIC RESOLUTION
|
|
|
|
@setup({'ifequal-numeric01': '{% ifequal x 5 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric01', {'x': '5'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric02': '{% ifequal x 5 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric02', {'x': 5})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric03': '{% ifequal x 5.2 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric03', {'x': 5})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric04': '{% ifequal x 5.2 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric04', {'x': 5.2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric05': '{% ifequal x 0.2 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric05', {'x': 0.2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric06': '{% ifequal x .2 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric06(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric06', {'x': 0.2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric07': '{% ifequal x 2. %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric07(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric07', {'x': 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric08': '{% ifequal x "5" %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric08(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric08', {'x': 5})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric09': '{% ifequal x "5" %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric09(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric09', {'x': '5'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric10': '{% ifequal x -5 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric10(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric10', {'x': -5})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric11': '{% ifequal x -5.2 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric11(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric11', {'x': -5.2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifequal-numeric12': '{% ifequal x +5 %}yes{% endifequal %}'})
|
|
|
|
def test_ifequal_numeric12(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-numeric12', {'x': 5})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
# FILTER EXPRESSIONS AS ARGUMENTS
|
|
|
|
@setup({'ifequal-filter01': '{% ifequal a|upper "A" %}x{% endifequal %}'})
|
|
|
|
def test_ifequal_filter01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-filter01', {'a': 'a'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'x')
|
|
|
|
|
|
|
|
@setup({'ifequal-filter02': '{% ifequal "A" a|upper %}x{% endifequal %}'})
|
|
|
|
def test_ifequal_filter02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-filter02', {'a': 'a'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'x')
|
|
|
|
|
|
|
|
@setup({'ifequal-filter03': '{% ifequal a|upper b|upper %}x{% endifequal %}'})
|
|
|
|
def test_ifequal_filter03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-filter03', {'a': 'x', 'b': 'X'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'x')
|
|
|
|
|
|
|
|
@setup({'ifequal-filter04': '{% ifequal x|slice:"1" "a" %}x{% endifequal %}'})
|
|
|
|
def test_ifequal_filter04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-filter04', {'x': 'aaa'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'x')
|
|
|
|
|
|
|
|
@setup({'ifequal-filter05': '{% ifequal x|slice:"1"|upper "A" %}x{% endifequal %}'})
|
|
|
|
def test_ifequal_filter05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifequal-filter05', {'x': 'aaa'})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'x')
|
|
|
|
|
|
|
|
|
2020-05-03 09:04:51 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango40Warning)
|
2014-12-04 04:36:17 +08:00
|
|
|
class IfNotEqualTagTests(SimpleTestCase):
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
@setup({'ifnotequal01': '{% ifnotequal a b %}yes{% endifnotequal %}'})
|
|
|
|
def test_ifnotequal01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifnotequal01', {'a': 1, 'b': 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifnotequal02': '{% ifnotequal a b %}yes{% endifnotequal %}'})
|
|
|
|
def test_ifnotequal02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifnotequal02', {'a': 1, 'b': 1})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'ifnotequal03': '{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}'})
|
|
|
|
def test_ifnotequal03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifnotequal03', {'a': 1, 'b': 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'yes')
|
|
|
|
|
|
|
|
@setup({'ifnotequal04': '{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}'})
|
|
|
|
def test_ifnotequal04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string('ifnotequal04', {'a': 1, 'b': 1})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, 'no')
|
2017-02-04 18:43:14 +08:00
|
|
|
|
2018-03-25 04:38:20 +08:00
|
|
|
@setup({'one_var': '{% ifnotequal a %}yes{% endifnotequal %}'})
|
|
|
|
def test_one_var(self):
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, "'ifnotequal' takes two arguments"):
|
|
|
|
self.engine.render_to_string('one_var', {'a': 1})
|
|
|
|
|
2017-02-04 18:43:14 +08:00
|
|
|
|
2020-05-03 09:04:51 +08:00
|
|
|
class DeprecationTests(SimpleTestCase):
|
|
|
|
@setup(
|
|
|
|
{'ifequal_warning': '{% ifequal a b %}yes{% endifequal %}'},
|
|
|
|
test_once=True,
|
|
|
|
)
|
|
|
|
def test_ifequal_warning(self):
|
|
|
|
msg = (
|
|
|
|
'The {% ifequal %} template tag is deprecated in favor of '
|
|
|
|
'{% if %}.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango40Warning, msg):
|
|
|
|
self.engine.render_to_string('ifequal_warning', {'a': 1, 'b': 2})
|
|
|
|
|
|
|
|
@setup(
|
|
|
|
{'ifnotequal_warning': '{% ifnotequal a b %}yes{% endifnoequal %}'},
|
|
|
|
test_once=True,
|
|
|
|
)
|
|
|
|
def test_ifnotequal_warning(self):
|
|
|
|
msg = (
|
|
|
|
'The {% ifnotequal %} template tag is deprecated in favor of '
|
|
|
|
'{% if %}.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango40Warning, msg):
|
|
|
|
self.engine.render_to_string('ifnotequal_warning', {'a': 1, 'b': 2})
|
|
|
|
|
2017-02-04 18:43:14 +08:00
|
|
|
def test_repr(self):
|
|
|
|
node = IfEqualNode(var1='a', var2='b', nodelist_true=[], nodelist_false=[], negate=False)
|
|
|
|
self.assertEqual(repr(node), '<IfEqualNode>')
|