File: test_custom.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (909 lines) | stat: -rw-r--r-- 38,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
import os

from django.template import Context, Engine, TemplateSyntaxError
from django.template.base import Node
from django.template.library import InvalidTemplateLibrary
from django.test import SimpleTestCase
from django.test.utils import extend_sys_path

from .templatetags import custom, inclusion
from .utils import ROOT

LIBRARIES = {
    "custom": "template_tests.templatetags.custom",
    "inclusion": "template_tests.templatetags.inclusion",
}


class CustomFilterTests(SimpleTestCase):
    def test_filter(self):
        engine = Engine(libraries=LIBRARIES)
        t = engine.from_string("{% load custom %}{{ string|trim:5 }}")
        self.assertEqual(
            t.render(Context({"string": "abcdefghijklmnopqrstuvwxyz"})), "abcde"
        )

    def test_decorated_filter(self):
        engine = Engine(libraries=LIBRARIES)
        t = engine.from_string("{% load custom %}{{ name|make_data_div }}")
        self.assertEqual(
            t.render(Context({"name": "foo"})), '<div data-name="foo"></div>'
        )


class TagTestCase(SimpleTestCase):
    @classmethod
    def setUpClass(cls):
        cls.engine = Engine(app_dirs=True, libraries=LIBRARIES)
        super().setUpClass()

    def verify_tag(self, tag, name):
        self.assertEqual(tag.__name__, name)
        self.assertEqual(tag.__doc__, "Expected %s __doc__" % name)
        self.assertEqual(tag.__dict__["anything"], "Expected %s __dict__" % name)


