Debian Codegraph @

tests/template_tests/test_custom.py

raw
1 import os
2
3 from django.template import Context, Engine, TemplateSyntaxError
4 from django.template.base import Node
5 from django.template.library import InvalidTemplateLibrary
6 from django.test import SimpleTestCase
7 from django.test.utils import extend_sys_path
8
9 from .templatetags import custom, inclusion
10 from .utils import ROOT
11
12 LIBRARIES = {
13 "custom": "template_tests.templatetags.custom",
14 "inclusion": "template_tests.templatetags.inclusion",
15 }
16
17
18 class CustomFilterTests(SimpleTestCase):
19 def test_filter(self):
20 engine = Engine(libraries=LIBRARIES)
21 t = engine.from_string("{% load custom %}{{ string|trim:5 }}")
22 self.assertEqual(
23 t.render(Context({"string": "abcdefghijklmnopqrstuvwxyz"})), "abcde"
24 )
25
26 def test_decorated_filter(self):
27 engine = Engine(libraries=LIBRARIES)
28 t = engine.from_string("{% load custom %}{{ name|make_data_div }}")
29 self.assertEqual(
30 t.render(Context({"name": "foo"})), '<div data-name="foo"></div>'
31 )
32
33
34 class TagTestCase(SimpleTestCase):
35 @classmethod
36 def setUpClass(cls):
37 cls.engine = Engine(app_dirs=True, libraries=LIBRARIES)
38 super().setUpClass()
39
40 def verify_tag(self, tag, name):
41 self.assertEqual(tag.__name__, name)
42 self.assertEqual(tag.__doc__, "Expected %s __doc__" % name)
43 self.assertEqual(tag.__dict__["anything"], "Expected %s __dict__" % name)
44
45
46 class SimpleTagTests(TagTestCase):
47 def test_simple_tags(self):
48 c = Context({"value": 42})
49
50 templates = [
51 ("{% load custom %}{% no_params %}", "no_params - Expected result"),
52 ("{% load custom %}{% one_param 37 %}", "one_param - Expected result: 37"),
53 (
54 "{% load custom %}{% explicit_no_context 37 %}",
55 "explicit_no_context - Expected result: 37",
56 ),
57 (
58 "{% load custom %}{% no_params_with_context %}",
59 "no_params_with_context - Expected result (context value: 42)",
60 ),
61 (
62 "{% load custom %}{% params_and_context 37 %}",
63 "params_and_context - Expected result (context value: 42): 37",
64 ),
65 (
66 "{% load custom %}{% simple_two_params 37 42 %}",
67 "simple_two_params - Expected result: 37, 42",
68 ),
69 (
70 "{% load custom %}{% simple_keyword_only_param kwarg=37 %}",
71 "simple_keyword_only_param - Expected result: 37",
72 ),
73 (
74 "{% load custom %}{% simple_keyword_only_default %}",
75 "simple_keyword_only_default - Expected result: 42",
76 ),
77 (
78 "{% load custom %}{% simple_keyword_only_default kwarg=37 %}",
79 "simple_keyword_only_default - Expected result: 37",
80 ),
81 (
82 "{% load custom %}{% simple_one_default 37 %}",
83 "simple_one_default - Expected result: 37, hi",
84 ),
85 (
86 '{% load custom %}{% simple_one_default 37 two="hello" %}',
87 "simple_one_default - Expected result: 37, hello",
88 ),
89 (
90 '{% load custom %}{% simple_one_default one=99 two="hello" %}',
91 "simple_one_default - Expected result: 99, hello",
92 ),
93 (
94 "{% load custom %}{% simple_one_default 37 42 %}",
95 "simple_one_default - Expected result: 37, 42",
96 ),
97 (
98 "{% load custom %}{% simple_unlimited_args 37 %}",
99 "simple_unlimited_args - Expected result: 37, hi",
100 ),
101 (
102 "{% load custom %}{% simple_unlimited_args 37 42 56 89 %}",
103 "simple_unlimited_args - Expected result: 37, 42, 56, 89",
104 ),
105 (
106 "{% load custom %}{% simple_only_unlimited_args %}",
107 "simple_only_unlimited_args - Expected result: ",
108 ),
109 (
110 "{% load custom %}{% simple_only_unlimited_args 37 42 56 89 %}",
111 "simple_only_unlimited_args - Expected result: 37, 42, 56, 89",
112 ),
113 (
114 "{% load custom %}"
115 '{% simple_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" '
116 "four=1|add:3 %}",
117 "simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / "
118 "eggs=scrambled, four=4",
119 ),
120 ]
121
122 for entry in templates:
123 t = self.engine.from_string(entry[0])
124 self.assertEqual(t.render(c), entry[1])
125
126 for entry in templates:
127 t = self.engine.from_string(
128 "%s as var %%}Result: {{ var }}" % entry[0][0:-2]
129 )
130 self.assertEqual(t.render(c), "Result: %s" % entry[1])
131
132 def test_simple_tag_errors(self):
133 errors = [
134 (
135 "'simple_one_default' received unexpected keyword argument 'three'",
136 '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}',
137 ),
138 (
139 "'simple_two_params' received too many positional arguments",
140 "{% load custom %}{% simple_two_params 37 42 56 %}",
141 ),
142 (
143 "'simple_one_default' received too many positional arguments",
144 "{% load custom %}{% simple_one_default 37 42 56 %}",
145 ),
146 (
147 "'simple_keyword_only_param' did not receive value(s) for the "
148 "argument(s): 'kwarg'",
149 "{% load custom %}{% simple_keyword_only_param %}",
150 ),
151 (
152 "'simple_keyword_only_param' received multiple values for "
153 "keyword argument 'kwarg'",
154 "{% load custom %}{% simple_keyword_only_param kwarg=42 kwarg=37 %}",
155 ),
156 (
157 "'simple_keyword_only_default' received multiple values for "
158 "keyword argument 'kwarg'",
159 "{% load custom %}{% simple_keyword_only_default kwarg=42 "
160 "kwarg=37 %}",
161 ),
162 (
163 "'simple_unlimited_args_kwargs' received some positional argument(s) "
164 "after some keyword argument(s)",
165 "{% load custom %}"
166 "{% simple_unlimited_args_kwargs 37 40|add:2 "
167 'eggs="scrambled" 56 four=1|add:3 %}',
168 ),
169 (
170 "'simple_unlimited_args_kwargs' received multiple values for keyword "
171 "argument 'eggs'",
172 "{% load custom %}"
173 "{% simple_unlimited_args_kwargs 37 "
174 'eggs="scrambled" eggs="scrambled" %}',
175 ),
176 ]
177
178 for entry in errors:
179 with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
180 self.engine.from_string(entry[1])
181
182 for entry in errors:
183 with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
184 self.engine.from_string("%s as var %%}" % entry[1][0:-2])
185
186 def test_simple_tag_escaping_autoescape_off(self):
187 c = Context({"name": "Jack & Jill"}, autoescape=False)
188 t = self.engine.from_string("{% load custom %}{% escape_naive %}")
189 self.assertEqual(t.render(c), "Hello Jack & Jill!")
190
191 def test_simple_tag_naive_escaping(self):
192 c = Context({"name": "Jack & Jill"})
193 t = self.engine.from_string("{% load custom %}{% escape_naive %}")
194 self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")
195
196 def test_simple_tag_explicit_escaping(self):
197 # Check we don't double escape
198 c = Context({"name": "Jack & Jill"})
199 t = self.engine.from_string("{% load custom %}{% escape_explicit %}")
200 self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")
201
202 def test_simple_tag_format_html_escaping(self):
203 # Check we don't double escape
204 c = Context({"name": "Jack & Jill"})
205 t = self.engine.from_string("{% load custom %}{% escape_format_html %}")
206 self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")
207
208 def test_simple_tag_registration(self):
209 # The decorators preserve the decorated function's docstring, name,
210 # and attributes.
211 self.verify_tag(custom.no_params, "no_params")
212 self.verify_tag(custom.one_param, "one_param")
213 self.verify_tag(custom.explicit_no_context, "explicit_no_context")
214 self.verify_tag(custom.no_params_with_context, "no_params_with_context")
215 self.verify_tag(custom.params_and_context, "params_and_context")
216 self.verify_tag(
217 custom.simple_unlimited_args_kwargs, "simple_unlimited_args_kwargs"
218 )
219 self.verify_tag(
220 custom.simple_tag_without_context_parameter,
221 "simple_tag_without_context_parameter",
222 )
223
224 def test_simple_tag_missing_context(self):
225 # The 'context' parameter must be present when takes_context is True
226 msg = (
227 "'simple_tag_without_context_parameter' is decorated with "
228 "takes_context=True so it must have a first argument of 'context'"
229 )
230 with self.assertRaisesMessage(TemplateSyntaxError, msg):
231 self.engine.from_string(
232 "{% load custom %}{% simple_tag_without_context_parameter 123 %}"
233 )
234
235 def test_simple_tag_missing_context_no_params(self):
236 msg = (
237 "'simple_tag_takes_context_without_params' is decorated with "
238 "takes_context=True so it must have a first argument of 'context'"
239 )
240 with self.assertRaisesMessage(TemplateSyntaxError, msg):
241 self.engine.from_string(
242 "{% load custom %}{% simple_tag_takes_context_without_params %}"
243 )
244
245
246 class SimpleBlockTagTests(TagTestCase):
247 def test_simple_block_tags(self):
248 c = Context({"value": 42})
249
250 templates = [
251 (
252 "{% load custom %}{% div %}content{% enddiv %}",
253 "<div id='test'>content</div>",
254 ),
255 (
256 "{% load custom %}{% one_param_block 37 %}inner"
257 "{% endone_param_block %}",
258 "one_param_block - Expected result: 37 with content inner",
259 ),
260 (
261 "{% load custom %}{% explicit_no_context_block 37 %}inner"
262 "{% endexplicit_no_context_block %}",
263 "explicit_no_context_block - Expected result: 37 with content inner",
264 ),
265 (
266 "{% load custom %}{% no_params_with_context_block %}inner"
267 "{% endno_params_with_context_block %}",
268 "no_params_with_context_block - Expected result (context value: 42) "
269 "(content value: inner)",
270 ),
271 (
272 "{% load custom %}{% params_and_context_block 37 %}inner"
273 "{% endparams_and_context_block %}",
274 "params_and_context_block - Expected result (context value: 42) "
275 "(content value: inner): 37",
276 ),
277 (
278 "{% load custom %}{% simple_two_params_block 37 42 %}inner"
279 "{% endsimple_two_params_block %}",
280 "simple_two_params_block - Expected result (content value: inner): "
281 "37, 42",
282 ),
283 (
284 "{% load custom %}{% simple_keyword_only_param_block kwarg=37 %}thirty "
285 "seven{% endsimple_keyword_only_param_block %}",
286 "simple_keyword_only_param_block - Expected result (content value: "
287 "thirty seven): 37",
288 ),
289 (
290 "{% load custom %}{% simple_keyword_only_default_block %}forty two"
291 "{% endsimple_keyword_only_default_block %}",
292 "simple_keyword_only_default_block - Expected result (content value: "
293 "forty two): 42",
294 ),
295 (
296 "{% load custom %}{% simple_keyword_only_default_block kwarg=37 %}"
297 "thirty seven{% endsimple_keyword_only_default_block %}",
298 "simple_keyword_only_default_block - Expected result (content value: "
299 "thirty seven): 37",
300 ),
301 (
302 "{% load custom %}{% simple_one_default_block 37 %}inner"
303 "{% endsimple_one_default_block %}",
304 "simple_one_default_block - Expected result (content value: inner): "
305 "37, hi",
306 ),
307 (
308 '{% load custom %}{% simple_one_default_block 37 two="hello" %}inner'
309 "{% endsimple_one_default_block %}",
310 "simple_one_default_block - Expected result (content value: inner): "
311 "37, hello",
312 ),
313 (
314 '{% load custom %}{% simple_one_default_block one=99 two="hello" %}'
315 "inner{% endsimple_one_default_block %}",
316 "simple_one_default_block - Expected result (content value: inner): "
317 "99, hello",
318 ),
319 (
320 "{% load custom %}{% simple_one_default_block 37 42 %}inner"
321 "{% endsimple_one_default_block %}",
322 "simple_one_default_block - Expected result (content value: inner): "
323 "37, 42",
324 ),
325 (
326 "{% load custom %}{% simple_unlimited_args_block 37 %}thirty seven"
327 "{% endsimple_unlimited_args_block %}",
328 "simple_unlimited_args_block - Expected result (content value: thirty "
329 "seven): 37, hi",
330 ),
331 (
332 "{% load custom %}{% simple_unlimited_args_block 37 42 56 89 %}numbers"
333 "{% endsimple_unlimited_args_block %}",
334 "simple_unlimited_args_block - Expected result "
335 "(content value: numbers): 37, 42, 56, 89",
336 ),
337 (
338 "{% load custom %}{% simple_only_unlimited_args_block %}inner"
339 "{% endsimple_only_unlimited_args_block %}",
340 "simple_only_unlimited_args_block - Expected result (content value: "
341 "inner): ",
342 ),
343 (
344 "{% load custom %}{% simple_only_unlimited_args_block 37 42 56 89 %}"
345 "numbers{% endsimple_only_unlimited_args_block %}",
346 "simple_only_unlimited_args_block - Expected result "
347 "(content value: numbers): 37, 42, 56, 89",
348 ),
349 (
350 "{% load custom %}"
351 '{% simple_unlimited_args_kwargs_block 37 40|add:2 56 eggs="scrambled" '
352 "four=1|add:3 %}inner content"
353 "{% endsimple_unlimited_args_kwargs_block %}",
354 "simple_unlimited_args_kwargs_block - Expected result (content value: "
355 "inner content): 37, 42, 56 / eggs=scrambled, four=4",
356 ),
357 ]
358
359 for entry in templates:
360 with self.subTest(entry[0]):
361 t = self.engine.from_string(entry[0])
362 self.assertEqual(t.render(c), entry[1])
363
364 def test_simple_block_tag_errors(self):
365 errors = [
366 (
367 "'simple_one_default_block' received unexpected keyword argument "
368 "'three'",
369 "{% load custom %}"
370 '{% simple_one_default_block 99 two="hello" three="foo" %}'
371 "{% endsimple_one_default_block %}",
372 ),
373 (
374 "'simple_two_params_block' received too many positional arguments",
375 "{% load custom %}{% simple_two_params_block 37 42 56 %}"
376 "{% endsimple_two_params_block %}",
377 ),
378 (
379 "'simple_one_default_block' received too many positional arguments",
380 "{% load custom %}{% simple_one_default_block 37 42 56 %}"
381 "{% endsimple_one_default_block %}",
382 ),
383 (
384 "'simple_keyword_only_param_block' did not receive value(s) for the "
385 "argument(s): 'kwarg'",
386 "{% load custom %}{% simple_keyword_only_param_block %}"
387 "{% endsimple_keyword_only_param_block %}",
388 ),
389 (
390 "'simple_keyword_only_param_block' received multiple values for "
391 "keyword argument 'kwarg'",
392 "{% load custom %}"
393 "{% simple_keyword_only_param_block kwarg=42 kwarg=37 %}"
394 "{% endsimple_keyword_only_param_block %}",
395 ),
396 (
397 "'simple_keyword_only_default_block' received multiple values for "
398 "keyword argument 'kwarg'",
399 "{% load custom %}{% simple_keyword_only_default_block kwarg=42 "
400 "kwarg=37 %}{% endsimple_keyword_only_default_block %}",
401 ),
402 (
403 "'simple_unlimited_args_kwargs_block' received some positional "
404 "argument(s) after some keyword argument(s)",
405 "{% load custom %}"
406 '{% simple_unlimited_args_kwargs_block 37 40|add:2 eggs="scrambled" 56 '
407 "four=1|add:3 %}{% endsimple_unlimited_args_kwargs_block %}",
408 ),
409 (
410 "'simple_unlimited_args_kwargs_block' received multiple values for "
411 "keyword argument 'eggs'",
412 "{% load custom %}"
413 "{% simple_unlimited_args_kwargs_block 37 "
414 'eggs="scrambled" eggs="scrambled" %}'
415 "{% endsimple_unlimited_args_kwargs_block %}",
416 ),
417 (
418 "Unclosed tag on line 1: 'div'. Looking for one of: enddiv.",
419 "{% load custom %}{% div %}Some content",
420 ),
421 (
422 "Unclosed tag on line 1: 'simple_one_default_block'. Looking for one "
423 "of: endsimple_one_default_block.",
424 "{% load custom %}{% simple_one_default_block %}Some content",
425 ),
426 (
427 "'simple_tag_without_content_parameter' must have a first argument "
428 "of 'content'",
429 "{% load custom %}{% simple_tag_without_content_parameter %}",
430 ),
431 (
432 "'simple_tag_with_context_without_content_parameter' is decorated with "
433 "takes_context=True so it must have a first argument of 'context' and "
434 "a second argument of 'content'",
435 "{% load custom %}"
436 "{% simple_tag_with_context_without_content_parameter %}",
437 ),
438 ]
439
440 for entry in errors:
441 with self.subTest(entry[1]):
442 with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
443 self.engine.from_string(entry[1])
444
445 def test_simple_block_tag_escaping_autoescape_off(self):
446 c = Context({"name": "Jack & Jill"}, autoescape=False)
447 t = self.engine.from_string(
448 "{% load custom %}{% escape_naive_block %}{{ name }} again"
449 "{% endescape_naive_block %}"
450 )
451 self.assertEqual(t.render(c), "Hello Jack & Jill: Jack & Jill again!")
452
453 def test_simple_block_tag_naive_escaping(self):
454 c = Context({"name": "Jack & Jill"})
455 t = self.engine.from_string(
456 "{% load custom %}{% escape_naive_block %}{{ name }} again"
457 "{% endescape_naive_block %}"
458 )
459 self.assertEqual(
460 t.render(c), "Hello Jack &amp; Jill: Jack &amp;amp; Jill again!"
461 )
462
463 def test_simple_block_tag_explicit_escaping(self):
464 # Check we don't double escape
465 c = Context({"name": "Jack & Jill"})
466 t = self.engine.from_string(
467 "{% load custom %}{% escape_explicit_block %}again"
468 "{% endescape_explicit_block %}"
469 )
470 self.assertEqual(t.render(c), "Hello Jack &amp; Jill: again!")
471
472 def test_simple_block_tag_format_html_escaping(self):
473 # Check we don't double escape
474 c = Context({"name": "Jack & Jill"})
475 t = self.engine.from_string(
476 "{% load custom %}{% escape_format_html_block %}again"
477 "{% endescape_format_html_block %}"
478 )
479 self.assertEqual(t.render(c), "Hello Jack &amp; Jill: again!")
480
481 def test_simple_block_tag_missing_context(self):
482 # The 'context' parameter must be present when takes_context is True
483 msg = (
484 "'simple_block_tag_without_context_parameter' is decorated with "
485 "takes_context=True so it must have a first argument of 'context'"
486 )
487 with self.assertRaisesMessage(TemplateSyntaxError, msg):
488 self.engine.from_string(
489 "{% load custom %}{% simple_block_tag_without_context_parameter 123 %}"
490 "{% endsimple_block_tag_without_context_parameter %}"
491 )
492
493 def test_simple_block_tag_missing_context_no_params(self):
494 msg = (
495 "'simple_tag_takes_context_without_params_block' is decorated with "
496 "takes_context=True so it must have a first argument of 'context'"
497 )
498 with self.assertRaisesMessage(TemplateSyntaxError, msg):
499 self.engine.from_string(
500 "{% load custom %}{% simple_tag_takes_context_without_params_block %}"
501 "{% endsimple_tag_takes_context_without_params_block %}"
502 )
503
504 def test_simple_block_tag_missing_content(self):
505 # The 'content' parameter must be present when takes_context is True
506 msg = (
507 "'simple_block_tag_without_content' must have a first argument of 'content'"
508 )
509 with self.assertRaisesMessage(TemplateSyntaxError, msg):
510 self.engine.from_string(
511 "{% load custom %}{% simple_block_tag_without_content %}"
512 "{% endsimple_block_tag_without_content %}"
513 )
514
515 def test_simple_block_tag_with_context_missing_content(self):
516 # The 'content' parameter must be present when takes_context is True
517 msg = "'simple_block_tag_with_context_without_content' is decorated with "
518 "takes_context=True so it must have a first argument of 'context' and a "
519 "second argument of 'content'"
520 with self.assertRaisesMessage(TemplateSyntaxError, msg):
521 self.engine.from_string(
522 "{% load custom %}{% simple_block_tag_with_context_without_content %}"
523 "{% endsimple_block_tag_with_context_without_content %}"
524 )
525
526 def test_simple_block_gets_context(self):
527 c = Context({"name": "Jack & Jill"})
528 t = self.engine.from_string("{% load custom %}{% div %}{{ name }}{% enddiv %}")
529 self.assertEqual(t.render(c), "<div id='test'>Jack &amp; Jill</div>")
530
531 def test_simple_block_capture_as(self):
532 c = Context({"name": "Jack & Jill"})
533 t = self.engine.from_string(
534 "{% load custom %}{% div as div_content %}{{ name }}{% enddiv %}"
535 "My div is: {{ div_content }}"
536 )
537 self.assertEqual(t.render(c), "My div is: <div id='test'>Jack &amp; Jill</div>")
538
539 def test_simple_block_nested(self):
540 c = Context({"name": "Jack & Jill"})
541 t = self.engine.from_string(
542 "{% load custom %}Start{% div id='outer' %}Before{% div id='inner' %}"
543 "{{ name }}{% enddiv %}After{% enddiv %}End"
544 )
545 self.assertEqual(
546 t.render(c),
547 "Start<div id='outer'>Before<div id='inner'>Jack &amp; Jill</div>After"
548 "</div>End",
549 )
550
551 def test_different_simple_block_nested(self):
552 c = Context({"name": "Jack & Jill"})
553 t = self.engine.from_string(
554 "{% load custom %}Start{% div id='outer' %}Before"
555 "{% simple_keyword_only_default_block %}Inner"
556 "{% endsimple_keyword_only_default_block %}"
557 "After{% enddiv %}End"
558 )
559 self.assertEqual(
560 t.render(c),
561 "Start<div id='outer'>Before"
562 "simple_keyword_only_default_block - Expected result (content value: "
563 "Inner): 42After</div>End",
564 )
565
566 def test_custom_end_tag(self):
567 c = Context({"name": "Jack & Jill"})
568 t = self.engine.from_string(
569 "{% load custom %}{% div_custom_end %}{{ name }}{% divend %}"
570 )
571 self.assertEqual(t.render(c), "<div>Jack &amp; Jill</div>")
572
573 with self.assertRaisesMessage(
574 TemplateSyntaxError,
575 "'enddiv_custom_end', expected 'divend'. Did you forget to register or "
576 "load this tag?",
577 ):
578 self.engine.from_string(
579 "{% load custom %}{% div_custom_end %}{{ name }}{% enddiv_custom_end %}"
580 )
581
582
583 class InclusionTagTests(TagTestCase):
584 def test_inclusion_tags(self):
585 c = Context({"value": 42})
586
587 templates = [
588 (
589 "{% load inclusion %}{% inclusion_no_params %}",
590 "inclusion_no_params - Expected result\n",
591 ),
592 (
593 "{% load inclusion %}{% inclusion_one_param 37 %}",
594 "inclusion_one_param - Expected result: 37\n",
595 ),
596 (
597 "{% load inclusion %}{% inclusion_explicit_no_context 37 %}",
598 "inclusion_explicit_no_context - Expected result: 37\n",
599 ),
600 (
601 "{% load inclusion %}{% inclusion_no_params_with_context %}",
602 "inclusion_no_params_with_context - Expected result (context value: "
603 "42)\n",
604 ),
605 (
606 "{% load inclusion %}{% inclusion_params_and_context 37 %}",
607 "inclusion_params_and_context - Expected result (context value: 42): "
608 "37\n",
609 ),
610 (
611 "{% load inclusion %}{% inclusion_two_params 37 42 %}",
612 "inclusion_two_params - Expected result: 37, 42\n",
613 ),
614 (
615 "{% load inclusion %}{% inclusion_one_default 37 %}",
616 "inclusion_one_default - Expected result: 37, hi\n",
617 ),
618 (
619 '{% load inclusion %}{% inclusion_one_default 37 two="hello" %}',
620 "inclusion_one_default - Expected result: 37, hello\n",
621 ),
622 (
623 '{% load inclusion %}{% inclusion_one_default one=99 two="hello" %}',
624 "inclusion_one_default - Expected result: 99, hello\n",
625 ),
626 (
627 "{% load inclusion %}{% inclusion_one_default 37 42 %}",
628 "inclusion_one_default - Expected result: 37, 42\n",
629 ),
630 (
631 "{% load inclusion %}{% inclusion_keyword_only_default kwarg=37 %}",
632 "inclusion_keyword_only_default - Expected result: 37\n",
633 ),
634 (
635 "{% load inclusion %}{% inclusion_unlimited_args 37 %}",
636 "inclusion_unlimited_args - Expected result: 37, hi\n",
637 ),
638 (
639 "{% load inclusion %}{% inclusion_unlimited_args 37 42 56 89 %}",
640 "inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n",
641 ),
642 (
643 "{% load inclusion %}{% inclusion_only_unlimited_args %}",
644 "inclusion_only_unlimited_args - Expected result: \n",
645 ),
646 (
647 "{% load inclusion %}{% inclusion_only_unlimited_args 37 42 56 89 %}",
648 "inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n",
649 ),
650 (
651 "{% load inclusion %}"
652 '{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" '
653 "four=1|add:3 %}",
654 "inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / "
655 "eggs=scrambled, four=4\n",
656 ),
657 ]
658
659 for entry in templates:
660 t = self.engine.from_string(entry[0])
661 self.assertEqual(t.render(c), entry[1])
662
663 def test_inclusion_tag_errors(self):
664 errors = [
665 (
666 "'inclusion_one_default' received unexpected keyword argument 'three'",
667 "{% load inclusion %}"
668 '{% inclusion_one_default 99 two="hello" three="foo" %}',
669 ),
670 (
671 "'inclusion_two_params' received too many positional arguments",
672 "{% load inclusion %}{% inclusion_two_params 37 42 56 %}",
673 ),
674 (
675 "'inclusion_one_default' received too many positional arguments",
676 "{% load inclusion %}{% inclusion_one_default 37 42 56 %}",
677 ),
678 (
679 "'inclusion_one_default' did not receive value(s) for the argument(s): "
680 "'one'",
681 "{% load inclusion %}{% inclusion_one_default %}",
682 ),
683 (
684 "'inclusion_keyword_only_default' received multiple values "
685 "for keyword argument 'kwarg'",
686 "{% load inclusion %}{% inclusion_keyword_only_default "
687 "kwarg=37 kwarg=42 %}",
688 ),
689 (
690 "'inclusion_unlimited_args' did not receive value(s) for the "
691 "argument(s): 'one'",
692 "{% load inclusion %}{% inclusion_unlimited_args %}",
693 ),
694 (
695 "'inclusion_unlimited_args_kwargs' received some positional "
696 "argument(s) after some keyword argument(s)",
697 "{% load inclusion %}"
698 "{% inclusion_unlimited_args_kwargs 37 40|add:2 "
699 'eggs="boiled" 56 four=1|add:3 %}',
700 ),
701 (
702 "'inclusion_unlimited_args_kwargs' received multiple values for "
703 "keyword argument 'eggs'",
704 "{% load inclusion %}"
705 "{% inclusion_unlimited_args_kwargs 37 "
706 'eggs="scrambled" eggs="scrambled" %}',
707 ),
708 ]
709
710 for entry in errors:
711 with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
712 self.engine.from_string(entry[1])
713
714 def test_include_tag_missing_context(self):
715 # The 'context' parameter must be present when takes_context is True
716 msg = (
717 "'inclusion_tag_without_context_parameter' is decorated with "
718 "takes_context=True so it must have a first argument of 'context'"
719 )
720 with self.assertRaisesMessage(TemplateSyntaxError, msg):
721 self.engine.from_string(
722 "{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}"
723 )
724
725 def test_include_tag_missing_context_no_params(self):
726 msg = (
727 "'inclusion_tag_takes_context_without_params' is decorated with "
728 "takes_context=True so it must have a first argument of 'context'"
729 )
730 with self.assertRaisesMessage(TemplateSyntaxError, msg):
731 self.engine.from_string(
732 "{% load inclusion %}{% inclusion_tag_takes_context_without_params %}"
733 )
734
735 def test_inclusion_tags_from_template(self):
736 c = Context({"value": 42})
737
738 templates = [
739 (
740 "{% load inclusion %}{% inclusion_no_params_from_template %}",
741 "inclusion_no_params_from_template - Expected result\n",
742 ),
743 (
744 "{% load inclusion %}{% inclusion_one_param_from_template 37 %}",
745 "inclusion_one_param_from_template - Expected result: 37\n",
746 ),
747 (
748 "{% load inclusion %}"
749 "{% inclusion_explicit_no_context_from_template 37 %}",
750 "inclusion_explicit_no_context_from_template - Expected result: 37\n",
751 ),
752 (
753 "{% load inclusion %}"
754 "{% inclusion_no_params_with_context_from_template %}",
755 "inclusion_no_params_with_context_from_template - Expected result "
756 "(context value: 42)\n",
757 ),
758 (
759 "{% load inclusion %}"
760 "{% inclusion_params_and_context_from_template 37 %}",
761 "inclusion_params_and_context_from_template - Expected result (context "
762 "value: 42): 37\n",
763 ),
764 (
765 "{% load inclusion %}{% inclusion_two_params_from_template 37 42 %}",
766 "inclusion_two_params_from_template - Expected result: 37, 42\n",
767 ),
768 (
769 "{% load inclusion %}{% inclusion_one_default_from_template 37 %}",
770 "inclusion_one_default_from_template - Expected result: 37, hi\n",
771 ),
772 (
773 "{% load inclusion %}{% inclusion_one_default_from_template 37 42 %}",
774 "inclusion_one_default_from_template - Expected result: 37, 42\n",
775 ),
776 (
777 "{% load inclusion %}{% inclusion_unlimited_args_from_template 37 %}",
778 "inclusion_unlimited_args_from_template - Expected result: 37, hi\n",
779 ),
780 (
781 "{% load inclusion %}"
782 "{% inclusion_unlimited_args_from_template 37 42 56 89 %}",
783 "inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, "
784 "89\n",
785 ),
786 (
787 "{% load inclusion %}{% inclusion_only_unlimited_args_from_template %}",
788 "inclusion_only_unlimited_args_from_template - Expected result: \n",
789 ),
790 (
791 "{% load inclusion %}"
792 "{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}",
793 "inclusion_only_unlimited_args_from_template - Expected result: 37, "
794 "42, 56, 89\n",
795 ),
796 ]
797
798 for entry in templates:
799 t = self.engine.from_string(entry[0])
800 self.assertEqual(t.render(c), entry[1])
801
802 def test_inclusion_tag_registration(self):
803 # The decorators preserve the decorated function's docstring, name,
804 # and attributes.
805 self.verify_tag(inclusion.inclusion_no_params, "inclusion_no_params")
806 self.verify_tag(inclusion.inclusion_one_param, "inclusion_one_param")
807 self.verify_tag(
808 inclusion.inclusion_explicit_no_context, "inclusion_explicit_no_context"
809 )
810 self.verify_tag(
811 inclusion.inclusion_no_params_with_context,
812 "inclusion_no_params_with_context",
813 )
814 self.verify_tag(
815 inclusion.inclusion_params_and_context, "inclusion_params_and_context"
816 )
817 self.verify_tag(inclusion.inclusion_two_params, "inclusion_two_params")
818 self.verify_tag(inclusion.inclusion_one_default, "inclusion_one_default")
819 self.verify_tag(inclusion.inclusion_unlimited_args, "inclusion_unlimited_args")
820 self.verify_tag(
821 inclusion.inclusion_only_unlimited_args, "inclusion_only_unlimited_args"
822 )
823 self.verify_tag(
824 inclusion.inclusion_tag_without_context_parameter,
825 "inclusion_tag_without_context_parameter",
826 )
827 self.verify_tag(inclusion.inclusion_tag_use_l10n, "inclusion_tag_use_l10n")
828 self.verify_tag(
829 inclusion.inclusion_unlimited_args_kwargs, "inclusion_unlimited_args_kwargs"
830 )
831
832 def test_15070_use_l10n(self):
833 """
834 Inclusion tag passes down `use_l10n` of context to the
835 Context of the included/rendered template as well.
836 """
837 c = Context({})
838 t = self.engine.from_string("{% load inclusion %}{% inclusion_tag_use_l10n %}")
839 self.assertEqual(t.render(c).strip(), "None")
840
841 c.use_l10n = True
842 self.assertEqual(t.render(c).strip(), "True")
843
844 def test_no_render_side_effect(self):
845 """
846 #23441 -- InclusionNode shouldn't modify its nodelist at render time.
847 """
848 engine = Engine(app_dirs=True, libraries=LIBRARIES)
849 template = engine.from_string("{% load inclusion %}{% inclusion_no_params %}")
850 count = template.nodelist.get_nodes_by_type(Node)
851 template.render(Context({}))
852 self.assertEqual(template.nodelist.get_nodes_by_type(Node), count)
853
854 def test_render_context_is_cleared(self):
855 """
856 #24555 -- InclusionNode should push and pop the render_context stack
857 when rendering. Otherwise, leftover values such as blocks from
858 extending can interfere with subsequent rendering.
859 """
860 engine = Engine(app_dirs=True, libraries=LIBRARIES)
861 template = engine.from_string(
862 "{% load inclusion %}{% inclusion_extends1 %}{% inclusion_extends2 %}"
863 )
864 self.assertEqual(template.render(Context({})).strip(), "one\ntwo")
865
866
867 class TemplateTagLoadingTests(SimpleTestCase):
868 @classmethod
869 def setUpClass(cls):
870 cls.egg_dir = os.path.join(ROOT, "eggs")
871 super().setUpClass()
872
873 def test_load_error(self):
874 msg = (
875 "Invalid template library specified. ImportError raised when "
876 "trying to load 'template_tests.broken_tag': cannot import name "
877 "'Xtemplate'"
878 )
879 with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
880 Engine(libraries={"broken_tag": "template_tests.broken_tag"})
881
882 def test_load_error_egg(self):
883 egg_name = "%s/tagsegg.egg" % self.egg_dir
884 msg = (
885 "Invalid template library specified. ImportError raised when "
886 "trying to load 'tagsegg.templatetags.broken_egg': cannot "
887 "import name 'Xtemplate'"
888 )
889 with extend_sys_path(egg_name):
890 with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
891 Engine(libraries={"broken_egg": "tagsegg.templatetags.broken_egg"})
892
893 def test_load_working_egg(self):
894 ttext = "{% load working_egg %}"
895 egg_name = "%s/tagsegg.egg" % self.egg_dir
896 with extend_sys_path(egg_name):
897 engine = Engine(
898 libraries={
899 "working_egg": "tagsegg.templatetags.working_egg",
900 }
901 )
902 engine.from_string(ttext)
903
904 def test_load_annotated_function(self):
905 Engine(
906 libraries={
907 "annotated_tag_function": "template_tests.annotated_tag_function",
908 }
909 )