2009-03-23 17:40:25 +08:00
|
|
|
"""
|
|
|
|
Testing some internals of the template processing. These are *not* examples to be copied in user code.
|
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-07-01 20:22:27 +08:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2014-11-11 04:40:26 +08:00
|
|
|
from django.template import Library, Template, TemplateSyntaxError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template.base import (
|
|
|
|
FilterExpression, Parser, TokenParser, Variable,
|
|
|
|
)
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import override_settings
|
2013-02-14 16:45:55 +08:00
|
|
|
from django.utils import six
|
2010-11-04 12:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ParserTests(TestCase):
|
|
|
|
def test_token_parsing(self):
|
|
|
|
# Tests for TokenParser behavior in the face of quoted strings with
|
|
|
|
# spaces.
|
|
|
|
|
|
|
|
p = TokenParser("tag thevar|filter sometag")
|
|
|
|
self.assertEqual(p.tagname, "tag")
|
|
|
|
self.assertEqual(p.value(), "thevar|filter")
|
|
|
|
self.assertTrue(p.more())
|
|
|
|
self.assertEqual(p.tag(), "sometag")
|
|
|
|
self.assertFalse(p.more())
|
|
|
|
|
|
|
|
p = TokenParser('tag "a value"|filter sometag')
|
|
|
|
self.assertEqual(p.tagname, "tag")
|
|
|
|
self.assertEqual(p.value(), '"a value"|filter')
|
|
|
|
self.assertTrue(p.more())
|
|
|
|
self.assertEqual(p.tag(), "sometag")
|
|
|
|
self.assertFalse(p.more())
|
|
|
|
|
|
|
|
p = TokenParser("tag 'a value'|filter sometag")
|
|
|
|
self.assertEqual(p.tagname, "tag")
|
|
|
|
self.assertEqual(p.value(), "'a value'|filter")
|
|
|
|
self.assertTrue(p.more())
|
|
|
|
self.assertEqual(p.tag(), "sometag")
|
|
|
|
self.assertFalse(p.more())
|
|
|
|
|
|
|
|
def test_filter_parsing(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
c = {"article": {"section": "News"}}
|
2010-11-04 12:47:05 +08:00
|
|
|
p = Parser("")
|
|
|
|
|
|
|
|
def fe_test(s, val):
|
|
|
|
self.assertEqual(FilterExpression(s, p).resolve(c), val)
|
|
|
|
|
2012-06-08 00:08:47 +08: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 12:47:05 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
fe = FilterExpression(r'"Some \"Good\" News"', p)
|
2010-11-04 12:47:05 +08:00
|
|
|
self.assertEqual(fe.filters, [])
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(fe.var, 'Some "Good" News')
|
2010-11-04 12:47:05 +08:00
|
|
|
|
|
|
|
# Filtered variables should reject access of attributes beginning with
|
|
|
|
# underscores.
|
2013-12-09 01:20:06 +08:00
|
|
|
self.assertRaises(TemplateSyntaxError, FilterExpression, "article._hidden|upper", p)
|
2010-11-04 12:47:05 +08:00
|
|
|
|
|
|
|
def test_variable_parsing(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
c = {"article": {"section": "News"}}
|
2010-11-04 12:47:05 +08:00
|
|
|
self.assertEqual(Variable("article.section").resolve(c), "News")
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(Variable('"News"').resolve(c), "News")
|
|
|
|
self.assertEqual(Variable("'News'").resolve(c), "News")
|
2010-11-04 12:47:05 +08: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-08 00:08:47 +08:00
|
|
|
Variable(r'"Some \"Good\" News"').resolve(c), 'Some "Good" News'
|
2010-11-04 12:47:05 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2012-06-08 00:08:47 +08:00
|
|
|
Variable(r"'Some \'Better\' News'").resolve(c), "Some 'Better' News"
|
2010-11-04 12:47:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Variables should reject access of attributes beginning with
|
|
|
|
# underscores.
|
2013-12-09 01:20:06 +08:00
|
|
|
self.assertRaises(TemplateSyntaxError, Variable, "article._hidden")
|
2013-02-14 16:45:55 +08:00
|
|
|
|
2013-09-19 21:27:19 +08:00
|
|
|
# Variables should raise on non string type
|
|
|
|
with six.assertRaisesRegex(self, TypeError, "Variable must be a string or number, got <(class|type) 'dict'>"):
|
|
|
|
Variable({})
|
|
|
|
|
2015-02-15 22:42:05 +08:00
|
|
|
@override_settings(DEBUG=True)
|
2013-02-14 16:45:55 +08:00
|
|
|
def test_compile_filter_error(self):
|
|
|
|
# regression test for #19819
|
|
|
|
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
|
|
|
|
with six.assertRaisesRegex(self, TemplateSyntaxError, msg) as cm:
|
|
|
|
Template("{% if 1 %}{{ foo@bar }}{% endif %}")
|
|
|
|
self.assertEqual(cm.exception.django_template_source[1], (10, 23))
|
2013-09-04 18:53:55 +08:00
|
|
|
|
|
|
|
def test_filter_args_count(self):
|
|
|
|
p = Parser("")
|
|
|
|
l = Library()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-04 18:53:55 +08:00
|
|
|
@l.filter
|
|
|
|
def no_arguments(value):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-04 18:53:55 +08:00
|
|
|
@l.filter
|
|
|
|
def one_argument(value, arg):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-04 18:53:55 +08:00
|
|
|
@l.filter
|
|
|
|
def one_opt_argument(value, arg=False):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-04 18:53:55 +08:00
|
|
|
@l.filter
|
|
|
|
def two_arguments(value, arg, arg2):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-04 18:53:55 +08:00
|
|
|
@l.filter
|
|
|
|
def two_one_opt_arg(value, arg, arg2=False):
|
|
|
|
pass
|
|
|
|
p.add_library(l)
|
|
|
|
for expr in (
|
|
|
|
'1|no_arguments:"1"',
|
|
|
|
'1|two_arguments',
|
|
|
|
'1|two_arguments:"1"',
|
|
|
|
'1|two_one_opt_arg',
|
2013-10-18 17:02:43 +08:00
|
|
|
):
|
2013-09-04 18:53:55 +08:00
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
|
|
|
FilterExpression(expr, p)
|
|
|
|
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 17:02:43 +08:00
|
|
|
):
|
2013-09-04 18:53:55 +08:00
|
|
|
FilterExpression(expr, p)
|