test_extends.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. from django.test import SimpleTestCase
  2. from ..utils import setup
  3. inheritance_templates = {
  4. 'inheritance01': "1{% block first %}&{% endblock %}3{% block second %}_{% endblock %}",
  5. 'inheritance02': "{% extends 'inheritance01' %}"
  6. "{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
  7. 'inheritance03': "{% extends 'inheritance02' %}",
  8. 'inheritance04': "{% extends 'inheritance01' %}",
  9. 'inheritance05': "{% extends 'inheritance02' %}",
  10. 'inheritance06': "{% extends foo %}",
  11. 'inheritance07': "{% extends 'inheritance01' %}{% block second %}5{% endblock %}",
  12. 'inheritance08': "{% extends 'inheritance02' %}{% block second %}5{% endblock %}",
  13. 'inheritance09': "{% extends 'inheritance04' %}",
  14. 'inheritance10': "{% extends 'inheritance04' %} ",
  15. 'inheritance11': "{% extends 'inheritance04' %}"
  16. "{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
  17. 'inheritance12': "{% extends 'inheritance07' %}{% block first %}2{% endblock %}",
  18. 'inheritance13': "{% extends 'inheritance02' %}"
  19. "{% block first %}a{% endblock %}{% block second %}b{% endblock %}",
  20. 'inheritance14': "{% extends 'inheritance01' %}{% block newblock %}NO DISPLAY{% endblock %}",
  21. 'inheritance15': "{% extends 'inheritance01' %}"
  22. "{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}",
  23. 'inheritance16': "{% extends 'inheritance15' %}{% block inner %}out{% endblock %}",
  24. 'inheritance17': "{% load testtags %}{% block first %}1234{% endblock %}",
  25. 'inheritance18': "{% load testtags %}{% echo this that theother %}5678",
  26. 'inheritance19': "{% extends 'inheritance01' %}"
  27. "{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}",
  28. 'inheritance20': "{% extends 'inheritance01' %}{% block first %}{{ block.super }}a{% endblock %}",
  29. 'inheritance21': "{% extends 'inheritance02' %}{% block first %}{{ block.super }}a{% endblock %}",
  30. 'inheritance22': "{% extends 'inheritance04' %}{% block first %}{{ block.super }}a{% endblock %}",
  31. 'inheritance23': "{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}",
  32. 'inheritance24': "{% extends context_template %}"
  33. "{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
  34. 'inheritance25': "{% extends context_template.1 %}"
  35. "{% block first %}2{% endblock %}{% block second %}4{% endblock %}",
  36. 'inheritance26': "no tags",
  37. 'inheritance27': "{% extends 'inheritance26' %}",
  38. 'inheritance 28': "{% block first %}!{% endblock %}",
  39. 'inheritance29': "{% extends 'inheritance 28' %}",
  40. 'inheritance30': "1{% if optional %}{% block opt %}2{% endblock %}{% endif %}3",
  41. 'inheritance31': "{% extends 'inheritance30' %}{% block opt %}two{% endblock %}",
  42. 'inheritance32': "{% extends 'inheritance30' %}{% block opt %}two{% endblock %}",
  43. 'inheritance33': "1{% ifequal optional 1 %}{% block opt %}2{% endblock %}{% endifequal %}3",
  44. 'inheritance34': "{% extends 'inheritance33' %}{% block opt %}two{% endblock %}",
  45. 'inheritance35': "{% extends 'inheritance33' %}{% block opt %}two{% endblock %}",
  46. 'inheritance36': "{% for n in numbers %}_{% block opt %}{{ n }}{% endblock %}{% endfor %}_",
  47. 'inheritance37': "{% extends 'inheritance36' %}{% block opt %}X{% endblock %}",
  48. 'inheritance38': "{% extends 'inheritance36' %}{% block opt %}X{% endblock %}",
  49. 'inheritance39': "{% extends 'inheritance30' %}{% block opt %}new{{ block.super }}{% endblock %}",
  50. 'inheritance40': "{% extends 'inheritance33' %}{% block opt %}new{{ block.super }}{% endblock %}",
  51. 'inheritance41': "{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}",
  52. 'inheritance42': "{% extends 'inheritance02'|cut:' ' %}",
  53. }
  54. class InheritanceTests(SimpleTestCase):
  55. libraries = {'testtags': 'template_tests.templatetags.testtags'}
  56. @setup(inheritance_templates)
  57. def test_inheritance01(self):
  58. """
  59. Standard template with no inheritance
  60. """
  61. output = self.engine.render_to_string('inheritance01')
  62. self.assertEqual(output, '1&3_')
  63. @setup(inheritance_templates)
  64. def test_inheritance02(self):
  65. """
  66. Standard two-level inheritance
  67. """
  68. output = self.engine.render_to_string('inheritance02')
  69. self.assertEqual(output, '1234')
  70. @setup(inheritance_templates)
  71. def test_inheritance03(self):
  72. """
  73. Three-level with no redefinitions on third level
  74. """
  75. output = self.engine.render_to_string('inheritance03')
  76. self.assertEqual(output, '1234')
  77. @setup(inheritance_templates)
  78. def test_inheritance04(self):
  79. """
  80. Two-level with no redefinitions on second level
  81. """
  82. output = self.engine.render_to_string('inheritance04')
  83. self.assertEqual(output, '1&3_')
  84. @setup(inheritance_templates)
  85. def test_inheritance05(self):
  86. """
  87. Two-level with double quotes instead of single quotes
  88. """
  89. output = self.engine.render_to_string('inheritance05')
  90. self.assertEqual(output, '1234')
  91. @setup(inheritance_templates)
  92. def test_inheritance06(self):
  93. """
  94. Three-level with variable parent-template name
  95. """
  96. output = self.engine.render_to_string('inheritance06', {'foo': 'inheritance02'})
  97. self.assertEqual(output, '1234')
  98. @setup(inheritance_templates)
  99. def test_inheritance07(self):
  100. """
  101. Two-level with one block defined, one block not defined
  102. """
  103. output = self.engine.render_to_string('inheritance07')
  104. self.assertEqual(output, '1&35')
  105. @setup(inheritance_templates)
  106. def test_inheritance08(self):
  107. """
  108. Three-level with one block defined on this level, two blocks
  109. defined next level
  110. """
  111. output = self.engine.render_to_string('inheritance08')
  112. self.assertEqual(output, '1235')
  113. @setup(inheritance_templates)
  114. def test_inheritance09(self):
  115. """
  116. Three-level with second and third levels blank
  117. """
  118. output = self.engine.render_to_string('inheritance09')
  119. self.assertEqual(output, '1&3_')
  120. @setup(inheritance_templates)
  121. def test_inheritance10(self):
  122. """
  123. Three-level with space NOT in a block -- should be ignored
  124. """
  125. output = self.engine.render_to_string('inheritance10')
  126. self.assertEqual(output, '1&3_')
  127. @setup(inheritance_templates)
  128. def test_inheritance11(self):
  129. """
  130. Three-level with both blocks defined on this level, but none on
  131. second level
  132. """
  133. output = self.engine.render_to_string('inheritance11')
  134. self.assertEqual(output, '1234')
  135. @setup(inheritance_templates)
  136. def test_inheritance12(self):
  137. """
  138. Three-level with this level providing one and second level
  139. providing the other
  140. """
  141. output = self.engine.render_to_string('inheritance12')
  142. self.assertEqual(output, '1235')
  143. @setup(inheritance_templates)
  144. def test_inheritance13(self):
  145. """
  146. Three-level with this level overriding second level
  147. """
  148. output = self.engine.render_to_string('inheritance13')
  149. self.assertEqual(output, '1a3b')
  150. @setup(inheritance_templates)
  151. def test_inheritance14(self):
  152. """
  153. A block defined only in a child template shouldn't be displayed
  154. """
  155. output = self.engine.render_to_string('inheritance14')
  156. self.assertEqual(output, '1&3_')
  157. @setup(inheritance_templates)
  158. def test_inheritance15(self):
  159. """
  160. A block within another block
  161. """
  162. output = self.engine.render_to_string('inheritance15')
  163. self.assertEqual(output, '12inner3_')
  164. @setup(inheritance_templates)
  165. def test_inheritance16(self):
  166. """
  167. A block within another block (level 2)
  168. """
  169. output = self.engine.render_to_string('inheritance16')
  170. self.assertEqual(output, '12out3_')
  171. @setup(inheritance_templates)
  172. def test_inheritance17(self):
  173. """
  174. {% load %} tag (parent -- setup for exception04)
  175. """
  176. output = self.engine.render_to_string('inheritance17')
  177. self.assertEqual(output, '1234')
  178. @setup(inheritance_templates)
  179. def test_inheritance18(self):
  180. """
  181. {% load %} tag (standard usage, without inheritance)
  182. """
  183. output = self.engine.render_to_string('inheritance18')
  184. self.assertEqual(output, 'this that theother5678')
  185. @setup(inheritance_templates)
  186. def test_inheritance19(self):
  187. """
  188. {% load %} tag (within a child template)
  189. """
  190. output = self.engine.render_to_string('inheritance19')
  191. self.assertEqual(output, '140056783_')
  192. @setup(inheritance_templates)
  193. def test_inheritance20(self):
  194. """
  195. Two-level inheritance with {{ block.super }}
  196. """
  197. output = self.engine.render_to_string('inheritance20')
  198. self.assertEqual(output, '1&a3_')
  199. @setup(inheritance_templates)
  200. def test_inheritance21(self):
  201. """
  202. Three-level inheritance with {{ block.super }} from parent
  203. """
  204. output = self.engine.render_to_string('inheritance21')
  205. self.assertEqual(output, '12a34')
  206. @setup(inheritance_templates)
  207. def test_inheritance22(self):
  208. """
  209. Three-level inheritance with {{ block.super }} from grandparent
  210. """
  211. output = self.engine.render_to_string('inheritance22')
  212. self.assertEqual(output, '1&a3_')
  213. @setup(inheritance_templates)
  214. def test_inheritance23(self):
  215. """
  216. Three-level inheritance with {{ block.super }} from parent and
  217. grandparent
  218. """
  219. output = self.engine.render_to_string('inheritance23')
  220. self.assertEqual(output, '1&ab3_')
  221. @setup(inheritance_templates)
  222. def test_inheritance24(self):
  223. """
  224. Inheritance from local context without use of template loader
  225. """
  226. context_template = self.engine.from_string("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}")
  227. output = self.engine.render_to_string('inheritance24', {'context_template': context_template})
  228. self.assertEqual(output, '1234')
  229. @setup(inheritance_templates)
  230. def test_inheritance25(self):
  231. """
  232. Inheritance from local context with variable parent template
  233. """
  234. context_template = [
  235. self.engine.from_string("Wrong"),
  236. self.engine.from_string("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}"),
  237. ]
  238. output = self.engine.render_to_string('inheritance25', {'context_template': context_template})
  239. self.assertEqual(output, '1234')
  240. @setup(inheritance_templates)
  241. def test_inheritance26(self):
  242. """
  243. Set up a base template to extend
  244. """
  245. output = self.engine.render_to_string('inheritance26')
  246. self.assertEqual(output, 'no tags')
  247. @setup(inheritance_templates)
  248. def test_inheritance27(self):
  249. """
  250. Inheritance from a template that doesn't have any blocks
  251. """
  252. output = self.engine.render_to_string('inheritance27')
  253. self.assertEqual(output, 'no tags')
  254. @setup(inheritance_templates)
  255. def test_inheritance_28(self):
  256. """
  257. Set up a base template with a space in it.
  258. """
  259. output = self.engine.render_to_string('inheritance 28')
  260. self.assertEqual(output, '!')
  261. @setup(inheritance_templates)
  262. def test_inheritance29(self):
  263. """
  264. Inheritance from a template with a space in its name should work.
  265. """
  266. output = self.engine.render_to_string('inheritance29')
  267. self.assertEqual(output, '!')
  268. @setup(inheritance_templates)
  269. def test_inheritance30(self):
  270. """
  271. Base template, putting block in a conditional {% if %} tag
  272. """
  273. output = self.engine.render_to_string('inheritance30', {'optional': True})
  274. self.assertEqual(output, '123')
  275. # Inherit from a template with block wrapped in an {% if %} tag
  276. # (in parent), still gets overridden
  277. @setup(inheritance_templates)
  278. def test_inheritance31(self):
  279. output = self.engine.render_to_string('inheritance31', {'optional': True})
  280. self.assertEqual(output, '1two3')
  281. @setup(inheritance_templates)
  282. def test_inheritance32(self):
  283. output = self.engine.render_to_string('inheritance32')
  284. self.assertEqual(output, '13')
  285. @setup(inheritance_templates)
  286. def test_inheritance33(self):
  287. """
  288. Base template, putting block in a conditional {% ifequal %} tag
  289. """
  290. output = self.engine.render_to_string('inheritance33', {'optional': 1})
  291. self.assertEqual(output, '123')
  292. @setup(inheritance_templates)
  293. def test_inheritance34(self):
  294. """
  295. Inherit from a template with block wrapped in an {% ifequal %} tag
  296. (in parent), still gets overridden
  297. """
  298. output = self.engine.render_to_string('inheritance34', {'optional': 1})
  299. self.assertEqual(output, '1two3')
  300. @setup(inheritance_templates)
  301. def test_inheritance35(self):
  302. """
  303. Inherit from a template with block wrapped in an {% ifequal %} tag
  304. (in parent), still gets overridden
  305. """
  306. output = self.engine.render_to_string('inheritance35', {'optional': 2})
  307. self.assertEqual(output, '13')
  308. @setup(inheritance_templates)
  309. def test_inheritance36(self):
  310. """
  311. Base template, putting block in a {% for %} tag
  312. """
  313. output = self.engine.render_to_string('inheritance36', {'numbers': '123'})
  314. self.assertEqual(output, '_1_2_3_')
  315. @setup(inheritance_templates)
  316. def test_inheritance37(self):
  317. """
  318. Inherit from a template with block wrapped in an {% for %} tag
  319. (in parent), still gets overridden
  320. """
  321. output = self.engine.render_to_string('inheritance37', {'numbers': '123'})
  322. self.assertEqual(output, '_X_X_X_')
  323. @setup(inheritance_templates)
  324. def test_inheritance38(self):
  325. """
  326. Inherit from a template with block wrapped in an {% for %} tag
  327. (in parent), still gets overridden
  328. """
  329. output = self.engine.render_to_string('inheritance38')
  330. self.assertEqual(output, '_')
  331. # The super block will still be found.
  332. @setup(inheritance_templates)
  333. def test_inheritance39(self):
  334. output = self.engine.render_to_string('inheritance39', {'optional': True})
  335. self.assertEqual(output, '1new23')
  336. @setup(inheritance_templates)
  337. def test_inheritance40(self):
  338. output = self.engine.render_to_string('inheritance40', {'optional': 1})
  339. self.assertEqual(output, '1new23')
  340. @setup(inheritance_templates)
  341. def test_inheritance41(self):
  342. output = self.engine.render_to_string('inheritance41', {'numbers': '123'})
  343. self.assertEqual(output, '_new1_new2_new3_')
  344. @setup(inheritance_templates)
  345. def test_inheritance42(self):
  346. """
  347. Expression starting and ending with a quote
  348. """
  349. output = self.engine.render_to_string('inheritance42')
  350. self.assertEqual(output, '1234')