From cd4282816db9164791cd0ac97a3dc329ad92c522 Mon Sep 17 00:00:00 2001 From: Preston Timmons Date: Fri, 16 Jan 2015 17:34:32 -0600 Subject: Fixed #18651 -- Enabled optional assignments for simple_tag(). --- tests/template_tests/templatetags/custom.py | 91 ++---------- tests/template_tests/test_custom.py | 212 ++++++++-------------------- 2 files changed, 68 insertions(+), 235 deletions(-) (limited to 'tests') diff --git a/tests/template_tests/templatetags/custom.py b/tests/template_tests/templatetags/custom.py index 953af07ef1..50ebd353ec 100644 --- a/tests/template_tests/templatetags/custom.py +++ b/tests/template_tests/templatetags/custom.py @@ -1,4 +1,5 @@ import operator +import warnings from django import template from django.template.defaultfilters import stringfilter @@ -125,83 +126,17 @@ def minustwo_overridden_name(value): register.simple_tag(lambda x: x - 1, name='minusone') -@register.assignment_tag -def assignment_no_params(): - """Expected assignment_no_params __doc__""" - return "assignment_no_params - Expected result" -assignment_no_params.anything = "Expected assignment_no_params __dict__" +with warnings.catch_warnings(): + warnings.simplefilter('ignore') + @register.assignment_tag + def assignment_no_params(): + """Expected assignment_no_params __doc__""" + return "assignment_no_params - Expected result" + assignment_no_params.anything = "Expected assignment_no_params __dict__" -@register.assignment_tag -def assignment_one_param(arg): - """Expected assignment_one_param __doc__""" - return "assignment_one_param - Expected result: %s" % arg -assignment_one_param.anything = "Expected assignment_one_param __dict__" - - -@register.assignment_tag(takes_context=False) -def assignment_explicit_no_context(arg): - """Expected assignment_explicit_no_context __doc__""" - return "assignment_explicit_no_context - Expected result: %s" % arg -assignment_explicit_no_context.anything = "Expected assignment_explicit_no_context __dict__" - - -@register.assignment_tag(takes_context=True) -def assignment_no_params_with_context(context): - """Expected assignment_no_params_with_context __doc__""" - return "assignment_no_params_with_context - Expected result (context value: %s)" % context['value'] -assignment_no_params_with_context.anything = "Expected assignment_no_params_with_context __dict__" - - -@register.assignment_tag(takes_context=True) -def assignment_params_and_context(context, arg): - """Expected assignment_params_and_context __doc__""" - return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg) -assignment_params_and_context.anything = "Expected assignment_params_and_context __dict__" - - -@register.assignment_tag -def assignment_two_params(one, two): - """Expected assignment_two_params __doc__""" - return "assignment_two_params - Expected result: %s, %s" % (one, two) -assignment_two_params.anything = "Expected assignment_two_params __dict__" - - -@register.assignment_tag -def assignment_one_default(one, two='hi'): - """Expected assignment_one_default __doc__""" - return "assignment_one_default - Expected result: %s, %s" % (one, two) -assignment_one_default.anything = "Expected assignment_one_default __dict__" - - -@register.assignment_tag -def assignment_unlimited_args(one, two='hi', *args): - """Expected assignment_unlimited_args __doc__""" - return "assignment_unlimited_args - Expected result: %s" % (', '.join(six.text_type(arg) for arg in [one, two] + list(args))) -assignment_unlimited_args.anything = "Expected assignment_unlimited_args __dict__" - - -@register.assignment_tag -def assignment_only_unlimited_args(*args): - """Expected assignment_only_unlimited_args __doc__""" - return "assignment_only_unlimited_args - Expected result: %s" % ', '.join(six.text_type(arg) for arg in args) -assignment_only_unlimited_args.anything = "Expected assignment_only_unlimited_args __dict__" - - -@register.assignment_tag -def assignment_unlimited_args_kwargs(one, two='hi', *args, **kwargs): - """Expected assignment_unlimited_args_kwargs __doc__""" - # Sort the dictionary by key to guarantee the order for testing. - sorted_kwarg = sorted(six.iteritems(kwargs), key=operator.itemgetter(0)) - return "assignment_unlimited_args_kwargs - Expected result: %s / %s" % ( - ', '.join(six.text_type(arg) for arg in [one, two] + list(args)), - ', '.join('%s=%s' % (k, v) for (k, v) in sorted_kwarg) - ) -assignment_unlimited_args_kwargs.anything = "Expected assignment_unlimited_args_kwargs __dict__" - - -@register.assignment_tag(takes_context=True) -def assignment_tag_without_context_parameter(arg): - """Expected assignment_tag_without_context_parameter __doc__""" - return "Expected result" -assignment_tag_without_context_parameter.anything = "Expected assignment_tag_without_context_parameter __dict__" + @register.assignment_tag(takes_context=True) + def assignment_tag_without_context_parameter(arg): + """Expected assignment_tag_without_context_parameter __doc__""" + return "Expected result" + assignment_tag_without_context_parameter.anything = "Expected assignment_tag_without_context_parameter __dict__" diff --git a/tests/template_tests/test_custom.py b/tests/template_tests/test_custom.py index 77d089661f..66bb3fd156 100644 --- a/tests/template_tests/test_custom.py +++ b/tests/template_tests/test_custom.py @@ -28,70 +28,61 @@ class CustomTagTests(TestCase): def test_simple_tags(self): c = template.Context({'value': 42}) - t = template.Template('{% load custom %}{% no_params %}') - self.assertEqual(t.render(c), 'no_params - Expected result') - - t = template.Template('{% load custom %}{% one_param 37 %}') - self.assertEqual(t.render(c), 'one_param - Expected result: 37') - - t = template.Template('{% load custom %}{% explicit_no_context 37 %}') - self.assertEqual(t.render(c), 'explicit_no_context - Expected result: 37') - - t = template.Template('{% load custom %}{% no_params_with_context %}') - self.assertEqual(t.render(c), 'no_params_with_context - Expected result (context value: 42)') - - t = template.Template('{% load custom %}{% params_and_context 37 %}') - self.assertEqual(t.render(c), 'params_and_context - Expected result (context value: 42): 37') - - t = template.Template('{% load custom %}{% simple_two_params 37 42 %}') - self.assertEqual(t.render(c), 'simple_two_params - Expected result: 37, 42') - - t = template.Template('{% load custom %}{% simple_one_default 37 %}') - self.assertEqual(t.render(c), 'simple_one_default - Expected result: 37, hi') - - t = template.Template('{% load custom %}{% simple_one_default 37 two="hello" %}') - self.assertEqual(t.render(c), 'simple_one_default - Expected result: 37, hello') - - t = template.Template('{% load custom %}{% simple_one_default one=99 two="hello" %}') - self.assertEqual(t.render(c), 'simple_one_default - Expected result: 99, hello') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'simple_one_default' received unexpected keyword argument 'three'", - template.Template, '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}') - - t = template.Template('{% load custom %}{% simple_one_default 37 42 %}') - self.assertEqual(t.render(c), 'simple_one_default - Expected result: 37, 42') - - t = template.Template('{% load custom %}{% simple_unlimited_args 37 %}') - self.assertEqual(t.render(c), 'simple_unlimited_args - Expected result: 37, hi') - - t = template.Template('{% load custom %}{% simple_unlimited_args 37 42 56 89 %}') - self.assertEqual(t.render(c), 'simple_unlimited_args - Expected result: 37, 42, 56, 89') - - t = template.Template('{% load custom %}{% simple_only_unlimited_args %}') - self.assertEqual(t.render(c), 'simple_only_unlimited_args - Expected result: ') - - t = template.Template('{% load custom %}{% simple_only_unlimited_args 37 42 56 89 %}') - self.assertEqual(t.render(c), 'simple_only_unlimited_args - Expected result: 37, 42, 56, 89') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'simple_two_params' received too many positional arguments", - template.Template, '{% load custom %}{% simple_two_params 37 42 56 %}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'simple_one_default' received too many positional arguments", - template.Template, '{% load custom %}{% simple_one_default 37 42 56 %}') - - t = template.Template('{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}') - self.assertEqual(t.render(c), 'simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'simple_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)", - template.Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'simple_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'", - template.Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}') + templates = [ + ('{% load custom %}{% no_params %}', 'no_params - Expected result'), + ('{% load custom %}{% one_param 37 %}', 'one_param - Expected result: 37'), + ('{% load custom %}{% explicit_no_context 37 %}', 'explicit_no_context - Expected result: 37'), + ('{% load custom %}{% no_params_with_context %}', + 'no_params_with_context - Expected result (context value: 42)'), + ('{% load custom %}{% params_and_context 37 %}', + 'params_and_context - Expected result (context value: 42): 37'), + ('{% load custom %}{% simple_two_params 37 42 %}', 'simple_two_params - Expected result: 37, 42'), + ('{% load custom %}{% simple_one_default 37 %}', 'simple_one_default - Expected result: 37, hi'), + ('{% load custom %}{% simple_one_default 37 two="hello" %}', + 'simple_one_default - Expected result: 37, hello'), + ('{% load custom %}{% simple_one_default one=99 two="hello" %}', + 'simple_one_default - Expected result: 99, hello'), + ('{% load custom %}{% simple_one_default 37 42 %}', + 'simple_one_default - Expected result: 37, 42'), + ('{% load custom %}{% simple_unlimited_args 37 %}', 'simple_unlimited_args - Expected result: 37, hi'), + ('{% load custom %}{% simple_unlimited_args 37 42 56 89 %}', + 'simple_unlimited_args - Expected result: 37, 42, 56, 89'), + ('{% load custom %}{% simple_only_unlimited_args %}', 'simple_only_unlimited_args - Expected result: '), + ('{% load custom %}{% simple_only_unlimited_args 37 42 56 89 %}', + 'simple_only_unlimited_args - Expected result: 37, 42, 56, 89'), + ('{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}', + 'simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4'), + ] + + for entry in templates: + t = template.Template(entry[0]) + self.assertEqual(t.render(c), entry[1]) + + for entry in templates: + t = template.Template("%s as var %%}Result: {{ var }}" % entry[0][0:-2]) + self.assertEqual(t.render(c), "Result: %s" % entry[1]) + + def test_simple_tag_errors(self): + errors = [ + ("'simple_one_default' received unexpected keyword argument 'three'", + '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}'), + ("'simple_two_params' received too many positional arguments", + '{% load custom %}{% simple_two_params 37 42 56 %}'), + ("'simple_one_default' received too many positional arguments", + '{% load custom %}{% simple_one_default 37 42 56 %}'), + ("'simple_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)", + '{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}'), + ("'simple_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'", + '{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}'), + ] + + for entry in errors: + six.assertRaisesRegex(self, template.TemplateSyntaxError, entry[0], template.Template, entry[1]) + + for entry in errors: + six.assertRaisesRegex( + self, template.TemplateSyntaxError, entry[0], template.Template, "%s as var %%}" % entry[1][0:-2], + ) def test_simple_tag_registration(self): # Test that the decorators preserve the decorated function's docstring, name and attributes. @@ -278,102 +269,9 @@ class CustomTagTests(TestCase): t = template.Template('{% load custom %}{% assignment_no_params as var %}The result is: {{ var }}') self.assertEqual(t.render(c), 'The result is: assignment_no_params - Expected result') - t = template.Template('{% load custom %}{% assignment_one_param 37 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_one_param - Expected result: 37') - - t = template.Template('{% load custom %}{% assignment_explicit_no_context 37 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_explicit_no_context - Expected result: 37') - - t = template.Template('{% load custom %}{% assignment_no_params_with_context as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_no_params_with_context - Expected result (context value: 42)') - - t = template.Template('{% load custom %}{% assignment_params_and_context 37 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_params_and_context - Expected result (context value: 42): 37') - - t = template.Template('{% load custom %}{% assignment_two_params 37 42 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_two_params - Expected result: 37, 42') - - t = template.Template('{% load custom %}{% assignment_one_default 37 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_one_default - Expected result: 37, hi') - - t = template.Template('{% load custom %}{% assignment_one_default 37 two="hello" as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_one_default - Expected result: 37, hello') - - t = template.Template('{% load custom %}{% assignment_one_default one=99 two="hello" as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_one_default - Expected result: 99, hello') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_default' received unexpected keyword argument 'three'", - template.Template, '{% load custom %}{% assignment_one_default 99 two="hello" three="foo" as var %}') - - t = template.Template('{% load custom %}{% assignment_one_default 37 42 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_one_default - Expected result: 37, 42') - - t = template.Template('{% load custom %}{% assignment_unlimited_args 37 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_unlimited_args - Expected result: 37, hi') - - t = template.Template('{% load custom %}{% assignment_unlimited_args 37 42 56 89 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_unlimited_args - Expected result: 37, 42, 56, 89') - - t = template.Template('{% load custom %}{% assignment_only_unlimited_args as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_only_unlimited_args - Expected result: ') - - t = template.Template('{% load custom %}{% assignment_only_unlimited_args 37 42 56 89 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_only_unlimited_args - Expected result: 37, 42, 56, 89') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'", - template.Template, '{% load custom %}{% assignment_one_param 37 %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'", - template.Template, '{% load custom %}{% assignment_one_param 37 as %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'", - template.Template, '{% load custom %}{% assignment_one_param 37 ass var %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_two_params' received too many positional arguments", - template.Template, '{% load custom %}{% assignment_two_params 37 42 56 as var %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_default' received too many positional arguments", - template.Template, '{% load custom %}{% assignment_one_default 37 42 56 as var %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_one_default' did not receive value\(s\) for the argument\(s\): 'one'", - template.Template, '{% load custom %}{% assignment_one_default as var %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_unlimited_args' did not receive value\(s\) for the argument\(s\): 'one'", - template.Template, '{% load custom %}{% assignment_unlimited_args as var %}The result is: {{ var }}') - - t = template.Template('{% load custom %}{% assignment_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 as var %}The result is: {{ var }}') - self.assertEqual(t.render(c), 'The result is: assignment_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)", - template.Template, '{% load custom %}{% assignment_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 as var %}The result is: {{ var }}') - - six.assertRaisesRegex(self, template.TemplateSyntaxError, - "'assignment_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'", - template.Template, '{% load custom %}{% assignment_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" as var %}The result is: {{ var }}') - def test_assignment_tag_registration(self): # Test that the decorators preserve the decorated function's docstring, name and attributes. self.verify_tag(custom.assignment_no_params, 'assignment_no_params') - self.verify_tag(custom.assignment_one_param, 'assignment_one_param') - self.verify_tag(custom.assignment_explicit_no_context, 'assignment_explicit_no_context') - self.verify_tag(custom.assignment_no_params_with_context, 'assignment_no_params_with_context') - self.verify_tag(custom.assignment_params_and_context, 'assignment_params_and_context') - self.verify_tag(custom.assignment_one_default, 'assignment_one_default') - self.verify_tag(custom.assignment_two_params, 'assignment_two_params') - self.verify_tag(custom.assignment_unlimited_args, 'assignment_unlimited_args') - self.verify_tag(custom.assignment_only_unlimited_args, 'assignment_only_unlimited_args') - self.verify_tag(custom.assignment_unlimited_args, 'assignment_unlimited_args') - self.verify_tag(custom.assignment_unlimited_args_kwargs, 'assignment_unlimited_args_kwargs') - self.verify_tag(custom.assignment_tag_without_context_parameter, 'assignment_tag_without_context_parameter') def test_assignment_tag_missing_context(self): # The 'context' parameter must be present when takes_context is True -- cgit v1.3