| 1 |
from django.template import Context, Template, Variable, VariableDoesNotExist |
| 2 |
from django.template.base import DebugLexer, Lexer, TokenType |
| 3 |
from django.test import SimpleTestCase |
| 4 |
from django.utils.translation import gettext_lazy |
| 5 |
|
| 6 |
|
| 7 |
class LexerTestMixin: |
| 8 |
template_string = ( |
| 9 |
"text\n" |
| 10 |
"{% if test %}{{ varvalue }}{% endif %}" |
| 11 |
"{#comment {{not a var}} {%not a block%} #}" |
| 12 |
"end text" |
| 13 |
) |
| 14 |
expected_token_tuples = [ |
| 15 |
# (token_type, contents, lineno, position) |
| 16 |
(TokenType.TEXT, "text\n", 1, (0, 5)), |
| 17 |
(TokenType.BLOCK, "if test", 2, (5, 18)), |
| 18 |
(TokenType.VAR, "varvalue", 2, (18, 32)), |
| 19 |
(TokenType.BLOCK, "endif", 2, (32, 43)), |
| 20 |
(TokenType.COMMENT, "comment {{not a var}} {%not a block%}", 2, (43, 85)), |
| 21 |
(TokenType.TEXT, "end text", 2, (85, 93)), |
| 22 |
] |
| 23 |
|
| 24 |
def test_tokenize(self): |
| 25 |
tokens = self.lexer_class(self.template_string).tokenize() |
| 26 |
token_tuples = [ |
| 27 |
(t.token_type, t.contents, t.lineno, t.position) for t in tokens |
| 28 |
] |
| 29 |
self.assertEqual(token_tuples, self.make_expected()) |
| 30 |
|
| 31 |
def make_expected(self): |
| 32 |
raise NotImplementedError("This method must be implemented by a subclass.") |
| 33 |
|
| 34 |
|
| 35 |
class LexerTests(LexerTestMixin, SimpleTestCase): |
| 36 |
lexer_class = Lexer |
| 37 |
|
| 38 |
def make_expected(self): |
| 39 |
# The non-debug lexer does not record position. |
| 40 |
return [t[:-1] + (None,) for t in self.expected_token_tuples] |
| 41 |
|
| 42 |
|
| 43 |
class DebugLexerTests(LexerTestMixin, SimpleTestCase): |
| 44 |
lexer_class = DebugLexer |
| 45 |
|
| 46 |
def make_expected(self): |
| 47 |
return self.expected_token_tuples |
| 48 |
|
| 49 |
|
| 50 |
class TemplateTests(SimpleTestCase): |
| 51 |
def test_lazy_template_string(self): |
| 52 |
template_string = gettext_lazy("lazy string") |
| 53 |
self.assertEqual(Template(template_string).render(Context()), template_string) |
| 54 |
|
| 55 |
def test_repr(self): |
| 56 |
template = Template( |
| 57 |
"<html><body>\n" |
| 58 |
"{% if test %}<h1>{{ varvalue }}</h1>{% endif %}" |
| 59 |
"</body></html>" |
| 60 |
) |
| 61 |
self.assertEqual( |
| 62 |
repr(template), |
| 63 |
'<Template template_string="<html><body>{% if t...">', |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
class VariableDoesNotExistTests(SimpleTestCase): |
| 68 |
def test_str(self): |
| 69 |
exc = VariableDoesNotExist(msg="Failed lookup in %r", params=({"foo": "bar"},)) |
| 70 |
self.assertEqual(str(exc), "Failed lookup in {'foo': 'bar'}") |
| 71 |
|
| 72 |
|
| 73 |
class VariableTests(SimpleTestCase): |
| 74 |
def test_integer_literals(self): |
| 75 |
self.assertEqual( |
| 76 |
Variable("999999999999999999999999999").literal, 999999999999999999999999999 |
| 77 |
) |
| 78 |
|
| 79 |
def test_nonliterals(self): |
| 80 |
"""Variable names that aren't resolved as literals.""" |
| 81 |
var_names = [] |
| 82 |
for var in ("inf", "infinity", "iNFiniTy", "nan"): |
| 83 |
var_names.extend((var, "-" + var, "+" + var)) |
| 84 |
for var in var_names: |
| 85 |
with self.subTest(var=var): |
| 86 |
self.assertIsNone(Variable(var).literal) |