2015-02-22 03:13:26 +08:00
|
|
|
import os
|
|
|
|
|
2015-04-12 07:41:45 +08:00
|
|
|
from django.template import Context, Engine, TemplateSyntaxError
|
2015-04-01 10:57:26 +08:00
|
|
|
from django.template.base import Node
|
2015-05-09 04:10:36 +08:00
|
|
|
from django.template.library import InvalidTemplateLibrary
|
2015-09-04 03:52:04 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-02-22 03:13:26 +08:00
|
|
|
from django.test.utils import extend_sys_path
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2015-01-09 23:36:36 +08:00
|
|
|
from .templatetags import custom, inclusion
|
2015-02-22 03:13:26 +08:00
|
|
|
from .utils import ROOT
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
LIBRARIES = {
|
|
|
|
'custom': 'template_tests.templatetags.custom',
|
|
|
|
'inclusion': 'template_tests.templatetags.inclusion',
|
|
|
|
}
|
|
|
|
|
2009-03-25 11:45:56 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
class CustomFilterTests(SimpleTestCase):
|
|
|
|
|
2010-11-04 12:47:05 +08:00
|
|
|
def test_filter(self):
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(libraries=LIBRARIES)
|
|
|
|
t = engine.from_string("{% load custom %}{{ string|trim:5 }}")
|
2010-11-04 12:47:05 +08:00
|
|
|
self.assertEqual(
|
2015-02-04 04:41:51 +08:00
|
|
|
t.render(Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
|
2012-06-08 00:08:47 +08:00
|
|
|
"abcde"
|
2010-11-04 12:47:05 +08:00
|
|
|
)
|
2010-12-19 23:00:50 +08:00
|
|
|
|
2018-05-25 23:11:46 +08:00
|
|
|
def test_decorated_filter(self):
|
|
|
|
engine = Engine(libraries=LIBRARIES)
|
|
|
|
t = engine.from_string('{% load custom %}{{ name|make_data_div }}')
|
|
|
|
self.assertEqual(t.render(Context({'name': 'foo'})), '<div data-name="foo"></div>')
|
|
|
|
|
2010-12-19 23:00:50 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
class TagTestCase(SimpleTestCase):
|
|
|
|
|
2015-04-12 07:41:45 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2015-05-09 04:10:36 +08:00
|
|
|
cls.engine = Engine(app_dirs=True, libraries=LIBRARIES)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2015-04-12 07:41:45 +08:00
|
|
|
|
2010-12-19 23:00:50 +08:00
|
|
|
def verify_tag(self, tag, name):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(tag.__name__, name)
|
|
|
|
self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
|
|
|
|
self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
|
2010-12-19 23:00:50 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
|
|
|
|
class SimpleTagTests(TagTestCase):
|
|
|
|
|
2010-12-19 23:00:50 +08:00
|
|
|
def test_simple_tags(self):
|
2015-02-04 04:41:51 +08:00
|
|
|
c = Context({'value': 42})
|
2010-12-19 23:00:50 +08:00
|
|
|
|
2015-01-17 07:34:32 +08:00
|
|
|
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'),
|
2017-05-11 22:09:44 +08:00
|
|
|
('{% load custom %}{% simple_keyword_only_param kwarg=37 %}',
|
|
|
|
'simple_keyword_only_param - Expected result: 37'),
|
|
|
|
('{% load custom %}{% simple_keyword_only_default %}',
|
|
|
|
'simple_keyword_only_default - Expected result: 42'),
|
2019-12-30 05:04:14 +08:00
|
|
|
(
|
|
|
|
'{% load custom %}{% simple_keyword_only_default kwarg=37 %}',
|
|
|
|
'simple_keyword_only_default - Expected result: 37',
|
|
|
|
),
|
2015-01-17 07:34:32 +08:00
|
|
|
('{% 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:
|
2015-04-12 07:41:45 +08:00
|
|
|
t = self.engine.from_string(entry[0])
|
2015-01-17 07:34:32 +08:00
|
|
|
self.assertEqual(t.render(c), entry[1])
|
|
|
|
|
|
|
|
for entry in templates:
|
2015-04-12 07:41:45 +08:00
|
|
|
t = self.engine.from_string("%s as var %%}Result: {{ var }}" % entry[0][0:-2])
|
2015-01-17 07:34:32 +08:00
|
|
|
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 %}'),
|
2017-05-11 22:09:44 +08:00
|
|
|
("'simple_keyword_only_param' did not receive value(s) for the argument(s): 'kwarg'",
|
|
|
|
'{% load custom %}{% simple_keyword_only_param %}'),
|
2019-12-30 05:04:14 +08:00
|
|
|
(
|
|
|
|
"'simple_keyword_only_param' received multiple values for "
|
|
|
|
"keyword argument 'kwarg'",
|
|
|
|
'{% load custom %}{% simple_keyword_only_param kwarg=42 '
|
|
|
|
'kwarg=37 %}',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"'simple_keyword_only_default' received multiple values for "
|
|
|
|
"keyword argument 'kwarg'",
|
|
|
|
'{% load custom %}{% simple_keyword_only_default kwarg=42 '
|
|
|
|
'kwarg=37 %}',
|
|
|
|
),
|
2015-02-04 04:41:51 +08:00
|
|
|
("'simple_unlimited_args_kwargs' received some positional argument(s) after some keyword argument(s)",
|
2015-01-17 07:34:32 +08:00
|
|
|
'{% 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:
|
2015-02-04 04:41:51 +08:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
2015-04-12 07:41:45 +08:00
|
|
|
self.engine.from_string(entry[1])
|
2015-01-17 07:34:32 +08:00
|
|
|
|
|
|
|
for entry in errors:
|
2015-02-04 04:41:51 +08:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
2015-04-12 07:41:45 +08:00
|
|
|
self.engine.from_string("%s as var %%}" % entry[1][0:-2])
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-06-15 18:17:09 +08:00
|
|
|
def test_simple_tag_escaping_autoescape_off(self):
|
|
|
|
c = Context({'name': "Jack & Jill"}, autoescape=False)
|
|
|
|
t = self.engine.from_string("{% load custom %}{% escape_naive %}")
|
|
|
|
self.assertEqual(t.render(c), "Hello Jack & Jill!")
|
|
|
|
|
|
|
|
def test_simple_tag_naive_escaping(self):
|
|
|
|
c = Context({'name': "Jack & Jill"})
|
|
|
|
t = self.engine.from_string("{% load custom %}{% escape_naive %}")
|
|
|
|
self.assertEqual(t.render(c), "Hello Jack & Jill!")
|
|
|
|
|
|
|
|
def test_simple_tag_explicit_escaping(self):
|
|
|
|
# Check we don't double escape
|
|
|
|
c = Context({'name': "Jack & Jill"})
|
|
|
|
t = self.engine.from_string("{% load custom %}{% escape_explicit %}")
|
|
|
|
self.assertEqual(t.render(c), "Hello Jack & Jill!")
|
|
|
|
|
|
|
|
def test_simple_tag_format_html_escaping(self):
|
|
|
|
# Check we don't double escape
|
|
|
|
c = Context({'name': "Jack & Jill"})
|
|
|
|
t = self.engine.from_string("{% load custom %}{% escape_format_html %}")
|
|
|
|
self.assertEqual(t.render(c), "Hello Jack & Jill!")
|
|
|
|
|
2010-12-19 23:00:50 +08:00
|
|
|
def test_simple_tag_registration(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# The decorators preserve the decorated function's docstring, name,
|
|
|
|
# and attributes.
|
2010-12-19 23:00:50 +08:00
|
|
|
self.verify_tag(custom.no_params, 'no_params')
|
|
|
|
self.verify_tag(custom.one_param, 'one_param')
|
|
|
|
self.verify_tag(custom.explicit_no_context, 'explicit_no_context')
|
|
|
|
self.verify_tag(custom.no_params_with_context, 'no_params_with_context')
|
|
|
|
self.verify_tag(custom.params_and_context, 'params_and_context')
|
2011-09-27 20:15:15 +08:00
|
|
|
self.verify_tag(custom.simple_unlimited_args_kwargs, 'simple_unlimited_args_kwargs')
|
|
|
|
self.verify_tag(custom.simple_tag_without_context_parameter, 'simple_tag_without_context_parameter')
|
2010-12-19 23:00:50 +08:00
|
|
|
|
|
|
|
def test_simple_tag_missing_context(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
# The 'context' parameter must be present when takes_context is True
|
2015-02-04 04:41:51 +08:00
|
|
|
msg = (
|
|
|
|
"'simple_tag_without_context_parameter' is decorated with "
|
|
|
|
"takes_context=True so it must have a first argument of 'context'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2015-04-12 07:41:45 +08:00
|
|
|
self.engine.from_string('{% load custom %}{% simple_tag_without_context_parameter 123 %}')
|
2011-09-27 20:15:15 +08:00
|
|
|
|
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
class InclusionTagTests(TagTestCase):
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
def test_inclusion_tags(self):
|
|
|
|
c = Context({'value': 42})
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
templates = [
|
|
|
|
('{% 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'),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
'{% load inclusion %}{% inclusion_one_default 37 %}',
|
|
|
|
'inclusion_one_default - Expected result: 37, hi\n'
|
|
|
|
),
|
2015-02-04 04:41:51 +08:00
|
|
|
('{% 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'),
|
2019-12-30 05:04:14 +08:00
|
|
|
(
|
|
|
|
'{% load inclusion %}{% inclusion_keyword_only_default kwarg=37 %}',
|
|
|
|
'inclusion_keyword_only_default - Expected result: 37\n',
|
|
|
|
),
|
2015-02-04 04:41:51 +08:00
|
|
|
('{% 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'),
|
|
|
|
]
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
for entry in templates:
|
2015-04-12 07:41:45 +08:00
|
|
|
t = self.engine.from_string(entry[0])
|
2015-02-04 04:41:51 +08:00
|
|
|
self.assertEqual(t.render(c), entry[1])
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
def test_inclusion_tag_errors(self):
|
|
|
|
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 %}'),
|
2019-12-30 05:04:14 +08:00
|
|
|
(
|
|
|
|
"'inclusion_keyword_only_default' received multiple values "
|
|
|
|
"for keyword argument 'kwarg'",
|
|
|
|
'{% load inclusion %}{% inclusion_keyword_only_default '
|
|
|
|
'kwarg=37 kwarg=42 %}',
|
|
|
|
),
|
2015-02-04 04:41:51 +08:00
|
|
|
("'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" %}'),
|
|
|
|
]
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
for entry in errors:
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
|
2015-04-12 07:41:45 +08:00
|
|
|
self.engine.from_string(entry[1])
|
2011-09-27 20:15:15 +08:00
|
|
|
|
|
|
|
def test_include_tag_missing_context(self):
|
|
|
|
# The 'context' parameter must be present when takes_context is True
|
2015-02-04 04:41:51 +08:00
|
|
|
msg = (
|
|
|
|
"'inclusion_tag_without_context_parameter' is decorated with "
|
|
|
|
"takes_context=True so it must have a first argument of 'context'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2015-04-12 07:41:45 +08:00
|
|
|
self.engine.from_string('{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}')
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2011-06-12 00:22:45 +08:00
|
|
|
def test_inclusion_tags_from_template(self):
|
2015-02-04 04:41:51 +08:00
|
|
|
c = Context({'value': 42})
|
2011-06-12 00:22:45 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
templates = [
|
|
|
|
('{% 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'),
|
|
|
|
]
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
for entry in templates:
|
2015-04-12 07:41:45 +08:00
|
|
|
t = self.engine.from_string(entry[0])
|
2015-02-04 04:41:51 +08:00
|
|
|
self.assertEqual(t.render(c), entry[1])
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2011-03-15 16:20:14 +08:00
|
|
|
def test_inclusion_tag_registration(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# The decorators preserve the decorated function's docstring, name,
|
|
|
|
# and attributes.
|
2015-01-09 23:36:36 +08:00
|
|
|
self.verify_tag(inclusion.inclusion_no_params, 'inclusion_no_params')
|
|
|
|
self.verify_tag(inclusion.inclusion_one_param, 'inclusion_one_param')
|
|
|
|
self.verify_tag(inclusion.inclusion_explicit_no_context, 'inclusion_explicit_no_context')
|
|
|
|
self.verify_tag(inclusion.inclusion_no_params_with_context, 'inclusion_no_params_with_context')
|
|
|
|
self.verify_tag(inclusion.inclusion_params_and_context, 'inclusion_params_and_context')
|
|
|
|
self.verify_tag(inclusion.inclusion_two_params, 'inclusion_two_params')
|
|
|
|
self.verify_tag(inclusion.inclusion_one_default, 'inclusion_one_default')
|
|
|
|
self.verify_tag(inclusion.inclusion_unlimited_args, 'inclusion_unlimited_args')
|
|
|
|
self.verify_tag(inclusion.inclusion_only_unlimited_args, 'inclusion_only_unlimited_args')
|
|
|
|
self.verify_tag(inclusion.inclusion_tag_without_context_parameter, 'inclusion_tag_without_context_parameter')
|
|
|
|
self.verify_tag(inclusion.inclusion_tag_use_l10n, 'inclusion_tag_use_l10n')
|
|
|
|
self.verify_tag(inclusion.inclusion_unlimited_args_kwargs, 'inclusion_unlimited_args_kwargs')
|
2011-04-28 21:41:28 +08:00
|
|
|
|
|
|
|
def test_15070_use_l10n(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Inclusion tag passes down `use_l10n` of context to the
|
2011-04-28 21:41:28 +08:00
|
|
|
Context of the included/rendered template as well.
|
|
|
|
"""
|
2015-02-04 04:41:51 +08:00
|
|
|
c = Context({})
|
2015-04-12 07:41:45 +08:00
|
|
|
t = self.engine.from_string('{% load inclusion %}{% inclusion_tag_use_l10n %}')
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(t.render(c).strip(), 'None')
|
2011-04-28 21:41:28 +08:00
|
|
|
|
|
|
|
c.use_l10n = True
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(t.render(c).strip(), 'True')
|
2011-05-03 19:52:42 +08:00
|
|
|
|
2015-04-01 10:57:26 +08:00
|
|
|
def test_no_render_side_effect(self):
|
|
|
|
"""
|
|
|
|
#23441 -- InclusionNode shouldn't modify its nodelist at render time.
|
|
|
|
"""
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(app_dirs=True, libraries=LIBRARIES)
|
2015-04-01 10:57:26 +08:00
|
|
|
template = engine.from_string('{% load inclusion %}{% inclusion_no_params %}')
|
|
|
|
count = template.nodelist.get_nodes_by_type(Node)
|
|
|
|
template.render(Context({}))
|
|
|
|
self.assertEqual(template.nodelist.get_nodes_by_type(Node), count)
|
|
|
|
|
|
|
|
def test_render_context_is_cleared(self):
|
|
|
|
"""
|
|
|
|
#24555 -- InclusionNode should push and pop the render_context stack
|
|
|
|
when rendering. Otherwise, leftover values such as blocks from
|
|
|
|
extending can interfere with subsequent rendering.
|
|
|
|
"""
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(app_dirs=True, libraries=LIBRARIES)
|
2015-04-01 10:57:26 +08:00
|
|
|
template = engine.from_string('{% load inclusion %}{% inclusion_extends1 %}{% inclusion_extends2 %}')
|
|
|
|
self.assertEqual(template.render(Context({})).strip(), 'one\ntwo')
|
|
|
|
|
2015-02-04 04:41:51 +08:00
|
|
|
|
2015-02-22 03:13:26 +08:00
|
|
|
class TemplateTagLoadingTests(SimpleTestCase):
|
|
|
|
|
2015-04-12 07:41:45 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.egg_dir = os.path.join(ROOT, 'eggs')
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2015-02-22 03:13:26 +08:00
|
|
|
|
|
|
|
def test_load_error(self):
|
2015-05-09 04:10:36 +08:00
|
|
|
msg = (
|
|
|
|
"Invalid template library specified. ImportError raised when "
|
|
|
|
"trying to load 'template_tests.broken_tag': cannot import name "
|
2017-01-20 10:10:33 +08:00
|
|
|
"'Xtemplate'"
|
2015-05-09 04:10:36 +08:00
|
|
|
)
|
2017-01-20 10:10:33 +08:00
|
|
|
with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
|
|
|
|
Engine(libraries={'broken_tag': 'template_tests.broken_tag'})
|
2015-02-22 03:13:26 +08:00
|
|
|
|
|
|
|
def test_load_error_egg(self):
|
|
|
|
egg_name = '%s/tagsegg.egg' % self.egg_dir
|
2015-05-09 04:10:36 +08:00
|
|
|
msg = (
|
|
|
|
"Invalid template library specified. ImportError raised when "
|
|
|
|
"trying to load 'tagsegg.templatetags.broken_egg': cannot "
|
2017-01-20 10:10:33 +08:00
|
|
|
"import name 'Xtemplate'"
|
2015-05-09 04:10:36 +08:00
|
|
|
)
|
2015-02-22 03:13:26 +08:00
|
|
|
with extend_sys_path(egg_name):
|
2017-01-20 10:10:33 +08:00
|
|
|
with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
|
|
|
|
Engine(libraries={'broken_egg': 'tagsegg.templatetags.broken_egg'})
|
2015-02-22 03:13:26 +08:00
|
|
|
|
|
|
|
def test_load_working_egg(self):
|
|
|
|
ttext = "{% load working_egg %}"
|
|
|
|
egg_name = '%s/tagsegg.egg' % self.egg_dir
|
|
|
|
with extend_sys_path(egg_name):
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(libraries={
|
|
|
|
'working_egg': 'tagsegg.templatetags.working_egg',
|
|
|
|
})
|
|
|
|
engine.from_string(ttext)
|
2015-09-18 03:16:14 +08:00
|
|
|
|
|
|
|
def test_load_annotated_function(self):
|
|
|
|
Engine(libraries={
|
|
|
|
'annotated_tag_function': 'template_tests.annotated_tag_function',
|
|
|
|
})
|