Debian Codegraph @

tests/template_tests/syntax_tests/test_with.py

raw
1 from django.template import TemplateSyntaxError
2 from django.template.defaulttags import WithNode
3 from django.test import SimpleTestCase
4
5 from ..utils import setup
6
7
8 class WithTagTests(SimpleTestCase):
9 at_least_with_one_msg = "'with' expected at least one variable assignment"
10
11 @setup({"with01": "{% with key=dict.key %}{{ key }}{% endwith %}"})
12 def test_with01(self):
13 output = self.engine.render_to_string("with01", {"dict": {"key": 50}})
14 self.assertEqual(output, "50")
15
16 @setup({"legacywith01": "{% with dict.key as key %}{{ key }}{% endwith %}"})
17 def test_legacywith01(self):
18 output = self.engine.render_to_string("legacywith01", {"dict": {"key": 50}})
19 self.assertEqual(output, "50")
20
21 @setup(
22 {
23 "with02": "{{ key }}{% with key=dict.key %}"
24 "{{ key }}-{{ dict.key }}-{{ key }}"
25 "{% endwith %}{{ key }}"
26 }
27 )
28 def test_with02(self):
29 output = self.engine.render_to_string("with02", {"dict": {"key": 50}})
30 if self.engine.string_if_invalid:
31 self.assertEqual(output, "INVALID50-50-50INVALID")
32 else:
33 self.assertEqual(output, "50-50-50")
34
35 @setup(
36 {
37 "legacywith02": "{{ key }}{% with dict.key as key %}"
38 "{{ key }}-{{ dict.key }}-{{ key }}"
39 "{% endwith %}{{ key }}"
40 }
41 )
42 def test_legacywith02(self):
43 output = self.engine.render_to_string("legacywith02", {"dict": {"key": 50}})
44 if self.engine.string_if_invalid:
45 self.assertEqual(output, "INVALID50-50-50INVALID")
46 else:
47 self.assertEqual(output, "50-50-50")
48
49 @setup({"with03": "{% with a=alpha b=beta %}{{ a }}{{ b }}{% endwith %}"})
50 def test_with03(self):
51 output = self.engine.render_to_string("with03", {"alpha": "A", "beta": "B"})
52 self.assertEqual(output, "AB")
53
54 @setup({"with-error01": "{% with dict.key xx key %}{{ key }}{% endwith %}"})
55 def test_with_error01(self):
56 with self.assertRaisesMessage(TemplateSyntaxError, self.at_least_with_one_msg):
57 self.engine.render_to_string("with-error01", {"dict": {"key": 50}})
58
59 @setup({"with-error02": "{% with dict.key as %}{{ key }}{% endwith %}"})
60 def test_with_error02(self):
61 with self.assertRaisesMessage(TemplateSyntaxError, self.at_least_with_one_msg):
62 self.engine.render_to_string("with-error02", {"dict": {"key": 50}})
63
64
65 class WithNodeTests(SimpleTestCase):
66 def test_repr(self):
67 node = WithNode(nodelist=[], name="a", var="dict.key")
68 self.assertEqual(repr(node), "<WithNode>")