summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-12-19 15:00:50 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-12-19 15:00:50 +0000
commit314fabc930a1bb361ca06e9c948bb726ad8df99a (patch)
tree973d32f9ec0f1885ecd72d8fe45132fb4987b897 /tests
parent7adffaeaf6dfa22db0b6b2a29632b9150c7ac732 (diff)
Fixed #14908 -- Added a 'takes_context' argument to simple_tag. Thanks to Julien Phalip for driving the issue and providing the final patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14987 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/templates/custom.py47
-rw-r--r--tests/regressiontests/templates/templatetags/custom.py30
-rw-r--r--tests/regressiontests/templates/tests.py2
3 files changed, 76 insertions, 3 deletions
diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py
index bd1e441e7f..a517bd1810 100644
--- a/tests/regressiontests/templates/custom.py
+++ b/tests/regressiontests/templates/custom.py
@@ -1,11 +1,54 @@
from django import template
from django.utils.unittest import TestCase
+from templatetags import custom
-
-class CustomTests(TestCase):
+class CustomFilterTests(TestCase):
def test_filter(self):
t = template.Template("{% load custom %}{{ string|trim:5 }}")
self.assertEqual(
t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
u"abcde"
)
+
+
+class CustomTagTests(TestCase):
+ def verify_tag(self, tag, name):
+ self.assertEquals(tag.__name__, name)
+ self.assertEquals(tag.__doc__, 'Expected %s __doc__' % name)
+ self.assertEquals(tag.__dict__['anything'], 'Expected %s __dict__' % name)
+
+ def test_simple_tags(self):
+ c = template.Context({'value': 42})
+
+ t = template.Template('{% load custom %}{% no_params %}')
+ self.assertEquals(t.render(c), u'no_params - Expected result')
+
+ t = template.Template('{% load custom %}{% one_param 37 %}')
+ self.assertEquals(t.render(c), u'one_param - Expected result: 37')
+
+ t = template.Template('{% load custom %}{% explicit_no_context 37 %}')
+ self.assertEquals(t.render(c), u'explicit_no_context - Expected result: 37')
+
+ t = template.Template('{% load custom %}{% no_params_with_context %}')
+ self.assertEquals(t.render(c), u'no_params_with_context - Expected result (context value: 42)')
+
+ t = template.Template('{% load custom %}{% params_and_context 37 %}')
+ self.assertEquals(t.render(c), u'params_and_context - Expected result (context value: 42): 37')
+
+ def test_simple_tag_registration(self):
+ # Test that the decorators preserve the decorated function's docstring, name and attributes.
+ self.verify_tag(custom.no_params, 'no_params')
+ self.verify_tag(custom.one_param, 'one_param')
+ self.verify_tag(custom.explicit_no_context, 'explicit_no_context')
+ self.verify_tag(custom.no_params_with_context, 'no_params_with_context')
+ self.verify_tag(custom.params_and_context, 'params_and_context')
+
+ def test_simple_tag_missing_context(self):
+ # That the 'context' parameter must be present when takes_context is True
+ def a_simple_tag_without_parameters(arg):
+ """Expected __doc__"""
+ return "Expected result"
+
+ register = template.Library()
+ decorator = register.simple_tag(takes_context=True)
+ self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters)
diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py
index fdf8d1077f..701131626b 100644
--- a/tests/regressiontests/templates/templatetags/custom.py
+++ b/tests/regressiontests/templates/templatetags/custom.py
@@ -9,3 +9,33 @@ trim = stringfilter(trim)
register.filter(trim)
+@register.simple_tag
+def no_params():
+ """Expected no_params __doc__"""
+ return "no_params - Expected result"
+no_params.anything = "Expected no_params __dict__"
+
+@register.simple_tag
+def one_param(arg):
+ """Expected one_param __doc__"""
+ return "one_param - Expected result: %s" % arg
+one_param.anything = "Expected one_param __dict__"
+
+@register.simple_tag(takes_context=False)
+def explicit_no_context(arg):
+ """Expected explicit_no_context __doc__"""
+ return "explicit_no_context - Expected result: %s" % arg
+explicit_no_context.anything = "Expected explicit_no_context __dict__"
+
+@register.simple_tag(takes_context=True)
+def no_params_with_context(context):
+ """Expected no_params_with_context __doc__"""
+ return "no_params_with_context - Expected result (context value: %s)" % context['value']
+no_params_with_context.anything = "Expected no_params_with_context __dict__"
+
+@register.simple_tag(takes_context=True)
+def params_and_context(context, arg):
+ """Expected params_and_context __doc__"""
+ return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
+params_and_context.anything = "Expected params_and_context __dict__"
+
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index d363f50e6a..8f31bd4ad6 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -23,7 +23,7 @@ from django.utils.safestring import mark_safe
from django.utils.tzinfo import LocalTimezone
from context import ContextTests
-from custom import CustomTests
+from custom import CustomTagTests, CustomFilterTests
from parser import ParserTests
from unicode import UnicodeTests
from nodelist import NodelistTest