Ticket #13956: indefinite_inclusion_tag_args_with_tests.diff

File indefinite_inclusion_tag_args_with_tests.diff, 7.3 KB (added by Gregor Müllegger, 14 years ago)

Adding tests to previous patch.

  • django/template/__init__.py

    === modified file 'django/template/__init__.py'
     
    848848            return ''
    849849        return _render_value_in_context(output, context)
    850850
    851 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     851def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None):
    852852    "Returns a template.Node subclass."
    853853    bits = token.split_contents()[1:]
    854854    bmax = len(params)
    855855    def_len = defaults and len(defaults) or 0
    856856    bmin = bmax - def_len
    857     if(len(bits) < bmin or len(bits) > bmax):
    858         if bmin == bmax:
    859             message = "%s takes %s arguments" % (name, bmin)
    860         else:
    861             message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    862         raise TemplateSyntaxError(message)
     857    if varargs is None:
     858        if(len(bits) < bmin or len(bits) > bmax):
     859            if bmin == bmax:
     860                message = "%s takes %s arguments" % (name, bmin)
     861            else:
     862                message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
     863            raise TemplateSyntaxError(message)
     864    else:
     865        if(len(bits) < bmin):
     866            message = "%s takes at least %s arguments" % (name, bmin)
     867            raise TemplateSyntaxError(message)
    863868    return node_class(bits)
    864869
    865870class Library(object):
     
    916921        return func
    917922
    918923    def simple_tag(self,func):
    919         params, xx, xxx, defaults = getargspec(func)
     924        params, varargs, xx, defaults = getargspec(func)
    920925
    921926        class SimpleNode(Node):
    922927            def __init__(self, vars_to_resolve):
     
    926931                resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    927932                return func(*resolved_vars)
    928933
    929         compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     934        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs)
    930935        compile_func.__doc__ = func.__doc__
    931936        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    932937        return func
    933938
    934939    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
    935940        def dec(func):
    936             params, xx, xxx, defaults = getargspec(func)
     941            params, varargs, xx, defaults = getargspec(func)
    937942            if takes_context:
    938943                if params[0] == 'context':
    939944                    params = params[1:]
     
    968973                    if csrf_token is not None:
    969974                        new_context['csrf_token'] = csrf_token
    970975                    return self.nodelist.render(new_context)
    971 
    972             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
     976            compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs)
    973977            compile_func.__doc__ = func.__doc__
    974978            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    975979            return func
  • tests/regressiontests/templates/simple_tag.py

    === added file 'tests/regressiontests/templates/simple_tag.py'
     
     1import unittest
     2from django.template import Context, Template, TemplateSyntaxError
     3
     4
     5class SimpleTagTests(unittest.TestCase):
     6    def test_simple_tag_with_fixed_number_of_arguments(self):
     7        c = Context({})
     8        t = Template('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}')
     9        self.assertEquals(t.render(c), u'one, two, three')
     10
     11        c = Context({'one': '1', 'two': '2', 'three': '3'})
     12        t = Template('{% load custom %}{% simple_tag_with_three_arguments one two three %}')
     13        self.assertEquals(t.render(c), u'1, 2, 3')
     14
     15        self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}')
     16        self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}')
     17
     18    def test_simple_tag_with_var_number_of_arguments(self):
     19        c = Context({})
     20        t = Template('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}')
     21        self.assertEquals(t.render(c), u'one, two, three')
     22
     23        c = Context({'one': '1', 'two': '2', 'three': '3'})
     24        t = Template('{% load custom %}{% simple_tag_with_two_default_arguments one three %}')
     25        self.assertEquals(t.render(c), u'1, 3, 3')
     26
     27        c = Context({'one': 'first'})
     28        t = Template('{% load custom %}{% simple_tag_with_two_default_arguments one %}')
     29        self.assertEquals(t.render(c), u'first, 2, 3')
     30
     31        self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments %}')
     32        self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}')
     33
     34    def test_simple_tag_with_unlimited_number_of_arguments(self):
     35        c = Context({})
     36        t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}')
     37        self.assertEquals(t.render(c), u'one, two, three')
     38
     39        c = Context({})
     40        t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}')
     41        self.assertEquals(t.render(c), u'one, 2')
     42
     43        c = Context({'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'})
     44        t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments one two three four five %}')
     45        self.assertEquals(t.render(c), u'1, 2, 3, 4, 5')
     46
     47        c = Context({'one': 'first'})
     48        t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments one %}')
     49        self.assertEquals(t.render(c), u'first, 2')
     50
     51        self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments %}')
  • tests/regressiontests/templates/templatetags/custom.py

    === modified file 'tests/regressiontests/templates/templatetags/custom.py'
     
    99
    1010register.filter(trim)
    1111
     12
     13def simple_tag_with_three_arguments(one, two, three):
     14    return ', '.join((one, two, three))
     15
     16register.simple_tag(simple_tag_with_three_arguments)
     17
     18
     19def simple_tag_with_two_default_arguments(one, two='2', three='3'):
     20    return ', '.join((one, two, three))
     21
     22register.simple_tag(simple_tag_with_two_default_arguments)
     23
     24
     25def simple_tag_with_unlimited_arguments(one, two='2', *args):
     26    return ', '.join([one, two] + list(args))
     27
     28register.simple_tag(simple_tag_with_unlimited_arguments)
  • tests/regressiontests/templates/tests.py

    === modified file 'tests/regressiontests/templates/tests.py'
     
    2727from unicode import unicode_tests
    2828from nodelist import NodelistTest
    2929from smartif import *
     30from simple_tag import SimpleTagTests
    3031
    3132try:
    3233    from loaders import *
Back to Top