diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py index 15f625dbd5..fe5b095207 100644 --- a/tests/regressiontests/templates/custom.py +++ b/tests/regressiontests/templates/custom.py @@ -52,3 +52,29 @@ class CustomTagTests(TestCase): register = template.Library() decorator = register.simple_tag(takes_context=True) self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters) + + def test_inclusion_tags(self): + c = template.Context({'value': 42}) + + t = template.Template('{% load custom %}{% inclusion_no_params %}') + self.assertEquals(t.render(c), u'inclusion_no_params - Expected result\n') + + t = template.Template('{% load custom %}{% inclusion_one_param 37 %}') + self.assertEquals(t.render(c), u'inclusion_one_param - Expected result: 37\n') + + t = template.Template('{% load custom %}{% inclusion_explicit_no_context 37 %}') + self.assertEquals(t.render(c), u'inclusion_explicit_no_context - Expected result: 37\n') + + t = template.Template('{% load custom %}{% inclusion_no_params_with_context %}') + self.assertEquals(t.render(c), u'inclusion_no_params_with_context - Expected result (context value: 42)\n') + + t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}') + self.assertEquals(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n') + + def test_inclusion_tag_registration(self): + # Test that the decorators preserve the decorated function's docstring, name and attributes. + self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params') + self.verify_tag(custom.inclusion_one_param, 'inclusion_one_param') + self.verify_tag(custom.inclusion_explicit_no_context, 'inclusion_explicit_no_context') + self.verify_tag(custom.inclusion_no_params_with_context, 'inclusion_no_params_with_context') + self.verify_tag(custom.inclusion_params_and_context, 'inclusion_params_and_context') diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py index 701131626b..b2e8a1681c 100644 --- a/tests/regressiontests/templates/templatetags/custom.py +++ b/tests/regressiontests/templates/templatetags/custom.py @@ -39,3 +39,33 @@ def params_and_context(context, arg): return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg) params_and_context.anything = "Expected params_and_context __dict__" +@register.inclusion_tag('inclusion.html') +def inclusion_no_params(): + """Expected inclusion_no_params __doc__""" + return {"result" : "inclusion_no_params - Expected result"} +inclusion_no_params.anything = "Expected inclusion_no_params __dict__" + +@register.inclusion_tag('inclusion.html') +def inclusion_one_param(arg): + """Expected inclusion_one_param __doc__""" + return {"result" : "inclusion_one_param - Expected result: %s" % arg} +inclusion_one_param.anything = "Expected inclusion_one_param __dict__" + +@register.inclusion_tag('inclusion.html', takes_context=False) +def inclusion_explicit_no_context(arg): + """Expected inclusion_explicit_no_context __doc__""" + return {"result" : "inclusion_explicit_no_context - Expected result: %s" % arg} +inclusion_explicit_no_context.anything = "Expected inclusion_explicit_no_context __dict__" + +@register.inclusion_tag('inclusion.html', takes_context=True) +def inclusion_no_params_with_context(context): + """Expected inclusion_no_params_with_context __doc__""" + return {"result" : "inclusion_no_params_with_context - Expected result (context value: %s)" % context['value']} +inclusion_no_params_with_context.anything = "Expected inclusion_no_params_with_context __dict__" + +@register.inclusion_tag('inclusion.html', takes_context=True) +def inclusion_params_and_context(context, arg): + """Expected inclusion_params_and_context __doc__""" + return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)} +inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__" +