2014-12-07 16:43:10 +08:00
|
|
|
from django.template import TemplateSyntaxError
|
2015-08-17 20:57:38 +08:00
|
|
|
from django.test import SimpleTestCase
|
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
|
|
|
|
|
|
|
|
2014-12-04 04:36:17 +08:00
|
|
|
class FirstOfTagTests(SimpleTestCase):
|
2014-11-12 09:32:44 +08:00
|
|
|
@setup({"firstof01": "{% firstof a b c %}"})
|
|
|
|
def test_firstof01(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof01", {"a": 0, "c": 0, "b": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "")
|
|
|
|
|
|
|
|
@setup({"firstof02": "{% firstof a b c %}"})
|
|
|
|
def test_firstof02(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof02", {"a": 1, "c": 0, "b": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "1")
|
|
|
|
|
|
|
|
@setup({"firstof03": "{% firstof a b c %}"})
|
|
|
|
def test_firstof03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof03", {"a": 0, "c": 0, "b": 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "2")
|
|
|
|
|
|
|
|
@setup({"firstof04": "{% firstof a b c %}"})
|
|
|
|
def test_firstof04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof04", {"a": 0, "c": 3, "b": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "3")
|
|
|
|
|
|
|
|
@setup({"firstof05": "{% firstof a b c %}"})
|
|
|
|
def test_firstof05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof05", {"a": 1, "c": 3, "b": 2})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "1")
|
|
|
|
|
|
|
|
@setup({"firstof06": "{% firstof a b c %}"})
|
|
|
|
def test_firstof06(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof06", {"c": 3, "b": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "3")
|
|
|
|
|
|
|
|
@setup({"firstof07": '{% firstof a b "c" %}'})
|
|
|
|
def test_firstof07(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof07", {"a": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "c")
|
|
|
|
|
|
|
|
@setup({"firstof08": '{% firstof a b "c and d" %}'})
|
|
|
|
def test_firstof08(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof08", {"a": 0, "b": 0})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "c and d")
|
|
|
|
|
|
|
|
@setup({"firstof09": "{% firstof %}"})
|
|
|
|
def test_firstof09(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
self.engine.get_template("firstof09")
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
@setup({"firstof10": "{% firstof a %}"})
|
|
|
|
def test_firstof10(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
output = self.engine.render_to_string("firstof10", {"a": "<"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "<")
|
|
|
|
|
2015-08-17 20:57:38 +08:00
|
|
|
@setup({"firstof11": "{% firstof a b %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
def test_firstof11(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
output = self.engine.render_to_string("firstof11", {"a": "<", "b": ">"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "<")
|
|
|
|
|
2015-08-17 20:57:38 +08:00
|
|
|
@setup({"firstof12": "{% firstof a b %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
def test_firstof12(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
output = self.engine.render_to_string("firstof12", {"a": "", "b": ">"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, ">")
|
|
|
|
|
2015-08-17 20:57:38 +08:00
|
|
|
@setup({"firstof13": "{% autoescape off %}{% firstof a %}{% endautoescape %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
def test_firstof13(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
output = self.engine.render_to_string("firstof13", {"a": "<"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "<")
|
|
|
|
|
2015-08-17 20:57:38 +08:00
|
|
|
@setup({"firstof14": "{% firstof a|safe b %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
def test_firstof14(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
output = self.engine.render_to_string("firstof14", {"a": "<"})
|
2014-11-12 09:32:44 +08:00
|
|
|
self.assertEqual(output, "<")
|
2015-04-13 23:27:30 +08:00
|
|
|
|
|
|
|
@setup({"firstof15": "{% firstof a b c as myvar %}"})
|
|
|
|
def test_firstof15(self):
|
|
|
|
ctx = {"a": 0, "b": 2, "c": 3}
|
|
|
|
output = self.engine.render_to_string("firstof15", ctx)
|
|
|
|
self.assertEqual(ctx["myvar"], "2")
|
|
|
|
self.assertEqual(output, "")
|
2018-01-25 20:14:44 +08:00
|
|
|
|
|
|
|
@setup({"firstof16": "{% firstof a b c as myvar %}"})
|
|
|
|
def test_all_false_arguments_asvar(self):
|
|
|
|
ctx = {"a": 0, "b": 0, "c": 0}
|
|
|
|
output = self.engine.render_to_string("firstof16", ctx)
|
|
|
|
self.assertEqual(ctx["myvar"], "")
|
|
|
|
self.assertEqual(output, "")
|