class SimpleTagTests(TagTestCase):
    def test_simple_tags(self):
        c = Context({"value": 42})

        templates = [
            ("{% load custom %}{% no_params %}", "no_params - Expected result"),
            ("{% load custom %}{% one_param 37 %}", "one_param - Expected result: 37"),
            (
                "{% load custom %}{% explicit_no_context 37 %}",
                "explicit_no_context - Expected result: 37",
            ),
            (
                "{% load custom %}{% no_params_with_context %}",
                "no_params_with_context - Expected result (context value: 42)",
            ),
            (
                "{% load custom %}{% params_and_context 37 %}",
                "params_and_context - Expected result (context value: 42): 37",
            ),
            (
                "{% load custom %}{% simple_two_params 37 42 %}",
                "simple_two_params - Expected result: 37, 42",
            ),
            (
                "{% load custom %}{% simple_keyword_only_param kwarg=37 %}",
                "simple_keyword_only_param - Expected result: 37",
            ),
            (
                "{% load custom %}{% simple_keyword_only_default %}",
                "simple_keyword_only_default - Expected result: 42",
            ),
            (
                "{% load custom %}{% simple_keyword_only_default kwarg=37 %}",
                "simple_keyword_only_default - Expected result: 37",
            ),
            (
                "{% load custom %}{% simple_one_default 37 %}",
                "simple_one_default - Expected result: 37, hi",
            ),
            (
                '{% load custom %}{% simple_one_default 37 two="hello" %}',
                "simple_one_default - Expected result: 37, hello",
            ),
            (
                '{% load custom %}{% simple_one_default one=99 two="hello" %}',
                "simple_one_default - Expected result: 99, hello",
            ),
            (
                "{% load custom %}{% simple_one_default 37 42 %}",
                "simple_one_default - Expected result: 37, 42",
            ),
            (
                "{% load custom %}{% simple_unlimited_args 37 %}",
                "simple_unlimited_args - Expected result: 37, hi",
            ),
            (
                "{% load custom %}{% simple_unlimited_args 37 42 56 89 %}",
                "simple_unlimited_args - Expected result: 37, 42, 56, 89",
            ),
            (
                "{% load custom %}{% simple_only_unlimited_args %}",
                "simple_only_unlimited_args - Expected result: ",
            ),
            (
                "{% load custom %}{% simple_only_unlimited_args 37 42 56 89 %}",
                "simple_only_unlimited_args - Expected result: 37, 42, 56, 89",
            ),
            (
                "{% load custom %}"
                '{% simple_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" '
                "four=1|add:3 %}",
                "simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / "
                "eggs=scrambled, four=4",
            ),
        ]

        for entry in templates:
            t = self.engine.from_string(entry[0])
            self.assertEqual(t.render(c), entry[1])

        for entry in templates:
            t = self.engine.from_string(
                "%s as var %%}Result: {{ var }}" % entry[0][0:-2]
            )
            self.assertEqual(t.render(c), "Result: %s" % entry[1])

    def test_simple_tag_errors(self):
        errors = [
            (
                "'simple_one_default' received unexpected keyword argument 'three'",
                '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}',
            ),
            (
                "'simple_two_params' received too many positional arguments",
                "{% load custom %}{% simple_two_params 37 42 56 %}",
            ),
            (
                "'simple_one_default' received too many positional arguments",
                "{% load custom %}{% simple_one_default 37 42 56 %}",
            ),
            (
                "'simple_keyword_only_param' did not receive value(s) for the "
                "argument(s): 'kwarg'",
                "{% load custom %}{% simple_keyword_only_param %}",
            ),
            (
                "'simple_keyword_only_param' received multiple values for "
                "keyword argument 'kwarg'",
                "{% load custom %}{% simple_keyword_only_param kwarg=42 kwarg=37 %}",
            ),
            (
                "'simple_keyword_only_default' received multiple values for "
                "keyword argument 'kwarg'",
                "{% load custom %}{% simple_keyword_only_default kwarg=42 "
                "kwarg=37 %}",
            ),
            (
                "'simple_unlimited_args_kwargs' received some positional argument(s) "
                "after some keyword argument(s)",
                "{% load custom %}"
                "{% simple_unlimited_args_kwargs 37 40|add:2 "
                'eggs="scrambled" 56 four=1|add:3 %}',
            ),
            (
                "'simple_unlimited_args_kwargs' received multiple values for keyword "
                "argument 'eggs'",
                "{% load custom %}"
                "{% simple_unlimited_args_kwargs 37 "
                'eggs="scrambled" eggs="scrambled" %}',
            ),
        ]

        for entry in errors:
            with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
                self.engine.from_string(entry[1])

        for entry in errors:
            with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
                self.engine.from_string("%s as var %%}" % entry[1][0:-2])

    def test_simple_tag_escaping_autoescape_off(self):
        c = Context({"name": "Jack & Jill"}, autoescape=False)
        t = self.engine.from_string("{% load custom %}{% escape_naive %}")
        self.assertEqual(t.render(c), "Hello Jack & Jill!")

    def test_simple_tag_naive_escaping(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string("{% load custom %}{% escape_naive %}")
        self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")

    def test_simple_tag_explicit_escaping(self):
        # Check we don't double escape
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string("{% load custom %}{% escape_explicit %}")
        self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")

    def test_simple_tag_format_html_escaping(self):
        # Check we don't double escape
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string("{% load custom %}{% escape_format_html %}")
        self.assertEqual(t.render(c), "Hello Jack &amp; Jill!")

    def test_simple_tag_registration(self):
        # The decorators preserve the decorated function's docstring, name,
        # and attributes.
        self.verify_tag(custom.no_params, "no_params")
        self.verify_tag(custom.one_param, "one_param")
        self.verify_tag(custom.explicit_no_context, "explicit_no_context")
        self.verify_tag(custom.no_params_with_context, "no_params_with_context")
        self.verify_tag(custom.params_and_context, "params_and_context")
        self.verify_tag(
            custom.simple_unlimited_args_kwargs, "simple_unlimited_args_kwargs"
        )
        self.verify_tag(
            custom.simple_tag_without_context_parameter,
            "simple_tag_without_context_parameter",
        )

    def test_simple_tag_missing_context(self):
        # The 'context' parameter must be present when takes_context is True
        msg = (
            "'simple_tag_without_context_parameter' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_tag_without_context_parameter 123 %}"
            )

    def test_simple_tag_missing_context_no_params(self):
        msg = (
            "'simple_tag_takes_context_without_params' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_tag_takes_context_without_params %}"
            )


class SimpleBlockTagTests(TagTestCase):
    def test_simple_block_tags(self):
        c = Context({"value": 42})

        templates = [
            (
                "{% load custom %}{% div %}content{% enddiv %}",
                "<div id='test'>content</div>",
            ),
            (
                "{% load custom %}{% one_param_block 37 %}inner"
                "{% endone_param_block %}",
                "one_param_block - Expected result: 37 with content inner",
            ),
            (
                "{% load custom %}{% explicit_no_context_block 37 %}inner"
                "{% endexplicit_no_context_block %}",
                "explicit_no_context_block - Expected result: 37 with content inner",
            ),
            (
                "{% load custom %}{% no_params_with_context_block %}inner"
                "{% endno_params_with_context_block %}",
                "no_params_with_context_block - Expected result (context value: 42) "
                "(content value: inner)",
            ),
            (
                "{% load custom %}{% params_and_context_block 37 %}inner"
                "{% endparams_and_context_block %}",
                "params_and_context_block - Expected result (context value: 42) "
                "(content value: inner): 37",
            ),
            (
                "{% load custom %}{% simple_two_params_block 37 42 %}inner"
                "{% endsimple_two_params_block %}",
                "simple_two_params_block - Expected result (content value: inner): "
                "37, 42",
            ),
            (
                "{% load custom %}{% simple_keyword_only_param_block kwarg=37 %}thirty "
                "seven{% endsimple_keyword_only_param_block %}",
                "simple_keyword_only_param_block - Expected result (content value: "
                "thirty seven): 37",
            ),
            (
                "{% load custom %}{% simple_keyword_only_default_block %}forty two"
                "{% endsimple_keyword_only_default_block %}",
                "simple_keyword_only_default_block - Expected result (content value: "
                "forty two): 42",
            ),
            (
                "{% load custom %}{% simple_keyword_only_default_block kwarg=37 %}"
                "thirty seven{% endsimple_keyword_only_default_block %}",
                "simple_keyword_only_default_block - Expected result (content value: "
                "thirty seven): 37",
            ),
            (
                "{% load custom %}{% simple_one_default_block 37 %}inner"
                "{% endsimple_one_default_block %}",
                "simple_one_default_block - Expected result (content value: inner): "
                "37, hi",
            ),
            (
                '{% load custom %}{% simple_one_default_block 37 two="hello" %}inner'
                "{% endsimple_one_default_block %}",
                "simple_one_default_block - Expected result (content value: inner): "
                "37, hello",
            ),
            (
                '{% load custom %}{% simple_one_default_block one=99 two="hello" %}'
                "inner{% endsimple_one_default_block %}",
                "simple_one_default_block - Expected result (content value: inner): "
                "99, hello",
            ),
            (
                "{% load custom %}{% simple_one_default_block 37 42 %}inner"
                "{% endsimple_one_default_block %}",
                "simple_one_default_block - Expected result (content value: inner): "
                "37, 42",
            ),
            (
                "{% load custom %}{% simple_unlimited_args_block 37 %}thirty seven"
                "{% endsimple_unlimited_args_block %}",
                "simple_unlimited_args_block - Expected result (content value: thirty "
                "seven): 37, hi",
            ),
            (
                "{% load custom %}{% simple_unlimited_args_block 37 42 56 89 %}numbers"
                "{% endsimple_unlimited_args_block %}",
                "simple_unlimited_args_block - Expected result "
                "(content value: numbers): 37, 42, 56, 89",
            ),
            (
                "{% load custom %}{% simple_only_unlimited_args_block %}inner"
                "{% endsimple_only_unlimited_args_block %}",
                "simple_only_unlimited_args_block - Expected result (content value: "
                "inner): ",
            ),
            (
                "{% load custom %}{% simple_only_unlimited_args_block 37 42 56 89 %}"
                "numbers{% endsimple_only_unlimited_args_block %}",
                "simple_only_unlimited_args_block - Expected result "
                "(content value: numbers): 37, 42, 56, 89",
            ),
            (
                "{% load custom %}"
                '{% simple_unlimited_args_kwargs_block 37 40|add:2 56 eggs="scrambled" '
                "four=1|add:3 %}inner content"
                "{% endsimple_unlimited_args_kwargs_block %}",
                "simple_unlimited_args_kwargs_block - Expected result (content value: "
                "inner content): 37, 42, 56 / eggs=scrambled, four=4",
            ),
        ]

        for entry in templates:
            with self.subTest(entry[0]):
                t = self.engine.from_string(entry[0])
                self.assertEqual(t.render(c), entry[1])

    def test_simple_block_tag_errors(self):
        errors = [
            (
                "'simple_one_default_block' received unexpected keyword argument "
                "'three'",
                "{% load custom %}"
                '{% simple_one_default_block 99 two="hello" three="foo" %}'
                "{% endsimple_one_default_block %}",
            ),
            (
                "'simple_two_params_block' received too many positional arguments",
                "{% load custom %}{% simple_two_params_block 37 42 56 %}"
                "{% endsimple_two_params_block %}",
            ),
            (
                "'simple_one_default_block' received too many positional arguments",
                "{% load custom %}{% simple_one_default_block 37 42 56 %}"
                "{% endsimple_one_default_block %}",
            ),
            (
                "'simple_keyword_only_param_block' did not receive value(s) for the "
                "argument(s): 'kwarg'",
                "{% load custom %}{% simple_keyword_only_param_block %}"
                "{% endsimple_keyword_only_param_block %}",
            ),
            (
                "'simple_keyword_only_param_block' received multiple values for "
                "keyword argument 'kwarg'",
                "{% load custom %}"
                "{% simple_keyword_only_param_block kwarg=42 kwarg=37 %}"
                "{% endsimple_keyword_only_param_block %}",
            ),
            (
                "'simple_keyword_only_default_block' received multiple values for "
                "keyword argument 'kwarg'",
                "{% load custom %}{% simple_keyword_only_default_block kwarg=42 "
                "kwarg=37 %}{% endsimple_keyword_only_default_block %}",
            ),
            (
                "'simple_unlimited_args_kwargs_block' received some positional "
                "argument(s) after some keyword argument(s)",
                "{% load custom %}"
                '{% simple_unlimited_args_kwargs_block 37 40|add:2 eggs="scrambled" 56 '
                "four=1|add:3 %}{% endsimple_unlimited_args_kwargs_block %}",
            ),
            (
                "'simple_unlimited_args_kwargs_block' received multiple values for "
                "keyword argument 'eggs'",
                "{% load custom %}"
                "{% simple_unlimited_args_kwargs_block 37 "
                'eggs="scrambled" eggs="scrambled" %}'
                "{% endsimple_unlimited_args_kwargs_block %}",
            ),
            (
                "Unclosed tag on line 1: 'div'. Looking for one of: enddiv.",
                "{% load custom %}{% div %}Some content",
            ),
            (
                "Unclosed tag on line 1: 'simple_one_default_block'. Looking for one "
                "of: endsimple_one_default_block.",
                "{% load custom %}{% simple_one_default_block %}Some content",
            ),
            (
                "'simple_tag_without_content_parameter' must have a first argument "
                "of 'content'",
                "{% load custom %}{% simple_tag_without_content_parameter %}",
            ),
            (
                "'simple_tag_with_context_without_content_parameter' is decorated with "
                "takes_context=True so it must have a first argument of 'context' and "
                "a second argument of 'content'",
                "{% load custom %}"
                "{% simple_tag_with_context_without_content_parameter %}",
            ),
        ]

        for entry in errors:
            with self.subTest(entry[1]):
                with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
                    self.engine.from_string(entry[1])

    def test_simple_block_tag_escaping_autoescape_off(self):
        c = Context({"name": "Jack & Jill"}, autoescape=False)
        t = self.engine.from_string(
            "{% load custom %}{% escape_naive_block %}{{ name }} again"
            "{% endescape_naive_block %}"
        )
        self.assertEqual(t.render(c), "Hello Jack & Jill: Jack & Jill again!")

    def test_simple_block_tag_naive_escaping(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}{% escape_naive_block %}{{ name }} again"
            "{% endescape_naive_block %}"
        )
        self.assertEqual(
            t.render(c), "Hello Jack &amp; Jill: Jack &amp;amp; Jill again!"
        )

    def test_simple_block_tag_explicit_escaping(self):
        # Check we don't double escape
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}{% escape_explicit_block %}again"
            "{% endescape_explicit_block %}"
        )
        self.assertEqual(t.render(c), "Hello Jack &amp; Jill: again!")

    def test_simple_block_tag_format_html_escaping(self):
        # Check we don't double escape
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}{% escape_format_html_block %}again"
            "{% endescape_format_html_block %}"
        )
        self.assertEqual(t.render(c), "Hello Jack &amp; Jill: again!")

    def test_simple_block_tag_missing_context(self):
        # The 'context' parameter must be present when takes_context is True
        msg = (
            "'simple_block_tag_without_context_parameter' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_block_tag_without_context_parameter 123 %}"
                "{% endsimple_block_tag_without_context_parameter %}"
            )

    def test_simple_block_tag_missing_context_no_params(self):
        msg = (
            "'simple_tag_takes_context_without_params_block' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_tag_takes_context_without_params_block %}"
                "{% endsimple_tag_takes_context_without_params_block %}"
            )

    def test_simple_block_tag_missing_content(self):
        # The 'content' parameter must be present when takes_context is True
        msg = (
            "'simple_block_tag_without_content' must have a first argument of 'content'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_block_tag_without_content %}"
                "{% endsimple_block_tag_without_content %}"
            )

    def test_simple_block_tag_with_context_missing_content(self):
        # The 'content' parameter must be present when takes_context is True
        msg = "'simple_block_tag_with_context_without_content' is decorated with "
        "takes_context=True so it must have a first argument of 'context' and a "
        "second argument of 'content'"
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load custom %}{% simple_block_tag_with_context_without_content %}"
                "{% endsimple_block_tag_with_context_without_content %}"
            )

    def test_simple_block_gets_context(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string("{% load custom %}{% div %}{{ name }}{% enddiv %}")
        self.assertEqual(t.render(c), "<div id='test'>Jack &amp; Jill</div>")

    def test_simple_block_capture_as(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}{% div as div_content %}{{ name }}{% enddiv %}"
            "My div is: {{ div_content }}"
        )
        self.assertEqual(t.render(c), "My div is: <div id='test'>Jack &amp; Jill</div>")

    def test_simple_block_nested(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}Start{% div id='outer' %}Before{% div id='inner' %}"
            "{{ name }}{% enddiv %}After{% enddiv %}End"
        )
        self.assertEqual(
            t.render(c),
            "Start<div id='outer'>Before<div id='inner'>Jack &amp; Jill</div>After"
            "</div>End",
        )

    def test_different_simple_block_nested(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}Start{% div id='outer' %}Before"
            "{% simple_keyword_only_default_block %}Inner"
            "{% endsimple_keyword_only_default_block %}"
            "After{% enddiv %}End"
        )
        self.assertEqual(
            t.render(c),
            "Start<div id='outer'>Before"
            "simple_keyword_only_default_block - Expected result (content value: "
            "Inner): 42After</div>End",
        )

    def test_custom_end_tag(self):
        c = Context({"name": "Jack & Jill"})
        t = self.engine.from_string(
            "{% load custom %}{% div_custom_end %}{{ name }}{% divend %}"
        )
        self.assertEqual(t.render(c), "<div>Jack &amp; Jill</div>")

        with self.assertRaisesMessage(
            TemplateSyntaxError,
            "'enddiv_custom_end', expected 'divend'. Did you forget to register or "
            "load this tag?",
        ):
            self.engine.from_string(
                "{% load custom %}{% div_custom_end %}{{ name }}{% enddiv_custom_end %}"
            )


