diff --git a/tests/template_tests/test_custom.py b/tests/template_tests/test_custom.py index 77d089661f..8cf45b68d5 100644 --- a/tests/template_tests/test_custom.py +++ b/tests/template_tests/test_custom.py @@ -1,97 +1,99 @@ from __future__ import unicode_literals -from unittest import TestCase +import os -from django import template -from django.test import ignore_warnings +from django.template import Context, Template, TemplateSyntaxError +from django.test import SimpleTestCase, ignore_warnings +from django.test.utils import extend_sys_path from django.utils import six from django.utils.deprecation import RemovedInDjango20Warning from .templatetags import custom, inclusion +from .utils import ROOT -class CustomFilterTests(TestCase): +class CustomFilterTests(SimpleTestCase): def test_filter(self): - t = template.Template("{% load custom %}{{ string|trim:5 }}") + t = Template("{% load custom %}{{ string|trim:5 }}") self.assertEqual( - t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})), + t.render(Context({"string": "abcdefghijklmnopqrstuvwxyz"})), "abcde" ) -class CustomTagTests(TestCase): +class CustomTagTests(SimpleTestCase): def verify_tag(self, tag, name): self.assertEqual(tag.__name__, name) self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name) self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name) def test_simple_tags(self): - c = template.Context({'value': 42}) + c = Context({'value': 42}) - t = template.Template('{% load custom %}{% no_params %}') + t = Template('{% load custom %}{% no_params %}') self.assertEqual(t.render(c), 'no_params - Expected result') - t = template.Template('{% load custom %}{% one_param 37 %}') + t = 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 %}') + t = 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 %}') + t = 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 %}') + t = 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 %}') + t = 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 %}') + t = 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" %}') + t = 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" %}') + t = 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, + six.assertRaisesRegex(self, TemplateSyntaxError, "'simple_one_default' received unexpected keyword argument 'three'", - template.Template, '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}') + Template, '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}') - t = template.Template('{% load custom %}{% simple_one_default 37 42 %}') + t = 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 %}') + t = 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 %}') + t = 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 %}') + t = 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 %}') + t = 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, + six.assertRaisesRegex(self, TemplateSyntaxError, "'simple_two_params' received too many positional arguments", - template.Template, '{% load custom %}{% simple_two_params 37 42 56 %}') + Template, '{% load custom %}{% simple_two_params 37 42 56 %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'simple_one_default' received too many positional arguments", - template.Template, '{% load custom %}{% simple_one_default 37 42 56 %}') + 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 %}') + t = 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, + six.assertRaisesRegex(self, 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 %}') + Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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" %}') + Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}') def test_simple_tag_registration(self): # Test that the decorators preserve the decorated function's docstring, name and attributes. @@ -105,129 +107,129 @@ class CustomTagTests(TestCase): def test_simple_tag_missing_context(self): # The 'context' parameter must be present when takes_context is True - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'simple_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'", - template.Template, '{% load custom %}{% simple_tag_without_context_parameter 123 %}') + Template, '{% load custom %}{% simple_tag_without_context_parameter 123 %}') def test_inclusion_tags(self): - c = template.Context({'value': 42}) + c = Context({'value': 42}) - t = template.Template('{% load inclusion %}{% inclusion_no_params %}') + t = Template('{% load inclusion %}{% inclusion_no_params %}') self.assertEqual(t.render(c), 'inclusion_no_params - Expected result\n') - t = template.Template('{% load inclusion %}{% inclusion_one_param 37 %}') + t = Template('{% load inclusion %}{% inclusion_one_param 37 %}') self.assertEqual(t.render(c), 'inclusion_one_param - Expected result: 37\n') - t = template.Template('{% load inclusion %}{% inclusion_explicit_no_context 37 %}') + t = Template('{% load inclusion %}{% inclusion_explicit_no_context 37 %}') self.assertEqual(t.render(c), 'inclusion_explicit_no_context - Expected result: 37\n') - t = template.Template('{% load inclusion %}{% inclusion_no_params_with_context %}') + t = Template('{% load inclusion %}{% inclusion_no_params_with_context %}') self.assertEqual(t.render(c), 'inclusion_no_params_with_context - Expected result (context value: 42)\n') - t = template.Template('{% load inclusion %}{% inclusion_params_and_context 37 %}') + t = Template('{% load inclusion %}{% inclusion_params_and_context 37 %}') self.assertEqual(t.render(c), 'inclusion_params_and_context - Expected result (context value: 42): 37\n') - t = template.Template('{% load inclusion %}{% inclusion_two_params 37 42 %}') + t = Template('{% load inclusion %}{% inclusion_two_params 37 42 %}') self.assertEqual(t.render(c), 'inclusion_two_params - Expected result: 37, 42\n') - t = template.Template('{% load inclusion %}{% inclusion_one_default 37 %}') + t = Template('{% load inclusion %}{% inclusion_one_default 37 %}') self.assertEqual(t.render(c), 'inclusion_one_default - Expected result: 37, hi\n') - t = template.Template('{% load inclusion %}{% inclusion_one_default 37 two="hello" %}') + t = Template('{% load inclusion %}{% inclusion_one_default 37 two="hello" %}') self.assertEqual(t.render(c), 'inclusion_one_default - Expected result: 37, hello\n') - t = template.Template('{% load inclusion %}{% inclusion_one_default one=99 two="hello" %}') + t = Template('{% load inclusion %}{% inclusion_one_default one=99 two="hello" %}') self.assertEqual(t.render(c), 'inclusion_one_default - Expected result: 99, hello\n') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_one_default' received unexpected keyword argument 'three'", - template.Template, '{% load inclusion %}{% inclusion_one_default 99 two="hello" three="foo" %}') + Template, '{% load inclusion %}{% inclusion_one_default 99 two="hello" three="foo" %}') - t = template.Template('{% load inclusion %}{% inclusion_one_default 37 42 %}') + t = Template('{% load inclusion %}{% inclusion_one_default 37 42 %}') self.assertEqual(t.render(c), 'inclusion_one_default - Expected result: 37, 42\n') - t = template.Template('{% load inclusion %}{% inclusion_unlimited_args 37 %}') + t = Template('{% load inclusion %}{% inclusion_unlimited_args 37 %}') self.assertEqual(t.render(c), 'inclusion_unlimited_args - Expected result: 37, hi\n') - t = template.Template('{% load inclusion %}{% inclusion_unlimited_args 37 42 56 89 %}') + t = Template('{% load inclusion %}{% inclusion_unlimited_args 37 42 56 89 %}') self.assertEqual(t.render(c), 'inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n') - t = template.Template('{% load inclusion %}{% inclusion_only_unlimited_args %}') + t = Template('{% load inclusion %}{% inclusion_only_unlimited_args %}') self.assertEqual(t.render(c), 'inclusion_only_unlimited_args - Expected result: \n') - t = template.Template('{% load inclusion %}{% inclusion_only_unlimited_args 37 42 56 89 %}') + t = Template('{% load inclusion %}{% inclusion_only_unlimited_args 37 42 56 89 %}') self.assertEqual(t.render(c), 'inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_two_params' received too many positional arguments", - template.Template, '{% load inclusion %}{% inclusion_two_params 37 42 56 %}') + Template, '{% load inclusion %}{% inclusion_two_params 37 42 56 %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_one_default' received too many positional arguments", - template.Template, '{% load inclusion %}{% inclusion_one_default 37 42 56 %}') + Template, '{% load inclusion %}{% inclusion_one_default 37 42 56 %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_one_default' did not receive value\(s\) for the argument\(s\): 'one'", - template.Template, '{% load inclusion %}{% inclusion_one_default %}') + Template, '{% load inclusion %}{% inclusion_one_default %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_unlimited_args' did not receive value\(s\) for the argument\(s\): 'one'", - template.Template, '{% load inclusion %}{% inclusion_unlimited_args %}') + Template, '{% load inclusion %}{% inclusion_unlimited_args %}') - t = template.Template('{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}') + t = Template('{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}') self.assertEqual(t.render(c), 'inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4\n') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)", - template.Template, '{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}') + Template, '{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'", - template.Template, '{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}') + Template, '{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}') def test_include_tag_missing_context(self): # The 'context' parameter must be present when takes_context is True - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'inclusion_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'", - template.Template, '{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}') + Template, '{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}') def test_inclusion_tags_from_template(self): - c = template.Context({'value': 42}) + c = Context({'value': 42}) - t = template.Template('{% load inclusion %}{% inclusion_no_params_from_template %}') + t = Template('{% load inclusion %}{% inclusion_no_params_from_template %}') self.assertEqual(t.render(c), 'inclusion_no_params_from_template - Expected result\n') - t = template.Template('{% load inclusion %}{% inclusion_one_param_from_template 37 %}') + t = Template('{% load inclusion %}{% inclusion_one_param_from_template 37 %}') self.assertEqual(t.render(c), 'inclusion_one_param_from_template - Expected result: 37\n') - t = template.Template('{% load inclusion %}{% inclusion_explicit_no_context_from_template 37 %}') + t = Template('{% load inclusion %}{% inclusion_explicit_no_context_from_template 37 %}') self.assertEqual(t.render(c), 'inclusion_explicit_no_context_from_template - Expected result: 37\n') - t = template.Template('{% load inclusion %}{% inclusion_no_params_with_context_from_template %}') + t = Template('{% load inclusion %}{% inclusion_no_params_with_context_from_template %}') self.assertEqual(t.render(c), 'inclusion_no_params_with_context_from_template - Expected result (context value: 42)\n') - t = template.Template('{% load inclusion %}{% inclusion_params_and_context_from_template 37 %}') + t = Template('{% load inclusion %}{% inclusion_params_and_context_from_template 37 %}') self.assertEqual(t.render(c), 'inclusion_params_and_context_from_template - Expected result (context value: 42): 37\n') - t = template.Template('{% load inclusion %}{% inclusion_two_params_from_template 37 42 %}') + t = Template('{% load inclusion %}{% inclusion_two_params_from_template 37 42 %}') self.assertEqual(t.render(c), 'inclusion_two_params_from_template - Expected result: 37, 42\n') - t = template.Template('{% load inclusion %}{% inclusion_one_default_from_template 37 %}') + t = Template('{% load inclusion %}{% inclusion_one_default_from_template 37 %}') self.assertEqual(t.render(c), 'inclusion_one_default_from_template - Expected result: 37, hi\n') - t = template.Template('{% load inclusion %}{% inclusion_one_default_from_template 37 42 %}') + t = Template('{% load inclusion %}{% inclusion_one_default_from_template 37 42 %}') self.assertEqual(t.render(c), 'inclusion_one_default_from_template - Expected result: 37, 42\n') - t = template.Template('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 %}') + t = Template('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 %}') self.assertEqual(t.render(c), 'inclusion_unlimited_args_from_template - Expected result: 37, hi\n') - t = template.Template('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 42 56 89 %}') + t = Template('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 42 56 89 %}') self.assertEqual(t.render(c), 'inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n') - t = template.Template('{% load inclusion %}{% inclusion_only_unlimited_args_from_template %}') + t = Template('{% load inclusion %}{% inclusion_only_unlimited_args_from_template %}') self.assertEqual(t.render(c), 'inclusion_only_unlimited_args_from_template - Expected result: \n') - t = template.Template('{% load inclusion %}{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}') + t = Template('{% load inclusion %}{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}') self.assertEqual(t.render(c), 'inclusion_only_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n') def test_inclusion_tag_registration(self): @@ -252,12 +254,12 @@ class CustomTagTests(TestCase): Test that inclusion tag passes down `current_app` of context to the Context of the included/rendered template as well. """ - c = template.Context({}) - t = template.Template('{% load inclusion %}{% inclusion_tag_current_app %}') + c = Context({}) + t = Template('{% load inclusion %}{% inclusion_tag_current_app %}') self.assertEqual(t.render(c).strip(), 'None') # That part produces the deprecation warning - c = template.Context({}, current_app='advanced') + c = Context({}, current_app='advanced') self.assertEqual(t.render(c).strip(), 'advanced') def test_15070_use_l10n(self): @@ -265,100 +267,100 @@ class CustomTagTests(TestCase): Test that inclusion tag passes down `use_l10n` of context to the Context of the included/rendered template as well. """ - c = template.Context({}) - t = template.Template('{% load inclusion %}{% inclusion_tag_use_l10n %}') + c = Context({}) + t = Template('{% load inclusion %}{% inclusion_tag_use_l10n %}') self.assertEqual(t.render(c).strip(), 'None') c.use_l10n = True self.assertEqual(t.render(c).strip(), 'True') def test_assignment_tags(self): - c = template.Context({'value': 42}) + c = Context({'value': 42}) - t = template.Template('{% load custom %}{% assignment_no_params as var %}The result is: {{ var }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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, + six.assertRaisesRegex(self, TemplateSyntaxError, "'assignment_one_default' received unexpected keyword argument 'three'", - template.Template, '{% load custom %}{% assignment_one_default 99 two="hello" three="foo" as var %}') + 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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 }}') + t = 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, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_one_param 37 %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_one_param 37 as %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_one_param 37 ass var %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_two_params 37 42 56 as var %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_one_default 37 42 56 as var %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + Template, '{% load custom %}{% assignment_one_default as var %}The result is: {{ var }}') - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, 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 }}') + 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 }}') + t = 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, + six.assertRaisesRegex(self, 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 }}') + 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, + six.assertRaisesRegex(self, 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 }}') + 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. @@ -377,6 +379,41 @@ class CustomTagTests(TestCase): def test_assignment_tag_missing_context(self): # The 'context' parameter must be present when takes_context is True - six.assertRaisesRegex(self, template.TemplateSyntaxError, + six.assertRaisesRegex(self, TemplateSyntaxError, "'assignment_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'", - template.Template, '{% load custom %}{% assignment_tag_without_context_parameter 123 as var %}') + Template, '{% load custom %}{% assignment_tag_without_context_parameter 123 as var %}') + + +class TemplateTagLoadingTests(SimpleTestCase): + + def setUp(self): + self.egg_dir = os.path.join(ROOT, 'eggs') + + def test_load_error(self): + ttext = "{% load broken_tag %}" + with self.assertRaises(TemplateSyntaxError) as e: + Template(ttext) + + self.assertIn('ImportError', e.exception.args[0]) + self.assertIn('Xtemplate', e.exception.args[0]) + + def test_load_error_egg(self): + ttext = "{% load broken_egg %}" + egg_name = '%s/tagsegg.egg' % self.egg_dir + with extend_sys_path(egg_name): + with self.assertRaises(TemplateSyntaxError): + with self.settings(INSTALLED_APPS=['tagsegg']): + Template(ttext) + try: + with self.settings(INSTALLED_APPS=['tagsegg']): + Template(ttext) + except TemplateSyntaxError as e: + self.assertIn('ImportError', e.args[0]) + self.assertIn('Xtemplate', e.args[0]) + + def test_load_working_egg(self): + ttext = "{% load working_egg %}" + egg_name = '%s/tagsegg.egg' % self.egg_dir + with extend_sys_path(egg_name): + with self.settings(INSTALLED_APPS=['tagsegg']): + Template(ttext) diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index a49efcad4f..62902773d3 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -12,8 +12,7 @@ from django.template import ( Context, RequestContext, Template, TemplateSyntaxError, base as template_base, engines, loader, ) -from django.test import RequestFactory, SimpleTestCase -from django.test.utils import extend_sys_path, override_settings +from django.test import RequestFactory, SimpleTestCase, override_settings from django.utils._os import upath TEMPLATES_DIR = os.path.join(os.path.dirname(upath(__file__)), 'templates') @@ -183,42 +182,6 @@ class TemplateRegressionTests(SimpleTestCase): self.assertEqual(child.render({'parent': parent}), 'child') -class TemplateTagLoading(SimpleTestCase): - - def setUp(self): - self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__)) - - def test_load_error(self): - ttext = "{% load broken_tag %}" - self.assertRaises(template.TemplateSyntaxError, template.Template, ttext) - try: - template.Template(ttext) - except template.TemplateSyntaxError as e: - self.assertIn('ImportError', e.args[0]) - self.assertIn('Xtemplate', e.args[0]) - - def test_load_error_egg(self): - ttext = "{% load broken_egg %}" - egg_name = '%s/tagsegg.egg' % self.egg_dir - with extend_sys_path(egg_name): - with self.assertRaises(template.TemplateSyntaxError): - with self.settings(INSTALLED_APPS=['tagsegg']): - template.Template(ttext) - try: - with self.settings(INSTALLED_APPS=['tagsegg']): - template.Template(ttext) - except template.TemplateSyntaxError as e: - self.assertIn('ImportError', e.args[0]) - self.assertIn('Xtemplate', e.args[0]) - - def test_load_working_egg(self): - ttext = "{% load working_egg %}" - egg_name = '%s/tagsegg.egg' % self.egg_dir - with extend_sys_path(egg_name): - with self.settings(INSTALLED_APPS=['tagsegg']): - template.Template(ttext) - - class RequestContextTests(unittest.TestCase): def setUp(self):