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
This commit is contained in:
Russell Keith-Magee 2010-12-19 15:00:50 +00:00
parent 7adffaeaf6
commit 314fabc930
6 changed files with 134 additions and 20 deletions

View file

@ -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)

View file

@ -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__"

View file

@ -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