class InclusionTagTests(TagTestCase):
    def test_inclusion_tags(self):
        c = Context({"value": 42})

        templates = [
            (
                "{% load inclusion %}{% inclusion_no_params %}",
                "inclusion_no_params - Expected result\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_param 37 %}",
                "inclusion_one_param - Expected result: 37\n",
            ),
            (
                "{% load inclusion %}{% inclusion_explicit_no_context 37 %}",
                "inclusion_explicit_no_context - Expected result: 37\n",
            ),
            (
                "{% load inclusion %}{% inclusion_no_params_with_context %}",
                "inclusion_no_params_with_context - Expected result (context value: "
                "42)\n",
            ),
            (
                "{% load inclusion %}{% inclusion_params_and_context 37 %}",
                "inclusion_params_and_context - Expected result (context value: 42): "
                "37\n",
            ),
            (
                "{% load inclusion %}{% inclusion_two_params 37 42 %}",
                "inclusion_two_params - Expected result: 37, 42\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_default 37 %}",
                "inclusion_one_default - Expected result: 37, hi\n",
            ),
            (
                '{% load inclusion %}{% inclusion_one_default 37 two="hello" %}',
                "inclusion_one_default - Expected result: 37, hello\n",
            ),
            (
                '{% load inclusion %}{% inclusion_one_default one=99 two="hello" %}',
                "inclusion_one_default - Expected result: 99, hello\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_default 37 42 %}",
                "inclusion_one_default - Expected result: 37, 42\n",
            ),
            (
                "{% load inclusion %}{% inclusion_keyword_only_default kwarg=37 %}",
                "inclusion_keyword_only_default - Expected result: 37\n",
            ),
            (
                "{% load inclusion %}{% inclusion_unlimited_args 37 %}",
                "inclusion_unlimited_args - Expected result: 37, hi\n",
            ),
            (
                "{% load inclusion %}{% inclusion_unlimited_args 37 42 56 89 %}",
                "inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n",
            ),
            (
                "{% load inclusion %}{% inclusion_only_unlimited_args %}",
                "inclusion_only_unlimited_args - Expected result: \n",
            ),
            (
                "{% load inclusion %}{% inclusion_only_unlimited_args 37 42 56 89 %}",
                "inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n",
            ),
            (
                "{% load inclusion %}"
                '{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" '
                "four=1|add:3 %}",
                "inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / "
                "eggs=scrambled, four=4\n",
            ),
        ]

        for entry in templates:
            t = self.engine.from_string(entry[0])
            self.assertEqual(t.render(c), entry[1])

    def test_inclusion_tag_errors(self):
        errors = [
            (
                "'inclusion_one_default' received unexpected keyword argument 'three'",
                "{% load inclusion %}"
                '{% inclusion_one_default 99 two="hello" three="foo" %}',
            ),
            (
                "'inclusion_two_params' received too many positional arguments",
                "{% load inclusion %}{% inclusion_two_params 37 42 56 %}",
            ),
            (
                "'inclusion_one_default' received too many positional arguments",
                "{% load inclusion %}{% inclusion_one_default 37 42 56 %}",
            ),
            (
                "'inclusion_one_default' did not receive value(s) for the argument(s): "
                "'one'",
                "{% load inclusion %}{% inclusion_one_default %}",
            ),
            (
                "'inclusion_keyword_only_default' received multiple values "
                "for keyword argument 'kwarg'",
                "{% load inclusion %}{% inclusion_keyword_only_default "
                "kwarg=37 kwarg=42 %}",
            ),
            (
                "'inclusion_unlimited_args' did not receive value(s) for the "
                "argument(s): 'one'",
                "{% load inclusion %}{% inclusion_unlimited_args %}",
            ),
            (
                "'inclusion_unlimited_args_kwargs' received some positional "
                "argument(s) after some keyword argument(s)",
                "{% load inclusion %}"
                "{% inclusion_unlimited_args_kwargs 37 40|add:2 "
                'eggs="boiled" 56 four=1|add:3 %}',
            ),
            (
                "'inclusion_unlimited_args_kwargs' received multiple values for "
                "keyword argument 'eggs'",
                "{% load inclusion %}"
                "{% inclusion_unlimited_args_kwargs 37 "
                'eggs="scrambled" eggs="scrambled" %}',
            ),
        ]

        for entry in errors:
            with self.assertRaisesMessage(TemplateSyntaxError, entry[0]):
                self.engine.from_string(entry[1])

    def test_include_tag_missing_context(self):
        # The 'context' parameter must be present when takes_context is True
        msg = (
            "'inclusion_tag_without_context_parameter' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}"
            )

    def test_include_tag_missing_context_no_params(self):
        msg = (
            "'inclusion_tag_takes_context_without_params' is decorated with "
            "takes_context=True so it must have a first argument of 'context'"
        )
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.from_string(
                "{% load inclusion %}{% inclusion_tag_takes_context_without_params %}"
            )

    def test_inclusion_tags_from_template(self):
        c = Context({"value": 42})

        templates = [
            (
                "{% load inclusion %}{% inclusion_no_params_from_template %}",
                "inclusion_no_params_from_template - Expected result\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_param_from_template 37 %}",
                "inclusion_one_param_from_template - Expected result: 37\n",
            ),
            (
                "{% load inclusion %}"
                "{% inclusion_explicit_no_context_from_template 37 %}",
                "inclusion_explicit_no_context_from_template - Expected result: 37\n",
            ),
            (
                "{% load inclusion %}"
                "{% inclusion_no_params_with_context_from_template %}",
                "inclusion_no_params_with_context_from_template - Expected result "
                "(context value: 42)\n",
            ),
            (
                "{% load inclusion %}"
                "{% inclusion_params_and_context_from_template 37 %}",
                "inclusion_params_and_context_from_template - Expected result (context "
                "value: 42): 37\n",
            ),
            (
                "{% load inclusion %}{% inclusion_two_params_from_template 37 42 %}",
                "inclusion_two_params_from_template - Expected result: 37, 42\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_default_from_template 37 %}",
                "inclusion_one_default_from_template - Expected result: 37, hi\n",
            ),
            (
                "{% load inclusion %}{% inclusion_one_default_from_template 37 42 %}",
                "inclusion_one_default_from_template - Expected result: 37, 42\n",
            ),
            (
                "{% load inclusion %}{% inclusion_unlimited_args_from_template 37 %}",
                "inclusion_unlimited_args_from_template - Expected result: 37, hi\n",
            ),
            (
                "{% load inclusion %}"
                "{% inclusion_unlimited_args_from_template 37 42 56 89 %}",
                "inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, "
                "89\n",
            ),
            (
                "{% load inclusion %}{% inclusion_only_unlimited_args_from_template %}",
                "inclusion_only_unlimited_args_from_template - Expected result: \n",
            ),
            (
                "{% load inclusion %}"
                "{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}",
                "inclusion_only_unlimited_args_from_template - Expected result: 37, "
                "42, 56, 89\n",
            ),
        ]

        for entry in templates:
            t = self.engine.from_string(entry[0])
            self.assertEqual(t.render(c), entry[1])

    def test_inclusion_tag_registration(self):
        # The decorators preserve the decorated function's docstring, name,
        # and attributes.
        self.verify_tag(inclusion.inclusion_no_params, "inclusion_no_params")
        self.verify_tag(inclusion.inclusion_one_param, "inclusion_one_param")
        self.verify_tag(
            inclusion.inclusion_explicit_no_context, "inclusion_explicit_no_context"
        )
        self.verify_tag(
            inclusion.inclusion_no_params_with_context,
            "inclusion_no_params_with_context",
        )
        self.verify_tag(
            inclusion.inclusion_params_and_context, "inclusion_params_and_context"
        )
        self.verify_tag(inclusion.inclusion_two_params, "inclusion_two_params")
        self.verify_tag(inclusion.inclusion_one_default, "inclusion_one_default")
        self.verify_tag(inclusion.inclusion_unlimited_args, "inclusion_unlimited_args")
        self.verify_tag(
            inclusion.inclusion_only_unlimited_args, "inclusion_only_unlimited_args"
        )
        self.verify_tag(
            inclusion.inclusion_tag_without_context_parameter,
            "inclusion_tag_without_context_parameter",
        )
        self.verify_tag(inclusion.inclusion_tag_use_l10n, "inclusion_tag_use_l10n")
        self.verify_tag(
            inclusion.inclusion_unlimited_args_kwargs, "inclusion_unlimited_args_kwargs"
        )

    def test_15070_use_l10n(self):
        """
        Inclusion tag passes down `use_l10n` of context to the
        Context of the included/rendered template as well.
        """
        c = Context({})
        t = self.engine.from_string("{% load inclusion %}{% inclusion_tag_use_l10n %}")
        self.assertEqual(t.render(c).strip(), "None")

        c.use_l10n = True
        self.assertEqual(t.render(c).strip(), "True")

    def test_no_render_side_effect(self):
        """
        #23441 -- InclusionNode shouldn't modify its nodelist at render time.
        """
        engine = Engine(app_dirs=True, libraries=LIBRARIES)
        template = engine.from_string("{% load inclusion %}{% inclusion_no_params %}")
        count = template.nodelist.get_nodes_by_type(Node)
        template.render(Context({}))
        self.assertEqual(template.nodelist.get_nodes_by_type(Node), count)

    def test_render_context_is_cleared(self):
        """
        #24555 -- InclusionNode should push and pop the render_context stack
        when rendering. Otherwise, leftover values such as blocks from
        extending can interfere with subsequent rendering.
        """
        engine = Engine(app_dirs=True, libraries=LIBRARIES)
        template = engine.from_string(
            "{% load inclusion %}{% inclusion_extends1 %}{% inclusion_extends2 %}"
        )
        self.assertEqual(template.render(Context({})).strip(), "one\ntwo")


