Ticket #14960: tests.diff

File tests.diff, 4.4 KB (added by Andy Venet, 14 years ago)

Made some tests based on the ones on simple_tag

  • tests/regressiontests/templates/templatetags/custom.py

     
    3939    return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
    4040params_and_context.anything = "Expected params_and_context __dict__"
    4141
     42@register.inclusion_tag('inclusion.html')
     43def inclusion_no_params():
     44    """Expected inclusion_no_params __doc__"""
     45    return {"result" : "inclusion_no_params - Expected result"}
     46inclusion_no_params.anything = "Expected inclusion_no_params __dict__"
     47
     48@register.inclusion_tag('inclusion.html')
     49def inclusion_one_param(arg):
     50    """Expected inclusion_one_param __doc__"""
     51    return {"result" : "inclusion_one_param - Expected result: %s" % arg}
     52inclusion_one_param.anything = "Expected inclusion_one_param __dict__"
     53
     54@register.inclusion_tag('inclusion.html', takes_context=False)
     55def inclusion_explicit_no_context(arg):
     56    """Expected inclusion_explicit_no_context __doc__"""
     57    return {"result" : "inclusion_explicit_no_context - Expected result: %s" % arg}
     58inclusion_explicit_no_context.anything = "Expected inclusion_explicit_no_context __dict__"
     59
     60@register.inclusion_tag('inclusion.html', takes_context=True)
     61def inclusion_no_params_with_context(context):
     62    """Expected inclusion_no_params_with_context __doc__"""
     63    return {"result" : "inclusion_no_params_with_context - Expected result (context value: %s)" % context['value']}
     64inclusion_no_params_with_context.anything = "Expected inclusion_no_params_with_context __dict__"
     65
     66@register.inclusion_tag('inclusion.html', takes_context=True)
     67def inclusion_params_and_context(context, arg):
     68    """Expected inclusion_params_and_context __doc__"""
     69    return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)}
     70inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__"
     71
  • tests/regressiontests/templates/templates/inclusion.html

     
     1{{result}}
  • tests/regressiontests/templates/custom.py

     
    5252        register = template.Library()
    5353        decorator = register.simple_tag(takes_context=True)
    5454        self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters)
     55
     56    def test_inclusion_tags(self):
     57        c = template.Context({'value': 42})
     58
     59        t = template.Template('{% load custom %}{% inclusion_no_params %}')
     60        self.assertEquals(t.render(c), u'inclusion_no_params - Expected result\n')
     61
     62        t = template.Template('{% load custom %}{% inclusion_one_param 37 %}')
     63        self.assertEquals(t.render(c), u'inclusion_one_param - Expected result: 37\n')
     64
     65        t = template.Template('{% load custom %}{% inclusion_explicit_no_context 37 %}')
     66        self.assertEquals(t.render(c), u'inclusion_explicit_no_context - Expected result: 37\n')
     67
     68        t = template.Template('{% load custom %}{% inclusion_no_params_with_context %}')
     69        self.assertEquals(t.render(c), u'inclusion_no_params_with_context - Expected result (context value: 42)\n')
     70
     71        t = template.Template('{% load custom %}{% inclusion_params_and_context 37%}')
     72        self.assertEquals(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
     73
     74
     75    def test_inclusion_tag_registration(self):
     76        # Test that the decorators preserve the decorated function's docstring, name and attributes.
     77        self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
     78        self.verify_tag(custom.inclusion_one_param, 'inclusion_one_param')
     79        self.verify_tag(custom.inclusion_explicit_no_context, 'inclusion_explicit_no_context')
     80        self.verify_tag(custom.inclusion_no_params_with_context, 'inclusion_no_params_with_context')
     81        self.verify_tag(custom.inclusion_params_and_context, 'inclusion_params_and_context')
     82
     83
Back to Top