2019-05-07 10:02:08 +08:00
|
|
|
import functools
|
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
from django.template import Library
|
|
|
|
from django.template.base import Node
|
2018-11-24 10:01:44 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
|
2018-11-24 10:01:44 +08:00
|
|
|
class FilterRegistrationTests(SimpleTestCase):
|
2015-05-09 04:10:36 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.library = Library()
|
|
|
|
|
|
|
|
def test_filter(self):
|
|
|
|
@self.library.filter
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.filters["func"], func)
|
|
|
|
|
|
|
|
def test_filter_parens(self):
|
|
|
|
@self.library.filter()
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.filters["func"], func)
|
|
|
|
|
|
|
|
def test_filter_name_arg(self):
|
|
|
|
@self.library.filter("name")
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.filters["name"], func)
|
|
|
|
|
|
|
|
def test_filter_name_kwarg(self):
|
|
|
|
@self.library.filter(name="name")
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.filters["name"], func)
|
|
|
|
|
|
|
|
def test_filter_call(self):
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.library.filter("name", func)
|
|
|
|
self.assertEqual(self.library.filters["name"], func)
|
|
|
|
|
|
|
|
def test_filter_invalid(self):
|
|
|
|
msg = "Unsupported arguments to Library.filter: (None, '')"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.library.filter(None, "")
|
|
|
|
|
|
|
|
|
2018-11-24 10:01:44 +08:00
|
|
|
class InclusionTagRegistrationTests(SimpleTestCase):
|
2015-05-09 04:10:36 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.library = Library()
|
|
|
|
|
|
|
|
def test_inclusion_tag(self):
|
|
|
|
@self.library.inclusion_tag("template.html")
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertIn("func", self.library.tags)
|
|
|
|
|
|
|
|
def test_inclusion_tag_name(self):
|
|
|
|
@self.library.inclusion_tag("template.html", name="name")
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertIn("name", self.library.tags)
|
|
|
|
|
2019-05-07 10:02:08 +08:00
|
|
|
def test_inclusion_tag_wrapped(self):
|
|
|
|
@self.library.inclusion_tag("template.html")
|
|
|
|
@functools.lru_cache(maxsize=32)
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-05-07 10:02:08 +08:00
|
|
|
func_wrapped = self.library.tags["func"].__wrapped__
|
|
|
|
self.assertIs(func_wrapped, func)
|
|
|
|
self.assertTrue(hasattr(func_wrapped, "cache_info"))
|
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
|
2018-11-24 10:01:44 +08:00
|
|
|
class SimpleTagRegistrationTests(SimpleTestCase):
|
2015-05-09 04:10:36 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.library = Library()
|
|
|
|
|
|
|
|
def test_simple_tag(self):
|
|
|
|
@self.library.simple_tag
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertIn("func", self.library.tags)
|
|
|
|
|
|
|
|
def test_simple_tag_parens(self):
|
|
|
|
@self.library.simple_tag()
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertIn("func", self.library.tags)
|
|
|
|
|
|
|
|
def test_simple_tag_name_kwarg(self):
|
|
|
|
@self.library.simple_tag(name="name")
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertIn("name", self.library.tags)
|
|
|
|
|
|
|
|
def test_simple_tag_invalid(self):
|
|
|
|
msg = "Invalid arguments provided to simple_tag"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.library.simple_tag("invalid")
|
|
|
|
|
2019-05-07 10:02:08 +08:00
|
|
|
def test_simple_tag_wrapped(self):
|
|
|
|
@self.library.simple_tag
|
|
|
|
@functools.lru_cache(maxsize=32)
|
|
|
|
def func():
|
|
|
|
return ""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-05-07 10:02:08 +08:00
|
|
|
func_wrapped = self.library.tags["func"].__wrapped__
|
|
|
|
self.assertIs(func_wrapped, func)
|
|
|
|
self.assertTrue(hasattr(func_wrapped, "cache_info"))
|
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
|
2018-11-24 10:01:44 +08:00
|
|
|
class TagRegistrationTests(SimpleTestCase):
|
2015-05-09 04:10:36 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.library = Library()
|
|
|
|
|
|
|
|
def test_tag(self):
|
|
|
|
@self.library.tag
|
|
|
|
def func(parser, token):
|
|
|
|
return Node()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.tags["func"], func)
|
|
|
|
|
|
|
|
def test_tag_parens(self):
|
|
|
|
@self.library.tag()
|
|
|
|
def func(parser, token):
|
|
|
|
return Node()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.tags["func"], func)
|
|
|
|
|
|
|
|
def test_tag_name_arg(self):
|
|
|
|
@self.library.tag("name")
|
|
|
|
def func(parser, token):
|
|
|
|
return Node()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.tags["name"], func)
|
|
|
|
|
|
|
|
def test_tag_name_kwarg(self):
|
|
|
|
@self.library.tag(name="name")
|
|
|
|
def func(parser, token):
|
|
|
|
return Node()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.assertEqual(self.library.tags["name"], func)
|
|
|
|
|
|
|
|
def test_tag_call(self):
|
|
|
|
def func(parser, token):
|
|
|
|
return Node()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
self.library.tag("name", func)
|
|
|
|
self.assertEqual(self.library.tags["name"], func)
|
|
|
|
|
|
|
|
def test_tag_invalid(self):
|
|
|
|
msg = "Unsupported arguments to Library.tag: (None, '')"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.library.tag(None, "")
|