Cleaned up formatting in template_tests.test_custom.
This commit is contained in:
parent
4d3470e52e
commit
23c4cbc0fa
|
@ -1,32 +1,34 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from unittest import TestCase
|
from django.template import Context, Template, TemplateSyntaxError
|
||||||
|
from django.test import SimpleTestCase, ignore_warnings
|
||||||
from django import template
|
|
||||||
from django.test import ignore_warnings
|
|
||||||
from django.utils import six
|
|
||||||
from django.utils.deprecation import RemovedInDjango20Warning
|
from django.utils.deprecation import RemovedInDjango20Warning
|
||||||
|
|
||||||
from .templatetags import custom, inclusion
|
from .templatetags import custom, inclusion
|
||||||
|
|
||||||
|
|
||||||
class CustomFilterTests(TestCase):
|
class CustomFilterTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_filter(self):
|
def test_filter(self):
|
||||||
t = template.Template("{% load custom %}{{ string|trim:5 }}")
|
t = Template("{% load custom %}{{ string|trim:5 }}")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
|
t.render(Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
|
||||||
"abcde"
|
"abcde"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class CustomTagTests(TestCase):
|
class TagTestCase(SimpleTestCase):
|
||||||
|
|
||||||
def verify_tag(self, tag, name):
|
def verify_tag(self, tag, name):
|
||||||
self.assertEqual(tag.__name__, name)
|
self.assertEqual(tag.__name__, name)
|
||||||
self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
|
self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
|
||||||
self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
|
self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleTagTests(TagTestCase):
|
||||||
|
|
||||||
def test_simple_tags(self):
|
def test_simple_tags(self):
|
||||||
c = template.Context({'value': 42})
|
c = Context({'value': 42})
|
||||||
|
|
||||||
templates = [
|
templates = [
|
||||||
('{% load custom %}{% no_params %}', 'no_params - Expected result'),
|
('{% load custom %}{% no_params %}', 'no_params - Expected result'),
|
||||||
|
@ -55,11 +57,11 @@ class CustomTagTests(TestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
for entry in templates:
|
for entry in templates:
|
||||||
t = template.Template(entry[0])
|
t = Template(entry[0])
|
||||||
self.assertEqual(t.render(c), entry[1])
|
self.assertEqual(t.render(c), entry[1])
|
||||||
|
|
||||||
for entry in templates:
|
for entry in templates:
|
||||||
t = template.Template("%s as var %%}Result: {{ var }}" % entry[0][0:-2])
|
t = Template("%s as var %%}Result: {{ var }}" % entry[0][0:-2])
|
||||||
self.assertEqual(t.render(c), "Result: %s" % entry[1])
|
self.assertEqual(t.render(c), "Result: %s" % entry[1])
|
||||||
|
|
||||||
def test_simple_tag_errors(self):
|
def test_simple_tag_errors(self):
|
||||||
|
@ -70,19 +72,19 @@ class CustomTagTests(TestCase):
|
||||||
'{% load custom %}{% simple_two_params 37 42 56 %}'),
|
'{% load custom %}{% simple_two_params 37 42 56 %}'),
|
||||||
("'simple_one_default' received too many positional arguments",
|
("'simple_one_default' received too many positional arguments",
|
||||||
'{% load custom %}{% simple_one_default 37 42 56 %}'),
|
'{% load custom %}{% simple_one_default 37 42 56 %}'),
|
||||||
("'simple_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)",
|
("'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 %}'),
|
'{% 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'",
|
("'simple_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
|
||||||
'{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}'),
|
'{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for entry in errors:
|
for entry in errors:
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError, entry[0], template.Template, entry[1])
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
||||||
|
Template(entry[1])
|
||||||
|
|
||||||
for entry in errors:
|
for entry in errors:
|
||||||
six.assertRaisesRegex(
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
||||||
self, template.TemplateSyntaxError, entry[0], template.Template, "%s as var %%}" % entry[1][0:-2],
|
Template("%s as var %%}" % entry[1][0:-2])
|
||||||
)
|
|
||||||
|
|
||||||
def test_simple_tag_registration(self):
|
def test_simple_tag_registration(self):
|
||||||
# Test that the decorators preserve the decorated function's docstring, name and attributes.
|
# Test that the decorators preserve the decorated function's docstring, name and attributes.
|
||||||
|
@ -96,130 +98,120 @@ class CustomTagTests(TestCase):
|
||||||
|
|
||||||
def test_simple_tag_missing_context(self):
|
def test_simple_tag_missing_context(self):
|
||||||
# The 'context' parameter must be present when takes_context is True
|
# The 'context' parameter must be present when takes_context is True
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
msg = (
|
||||||
"'simple_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
|
"'simple_tag_without_context_parameter' is decorated with "
|
||||||
template.Template, '{% load custom %}{% simple_tag_without_context_parameter 123 %}')
|
"takes_context=True so it must have a first argument of 'context'"
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||||
|
Template('{% load custom %}{% simple_tag_without_context_parameter 123 %}')
|
||||||
|
|
||||||
|
|
||||||
|
class InclusionTagTests(TagTestCase):
|
||||||
|
|
||||||
def test_inclusion_tags(self):
|
def test_inclusion_tags(self):
|
||||||
c = template.Context({'value': 42})
|
c = Context({'value': 42})
|
||||||
|
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_no_params %}')
|
templates = [
|
||||||
self.assertEqual(t.render(c), 'inclusion_no_params - Expected result\n')
|
('{% load inclusion %}{% inclusion_no_params %}', 'inclusion_no_params - Expected result\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_param 37 %}', 'inclusion_one_param - Expected result: 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_explicit_no_context 37 %}',
|
||||||
|
'inclusion_explicit_no_context - Expected result: 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_no_params_with_context %}',
|
||||||
|
'inclusion_no_params_with_context - Expected result (context value: 42)\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_params_and_context 37 %}',
|
||||||
|
'inclusion_params_and_context - Expected result (context value: 42): 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_two_params 37 42 %}',
|
||||||
|
'inclusion_two_params - Expected result: 37, 42\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default 37 %}', 'inclusion_one_default - Expected result: 37, hi\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default 37 two="hello" %}',
|
||||||
|
'inclusion_one_default - Expected result: 37, hello\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default one=99 two="hello" %}',
|
||||||
|
'inclusion_one_default - Expected result: 99, hello\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default 37 42 %}',
|
||||||
|
'inclusion_one_default - Expected result: 37, 42\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_unlimited_args 37 %}',
|
||||||
|
'inclusion_unlimited_args - Expected result: 37, hi\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_unlimited_args 37 42 56 89 %}',
|
||||||
|
'inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_only_unlimited_args %}',
|
||||||
|
'inclusion_only_unlimited_args - Expected result: \n'),
|
||||||
|
('{% load inclusion %}{% inclusion_only_unlimited_args 37 42 56 89 %}',
|
||||||
|
'inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}',
|
||||||
|
'inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4\n'),
|
||||||
|
]
|
||||||
|
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_one_param 37 %}')
|
for entry in templates:
|
||||||
self.assertEqual(t.render(c), 'inclusion_one_param - Expected result: 37\n')
|
t = Template(entry[0])
|
||||||
|
self.assertEqual(t.render(c), entry[1])
|
||||||
|
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_explicit_no_context 37 %}')
|
def test_inclusion_tag_errors(self):
|
||||||
self.assertEqual(t.render(c), 'inclusion_explicit_no_context - Expected result: 37\n')
|
errors = [
|
||||||
|
("'inclusion_one_default' received unexpected keyword argument 'three'",
|
||||||
|
'{% load inclusion %}{% inclusion_one_default 99 two="hello" three="foo" %}'),
|
||||||
|
("'inclusion_two_params' received too many positional arguments",
|
||||||
|
'{% load inclusion %}{% inclusion_two_params 37 42 56 %}'),
|
||||||
|
("'inclusion_one_default' received too many positional arguments",
|
||||||
|
'{% load inclusion %}{% inclusion_one_default 37 42 56 %}'),
|
||||||
|
("'inclusion_one_default' did not receive value(s) for the argument(s): 'one'",
|
||||||
|
'{% load inclusion %}{% inclusion_one_default %}'),
|
||||||
|
("'inclusion_unlimited_args' did not receive value(s) for the argument(s): 'one'",
|
||||||
|
'{% load inclusion %}{% inclusion_unlimited_args %}'),
|
||||||
|
(
|
||||||
|
"'inclusion_unlimited_args_kwargs' received some positional argument(s) "
|
||||||
|
"after some keyword argument(s)",
|
||||||
|
'{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 40|add:2 eggs="boiled" 56 four=1|add:3 %}',
|
||||||
|
),
|
||||||
|
("'inclusion_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
|
||||||
|
'{% load inclusion %}{% inclusion_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}'),
|
||||||
|
]
|
||||||
|
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_no_params_with_context %}')
|
for entry in errors:
|
||||||
self.assertEqual(t.render(c), 'inclusion_no_params_with_context - Expected result (context value: 42)\n')
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
||||||
|
Template(entry[1])
|
||||||
t = template.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 %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_two_params - Expected result: 37, 42\n')
|
|
||||||
|
|
||||||
t = template.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" %}')
|
|
||||||
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" %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_one_default - Expected result: 99, hello\n')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
|
||||||
"'inclusion_one_default' received unexpected keyword argument 'three'",
|
|
||||||
template.Template, '{% load inclusion %}{% inclusion_one_default 99 two="hello" three="foo" %}')
|
|
||||||
|
|
||||||
t = template.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 %}')
|
|
||||||
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 %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n')
|
|
||||||
|
|
||||||
t = template.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 %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
|
||||||
"'inclusion_two_params' received too many positional arguments",
|
|
||||||
template.Template, '{% load inclusion %}{% inclusion_two_params 37 42 56 %}')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
|
||||||
"'inclusion_one_default' received too many positional arguments",
|
|
||||||
template.Template, '{% load inclusion %}{% inclusion_one_default 37 42 56 %}')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
|
||||||
"'inclusion_one_default' did not receive value\(s\) for the argument\(s\): 'one'",
|
|
||||||
template.Template, '{% load inclusion %}{% inclusion_one_default %}')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
|
||||||
"'inclusion_unlimited_args' did not receive value\(s\) for the argument\(s\): 'one'",
|
|
||||||
template.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 %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4\n')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.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 %}')
|
|
||||||
|
|
||||||
six.assertRaisesRegex(self, template.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" %}')
|
|
||||||
|
|
||||||
def test_include_tag_missing_context(self):
|
def test_include_tag_missing_context(self):
|
||||||
# The 'context' parameter must be present when takes_context is True
|
# The 'context' parameter must be present when takes_context is True
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
msg = (
|
||||||
"'inclusion_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
|
"'inclusion_tag_without_context_parameter' is decorated with "
|
||||||
template.Template, '{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}')
|
"takes_context=True so it must have a first argument of 'context'"
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||||
|
Template('{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}')
|
||||||
|
|
||||||
def test_inclusion_tags_from_template(self):
|
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 %}')
|
templates = [
|
||||||
self.assertEqual(t.render(c), 'inclusion_no_params_from_template - Expected result\n')
|
('{% load inclusion %}{% inclusion_no_params_from_template %}',
|
||||||
|
'inclusion_no_params_from_template - Expected result\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_param_from_template 37 %}',
|
||||||
|
'inclusion_one_param_from_template - Expected result: 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_explicit_no_context_from_template 37 %}',
|
||||||
|
'inclusion_explicit_no_context_from_template - Expected result: 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_no_params_with_context_from_template %}',
|
||||||
|
'inclusion_no_params_with_context_from_template - Expected result (context value: 42)\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_params_and_context_from_template 37 %}',
|
||||||
|
'inclusion_params_and_context_from_template - Expected result (context value: 42): 37\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_two_params_from_template 37 42 %}',
|
||||||
|
'inclusion_two_params_from_template - Expected result: 37, 42\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default_from_template 37 %}',
|
||||||
|
'inclusion_one_default_from_template - Expected result: 37, hi\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_one_default_from_template 37 42 %}',
|
||||||
|
'inclusion_one_default_from_template - Expected result: 37, 42\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 %}',
|
||||||
|
'inclusion_unlimited_args_from_template - Expected result: 37, hi\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_unlimited_args_from_template 37 42 56 89 %}',
|
||||||
|
'inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n'),
|
||||||
|
('{% load inclusion %}{% inclusion_only_unlimited_args_from_template %}',
|
||||||
|
'inclusion_only_unlimited_args_from_template - Expected result: \n'),
|
||||||
|
('{% load inclusion %}{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}',
|
||||||
|
'inclusion_only_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n'),
|
||||||
|
]
|
||||||
|
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_one_param_from_template 37 %}')
|
for entry in templates:
|
||||||
self.assertEqual(t.render(c), 'inclusion_one_param_from_template - Expected result: 37\n')
|
t = Template(entry[0])
|
||||||
|
self.assertEqual(t.render(c), entry[1])
|
||||||
t = template.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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
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 %}')
|
|
||||||
self.assertEqual(t.render(c), 'inclusion_only_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n')
|
|
||||||
|
|
||||||
def test_inclusion_tag_registration(self):
|
def test_inclusion_tag_registration(self):
|
||||||
# Test that the decorators preserve the decorated function's docstring, name and attributes.
|
# Test that the decorators preserve the decorated function's docstring, name and attributes.
|
||||||
|
@ -243,12 +235,12 @@ class CustomTagTests(TestCase):
|
||||||
Test that inclusion tag passes down `current_app` of context to the
|
Test that inclusion tag passes down `current_app` of context to the
|
||||||
Context of the included/rendered template as well.
|
Context of the included/rendered template as well.
|
||||||
"""
|
"""
|
||||||
c = template.Context({})
|
c = Context({})
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_tag_current_app %}')
|
t = Template('{% load inclusion %}{% inclusion_tag_current_app %}')
|
||||||
self.assertEqual(t.render(c).strip(), 'None')
|
self.assertEqual(t.render(c).strip(), 'None')
|
||||||
|
|
||||||
# That part produces the deprecation warning
|
# That part produces the deprecation warning
|
||||||
c = template.Context({}, current_app='advanced')
|
c = Context({}, current_app='advanced')
|
||||||
self.assertEqual(t.render(c).strip(), 'advanced')
|
self.assertEqual(t.render(c).strip(), 'advanced')
|
||||||
|
|
||||||
def test_15070_use_l10n(self):
|
def test_15070_use_l10n(self):
|
||||||
|
@ -256,17 +248,20 @@ class CustomTagTests(TestCase):
|
||||||
Test that inclusion tag passes down `use_l10n` of context to the
|
Test that inclusion tag passes down `use_l10n` of context to the
|
||||||
Context of the included/rendered template as well.
|
Context of the included/rendered template as well.
|
||||||
"""
|
"""
|
||||||
c = template.Context({})
|
c = Context({})
|
||||||
t = template.Template('{% load inclusion %}{% inclusion_tag_use_l10n %}')
|
t = Template('{% load inclusion %}{% inclusion_tag_use_l10n %}')
|
||||||
self.assertEqual(t.render(c).strip(), 'None')
|
self.assertEqual(t.render(c).strip(), 'None')
|
||||||
|
|
||||||
c.use_l10n = True
|
c.use_l10n = True
|
||||||
self.assertEqual(t.render(c).strip(), 'True')
|
self.assertEqual(t.render(c).strip(), 'True')
|
||||||
|
|
||||||
def test_assignment_tags(self):
|
|
||||||
c = template.Context({'value': 42})
|
|
||||||
|
|
||||||
t = template.Template('{% load custom %}{% assignment_no_params as var %}The result is: {{ var }}')
|
class AssignmentTagTests(TagTestCase):
|
||||||
|
|
||||||
|
def test_assignment_tags(self):
|
||||||
|
c = Context({'value': 42})
|
||||||
|
|
||||||
|
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')
|
self.assertEqual(t.render(c), 'The result is: assignment_no_params - Expected result')
|
||||||
|
|
||||||
def test_assignment_tag_registration(self):
|
def test_assignment_tag_registration(self):
|
||||||
|
@ -275,6 +270,9 @@ class CustomTagTests(TestCase):
|
||||||
|
|
||||||
def test_assignment_tag_missing_context(self):
|
def test_assignment_tag_missing_context(self):
|
||||||
# The 'context' parameter must be present when takes_context is True
|
# The 'context' parameter must be present when takes_context is True
|
||||||
six.assertRaisesRegex(self, template.TemplateSyntaxError,
|
msg = (
|
||||||
"'assignment_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
|
"'assignment_tag_without_context_parameter' is decorated with "
|
||||||
template.Template, '{% load custom %}{% assignment_tag_without_context_parameter 123 as var %}')
|
"takes_context=True so it must have a first argument of 'context'"
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||||
|
Template('{% load custom %}{% assignment_tag_without_context_parameter 123 as var %}')
|
||||||
|
|
Loading…
Reference in New Issue