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
|
from django.template import NodeList, TemplateSyntaxError
from django.template.base import Node
from django.template.loader_tags import ExtendsNode
from django.test import SimpleTestCase
from ..utils import setup
inheritance_templates = {
"inheritance01": (
"1{% block first %}&{% endblock %}3{% block second %}_{% endblock %}"
),
"inheritance02": "{% extends 'inheritance01' %}"
"{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
"inheritance03": "{% extends 'inheritance02' %}",
"inheritance04": "{% extends 'inheritance01' %}",
"inheritance05": "{% extends 'inheritance02' %}",
"inheritance06": "{% extends foo %}",
"inheritance07": "{% extends 'inheritance01' %}{% block second %}5{% endblock %}",
"inheritance08": "{% extends 'inheritance02' %}{% block second %}5{% endblock %}",
"inheritance09": "{% extends 'inheritance04' %}",
"inheritance10": "{% extends 'inheritance04' %} ",
"inheritance11": "{% extends 'inheritance04' %}"
"{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
"inheritance12": "{% extends 'inheritance07' %}{% block first %}2{% endblock %}",
"inheritance13": "{% extends 'inheritance02' %}"
"{% block first %}a{% endblock %}{% block second %}b{% endblock %}",
"inheritance14": (
"{% extends 'inheritance01' %}{% block newblock %}NO DISPLAY{% endblock %}"
),
"inheritance15": "{% extends 'inheritance01' %}"
"{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}",
"inheritance16": "{% extends 'inheritance15' %}{% block inner %}out{% endblock %}",
"inheritance17": "{% load testtags %}{% block first %}1234{% endblock %}",
"inheritance18": "{% load testtags %}{% echo this that theother %}5678",
"inheritance19": "{% extends 'inheritance01' %}"
"{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}",
"inheritance20": (
"{% extends 'inheritance01' %}{% block first %}{{ block.super }}a{% endblock %}"
),
"inheritance21": (
"{% extends 'inheritance02' %}{% block first %}{{ block.super }}a{% endblock %}"
),
"inheritance22": (
"{% extends 'inheritance04' %}{% block first %}{{ block.super }}a{% endblock %}"
),
"inheritance23": (
"{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}"
),
"inheritance24": "{% extends context_template %}"
"{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
"inheritance25": "{% extends context_template.1 %}"
"{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
"inheritance26": "no tags",
"inheritance27": "{% extends 'inheritance26' %}",
"inheritance 28": "{% block first %}!{% endblock %}",
"inheritance29": "{% extends 'inheritance 28' %}",
"inheritance30": "1{% if optional %}{% block opt %}2{% endblock %}{% endif %}3",
"inheritance31": "{% extends 'inheritance30' %}{% block opt %}two{% endblock %}",
"inheritance32": "{% extends 'inheritance30' %}{% block opt %}two{% endblock %}",
"inheritance33": (
"1{% if optional == 1 %}{% block opt %}2{% endblock %}{% endif %}3"
),
"inheritance34": "{% extends 'inheritance33' %}{% block opt %}two{% endblock %}",
"inheritance35": "{% extends 'inheritance33' %}{% block opt %}two{% endblock %}",
"inheritance36": (
"{% for n in numbers %}_{% block opt %}{{ n }}{% endblock %}{% endfor %}_"
),
"inheritance37": "{% extends 'inheritance36' %}{% block opt %}X{% endblock %}",
"inheritance38": "{% extends 'inheritance36' %}{% block opt %}X{% endblock %}",
"inheritance39": (
"{% extends 'inheritance30' %}{% block opt %}new{{ block.super }}{% endblock %}"
),
"inheritance40": (
"{% extends 'inheritance33' %}{% block opt %}new{{ block.super }}{% endblock %}"
),
"inheritance41": (
"{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}"
),
"inheritance42": "{% extends 'inheritance02'|cut:' ' %}",
"inheritance_empty": "{% extends %}",
"extends_duplicate": "{% extends 'base.html' %}{% extends 'base.html' %}",
"duplicate_block": (
"{% extends 'base.html' %}{% block content %}2{% endblock %}{% block content %}"
"4{% endblock %}"
),
}
class InheritanceTests(SimpleTestCase):
libraries = {"testtags": "template_tests.templatetags.testtags"}
@setup(inheritance_templates)
def test_inheritance01(self):
"""
Standard template with no inheritance
"""
output = self.engine.render_to_string("inheritance01")
self.assertEqual(output, "1&3_")
@setup(inheritance_templates)
def test_inheritance02(self):
"""
Standard two-level inheritance
"""
output = self.engine.render_to_string("inheritance02")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance03(self):
"""
Three-level with no redefinitions on third level
"""
output = self.engine.render_to_string("inheritance03")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance04(self):
"""
Two-level with no redefinitions on second level
"""
output = self.engine.render_to_string("inheritance04")
self.assertEqual(output, "1&3_")
@setup(inheritance_templates)
def test_inheritance05(self):
"""
Two-level with double quotes instead of single quotes
"""
output = self.engine.render_to_string("inheritance05")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance06(self):
"""
Three-level with variable parent-template name
"""
output = self.engine.render_to_string("inheritance06", {"foo": "inheritance02"})
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance07(self):
"""
Two-level with one block defined, one block not defined
"""
output = self.engine.render_to_string("inheritance07")
self.assertEqual(output, "1&35")
@setup(inheritance_templates)
def test_inheritance08(self):
"""
Three-level with one block defined on this level, two blocks
defined next level
"""
output = self.engine.render_to_string("inheritance08")
self.assertEqual(output, "1235")
@setup(inheritance_templates)
def test_inheritance09(self):
"""
Three-level with second and third levels blank
"""
output = self.engine.render_to_string("inheritance09")
self.assertEqual(output, "1&3_")
@setup(inheritance_templates)
def test_inheritance10(self):
"""
Three-level with space NOT in a block -- should be ignored
"""
output = self.engine.render_to_string("inheritance10")
self.assertEqual(output, "1&3_")
@setup(inheritance_templates)
def test_inheritance11(self):
"""
Three-level with both blocks defined on this level, but none on
second level
"""
output = self.engine.render_to_string("inheritance11")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance12(self):
"""
Three-level with this level providing one and second level
providing the other
"""
output = self.engine.render_to_string("inheritance12")
self.assertEqual(output, "1235")
@setup(inheritance_templates)
def test_inheritance13(self):
"""
Three-level with this level overriding second level
"""
output = self.engine.render_to_string("inheritance13")
self.assertEqual(output, "1a3b")
@setup(inheritance_templates)
def test_inheritance14(self):
"""
A block defined only in a child template shouldn't be displayed
"""
output = self.engine.render_to_string("inheritance14")
self.assertEqual(output, "1&3_")
@setup(inheritance_templates)
def test_inheritance15(self):
"""
A block within another block
"""
output = self.engine.render_to_string("inheritance15")
self.assertEqual(output, "12inner3_")
@setup(inheritance_templates)
def test_inheritance16(self):
"""
A block within another block (level 2)
"""
output = self.engine.render_to_string("inheritance16")
self.assertEqual(output, "12out3_")
@setup(inheritance_templates)
def test_inheritance17(self):
"""
{% load %} tag (parent -- setup for exception04)
"""
output = self.engine.render_to_string("inheritance17")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance18(self):
"""
{% load %} tag (standard usage, without inheritance)
"""
output = self.engine.render_to_string("inheritance18")
self.assertEqual(output, "this that theother5678")
@setup(inheritance_templates)
def test_inheritance19(self):
"""
{% load %} tag (within a child template)
"""
output = self.engine.render_to_string("inheritance19")
self.assertEqual(output, "140056783_")
@setup(inheritance_templates)
def test_inheritance20(self):
"""
Two-level inheritance with {{ block.super }}
"""
output = self.engine.render_to_string("inheritance20")
self.assertEqual(output, "1&a3_")
@setup(inheritance_templates)
def test_inheritance21(self):
"""
Three-level inheritance with {{ block.super }} from parent
"""
output = self.engine.render_to_string("inheritance21")
self.assertEqual(output, "12a34")
@setup(inheritance_templates)
def test_inheritance22(self):
"""
Three-level inheritance with {{ block.super }} from grandparent
"""
output = self.engine.render_to_string("inheritance22")
self.assertEqual(output, "1&a3_")
@setup(inheritance_templates)
def test_inheritance23(self):
"""
Three-level inheritance with {{ block.super }} from parent and
grandparent
"""
output = self.engine.render_to_string("inheritance23")
self.assertEqual(output, "1&ab3_")
@setup(inheritance_templates)
def test_inheritance24(self):
"""
Inheritance from local context without use of template loader
"""
context_template = self.engine.from_string(
"1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}"
)
output = self.engine.render_to_string(
"inheritance24", {"context_template": context_template}
)
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance25(self):
"""
Inheritance from local context with variable parent template
"""
context_template = [
self.engine.from_string("Wrong"),
self.engine.from_string(
"1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}"
),
]
output = self.engine.render_to_string(
"inheritance25", {"context_template": context_template}
)
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance26(self):
"""
Set up a base template to extend
"""
output = self.engine.render_to_string("inheritance26")
self.assertEqual(output, "no tags")
@setup(inheritance_templates)
def test_inheritance27(self):
"""
Inheritance from a template that doesn't have any blocks
"""
output = self.engine.render_to_string("inheritance27")
self.assertEqual(output, "no tags")
@setup(inheritance_templates)
def test_inheritance_28(self):
"""
Set up a base template with a space in it.
"""
output = self.engine.render_to_string("inheritance 28")
self.assertEqual(output, "!")
@setup(inheritance_templates)
def test_inheritance29(self):
"""
Inheritance from a template with a space in its name should work.
"""
output = self.engine.render_to_string("inheritance29")
self.assertEqual(output, "!")
@setup(inheritance_templates)
def test_inheritance30(self):
"""
Base template, putting block in a conditional {% if %} tag
"""
output = self.engine.render_to_string("inheritance30", {"optional": True})
self.assertEqual(output, "123")
# Inherit from a template with block wrapped in an {% if %} tag
# (in parent), still gets overridden
@setup(inheritance_templates)
def test_inheritance31(self):
output = self.engine.render_to_string("inheritance31", {"optional": True})
self.assertEqual(output, "1two3")
@setup(inheritance_templates)
def test_inheritance32(self):
output = self.engine.render_to_string("inheritance32")
self.assertEqual(output, "13")
@setup(inheritance_templates)
def test_inheritance33(self):
"""
Base template, putting block in a conditional {% if %} tag
"""
output = self.engine.render_to_string("inheritance33", {"optional": 1})
self.assertEqual(output, "123")
@setup(inheritance_templates)
def test_inheritance34(self):
"""
Inherit from a template with block wrapped in an {% if %} tag
(in parent), still gets overridden
"""
output = self.engine.render_to_string("inheritance34", {"optional": 1})
self.assertEqual(output, "1two3")
@setup(inheritance_templates)
def test_inheritance35(self):
"""
Inherit from a template with block wrapped in an {% if %} tag
(in parent), still gets overridden
"""
output = self.engine.render_to_string("inheritance35", {"optional": 2})
self.assertEqual(output, "13")
@setup(inheritance_templates)
def test_inheritance36(self):
"""
Base template, putting block in a {% for %} tag
"""
output = self.engine.render_to_string("inheritance36", {"numbers": "123"})
self.assertEqual(output, "_1_2_3_")
@setup(inheritance_templates)
def test_inheritance37(self):
"""
Inherit from a template with block wrapped in an {% for %} tag
(in parent), still gets overridden
"""
output = self.engine.render_to_string("inheritance37", {"numbers": "123"})
self.assertEqual(output, "_X_X_X_")
@setup(inheritance_templates)
def test_inheritance38(self):
"""
Inherit from a template with block wrapped in an {% for %} tag
(in parent), still gets overridden
"""
output = self.engine.render_to_string("inheritance38")
self.assertEqual(output, "_")
# The super block will still be found.
@setup(inheritance_templates)
def test_inheritance39(self):
output = self.engine.render_to_string("inheritance39", {"optional": True})
self.assertEqual(output, "1new23")
@setup(inheritance_templates)
def test_inheritance40(self):
output = self.engine.render_to_string("inheritance40", {"optional": 1})
self.assertEqual(output, "1new23")
@setup(inheritance_templates)
def test_inheritance41(self):
output = self.engine.render_to_string("inheritance41", {"numbers": "123"})
self.assertEqual(output, "_new1_new2_new3_")
@setup(inheritance_templates)
def test_inheritance42(self):
"""
Expression starting and ending with a quote
"""
output = self.engine.render_to_string("inheritance42")
self.assertEqual(output, "1234")
@setup(inheritance_templates)
def test_inheritance_empty(self):
with self.assertRaisesMessage(
TemplateSyntaxError, "'extends' takes one argument"
):
self.engine.render_to_string("inheritance_empty")
@setup(inheritance_templates)
def test_extends_duplicate(self):
msg = "'extends' cannot appear more than once in the same template"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string("extends_duplicate")
@setup(inheritance_templates)
def test_duplicate_block(self):
msg = "'block' tag with name 'content' appears more than once"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string("duplicate_block")
class ExtendsNodeTests(SimpleTestCase):
def test_extends_node_repr(self):
extends_node = ExtendsNode(
nodelist=NodeList([]),
parent_name=Node(),
template_dirs=[],
)
self.assertEqual(repr(extends_node), "<ExtendsNode: extends None>")
|