test_missing.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. # -*- coding: utf-8 -*-
  2. from __future__ import with_statement
  3. import django
  4. from django import template, test as django_test
  5. from django.core import urlresolvers
  6. from django.test import client
  7. from django.utils import encoding, html
  8. from django.views import debug
  9. class NoErrorClient(client.Client):
  10. """
  11. Test client which does not specially handle exceptions.
  12. Useful for testing HTTP 500 error handlers.
  13. """
  14. def store_exc_info(self, **kwargs):
  15. pass
  16. @django_test.override_settings(ROOT_URLCONF='missing.tests.context_urls')
  17. class ContextTagsTest(django_test.TestCase):
  18. def test_setcontext_1(self):
  19. with self.assertRaises(template.TemplateSyntaxError) as cm:
  20. t = template.Template("""
  21. {% setcontext foo bar %}
  22. FooBar
  23. {% endsetcontext %}
  24. """)
  25. self.assertIn('tag takes 2 arguments and the first argument', str(cm.exception))
  26. def test_setcontext_2(self):
  27. with self.assertRaises(template.TemplateSyntaxError) as cm:
  28. t = template.Template("""
  29. {% setcontext %}
  30. FooBar
  31. {% endsetcontext %}
  32. """)
  33. self.assertIn('tag takes 2 arguments and the first argument', str(cm.exception))
  34. def test_setcontext_3(self):
  35. t = template.Template("""
  36. {% setcontext as variable %}
  37. FooBar
  38. {% endsetcontext %}
  39. """)
  40. c = template.Context()
  41. o = t.render(c).strip()
  42. self.assertIn('variable', c)
  43. self.assertEquals(c['variable'].strip(), 'FooBar')
  44. self.assertEquals(o, '')
  45. def test_contextblock_1(self):
  46. with self.assertRaises(template.TemplateSyntaxError) as cm:
  47. t = template.Template("""
  48. {{ something }}
  49. {% contextblock %}
  50. {% endcontextblock %}
  51. """)
  52. self.assertIn('must be the first tag in the template', str(cm.exception))
  53. def test_contextblock_2(self):
  54. base = template.Template("""
  55. {% contextblock %}{% if double_call %}{% setcontext as bug %}bug{% endsetcontext %}{% endif %}{% endcontextblock %}{% spaceless %}<html>
  56. <body>
  57. <head>
  58. <title>{{ title }}</title>
  59. </head>
  60. <body>
  61. <h1>{{ title }}</h1>
  62. <p><a href="{{ homepage }}">{{ title }}</a></p>
  63. </body>
  64. </body>
  65. </html>{% endspaceless %}""")
  66. t = template.Template("""
  67. {% extends base %}
  68. {% contextblock %}
  69. {% load i18n %}
  70. {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %}
  71. {% url "homepage" as homepage %}
  72. {% setcontext as double_call %}true{% endsetcontext %}
  73. {{ block.super }}
  74. {% endcontextblock %}
  75. """)
  76. c = template.Context({
  77. 'username': 'Username',
  78. 'base': base,
  79. })
  80. o = t.render(c).strip()
  81. self.assertNotIn('bug', c)
  82. self.assertEqual(o, """<html><body><head><title>Username's blog</title></head><body><h1>Username's blog</h1><p><a href="/homepage/">Username's blog</a></p></body></body></html>""")
  83. def test_contextblock_3(self):
  84. base = template.Template("""
  85. {% contextblock %}{% if double_call %}{% setcontext as bug %}bug{% endsetcontext %}{% endif %}{% setcontext as double_call %}true{% endsetcontext %}{% endcontextblock %}{% spaceless %}<html>
  86. <body>
  87. <head>
  88. <title>{{ title }}</title>
  89. </head>
  90. <body>
  91. <h1>{{ title }}</h1>
  92. <p><a href="{{ homepage }}">{{ title }}</a></p>
  93. </body>
  94. </body>
  95. </html>{% endspaceless %}""")
  96. t = template.Template("""
  97. {% extends base %}
  98. {% contextblock %}
  99. {% load i18n %}
  100. {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %}
  101. {% url "homepage" as homepage %}
  102. {{ block.super }}
  103. {% endcontextblock %}
  104. """)
  105. c = template.Context({
  106. 'username': 'Username',
  107. 'base': base,
  108. })
  109. o = t.render(c).strip()
  110. self.assertNotIn('bug', c)
  111. self.assertEqual(o, """<html><body><head><title>Username's blog</title></head><body><h1>Username's blog</h1><p><a href="/homepage/">Username's blog</a></p></body></body></html>""")
  112. def test_contextblock_4(self):
  113. base1 = template.Template("""
  114. {% contextblock %}{% if double_call %}{% setcontext as bug %}bug{% endsetcontext %}{% endif %}{% endcontextblock %}{% spaceless %}<html>
  115. <body>
  116. <head>
  117. <title>{{ title }}</title>
  118. </head>
  119. <body>
  120. <h1>{{ title }}</h1>
  121. <p><a href="{{ homepage }}">{{ title }}</a></p>
  122. </body>
  123. </body>
  124. </html>{% endspaceless %}""")
  125. base2 = template.Template("""
  126. {% extends base1 %}
  127. {% contextblock %}
  128. {% url "homepage" as homepage %}
  129. {% endcontextblock %}
  130. """)
  131. t = template.Template("""
  132. {% extends base2 %}
  133. {% contextblock %}
  134. {% load i18n %}
  135. {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %}
  136. {% setcontext as double_call %}true{% endsetcontext %}
  137. {{ block.super }}
  138. {% endcontextblock %}
  139. """)
  140. c = template.Context({
  141. 'username': 'Username',
  142. 'base1': base1,
  143. 'base2': base2,
  144. })
  145. o = t.render(c).strip()
  146. self.assertNotIn('bug', c)
  147. self.assertEqual(o, """<html><body><head><title>Username's blog</title></head><body><h1>Username's blog</h1><p><a href="/homepage/">Username's blog</a></p></body></body></html>""")
  148. def test_contextblock_5(self):
  149. base1 = template.Template("""
  150. {% contextblock %}{% endcontextblock %}{% spaceless %}<html>
  151. <body>
  152. <head>
  153. <title>{{ title }}</title>
  154. </head>
  155. <body>
  156. <h1>{{ title }}</h1>
  157. <p><a href="{{ homepage }}">{{ title }}</a></p>
  158. </body>
  159. </body>
  160. </html>{% endspaceless %}""")
  161. base2 = template.Template("""
  162. {% extends base1 %}
  163. {% contextblock %}
  164. {% url "homepage" as homepage %}
  165. {% if double_call %}{% setcontext as bug %}bug{% endsetcontext %}{% endif %}
  166. {% setcontext as double_call %}true{% endsetcontext %}
  167. {% endcontextblock %}""")
  168. t = template.Template("""
  169. {% extends base2 %}
  170. {% contextblock %}
  171. {% load i18n %}
  172. {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %}
  173. {{ block.super }}
  174. {% endcontextblock %}
  175. """)
  176. c = template.Context({
  177. 'username': 'Username',
  178. 'base1': base1,
  179. 'base2': base2,
  180. })
  181. o = t.render(c).strip()
  182. self.assertNotIn('bug', c)
  183. self.assertEqual(o, """<html><body><head><title>Username's blog</title></head><body><h1>Username's blog</h1><p><a href="/homepage/">Username's blog</a></p></body></body></html>""")
  184. class LangTagsTest(django_test.TestCase):
  185. def test_translate_2(self):
  186. with self.assertRaises(template.TemplateSyntaxError) as cm:
  187. t = template.Template("""
  188. {% load lang_tags %}
  189. {% translate "FooBar" %}
  190. """)
  191. self.assertEquals("'translate' did not receive value(s) for the argument(s): 'lang_code'", str(cm.exception))
  192. class ListTagsTest(django_test.TestCase):
  193. def test_split_list_1(self):
  194. with self.assertRaises(template.TemplateSyntaxError) as cm:
  195. t = template.Template("""
  196. {% load list_tags %}
  197. {{ objects|split_list }}
  198. """)
  199. self.assertEquals('split_list requires 2 arguments, 1 provided', str(cm.exception))
  200. def test_split_list_2(self):
  201. t = template.Template("""
  202. {% load list_tags %}
  203. |{% for group in objects|split_list:"4" %}{{ group|length }}|{% endfor %}
  204. """)
  205. c = template.Context({
  206. 'objects': range(10),
  207. })
  208. o = t.render(c).strip()
  209. self.assertEquals(o, '|4|4|2|')
  210. def test_split_list_3(self):
  211. t = template.Template("""
  212. {% load list_tags %}
  213. {% for group in objects|split_list:"5" %}{{ group }}{% endfor %}
  214. """)
  215. numbers = range(9)
  216. c = template.Context({
  217. 'objects': numbers,
  218. })
  219. o = t.render(c).strip()
  220. self.assertEquals(o, encoding.force_text(list(numbers[0:5])) + encoding.force_text(list(numbers[5:])))
  221. def test_split_list_4(self):
  222. t = template.Template("""
  223. {% load list_tags %}
  224. {% for group in objects|split_list:"-1" %}{{ group }}{% endfor %}
  225. """)
  226. numbers = range(14)
  227. c = template.Context({
  228. 'objects': numbers,
  229. })
  230. o = t.render(c).strip()
  231. self.assertEquals(o, '')
  232. def test_split_list_5(self):
  233. t = template.Template("""
  234. {% load list_tags %}
  235. {% for group in objects|split_list:"0" %}{{ group }}{% endfor %}
  236. """)
  237. numbers = range(5)
  238. c = template.Context({
  239. 'objects': numbers,
  240. })
  241. o = t.render(c).strip()
  242. self.assertEquals(o, '')
  243. class StringTagsTest(django_test.TestCase):
  244. def test_ensure_sentence_1(self):
  245. with self.assertRaises(template.TemplateSyntaxError) as cm:
  246. t = template.Template("""
  247. {% load string_tags %}
  248. {{ "FooBar"|ensure_sentence:"" }}
  249. """)
  250. self.assertEquals('ensure_sentence requires 1 arguments, 2 provided', str(cm.exception))
  251. def _test_string(self, first, second):
  252. t = template.Template("""
  253. {% load string_tags %}
  254. {{ string|ensure_sentence }}
  255. """)
  256. c = template.Context({
  257. 'string': first,
  258. })
  259. o = t.render(c).strip()
  260. self.assertEquals(o, second)
  261. def test_ensure_sentence_2(self):
  262. self._test_string('FooBar', 'FooBar.')
  263. def test_ensure_sentence_3(self):
  264. self._test_string('FooBar.', 'FooBar.')
  265. def test_ensure_sentence_4(self):
  266. self._test_string('FooBar?', 'FooBar?')
  267. class UrlTagsTest(django_test.TestCase):
  268. def setUp(self):
  269. self.factory = client.RequestFactory()
  270. def test_slugify2_1(self):
  271. with self.assertRaises(template.TemplateSyntaxError) as cm:
  272. t = template.Template("""
  273. {% load url_tags %}
  274. {{ "FooBar"|slugify2:"" }}
  275. """)
  276. self.assertEquals('slugify2 requires 1 arguments, 2 provided', str(cm.exception))
  277. def _test_string(self, first, second):
  278. t = template.Template("""
  279. {% load url_tags %}
  280. {{ string|slugify2 }}
  281. """)
  282. c = template.Context({
  283. 'string': first,
  284. })
  285. o = t.render(c).strip()
  286. self.assertEquals(o, second)
  287. def test_slugify2_2(self):
  288. with self.settings(DEBUG=True):
  289. self._test_string(u'Işık ılık süt iç', u'isik-ilik-sut-ic')
  290. def test_slugify2_3(self):
  291. with self.settings(DEBUG=True):
  292. self._test_string(u'ČĆŽŠĐ čćžšđ', u'cczsdj-cczsdj')
  293. def test_slugify2_4(self):
  294. with self.settings(DEBUG=True):
  295. self._test_string(u'..test foobar..', u'test-foobar')
  296. def _test_url(self, url):
  297. request = self.factory.get('/foo/')
  298. t = template.Template("""
  299. {% load url_tags %}
  300. {% fullurl url %}
  301. """)
  302. c = template.RequestContext(request, {
  303. 'request': request,
  304. 'url': url,
  305. })
  306. o = t.render(c).strip()
  307. self.assertEquals(o, request.build_absolute_uri(url))
  308. def test_fullurl_1(self):
  309. request = self.factory.get('/foo/')
  310. t = template.Template("""
  311. {% load url_tags %}
  312. {% fullurl %}
  313. """)
  314. c = template.RequestContext(request, {
  315. 'request': request,
  316. })
  317. o = t.render(c).strip()
  318. self.assertEquals(o, request.build_absolute_uri())
  319. def test_fullurl_2(self):
  320. self._test_url(None)
  321. def test_fullurl_3(self):
  322. self._test_url('/bar/')
  323. @django_test.override_settings(ROOT_URLCONF='missing.tests.urltemplate_urls')
  324. class UrlTemplateTest(django_test.TestCase):
  325. def setUp(self):
  326. self.factory = client.RequestFactory()
  327. def _test_urltemplate(self, params, result):
  328. with self.settings(DEBUG=True):
  329. t = template.Template("""
  330. {%% load url_tags %%}
  331. {%% urltemplate %s %%}
  332. """ % params)
  333. c = template.Context()
  334. o = t.render(c).strip()
  335. self.assertEquals(o, result)
  336. def test_urltemplate_simply(self):
  337. self._test_urltemplate('"test1"', '/test1/')
  338. def test_urltemplate_nonexistent(self):
  339. with self.assertRaises(urlresolvers.NoReverseMatch):
  340. self._test_urltemplate('"nonexistent"', '')
  341. def test_urltemplate_mix(self):
  342. with self.assertRaisesMessage(ValueError, "Don't mix *args and **kwargs."):
  343. self._test_urltemplate('"test_kwargs" "2000" month="12"', '/test_kwargs/2000/12/{day}/')
  344. def test_urltemplate_args1(self):
  345. self._test_urltemplate('"test_args"', '/test_args/{0}/{1}/{2}/')
  346. def test_urltemplate_args2(self):
  347. self._test_urltemplate('"test_args" "2000"', '/test_args/2000/{1}/{2}/')
  348. self._test_urltemplate('"test_args" "2000" "12"', '/test_args/2000/12/{2}/')
  349. self._test_urltemplate('"test_args" "2000" "12" "1"', '/test_args/2000/12/1/')
  350. with self.assertRaises(urlresolvers.NoReverseMatch):
  351. self._test_urltemplate('"test_args" year="2000"', '/test_args/2000/{month}/{day}/')
  352. with self.assertRaises(urlresolvers.NoReverseMatch):
  353. self._test_urltemplate('"test_args" "2000" "12" "1" "foobar"', '/test_args/2000/12/1/')
  354. def test_urltemplate_kwargs1(self):
  355. self._test_urltemplate('"test_kwargs"', '/test_kwargs/{year}/{month}/{day}/')
  356. def test_urltemplate_kwargs2(self):
  357. self._test_urltemplate('"test_kwargs" "2000"', '/test_kwargs/2000/{month}/{day}/')
  358. self._test_urltemplate('"test_kwargs" "2000" "12"', '/test_kwargs/2000/12/{day}/')
  359. self._test_urltemplate('"test_kwargs" "2000" "12" "1"', '/test_kwargs/2000/12/1/')
  360. self._test_urltemplate('"test_kwargs" year="2000"', '/test_kwargs/2000/{month}/{day}/')
  361. self._test_urltemplate('"test_kwargs" year="2000" month="12"', '/test_kwargs/2000/12/{day}/')
  362. self._test_urltemplate('"test_kwargs" year="2000" month="12" day="1"', '/test_kwargs/2000/12/1/')
  363. self._test_urltemplate('"test_kwargs" year="2000" day="1"', '/test_kwargs/2000/{month}/1/')
  364. with self.assertRaises(urlresolvers.NoReverseMatch):
  365. self._test_urltemplate('"test_kwargs" "2000" "12" "1" "foobar"', '/test_kwargs/2000/12/1/')
  366. with self.assertRaises(urlresolvers.NoReverseMatch):
  367. self._test_urltemplate('"test_kwargs" foobar="42"', '/test_kwargs/{year}/{month}/{day}/')
  368. def test_urltemplate_mixed1(self):
  369. self._test_urltemplate('"test_mixed"', '/test_mixed/{year}/{0}/{day}/')
  370. def test_urltemplate_mixed2(self):
  371. self._test_urltemplate('"test_mixed" "2000"', '/test_mixed/2000/{0}/{day}/')
  372. self._test_urltemplate('"test_mixed" "2000" "12"', '/test_mixed/2000/12/{day}/')
  373. self._test_urltemplate('"test_mixed" "2000" "12" "1"', '/test_mixed/2000/12/1/')
  374. self._test_urltemplate('"test_mixed" year="2000"', '/test_mixed/2000/{0}/{day}/')
  375. self._test_urltemplate('"test_mixed" year="2000" day="1"', '/test_mixed/2000/{0}/1/')
  376. with self.assertRaises(urlresolvers.NoReverseMatch):
  377. self._test_urltemplate('"test_mixed" "2000" "12" "1" "foobar"', '/test_mixed/2000/12/1/')
  378. with self.assertRaises(urlresolvers.NoReverseMatch):
  379. self._test_urltemplate('"test_mixed" foobar="42"', '/test_mixed/{year}/{month}/{day}/')
  380. def test_urltemplate_possible1(self):
  381. self._test_urltemplate('"test_possible"', '/test_possible/{month}/{day}/')
  382. def test_urltemplate_possible2(self):
  383. self._test_urltemplate('"test_possible" month="12"', '/test_possible/12/{day}/')
  384. def test_urltemplate_possible3(self):
  385. self._test_urltemplate('"test_possible" year="2000"', '/test_possible/2000/{day}/')
  386. def test_urltemplate_api(self):
  387. self._test_urltemplate('"api_get_schema"', '/api/{api_name}/{resource_name}/schema/')
  388. self._test_urltemplate('"api_get_schema" api_name="v1"', '/api/v1/{resource_name}/schema/')
  389. def test_urltemplate_example(self):
  390. with self.settings(DEBUG=True):
  391. t = template.Template("""
  392. {% load url_tags %}
  393. {% with variable="42" %}
  394. {% urltemplate "view_name" arg1="value" arg2=variable %}
  395. {% endwith %}
  396. """)
  397. c = template.Context()
  398. o = t.render(c).strip()
  399. self.assertEquals(o, '/some/view/value/42/{param}/')
  400. @django_test.override_settings(ROOT_URLCONF='missing.tests.safereporting_urls')
  401. class SafeExceptionReporterFilterTest(django_test.TestCase):
  402. def setUp(self):
  403. self.c = NoErrorClient(TEST_PASSWORD='foobar', TEST_COOKIE='foobar', TEST_NORMAL='ok')
  404. def test_failure(self):
  405. with self.settings(DEBUG=True, DEFAULT_EXCEPTION_REPORTER_FILTER='missing.debug.SafeExceptionReporterFilter'):
  406. response = self.c.get('/failure/')
  407. self.assertEqual(response.context['settings']['ROOT_URLCONF'], debug.CLEANSED_SUBSTITUTE)
  408. self.assertEqual(response.context['settings']['CSRF_COOKIE_DOMAIN'], debug.CLEANSED_SUBSTITUTE)
  409. self.assertEqual(response.context['request'].META['TEST_PASSWORD'], debug.CLEANSED_SUBSTITUTE)
  410. self.assertEqual(response.context['request'].META['HTTP_COOKIE'], debug.CLEANSED_SUBSTITUTE)
  411. self.assertEqual(response.context['request'].META['TEST_COOKIE'], debug.CLEANSED_SUBSTITUTE)
  412. self.assertEqual(response.context['request'].META['TEST_NORMAL'], 'ok')
  413. response = self.c.post('/failure/', data={'csrfmiddlewaretoken': 'abcde', 'normal': 'ok'})
  414. post_items = dict(response.context['filtered_POST_items'])
  415. self.assertEqual(post_items['csrfmiddlewaretoken'], debug.CLEANSED_SUBSTITUTE)
  416. self.assertEqual(post_items['normal'], 'ok')
  417. @django_test.override_settings(DEBUG=True)
  418. class HTMLTagsTest(django_test.TestCase):
  419. def test_heading_1(self):
  420. t = template.Template("""
  421. {% load html_tags %}
  422. {% heading 1 "Test" %}
  423. """)
  424. c = template.Context()
  425. o = t.render(c).strip()
  426. self.assertEquals(o, """<h1 id="test" class="heading ">Test</h1>""")
  427. def test_heading_2(self):
  428. t = template.Template("""
  429. {% load html_tags %}
  430. {% set_base_heading_level 3 %}
  431. {% heading 1 "Test" %}
  432. """)
  433. c = template.Context()
  434. o = t.render(c).strip()
  435. self.assertEquals(o, """<h4 id="test" class="heading ">Test</h4>""")
  436. def test_heading_3(self):
  437. t = template.Template("""
  438. {% load html_tags %}
  439. {% heading 1 "Test" "test" %}
  440. """)
  441. c = template.Context()
  442. o = t.render(c).strip()
  443. self.assertEquals(o, """<h1 id="test" class="heading test">Test</h1>""")
  444. def test_heading_4(self):
  445. t = template.Template("""
  446. {% load html_tags %}
  447. {% heading 1 "Longer test with spaces and various characters!?" %}
  448. """)
  449. c = template.Context()
  450. o = t.render(c).strip()
  451. self.assertEquals(o, """<h1 id="longer-test-with-spaces-and-various-characters" class="heading ">Longer test with spaces and various characters!?</h1>""")
  452. def test_heading_5(self):
  453. t = template.Template("""
  454. {% load html_tags %}
  455. {% heading 1 "12345" %}
  456. """)
  457. c = template.Context()
  458. o = t.render(c).strip()
  459. self.assertEquals(o, """<h1 id="a12345" class="heading ">12345</h1>""")
  460. def test_heading_6(self):
  461. t = template.Template("""
  462. {% load html_tags %}
  463. {% heading 1 "Test" %}
  464. {% heading 1 "Test" %}
  465. """)
  466. c = template.Context()
  467. o = html.strip_spaces_between_tags(t.render(c).strip())
  468. self.assertEquals(o, """<h1 id="test" class="heading ">Test</h1><h1 id="test-0" class="heading ">Test</h1>""")
  469. def test_heading_7(self):
  470. t = template.Template("""
  471. {% load html_tags %}
  472. {% heading 1 "Test" %}
  473. """)
  474. c = template.Context({
  475. 'base_heading_level': 3,
  476. })
  477. o = t.render(c).strip()
  478. self.assertEquals(o, """<h4 id="test" class="heading ">Test</h4>""")
  479. def test_heading_8(self):
  480. t = template.Template("""
  481. {% load html_tags %}
  482. {% set_base_heading_level 2 %}
  483. {% heading 1 "Test" %}
  484. """)
  485. c = template.Context({
  486. 'base_heading_level': 3,
  487. })
  488. o = t.render(c).strip()
  489. self.assertEquals(o, """<h3 id="test" class="heading ">Test</h3>""")
  490. def test_heading_9(self):
  491. t = template.Template("""
  492. {% load html_tags %}
  493. {% with base_heading_level=2 %}
  494. {% heading 1 "Test" %}
  495. {% endwith %}
  496. """)
  497. c = template.Context({
  498. 'base_heading_level': 3,
  499. })
  500. o = t.render(c).strip()
  501. self.assertEquals(o, """<h3 id="test" class="heading ">Test</h3>""")
  502. def test_heading_10(self):
  503. t = template.Template("""
  504. {% load html_tags %}
  505. {% with base_heading_level=2 %}
  506. {% endwith %}
  507. {% heading 1 "Test" %}
  508. """)
  509. c = template.Context({
  510. 'base_heading_level': 3,
  511. })
  512. o = t.render(c).strip()
  513. self.assertEquals(o, """<h4 id="test" class="heading ">Test</h4>""")
  514. def test_heading_11(self):
  515. t = template.Template("""
  516. {% load html_tags %}
  517. {% with base_heading_level=2 %}
  518. {% set_base_heading_level 4 %}
  519. {% endwith %}
  520. {% heading 1 "Test" %}
  521. """)
  522. c = template.Context({
  523. 'base_heading_level': 3,
  524. })
  525. o = t.render(c).strip()
  526. self.assertEquals(o, """<h4 id="test" class="heading ">Test</h4>""")
  527. def test_heading_12(self):
  528. t = template.Template("""
  529. {% load html_tags %}
  530. {% with base_heading_level=2 %}
  531. {% set_base_heading_level 4 "True" %}
  532. {% endwith %}
  533. {% heading 1 "Test" %}
  534. """)
  535. c = template.Context({
  536. 'base_heading_level': 3,
  537. })
  538. o = t.render(c).strip()
  539. self.assertEquals(o, """<h5 id="test" class="heading ">Test</h5>""")
  540. def test_anchorify_example(self):
  541. t = template.Template("""
  542. {% load i18n html_tags %}
  543. <h1 id="{{ _("My Blog")|anchorify }}">{% trans "My Blog" %}</h1>
  544. """)
  545. c = template.Context()
  546. o = t.render(c).strip()
  547. self.assertEquals(o, """<h1 id="my-blog">My Blog</h1>""")