class TemplateTagLoadingTests(SimpleTestCase):
    @classmethod
    def setUpClass(cls):
        cls.egg_dir = os.path.join(ROOT, "eggs")
        super().setUpClass()

    def test_load_error(self):
        msg = (
            "Invalid template library specified. ImportError raised when "
            "trying to load 'template_tests.broken_tag': cannot import name "
            "'Xtemplate'"
        )
        with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
            Engine(libraries={"broken_tag": "template_tests.broken_tag"})

    def test_load_error_egg(self):
        egg_name = "%s/tagsegg.egg" % self.egg_dir
        msg = (
            "Invalid template library specified. ImportError raised when "
            "trying to load 'tagsegg.templatetags.broken_egg': cannot "
            "import name 'Xtemplate'"
        )
        with extend_sys_path(egg_name):
            with self.assertRaisesMessage(InvalidTemplateLibrary, msg):
                Engine(libraries={"broken_egg": "tagsegg.templatetags.broken_egg"})

    def test_load_working_egg(self):
        ttext = "{% load working_egg %}"
        egg_name = "%s/tagsegg.egg" % self.egg_dir
        with extend_sys_path(egg_name):
            engine = Engine(
                libraries={
                    "working_egg": "tagsegg.templatetags.working_egg",
                }
            )
            engine.from_string(ttext)

    def test_load_annotated_function(self):
        Engine(
            libraries={
                "annotated_tag_function": "template_tests.annotated_tag_function",
            }
        )