2009-03-23 09:40:25 +00:00
|
|
|
"""
|
|
|
|
Testing some internals of the template processing. These are *not* examples to be copied in user code.
|
|
|
|
"""
|
2015-03-06 15:53:25 +00:00
|
|
|
from django.template import Library, TemplateSyntaxError
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.template.base import (
|
2021-01-29 19:21:26 +00:00
|
|
|
FilterExpression, Lexer, Parser, Token, TokenType, Variable,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2015-05-08 20:10:36 +00:00
|
|
|
from django.template.defaultfilters import register as filter_library
|
2016-12-07 22:42:31 +00:00
|
|
|
from django.test import SimpleTestCase
|
2010-11-04 04:47:05 +00:00
|
|
|
|
|
|
|
|
2016-12-07 22:42:31 +00:00
|
|
|
class ParserTests(SimpleTestCase):
|
2015-02-21 19:20:20 +00:00
|
|
|
|
|
|
|
def test_token_smart_split(self):
|
|
|
|
"""
|
|
|
|
#7027 -- _() syntax should work with spaces
|
|
|
|
"""
|
2018-05-10 15:51:51 +00:00
|
|
|
token = Token(TokenType.BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
|
2015-02-21 19:20:20 +00:00
|
|
|
split = token.split_contents()
|
|
|
|
self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
|
|
|
|
|
2021-01-29 19:21:26 +00:00
|
|
|
def test_repr(self):
|
|
|
|
token = Token(TokenType.BLOCK, 'some text')
|
|
|
|
self.assertEqual(repr(token), '<Block token: "some text...">')
|
|
|
|
parser = Parser([token], builtins=[filter_library])
|
|
|
|
self.assertEqual(
|
|
|
|
repr(parser),
|
|
|
|
'<Parser tokens=[<Block token: "some text...">]>',
|
|
|
|
)
|
|
|
|
filter_expression = FilterExpression('news|upper', parser)
|
|
|
|
self.assertEqual(repr(filter_expression), "<FilterExpression 'news|upper'>")
|
|
|
|
lexer = Lexer('{% for i in 1 %}{{ a }}\n{% endfor %}')
|
|
|
|
self.assertEqual(
|
|
|
|
repr(lexer),
|
|
|
|
'<Lexer template_string="{% for i in 1 %}{{ a...", verbatim=False>',
|
|
|
|
)
|
|
|
|
|
2010-11-04 04:47:05 +00:00
|
|
|
def test_filter_parsing(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
c = {"article": {"section": "News"}}
|
2015-05-08 20:10:36 +00:00
|
|
|
p = Parser("", builtins=[filter_library])
|
2010-11-04 04:47:05 +00:00
|
|
|
|
|
|
|
def fe_test(s, val):
|
|
|
|
self.assertEqual(FilterExpression(s, p).resolve(c), val)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
fe_test("article.section", "News")
|
|
|
|
fe_test("article.section|upper", "NEWS")
|
|
|
|
fe_test('"News"', "News")
|
|
|
|
fe_test("'News'", "News")
|
|
|
|
fe_test(r'"Some \"Good\" News"', 'Some "Good" News')
|
|
|
|
fe_test(r'"Some \"Good\" News"', 'Some "Good" News')
|
|
|
|
fe_test(r"'Some \'Bad\' News'", "Some 'Bad' News")
|
2010-11-04 04:47:05 +00:00
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
fe = FilterExpression(r'"Some \"Good\" News"', p)
|
2010-11-04 04:47:05 +00:00
|
|
|
self.assertEqual(fe.filters, [])
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(fe.var, 'Some "Good" News')
|
2010-11-04 04:47:05 +00:00
|
|
|
|
|
|
|
# Filtered variables should reject access of attributes beginning with
|
|
|
|
# underscores.
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = "Variables and attributes may not begin with underscores: 'article._hidden'"
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
FilterExpression("article._hidden|upper", p)
|
2010-11-04 04:47:05 +00:00
|
|
|
|
|
|
|
def test_variable_parsing(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
c = {"article": {"section": "News"}}
|
2010-11-04 04:47:05 +00:00
|
|
|
self.assertEqual(Variable("article.section").resolve(c), "News")
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(Variable('"News"').resolve(c), "News")
|
|
|
|
self.assertEqual(Variable("'News'").resolve(c), "News")
|
2010-11-04 04:47:05 +00:00
|
|
|
|
|
|
|
# Translated strings are handled correctly.
|
|
|
|
self.assertEqual(Variable("_(article.section)").resolve(c), "News")
|
|
|
|
self.assertEqual(Variable('_("Good News")').resolve(c), "Good News")
|
|
|
|
self.assertEqual(Variable("_('Better News')").resolve(c), "Better News")
|
|
|
|
|
|
|
|
# Escaped quotes work correctly as well.
|
|
|
|
self.assertEqual(
|
2012-06-07 16:08:47 +00:00
|
|
|
Variable(r'"Some \"Good\" News"').resolve(c), 'Some "Good" News'
|
2010-11-04 04:47:05 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2012-06-07 16:08:47 +00:00
|
|
|
Variable(r"'Some \'Better\' News'").resolve(c), "Some 'Better' News"
|
2010-11-04 04:47:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Variables should reject access of attributes beginning with
|
|
|
|
# underscores.
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = "Variables and attributes may not begin with underscores: 'article._hidden'"
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
Variable("article._hidden")
|
2013-02-14 08:45:55 +00:00
|
|
|
|
2013-09-19 13:27:19 +00:00
|
|
|
# Variables should raise on non string type
|
2017-01-20 02:10:33 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, "Variable must be a string or number, got <class 'dict'>"):
|
2013-09-19 13:27:19 +00:00
|
|
|
Variable({})
|
|
|
|
|
2013-09-04 10:53:55 +00:00
|
|
|
def test_filter_args_count(self):
|
2016-11-14 22:40:28 +00:00
|
|
|
parser = Parser("")
|
|
|
|
register = Library()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
@register.filter
|
2013-09-04 10:53:55 +00:00
|
|
|
def no_arguments(value):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
@register.filter
|
2013-09-04 10:53:55 +00:00
|
|
|
def one_argument(value, arg):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
@register.filter
|
2013-09-04 10:53:55 +00:00
|
|
|
def one_opt_argument(value, arg=False):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
@register.filter
|
2013-09-04 10:53:55 +00:00
|
|
|
def two_arguments(value, arg, arg2):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2016-11-14 22:40:28 +00:00
|
|
|
@register.filter
|
2013-09-04 10:53:55 +00:00
|
|
|
def two_one_opt_arg(value, arg, arg2=False):
|
|
|
|
pass
|
2016-11-14 22:40:28 +00:00
|
|
|
parser.add_library(register)
|
2013-09-04 10:53:55 +00:00
|
|
|
for expr in (
|
|
|
|
'1|no_arguments:"1"',
|
|
|
|
'1|two_arguments',
|
|
|
|
'1|two_arguments:"1"',
|
|
|
|
'1|two_one_opt_arg',
|
2013-10-18 09:02:43 +00:00
|
|
|
):
|
2013-09-04 10:53:55 +00:00
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2016-11-14 22:40:28 +00:00
|
|
|
FilterExpression(expr, parser)
|
2013-09-04 10:53:55 +00:00
|
|
|
for expr in (
|
|
|
|
# Correct number of arguments
|
|
|
|
'1|no_arguments',
|
|
|
|
'1|one_argument:"1"',
|
|
|
|
# One optional
|
|
|
|
'1|one_opt_argument',
|
|
|
|
'1|one_opt_argument:"1"',
|
|
|
|
# Not supplying all
|
|
|
|
'1|two_one_opt_arg:"1"',
|
2013-10-18 09:02:43 +00:00
|
|
|
):
|
2016-11-14 22:40:28 +00:00
|
|
|
FilterExpression(expr, parser)
|