2018-07-31 13:57:11 +00:00
|
|
|
from django.template import Context, Template, Variable, VariableDoesNotExist
|
2021-08-09 06:16:29 +00:00
|
|
|
from django.template.base import DebugLexer, Lexer, TokenType
|
2017-01-26 20:10:15 +00:00
|
|
|
from django.test import SimpleTestCase
|
2018-07-31 13:57:11 +00:00
|
|
|
from django.utils.translation import gettext_lazy
|
|
|
|
|
|
|
|
|
2021-08-09 06:16:29 +00:00
|
|
|
class LexerTestMixin:
|
|
|
|
template_string = (
|
2022-02-03 19:24:19 +00:00
|
|
|
"text\n"
|
|
|
|
"{% if test %}{{ varvalue }}{% endif %}"
|
|
|
|
"{#comment {{not a var}} %{not a block}% #}"
|
|
|
|
"end text"
|
2021-08-09 06:16:29 +00:00
|
|
|
)
|
|
|
|
expected_token_tuples = [
|
|
|
|
# (token_type, contents, lineno, position)
|
2022-02-03 19:24:19 +00:00
|
|
|
(TokenType.TEXT, "text\n", 1, (0, 5)),
|
|
|
|
(TokenType.BLOCK, "if test", 2, (5, 18)),
|
|
|
|
(TokenType.VAR, "varvalue", 2, (18, 32)),
|
|
|
|
(TokenType.BLOCK, "endif", 2, (32, 43)),
|
|
|
|
(TokenType.COMMENT, "comment {{not a var}} %{not a block}%", 2, (43, 85)),
|
|
|
|
(TokenType.TEXT, "end text", 2, (85, 93)),
|
2021-08-09 06:16:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def test_tokenize(self):
|
|
|
|
tokens = self.lexer_class(self.template_string).tokenize()
|
2022-02-03 19:24:19 +00:00
|
|
|
token_tuples = [
|
|
|
|
(t.token_type, t.contents, t.lineno, t.position) for t in tokens
|
|
|
|
]
|
2021-08-09 06:16:29 +00:00
|
|
|
self.assertEqual(token_tuples, self.make_expected())
|
|
|
|
|
|
|
|
def make_expected(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
raise NotImplementedError("This method must be implemented by a subclass.")
|
2021-08-09 06:16:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LexerTests(LexerTestMixin, SimpleTestCase):
|
|
|
|
lexer_class = Lexer
|
|
|
|
|
|
|
|
def make_expected(self):
|
|
|
|
# The non-debug lexer does not record position.
|
|
|
|
return [t[:-1] + (None,) for t in self.expected_token_tuples]
|
|
|
|
|
|
|
|
|
|
|
|
class DebugLexerTests(LexerTestMixin, SimpleTestCase):
|
|
|
|
lexer_class = DebugLexer
|
|
|
|
|
|
|
|
def make_expected(self):
|
|
|
|
return self.expected_token_tuples
|
|
|
|
|
|
|
|
|
2018-07-31 13:57:11 +00:00
|
|
|
class TemplateTests(SimpleTestCase):
|
|
|
|
def test_lazy_template_string(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
template_string = gettext_lazy("lazy string")
|
2018-07-31 13:57:11 +00:00
|
|
|
self.assertEqual(Template(template_string).render(Context()), template_string)
|
2017-01-26 20:10:15 +00:00
|
|
|
|
2021-03-12 22:37:47 +00:00
|
|
|
def test_repr(self):
|
|
|
|
template = Template(
|
2022-02-03 19:24:19 +00:00
|
|
|
"<html><body>\n"
|
|
|
|
"{% if test %}<h1>{{ varvalue }}</h1>{% endif %}"
|
|
|
|
"</body></html>"
|
2021-03-12 22:37:47 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(template),
|
|
|
|
'<Template template_string="<html><body>{% if t...">',
|
|
|
|
)
|
|
|
|
|
2017-01-26 20:10:15 +00:00
|
|
|
|
|
|
|
class VariableDoesNotExistTests(SimpleTestCase):
|
|
|
|
def test_str(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
exc = VariableDoesNotExist(msg="Failed lookup in %r", params=({"foo": "bar"},))
|
2017-01-26 20:10:15 +00:00
|
|
|
self.assertEqual(str(exc), "Failed lookup in {'foo': 'bar'}")
|
2017-10-21 10:24:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VariableTests(SimpleTestCase):
|
|
|
|
def test_integer_literals(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Variable("999999999999999999999999999").literal, 999999999999999999999999999
|
|
|
|
)
|
2017-10-22 00:55:26 +00:00
|
|
|
|
|
|
|
def test_nonliterals(self):
|
|
|
|
"""Variable names that aren't resolved as literals."""
|
|
|
|
var_names = []
|
2022-02-03 19:24:19 +00:00
|
|
|
for var in ("inf", "infinity", "iNFiniTy", "nan"):
|
|
|
|
var_names.extend((var, "-" + var, "+" + var))
|
2017-10-22 00:55:26 +00:00
|
|
|
for var in var_names:
|
|
|
|
with self.subTest(var=var):
|
|
|
|
self.assertIsNone(Variable(var).literal)
|