tests.py 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. # -*- coding: utf-8 -*-
  2. from django.conf import settings
  3. if __name__ == '__main__':
  4. # When running this file in isolation, we need to set up the configuration
  5. # before importing 'template'.
  6. settings.configure()
  7. from datetime import datetime, timedelta
  8. import os
  9. import sys
  10. import traceback
  11. import unittest
  12. from django import template
  13. from django.core import urlresolvers
  14. from django.template import loader
  15. from django.template.loaders import app_directories, filesystem
  16. from django.utils.translation import activate, deactivate, ugettext as _
  17. from django.utils.safestring import mark_safe
  18. from django.utils.tzinfo import LocalTimezone
  19. from context import context_tests
  20. from custom import custom_filters
  21. from parser import filter_parsing, variable_parsing
  22. from unicode import unicode_tests
  23. from smartif import *
  24. try:
  25. from loaders import *
  26. except ImportError:
  27. pass # If setuptools isn't installed, that's fine. Just move on.
  28. import filters
  29. # Some other tests we would like to run
  30. __test__ = {
  31. 'unicode': unicode_tests,
  32. 'context': context_tests,
  33. 'filter_parsing': filter_parsing,
  34. 'custom_filters': custom_filters,
  35. }
  36. #################################
  37. # Custom template tag for tests #
  38. #################################
  39. register = template.Library()
  40. class EchoNode(template.Node):
  41. def __init__(self, contents):
  42. self.contents = contents
  43. def render(self, context):
  44. return " ".join(self.contents)
  45. def do_echo(parser, token):
  46. return EchoNode(token.contents.split()[1:])
  47. register.tag("echo", do_echo)
  48. template.libraries['django.templatetags.testtags'] = register
  49. #####################################
  50. # Helper objects for template tests #
  51. #####################################
  52. class SomeException(Exception):
  53. silent_variable_failure = True
  54. class SomeOtherException(Exception):
  55. pass
  56. class ContextStackException(Exception):
  57. pass
  58. class SomeClass:
  59. def __init__(self):
  60. self.otherclass = OtherClass()
  61. def method(self):
  62. return "SomeClass.method"
  63. def method2(self, o):
  64. return o
  65. def method3(self):
  66. raise SomeException
  67. def method4(self):
  68. raise SomeOtherException
  69. class OtherClass:
  70. def method(self):
  71. return "OtherClass.method"
  72. class UTF8Class:
  73. "Class whose __str__ returns non-ASCII data"
  74. def __str__(self):
  75. return u'ŠĐĆŽćžšđ'.encode('utf-8')
  76. class Templates(unittest.TestCase):
  77. def test_loaders_security(self):
  78. def test_template_sources(path, template_dirs, expected_sources):
  79. if isinstance(expected_sources, list):
  80. # Fix expected sources so they are normcased and abspathed
  81. expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources]
  82. # Test the two loaders (app_directores and filesystem).
  83. func1 = lambda p, t: list(app_directories.get_template_sources(p, t))
  84. func2 = lambda p, t: list(filesystem.get_template_sources(p, t))
  85. for func in (func1, func2):
  86. if isinstance(expected_sources, list):
  87. self.assertEqual(func(path, template_dirs), expected_sources)
  88. else:
  89. self.assertRaises(expected_sources, func, path, template_dirs)
  90. template_dirs = ['/dir1', '/dir2']
  91. test_template_sources('index.html', template_dirs,
  92. ['/dir1/index.html', '/dir2/index.html'])
  93. test_template_sources('/etc/passwd', template_dirs, [])
  94. test_template_sources('etc/passwd', template_dirs,
  95. ['/dir1/etc/passwd', '/dir2/etc/passwd'])
  96. test_template_sources('../etc/passwd', template_dirs, [])
  97. test_template_sources('../../../etc/passwd', template_dirs, [])
  98. test_template_sources('/dir1/index.html', template_dirs,
  99. ['/dir1/index.html'])
  100. test_template_sources('../dir2/index.html', template_dirs,
  101. ['/dir2/index.html'])
  102. test_template_sources('/dir1blah', template_dirs, [])
  103. test_template_sources('../dir1blah', template_dirs, [])
  104. # UTF-8 bytestrings are permitted.
  105. test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs,
  106. [u'/dir1/Ångström', u'/dir2/Ångström'])
  107. # Unicode strings are permitted.
  108. test_template_sources(u'Ångström', template_dirs,
  109. [u'/dir1/Ångström', u'/dir2/Ångström'])
  110. test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström'])
  111. test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'],
  112. [u'/Straße/Ångström'])
  113. # Invalid UTF-8 encoding in bytestrings is not. Should raise a
  114. # semi-useful error message.
  115. test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError)
  116. # Case insensitive tests (for win32). Not run unless we're on
  117. # a case insensitive operating system.
  118. if os.path.normcase('/TEST') == os.path.normpath('/test'):
  119. template_dirs = ['/dir1', '/DIR2']
  120. test_template_sources('index.html', template_dirs,
  121. ['/dir1/index.html', '/dir2/index.html'])
  122. test_template_sources('/DIR1/index.HTML', template_dirs,
  123. ['/dir1/index.html'])
  124. def test_token_smart_split(self):
  125. # Regression test for #7027
  126. token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
  127. split = token.split_contents()
  128. self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
  129. def test_url_reverse_no_settings_module(self):
  130. # Regression test for #9005
  131. from django.template import Template, Context, TemplateSyntaxError
  132. old_settings_module = settings.SETTINGS_MODULE
  133. old_template_debug = settings.TEMPLATE_DEBUG
  134. settings.SETTINGS_MODULE = None
  135. settings.TEMPLATE_DEBUG = True
  136. t = Template('{% url will_not_match %}')
  137. c = Context()
  138. try:
  139. rendered = t.render(c)
  140. except TemplateSyntaxError, e:
  141. # Assert that we are getting the template syntax error and not the
  142. # string encoding error.
  143. self.assertEquals(e.args[0], "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
  144. settings.SETTINGS_MODULE = old_settings_module
  145. settings.TEMPLATE_DEBUG = old_template_debug
  146. def test_templates(self):
  147. template_tests = self.get_template_tests()
  148. filter_tests = filters.get_filter_tests()
  149. # Quickly check that we aren't accidentally using a name in both
  150. # template and filter tests.
  151. overlapping_names = [name for name in filter_tests if name in template_tests]
  152. assert not overlapping_names, 'Duplicate test name(s): %s' % ', '.join(overlapping_names)
  153. template_tests.update(filter_tests)
  154. # Register our custom template loader.
  155. def test_template_loader(template_name, template_dirs=None):
  156. "A custom template loader that loads the unit-test templates."
  157. try:
  158. return (template_tests[template_name][0] , "test:%s" % template_name)
  159. except KeyError:
  160. raise template.TemplateDoesNotExist, template_name
  161. old_template_loaders = loader.template_source_loaders
  162. loader.template_source_loaders = [test_template_loader]
  163. failures = []
  164. tests = template_tests.items()
  165. tests.sort()
  166. # Turn TEMPLATE_DEBUG off, because tests assume that.
  167. old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False
  168. # Set TEMPLATE_STRING_IF_INVALID to a known string.
  169. old_invalid = settings.TEMPLATE_STRING_IF_INVALID
  170. expected_invalid_str = 'INVALID'
  171. for name, vals in tests:
  172. if isinstance(vals[2], tuple):
  173. normal_string_result = vals[2][0]
  174. invalid_string_result = vals[2][1]
  175. if '%s' in invalid_string_result:
  176. expected_invalid_str = 'INVALID %s'
  177. invalid_string_result = invalid_string_result % vals[2][2]
  178. template.invalid_var_format_string = True
  179. else:
  180. normal_string_result = vals[2]
  181. invalid_string_result = vals[2]
  182. if 'LANGUAGE_CODE' in vals[1]:
  183. activate(vals[1]['LANGUAGE_CODE'])
  184. else:
  185. activate('en-us')
  186. for invalid_str, result in [('', normal_string_result),
  187. (expected_invalid_str, invalid_string_result)]:
  188. settings.TEMPLATE_STRING_IF_INVALID = invalid_str
  189. try:
  190. test_template = loader.get_template(name)
  191. output = self.render(test_template, vals)
  192. except ContextStackException:
  193. failures.append("Template test (TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Context stack was left imbalanced" % (invalid_str, name))
  194. continue
  195. except Exception:
  196. exc_type, exc_value, exc_tb = sys.exc_info()
  197. if exc_type != result:
  198. tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
  199. failures.append("Template test (TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Got %s, exception: %s\n%s" % (invalid_str, name, exc_type, exc_value, tb))
  200. continue
  201. if output != result:
  202. failures.append("Template test (TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Expected %r, got %r" % (invalid_str, name, result, output))
  203. if 'LANGUAGE_CODE' in vals[1]:
  204. deactivate()
  205. if template.invalid_var_format_string:
  206. expected_invalid_str = 'INVALID'
  207. template.invalid_var_format_string = False
  208. loader.template_source_loaders = old_template_loaders
  209. deactivate()
  210. settings.TEMPLATE_DEBUG = old_td
  211. settings.TEMPLATE_STRING_IF_INVALID = old_invalid
  212. self.assertEqual(failures, [], "Tests failed:\n%s\n%s" %
  213. ('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
  214. def render(self, test_template, vals):
  215. context = template.Context(vals[1])
  216. before_stack_size = len(context.dicts)
  217. output = test_template.render(context)
  218. if len(context.dicts) != before_stack_size:
  219. raise ContextStackException
  220. return output
  221. def get_template_tests(self):
  222. # SYNTAX --
  223. # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
  224. return {
  225. ### BASIC SYNTAX ################################################
  226. # Plain text should go through the template parser untouched
  227. 'basic-syntax01': ("something cool", {}, "something cool"),
  228. # Variables should be replaced with their value in the current
  229. # context
  230. 'basic-syntax02': ("{{ headline }}", {'headline':'Success'}, "Success"),
  231. # More than one replacement variable is allowed in a template
  232. 'basic-syntax03': ("{{ first }} --- {{ second }}", {"first" : 1, "second" : 2}, "1 --- 2"),
  233. # Fail silently when a variable is not found in the current context
  234. 'basic-syntax04': ("as{{ missing }}df", {}, ("asdf","asINVALIDdf")),
  235. # A variable may not contain more than one word
  236. 'basic-syntax06': ("{{ multi word variable }}", {}, template.TemplateSyntaxError),
  237. # Raise TemplateSyntaxError for empty variable tags
  238. 'basic-syntax07': ("{{ }}", {}, template.TemplateSyntaxError),
  239. 'basic-syntax08': ("{{ }}", {}, template.TemplateSyntaxError),
  240. # Attribute syntax allows a template to call an object's attribute
  241. 'basic-syntax09': ("{{ var.method }}", {"var": SomeClass()}, "SomeClass.method"),
  242. # Multiple levels of attribute access are allowed
  243. 'basic-syntax10': ("{{ var.otherclass.method }}", {"var": SomeClass()}, "OtherClass.method"),
  244. # Fail silently when a variable's attribute isn't found
  245. 'basic-syntax11': ("{{ var.blech }}", {"var": SomeClass()}, ("","INVALID")),
  246. # Raise TemplateSyntaxError when trying to access a variable beginning with an underscore
  247. 'basic-syntax12': ("{{ var.__dict__ }}", {"var": SomeClass()}, template.TemplateSyntaxError),
  248. # Raise TemplateSyntaxError when trying to access a variable containing an illegal character
  249. 'basic-syntax13': ("{{ va>r }}", {}, template.TemplateSyntaxError),
  250. 'basic-syntax14': ("{{ (var.r) }}", {}, template.TemplateSyntaxError),
  251. 'basic-syntax15': ("{{ sp%am }}", {}, template.TemplateSyntaxError),
  252. 'basic-syntax16': ("{{ eggs! }}", {}, template.TemplateSyntaxError),
  253. 'basic-syntax17': ("{{ moo? }}", {}, template.TemplateSyntaxError),
  254. # Attribute syntax allows a template to call a dictionary key's value
  255. 'basic-syntax18': ("{{ foo.bar }}", {"foo" : {"bar" : "baz"}}, "baz"),
  256. # Fail silently when a variable's dictionary key isn't found
  257. 'basic-syntax19': ("{{ foo.spam }}", {"foo" : {"bar" : "baz"}}, ("","INVALID")),
  258. # Fail silently when accessing a non-simple method
  259. 'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")),
  260. # Don't get confused when parsing something that is almost, but not
  261. # quite, a template tag.
  262. 'basic-syntax21': ("a {{ moo %} b", {}, "a {{ moo %} b"),
  263. 'basic-syntax22': ("{{ moo #}", {}, "{{ moo #}"),
  264. # Will try to treat "moo #} {{ cow" as the variable. Not ideal, but
  265. # costly to work around, so this triggers an error.
  266. 'basic-syntax23': ("{{ moo #} {{ cow }}", {"cow": "cow"}, template.TemplateSyntaxError),
  267. # Embedded newlines make it not-a-tag.
  268. 'basic-syntax24': ("{{ moo\n }}", {}, "{{ moo\n }}"),
  269. # Literal strings are permitted inside variables, mostly for i18n
  270. # purposes.
  271. 'basic-syntax25': ('{{ "fred" }}', {}, "fred"),
  272. 'basic-syntax26': (r'{{ "\"fred\"" }}', {}, "\"fred\""),
  273. 'basic-syntax27': (r'{{ _("\"fred\"") }}', {}, "\"fred\""),
  274. # List-index syntax allows a template to access a certain item of a subscriptable object.
  275. 'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
  276. # Fail silently when the list index is out of range.
  277. 'list-index02': ("{{ var.5 }}", {"var": ["first item", "second item"]}, ("", "INVALID")),
  278. # Fail silently when the variable is not a subscriptable object.
  279. 'list-index03': ("{{ var.1 }}", {"var": None}, ("", "INVALID")),
  280. # Fail silently when variable is a dict without the specified key.
  281. 'list-index04': ("{{ var.1 }}", {"var": {}}, ("", "INVALID")),
  282. # Dictionary lookup wins out when dict's key is a string.
  283. 'list-index05': ("{{ var.1 }}", {"var": {'1': "hello"}}, "hello"),
  284. # But list-index lookup wins out when dict's key is an int, which
  285. # behind the scenes is really a dictionary lookup (for a dict)
  286. # after converting the key to an int.
  287. 'list-index06': ("{{ var.1 }}", {"var": {1: "hello"}}, "hello"),
  288. # Dictionary lookup wins out when there is a string and int version of the key.
  289. 'list-index07': ("{{ var.1 }}", {"var": {'1': "hello", 1: "world"}}, "hello"),
  290. # Basic filter usage
  291. 'filter-syntax01': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"),
  292. # Chained filters
  293. 'filter-syntax02': ("{{ var|upper|lower }}", {"var": "Django is the greatest!"}, "django is the greatest!"),
  294. # Raise TemplateSyntaxError for space between a variable and filter pipe
  295. 'filter-syntax03': ("{{ var |upper }}", {}, template.TemplateSyntaxError),
  296. # Raise TemplateSyntaxError for space after a filter pipe
  297. 'filter-syntax04': ("{{ var| upper }}", {}, template.TemplateSyntaxError),
  298. # Raise TemplateSyntaxError for a nonexistent filter
  299. 'filter-syntax05': ("{{ var|does_not_exist }}", {}, template.TemplateSyntaxError),
  300. # Raise TemplateSyntaxError when trying to access a filter containing an illegal character
  301. 'filter-syntax06': ("{{ var|fil(ter) }}", {}, template.TemplateSyntaxError),
  302. # Raise TemplateSyntaxError for invalid block tags
  303. 'filter-syntax07': ("{% nothing_to_see_here %}", {}, template.TemplateSyntaxError),
  304. # Raise TemplateSyntaxError for empty block tags
  305. 'filter-syntax08': ("{% %}", {}, template.TemplateSyntaxError),
  306. # Chained filters, with an argument to the first one
  307. 'filter-syntax09': ('{{ var|removetags:"b i"|upper|lower }}', {"var": "<b><i>Yes</i></b>"}, "yes"),
  308. # Literal string as argument is always "safe" from auto-escaping..
  309. 'filter-syntax10': (r'{{ var|default_if_none:" endquote\" hah" }}',
  310. {"var": None}, ' endquote" hah'),
  311. # Variable as argument
  312. 'filter-syntax11': (r'{{ var|default_if_none:var2 }}', {"var": None, "var2": "happy"}, 'happy'),
  313. # Default argument testing
  314. 'filter-syntax12': (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'),
  315. # Fail silently for methods that raise an exception with a
  316. # "silent_variable_failure" attribute
  317. 'filter-syntax13': (r'1{{ var.method3 }}2', {"var": SomeClass()}, ("12", "1INVALID2")),
  318. # In methods that raise an exception without a
  319. # "silent_variable_attribute" set to True, the exception propagates
  320. 'filter-syntax14': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
  321. # Escaped backslash in argument
  322. 'filter-syntax15': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'),
  323. # Escaped backslash using known escape char
  324. 'filter-syntax16': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'),
  325. # Empty strings can be passed as arguments to filters
  326. 'filter-syntax17': (r'{{ var|join:"" }}', {'var': ['a', 'b', 'c']}, 'abc'),
  327. # Make sure that any unicode strings are converted to bytestrings
  328. # in the final output.
  329. 'filter-syntax18': (r'{{ var }}', {'var': UTF8Class()}, u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'),
  330. # Numbers as filter arguments should work
  331. 'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."),
  332. #filters should accept empty string constants
  333. 'filter-syntax20': ('{{ ""|default_if_none:"was none" }}', {}, ""),
  334. ### COMMENT SYNTAX ########################################################
  335. 'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),
  336. 'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"),
  337. # Comments can contain invalid stuff.
  338. 'comment-syntax03': ("foo{# {% if %} #}", {}, "foo"),
  339. 'comment-syntax04': ("foo{# {% endblock %} #}", {}, "foo"),
  340. 'comment-syntax05': ("foo{# {% somerandomtag %} #}", {}, "foo"),
  341. 'comment-syntax06': ("foo{# {% #}", {}, "foo"),
  342. 'comment-syntax07': ("foo{# %} #}", {}, "foo"),
  343. 'comment-syntax08': ("foo{# %} #}bar", {}, "foobar"),
  344. 'comment-syntax09': ("foo{# {{ #}", {}, "foo"),
  345. 'comment-syntax10': ("foo{# }} #}", {}, "foo"),
  346. 'comment-syntax11': ("foo{# { #}", {}, "foo"),
  347. 'comment-syntax12': ("foo{# } #}", {}, "foo"),
  348. ### COMMENT TAG ###########################################################
  349. 'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"),
  350. 'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"),
  351. # Comment tag can contain invalid stuff.
  352. 'comment-tag03': ("foo{% comment %} {% if %} {% endcomment %}", {}, "foo"),
  353. 'comment-tag04': ("foo{% comment %} {% endblock %} {% endcomment %}", {}, "foo"),
  354. 'comment-tag05': ("foo{% comment %} {% somerandomtag %} {% endcomment %}", {}, "foo"),
  355. ### CYCLE TAG #############################################################
  356. 'cycle01': ('{% cycle a %}', {}, template.TemplateSyntaxError),
  357. 'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'),
  358. 'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'),
  359. 'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'),
  360. 'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
  361. 'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
  362. 'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
  363. 'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'),
  364. 'cycle09': ("{% for i in test %}{% cycle a,b %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'),
  365. 'cycle10': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}", {}, 'ab'),
  366. 'cycle11': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}", {}, 'abc'),
  367. 'cycle12': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}", {}, 'abca'),
  368. 'cycle13': ("{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'),
  369. 'cycle14': ("{% cycle one two as foo %}{% cycle foo %}", {'one': '1','two': '2'}, '12'),
  370. 'cycle15': ("{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}", {'test': range(5), 'aye': 'a', 'bee': 'b'}, 'a0,b1,a2,b3,a4,'),
  371. 'cycle16': ("{% cycle one|lower two as foo %}{% cycle foo %}", {'one': 'A','two': '2'}, 'a2'),
  372. ### EXCEPTIONS ############################################################
  373. # Raise exception for invalid template name
  374. 'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
  375. # Raise exception for invalid template name (in variable)
  376. 'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
  377. # Raise exception for extra {% extends %} tags
  378. 'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
  379. # Raise exception for custom tags used in child with {% load %} tag in parent, not in child
  380. 'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
  381. ### FILTER TAG ############################################################
  382. 'filter01': ('{% filter upper %}{% endfilter %}', {}, ''),
  383. 'filter02': ('{% filter upper %}django{% endfilter %}', {}, 'DJANGO'),
  384. 'filter03': ('{% filter upper|lower %}django{% endfilter %}', {}, 'django'),
  385. 'filter04': ('{% filter cut:remove %}djangospam{% endfilter %}', {'remove': 'spam'}, 'django'),
  386. ### FIRSTOF TAG ###########################################################
  387. 'firstof01': ('{% firstof a b c %}', {'a':0,'b':0,'c':0}, ''),
  388. 'firstof02': ('{% firstof a b c %}', {'a':1,'b':0,'c':0}, '1'),
  389. 'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
  390. 'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
  391. 'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
  392. 'firstof06': ('{% firstof a b c %}', {'b':0,'c':3}, '3'),
  393. 'firstof07': ('{% firstof a b "c" %}', {'a':0}, 'c'),
  394. 'firstof08': ('{% firstof a b "c and d" %}', {'a':0,'b':0}, 'c and d'),
  395. 'firstof09': ('{% firstof %}', {}, template.TemplateSyntaxError),
  396. ### FOR TAG ###############################################################
  397. 'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),
  398. 'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"),
  399. 'for-tag-vars01': ("{% for val in values %}{{ forloop.counter }}{% endfor %}", {"values": [6, 6, 6]}, "123"),
  400. 'for-tag-vars02': ("{% for val in values %}{{ forloop.counter0 }}{% endfor %}", {"values": [6, 6, 6]}, "012"),
  401. 'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"),
  402. 'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"),
  403. 'for-tag-vars05': ("{% for val in values %}{% if forloop.first %}f{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "fxx"),
  404. 'for-tag-vars06': ("{% for val in values %}{% if forloop.last %}l{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "xxl"),
  405. 'for-tag-unpack01': ("{% for key,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
  406. 'for-tag-unpack03': ("{% for key, value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
  407. 'for-tag-unpack04': ("{% for key , value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
  408. 'for-tag-unpack05': ("{% for key ,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
  409. 'for-tag-unpack06': ("{% for key value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError),
  410. 'for-tag-unpack07': ("{% for key,,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError),
  411. 'for-tag-unpack08': ("{% for key,value, in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError),
  412. # Ensure that a single loopvar doesn't truncate the list in val.
  413. 'for-tag-unpack09': ("{% for val in items %}{{ val.0 }}:{{ val.1 }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
  414. # Otherwise, silently truncate if the length of loopvars differs to the length of each set of items.
  415. 'for-tag-unpack10': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'orange'))}, "one:1/two:2/"),
  416. 'for-tag-unpack11': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, ("one:1,/two:2,/", "one:1,INVALID/two:2,INVALID/")),
  417. 'for-tag-unpack12': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2))}, ("one:1,carrot/two:2,/", "one:1,carrot/two:2,INVALID/")),
  418. 'for-tag-unpack13': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'cheese'))}, ("one:1,carrot/two:2,cheese/", "one:1,carrot/two:2,cheese/")),
  419. 'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"),
  420. 'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"),
  421. 'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"),
  422. ### IF TAG ################################################################
  423. 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),
  424. 'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"),
  425. 'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"),
  426. # Filters
  427. 'if-tag-filter01': ("{% if foo|length == 5 %}yes{% else %}no{% endif %}", {'foo': 'abcde'}, "yes"),
  428. 'if-tag-filter02': ("{% if foo|upper == 'ABC' %}yes{% else %}no{% endif %}", {}, "no"),
  429. # Equality
  430. 'if-tag-eq01': ("{% if foo == bar %}yes{% else %}no{% endif %}", {}, "yes"),
  431. 'if-tag-eq02': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1}, "no"),
  432. 'if-tag-eq03': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1, 'bar': 1}, "yes"),
  433. 'if-tag-eq04': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1, 'bar': 2}, "no"),
  434. 'if-tag-eq05': ("{% if foo == '' %}yes{% else %}no{% endif %}", {}, "no"),
  435. # Comparison
  436. 'if-tag-gt-01': ("{% if 2 > 1 %}yes{% else %}no{% endif %}", {}, "yes"),
  437. 'if-tag-gt-02': ("{% if 1 > 1 %}yes{% else %}no{% endif %}", {}, "no"),
  438. 'if-tag-gte-01': ("{% if 1 >= 1 %}yes{% else %}no{% endif %}", {}, "yes"),
  439. 'if-tag-gte-02': ("{% if 1 >= 2 %}yes{% else %}no{% endif %}", {}, "no"),
  440. 'if-tag-lt-01': ("{% if 1 < 2 %}yes{% else %}no{% endif %}", {}, "yes"),
  441. 'if-tag-lt-02': ("{% if 1 < 1 %}yes{% else %}no{% endif %}", {}, "no"),
  442. 'if-tag-lte-01': ("{% if 1 <= 1 %}yes{% else %}no{% endif %}", {}, "yes"),
  443. 'if-tag-lte-02': ("{% if 2 <= 1 %}yes{% else %}no{% endif %}", {}, "no"),
  444. # AND
  445. 'if-tag-and01': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'),
  446. 'if-tag-and02': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'),
  447. 'if-tag-and03': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'),
  448. 'if-tag-and04': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'),
  449. 'if-tag-and05': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False}, 'no'),
  450. 'if-tag-and06': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'bar': False}, 'no'),
  451. 'if-tag-and07': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True}, 'no'),
  452. 'if-tag-and08': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'bar': True}, 'no'),
  453. # OR
  454. 'if-tag-or01': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'),
  455. 'if-tag-or02': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'),
  456. 'if-tag-or03': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'),
  457. 'if-tag-or04': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'),
  458. 'if-tag-or05': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False}, 'no'),
  459. 'if-tag-or06': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'bar': False}, 'no'),
  460. 'if-tag-or07': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True}, 'yes'),
  461. 'if-tag-or08': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'bar': True}, 'yes'),
  462. # multiple ORs
  463. 'if-tag-or09': ("{% if foo or bar or baz %}yes{% else %}no{% endif %}", {'baz': True}, 'yes'),
  464. # NOT
  465. 'if-tag-not01': ("{% if not foo %}no{% else %}yes{% endif %}", {'foo': True}, 'yes'),
  466. 'if-tag-not02': ("{% if not not foo %}no{% else %}yes{% endif %}", {'foo': True}, 'no'),
  467. # not03 to not05 removed, now TemplateSyntaxErrors
  468. 'if-tag-not06': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {}, 'no'),
  469. 'if-tag-not07': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'),
  470. 'if-tag-not08': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'),
  471. 'if-tag-not09': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'),
  472. 'if-tag-not10': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'),
  473. 'if-tag-not11': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {}, 'no'),
  474. 'if-tag-not12': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'),
  475. 'if-tag-not13': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'),
  476. 'if-tag-not14': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'),
  477. 'if-tag-not15': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'),
  478. 'if-tag-not16': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {}, 'yes'),
  479. 'if-tag-not17': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'),
  480. 'if-tag-not18': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'),
  481. 'if-tag-not19': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'),
  482. 'if-tag-not20': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'),
  483. 'if-tag-not21': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {}, 'yes'),
  484. 'if-tag-not22': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'),
  485. 'if-tag-not23': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'),
  486. 'if-tag-not24': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'),
  487. 'if-tag-not25': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'),
  488. 'if-tag-not26': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {}, 'yes'),
  489. 'if-tag-not27': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'),
  490. 'if-tag-not28': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'),
  491. 'if-tag-not29': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'),
  492. 'if-tag-not30': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'),
  493. 'if-tag-not31': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {}, 'yes'),
  494. 'if-tag-not32': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'),
  495. 'if-tag-not33': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'),
  496. 'if-tag-not34': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'),
  497. 'if-tag-not35': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'),
  498. # Various syntax errors
  499. 'if-tag-error01': ("{% if %}yes{% endif %}", {}, template.TemplateSyntaxError),
  500. 'if-tag-error02': ("{% if foo and %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError),
  501. 'if-tag-error03': ("{% if foo or %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError),
  502. 'if-tag-error04': ("{% if not foo and %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError),
  503. 'if-tag-error05': ("{% if not foo or %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError),
  504. 'if-tag-error06': ("{% if abc def %}yes{% endif %}", {}, template.TemplateSyntaxError),
  505. 'if-tag-error07': ("{% if not %}yes{% endif %}", {}, template.TemplateSyntaxError),
  506. 'if-tag-error08': ("{% if and %}yes{% endif %}", {}, template.TemplateSyntaxError),
  507. 'if-tag-error09': ("{% if or %}yes{% endif %}", {}, template.TemplateSyntaxError),
  508. 'if-tag-error10': ("{% if == %}yes{% endif %}", {}, template.TemplateSyntaxError),
  509. 'if-tag-error11': ("{% if 1 == %}yes{% endif %}", {}, template.TemplateSyntaxError),
  510. 'if-tag-error12': ("{% if a not b %}yes{% endif %}", {}, template.TemplateSyntaxError),
  511. # Additional, more precise parsing tests are in SmartIfTests
  512. ### IFCHANGED TAG #########################################################
  513. 'ifchanged01': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,2,3)}, '123'),
  514. 'ifchanged02': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,1,3)}, '13'),
  515. 'ifchanged03': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,1,1)}, '1'),
  516. 'ifchanged04': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', {'num': (1, 2, 3), 'numx': (2, 2, 2)}, '122232'),
  517. 'ifchanged05': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (1, 2, 3)}, '1123123123'),
  518. 'ifchanged06': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (2, 2, 2)}, '1222'),
  519. 'ifchanged07': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% for x in numx %}{% ifchanged %}{{ x }}{% endifchanged %}{% for y in numy %}{% ifchanged %}{{ y }}{% endifchanged %}{% endfor %}{% endfor %}{% endfor %}', {'num': (1, 1, 1), 'numx': (2, 2, 2), 'numy': (3, 3, 3)}, '1233323332333'),
  520. 'ifchanged08': ('{% for data in datalist %}{% for c,d in data %}{% if c %}{% ifchanged %}{{ d }}{% endifchanged %}{% endif %}{% endfor %}{% endfor %}', {'datalist': [[(1, 'a'), (1, 'a'), (0, 'b'), (1, 'c')], [(0, 'a'), (1, 'c'), (1, 'd'), (1, 'd'), (0, 'e')]]}, 'accd'),
  521. # Test one parameter given to ifchanged.
  522. 'ifchanged-param01': ('{% for n in num %}{% ifchanged n %}..{% endifchanged %}{{ n }}{% endfor %}', { 'num': (1,2,3) }, '..1..2..3'),
  523. 'ifchanged-param02': ('{% for n in num %}{% for x in numx %}{% ifchanged n %}..{% endifchanged %}{{ x }}{% endfor %}{% endfor %}', { 'num': (1,2,3), 'numx': (5,6,7) }, '..567..567..567'),
  524. # Test multiple parameters to ifchanged.
  525. 'ifchanged-param03': ('{% for n in num %}{{ n }}{% for x in numx %}{% ifchanged x n %}{{ x }}{% endifchanged %}{% endfor %}{% endfor %}', { 'num': (1,1,2), 'numx': (5,6,6) }, '156156256'),
  526. # Test a date+hour like construct, where the hour of the last day
  527. # is the same but the date had changed, so print the hour anyway.
  528. 'ifchanged-param04': ('{% for d in days %}{% ifchanged %}{{ d.day }}{% endifchanged %}{% for h in d.hours %}{% ifchanged d h %}{{ h }}{% endifchanged %}{% endfor %}{% endfor %}', {'days':[{'day':1, 'hours':[1,2,3]},{'day':2, 'hours':[3]},] }, '112323'),
  529. # Logically the same as above, just written with explicit
  530. # ifchanged for the day.
  531. 'ifchanged-param05': ('{% for d in days %}{% ifchanged d.day %}{{ d.day }}{% endifchanged %}{% for h in d.hours %}{% ifchanged d.day h %}{{ h }}{% endifchanged %}{% endfor %}{% endfor %}', {'days':[{'day':1, 'hours':[1,2,3]},{'day':2, 'hours':[3]},] }, '112323'),
  532. # Test the else clause of ifchanged.
  533. 'ifchanged-else01': ('{% for id in ids %}{{ id }}{% ifchanged id %}-first{% else %}-other{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-first,1-other,2-first,2-other,2-other,3-first,'),
  534. 'ifchanged-else02': ('{% for id in ids %}{{ id }}-{% ifchanged id %}{% cycle red,blue %}{% else %}grey{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-red,1-grey,2-blue,2-grey,2-grey,3-red,'),
  535. 'ifchanged-else03': ('{% for id in ids %}{{ id }}{% ifchanged id %}-{% cycle red,blue %}{% else %}{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-red,1,2-blue,2,2,3-red,'),
  536. 'ifchanged-else04': ('{% for id in ids %}{% ifchanged %}***{{ id }}*{% else %}...{% endifchanged %}{{ forloop.counter }}{% endfor %}', {'ids': [1,1,2,2,2,3,4]}, '***1*1...2***2*3...4...5***3*6***4*7'),
  537. ### IFEQUAL TAG ###########################################################
  538. 'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
  539. 'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
  540. 'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"),
  541. 'ifequal04': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 1}, "yes"),
  542. 'ifequal05': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "test"}, "yes"),
  543. 'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"),
  544. 'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"),
  545. 'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"),
  546. 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
  547. 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
  548. # SMART SPLITTING
  549. 'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, "no"),
  550. 'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"),
  551. 'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"),
  552. 'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"),
  553. 'ifequal-split05': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': ''}, "no"),
  554. 'ifequal-split06': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i "love" you'}, "yes"),
  555. 'ifequal-split07': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i love you'}, "no"),
  556. 'ifequal-split08': (r"{% ifequal a 'I\'m happy' %}yes{% else %}no{% endifequal %}", {'a': "I'm happy"}, "yes"),
  557. 'ifequal-split09': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slash\man"}, "yes"),
  558. 'ifequal-split10': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slashman"}, "no"),
  559. # NUMERIC RESOLUTION
  560. 'ifequal-numeric01': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': '5'}, ''),
  561. 'ifequal-numeric02': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': 5}, 'yes'),
  562. 'ifequal-numeric03': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5}, ''),
  563. 'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'),
  564. 'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'),
  565. 'ifequal-numeric06': ('{% ifequal x .2 %}yes{% endifequal %}', {'x': .2}, 'yes'),
  566. 'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ''),
  567. 'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''),
  568. 'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'),
  569. 'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'),
  570. 'ifequal-numeric11': ('{% ifequal x -5.2 %}yes{% endifequal %}', {'x': -5.2}, 'yes'),
  571. 'ifequal-numeric12': ('{% ifequal x +5 %}yes{% endifequal %}', {'x': 5}, 'yes'),
  572. # FILTER EXPRESSIONS AS ARGUMENTS
  573. 'ifequal-filter01': ('{% ifequal a|upper "A" %}x{% endifequal %}', {'a': 'a'}, 'x'),
  574. 'ifequal-filter02': ('{% ifequal "A" a|upper %}x{% endifequal %}', {'a': 'a'}, 'x'),
  575. 'ifequal-filter03': ('{% ifequal a|upper b|upper %}x{% endifequal %}', {'a': 'x', 'b': 'X'}, 'x'),
  576. 'ifequal-filter04': ('{% ifequal x|slice:"1" "a" %}x{% endifequal %}', {'x': 'aaa'}, 'x'),
  577. 'ifequal-filter05': ('{% ifequal x|slice:"1"|upper "A" %}x{% endifequal %}', {'x': 'aaa'}, 'x'),
  578. ### IFNOTEQUAL TAG ########################################################
  579. 'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
  580. 'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),
  581. 'ifnotequal03': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
  582. 'ifnotequal04': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 1}, "no"),
  583. ### INCLUDE TAG ###########################################################
  584. 'include01': ('{% include "basic-syntax01" %}', {}, "something cool"),
  585. 'include02': ('{% include "basic-syntax02" %}', {'headline': 'Included'}, "Included"),
  586. 'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"),
  587. 'include04': ('a{% include "nonexistent" %}b', {}, "ab"),
  588. 'include 05': ('template with a space', {}, 'template with a space'),
  589. 'include06': ('{% include "include 05"%}', {}, 'template with a space'),
  590. ### NAMED ENDBLOCKS #######################################################
  591. # Basic test
  592. 'namedendblocks01': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock first %}3", {}, '1_2_3'),
  593. # Unbalanced blocks
  594. 'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}_{% endblock second %}3", {}, template.TemplateSyntaxError),
  595. 'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock second %}3", {}, template.TemplateSyntaxError),
  596. 'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock third %}3", {}, template.TemplateSyntaxError),
  597. 'namedendblocks05': ("1{% block first %}_{% block second %}2{% endblock first %}", {}, template.TemplateSyntaxError),
  598. # Mixed named and unnamed endblocks
  599. 'namedendblocks06': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'),
  600. 'namedendblocks07': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'),
  601. ### INHERITANCE ###########################################################
  602. # Standard template with no inheritance
  603. 'inheritance01': ("1{% block first %}&{% endblock %}3{% block second %}_{% endblock %}", {}, '1&3_'),
  604. # Standard two-level inheritance
  605. 'inheritance02': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
  606. # Three-level with no redefinitions on third level
  607. 'inheritance03': ("{% extends 'inheritance02' %}", {}, '1234'),
  608. # Two-level with no redefinitions on second level
  609. 'inheritance04': ("{% extends 'inheritance01' %}", {}, '1&3_'),
  610. # Two-level with double quotes instead of single quotes
  611. 'inheritance05': ('{% extends "inheritance02" %}', {}, '1234'),
  612. # Three-level with variable parent-template name
  613. 'inheritance06': ("{% extends foo %}", {'foo': 'inheritance02'}, '1234'),
  614. # Two-level with one block defined, one block not defined
  615. 'inheritance07': ("{% extends 'inheritance01' %}{% block second %}5{% endblock %}", {}, '1&35'),
  616. # Three-level with one block defined on this level, two blocks defined next level
  617. 'inheritance08': ("{% extends 'inheritance02' %}{% block second %}5{% endblock %}", {}, '1235'),
  618. # Three-level with second and third levels blank
  619. 'inheritance09': ("{% extends 'inheritance04' %}", {}, '1&3_'),
  620. # Three-level with space NOT in a block -- should be ignored
  621. 'inheritance10': ("{% extends 'inheritance04' %} ", {}, '1&3_'),
  622. # Three-level with both blocks defined on this level, but none on second level
  623. 'inheritance11': ("{% extends 'inheritance04' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
  624. # Three-level with this level providing one and second level providing the other
  625. 'inheritance12': ("{% extends 'inheritance07' %}{% block first %}2{% endblock %}", {}, '1235'),
  626. # Three-level with this level overriding second level
  627. 'inheritance13': ("{% extends 'inheritance02' %}{% block first %}a{% endblock %}{% block second %}b{% endblock %}", {}, '1a3b'),
  628. # A block defined only in a child template shouldn't be displayed
  629. 'inheritance14': ("{% extends 'inheritance01' %}{% block newblock %}NO DISPLAY{% endblock %}", {}, '1&3_'),
  630. # A block within another block
  631. 'inheritance15': ("{% extends 'inheritance01' %}{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}", {}, '12inner3_'),
  632. # A block within another block (level 2)
  633. 'inheritance16': ("{% extends 'inheritance15' %}{% block inner %}out{% endblock %}", {}, '12out3_'),
  634. # {% load %} tag (parent -- setup for exception04)
  635. 'inheritance17': ("{% load testtags %}{% block first %}1234{% endblock %}", {}, '1234'),
  636. # {% load %} tag (standard usage, without inheritance)
  637. 'inheritance18': ("{% load testtags %}{% echo this that theother %}5678", {}, 'this that theother5678'),
  638. # {% load %} tag (within a child template)
  639. 'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'),
  640. # Two-level inheritance with {{ block.super }}
  641. 'inheritance20': ("{% extends 'inheritance01' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1&a3_'),
  642. # Three-level inheritance with {{ block.super }} from parent
  643. 'inheritance21': ("{% extends 'inheritance02' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '12a34'),
  644. # Three-level inheritance with {{ block.super }} from grandparent
  645. 'inheritance22': ("{% extends 'inheritance04' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1&a3_'),
  646. # Three-level inheritance with {{ block.super }} from parent and grandparent
  647. 'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1&ab3_'),
  648. # Inheritance from local context without use of template loader
  649. 'inheritance24': ("{% extends context_template %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {'context_template': template.Template("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}")}, '1234'),
  650. # Inheritance from local context with variable parent template
  651. 'inheritance25': ("{% extends context_template.1 %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {'context_template': [template.Template("Wrong"), template.Template("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}")]}, '1234'),
  652. # Set up a base template to extend
  653. 'inheritance26': ("no tags", {}, 'no tags'),
  654. # Inheritance from a template that doesn't have any blocks
  655. 'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'),
  656. # Set up a base template with a space in it.
  657. 'inheritance 28': ("{% block first %}!{% endblock %}", {}, '!'),
  658. # Inheritance from a template with a space in its name should work.
  659. 'inheritance29': ("{% extends 'inheritance 28' %}", {}, '!'),
  660. ### I18N ##################################################################
  661. # {% spaceless %} tag
  662. 'spaceless01': ("{% spaceless %} <b> <i> text </i> </b> {% endspaceless %}", {}, "<b><i> text </i></b>"),
  663. 'spaceless02': ("{% spaceless %} <b> \n <i> text </i> \n </b> {% endspaceless %}", {}, "<b><i> text </i></b>"),
  664. 'spaceless03': ("{% spaceless %}<b><i>text</i></b>{% endspaceless %}", {}, "<b><i>text</i></b>"),
  665. # simple translation of a string delimited by '
  666. 'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"),
  667. # simple translation of a string delimited by "
  668. 'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"),
  669. # simple translation of a variable
  670. 'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u"Å"),
  671. # simple translation of a variable and filter
  672. 'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u'å'),
  673. # simple translation of a string with interpolation
  674. 'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"),
  675. # simple translation of a string to german
  676. 'i18n06': ('{% load i18n %}{% trans "Page not found" %}', {'LANGUAGE_CODE': 'de'}, "Seite nicht gefunden"),
  677. # translation of singular form
  678. 'i18n07': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 1}, "singular"),
  679. # translation of plural form
  680. 'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"),
  681. # simple non-translation (only marking) of a string to german
  682. 'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),
  683. # translation of a variable with a translated filter
  684. 'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
  685. # translation of a variable with a non-translated filter
  686. 'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
  687. # usage of the get_available_languages tag
  688. 'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),
  689. # translation of constant strings
  690. 'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
  691. 'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
  692. 'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
  693. 'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
  694. # Escaping inside blocktrans and trans works as if it was directly in the
  695. # template.
  696. 'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
  697. 'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
  698. 'i18n19': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': 'a & b'}, u'a &amp; b'),
  699. 'i18n20': ('{% load i18n %}{% trans andrew %}', {'andrew': 'a & b'}, u'a &amp; b'),
  700. 'i18n21': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': mark_safe('a & b')}, u'a & b'),
  701. 'i18n22': ('{% load i18n %}{% trans andrew %}', {'andrew': mark_safe('a & b')}, u'a & b'),
  702. ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
  703. 'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),
  704. 'invalidstr02': ('{{ var|default_if_none:"Foo" }}', {}, ('','INVALID')),
  705. 'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),
  706. 'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'),
  707. 'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'),
  708. 'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
  709. 'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
  710. ### MULTILINE #############################################################
  711. 'multiline01': ("""
  712. Hello,
  713. boys.
  714. How
  715. are
  716. you
  717. gentlemen.
  718. """,
  719. {},
  720. """
  721. Hello,
  722. boys.
  723. How
  724. are
  725. you
  726. gentlemen.
  727. """),
  728. ### REGROUP TAG ###########################################################
  729. 'regroup01': ('{% regroup data by bar as grouped %}' + \
  730. '{% for group in grouped %}' + \
  731. '{{ group.grouper }}:' + \
  732. '{% for item in group.list %}' + \
  733. '{{ item.foo }}' + \
  734. '{% endfor %},' + \
  735. '{% endfor %}',
  736. {'data': [ {'foo':'c', 'bar':1},
  737. {'foo':'d', 'bar':1},
  738. {'foo':'a', 'bar':2},
  739. {'foo':'b', 'bar':2},
  740. {'foo':'x', 'bar':3} ]},
  741. '1:cd,2:ab,3:x,'),
  742. # Test for silent failure when target variable isn't found
  743. 'regroup02': ('{% regroup data by bar as grouped %}' + \
  744. '{% for group in grouped %}' + \
  745. '{{ group.grouper }}:' + \
  746. '{% for item in group.list %}' + \
  747. '{{ item.foo }}' + \
  748. '{% endfor %},' + \
  749. '{% endfor %}',
  750. {}, ''),
  751. ### TEMPLATETAG TAG #######################################################
  752. 'templatetag01': ('{% templatetag openblock %}', {}, '{%'),
  753. 'templatetag02': ('{% templatetag closeblock %}', {}, '%}'),
  754. 'templatetag03': ('{% templatetag openvariable %}', {}, '{{'),
  755. 'templatetag04': ('{% templatetag closevariable %}', {}, '}}'),
  756. 'templatetag05': ('{% templatetag %}', {}, template.TemplateSyntaxError),
  757. 'templatetag06': ('{% templatetag foo %}', {}, template.TemplateSyntaxError),
  758. 'templatetag07': ('{% templatetag openbrace %}', {}, '{'),
  759. 'templatetag08': ('{% templatetag closebrace %}', {}, '}'),
  760. 'templatetag09': ('{% templatetag openbrace %}{% templatetag openbrace %}', {}, '{{'),
  761. 'templatetag10': ('{% templatetag closebrace %}{% templatetag closebrace %}', {}, '}}'),
  762. 'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
  763. 'templatetag12': ('{% templatetag closecomment %}', {}, '#}'),
  764. ### WIDTHRATIO TAG ########################################################
  765. 'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
  766. 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
  767. 'widthratio03': ('{% widthratio a b 100 %}', {'a':0,'b':100}, '0'),
  768. 'widthratio04': ('{% widthratio a b 100 %}', {'a':50,'b':100}, '50'),
  769. 'widthratio05': ('{% widthratio a b 100 %}', {'a':100,'b':100}, '100'),
  770. # 62.5 should round to 63
  771. 'widthratio06': ('{% widthratio a b 100 %}', {'a':50,'b':80}, '63'),
  772. # 71.4 should round to 71
  773. 'widthratio07': ('{% widthratio a b 100 %}', {'a':50,'b':70}, '71'),
  774. # Raise exception if we don't have 3 args, last one an integer
  775. 'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
  776. 'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
  777. 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, '50'),
  778. # #10043: widthratio should allow max_width to be a variable
  779. 'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'),
  780. ### WITH TAG ########################################################
  781. 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
  782. 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')),
  783. 'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
  784. 'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
  785. ### NOW TAG ########################################################
  786. # Simple case
  787. 'now01': ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),
  788. # Check parsing of escaped and special characters
  789. 'now02': ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
  790. # 'now03': ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
  791. # 'now04': ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
  792. ### URL TAG ########################################################
  793. # Successes
  794. 'url01': ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
  795. 'url02': ('{% url regressiontests.templates.views.client_action id=client.id,action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
  796. 'url02a': ('{% url regressiontests.templates.views.client_action client.id,"update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
  797. 'url03': ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
  798. 'url04': ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
  799. 'url05': (u'{% url метка_оператора v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  800. 'url06': (u'{% url метка_оператора_2 tag=v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  801. 'url07': (u'{% url regressiontests.templates.views.client2 tag=v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  802. 'url08': (u'{% url метка_оператора v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  803. 'url09': (u'{% url метка_оператора_2 tag=v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  804. 'url10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'),
  805. # Failures
  806. 'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),
  807. 'url-fail02': ('{% url no_such_view %}', {}, urlresolvers.NoReverseMatch),
  808. 'url-fail03': ('{% url regressiontests.templates.views.client %}', {}, urlresolvers.NoReverseMatch),
  809. # {% url ... as var %}
  810. 'url-asvar01': ('{% url regressiontests.templates.views.index as url %}', {}, ''),
  811. 'url-asvar02': ('{% url regressiontests.templates.views.index as url %}{{ url }}', {}, '/url_tag/'),
  812. 'url-asvar03': ('{% url no_such_view as url %}{{ url }}', {}, ''),
  813. ### CACHE TAG ######################################################
  814. 'cache01': ('{% load cache %}{% cache -1 test %}cache01{% endcache %}', {}, 'cache01'),
  815. 'cache02': ('{% load cache %}{% cache -1 test %}cache02{% endcache %}', {}, 'cache02'),
  816. 'cache03': ('{% load cache %}{% cache 2 test %}cache03{% endcache %}', {}, 'cache03'),
  817. 'cache04': ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'),
  818. 'cache05': ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'),
  819. 'cache06': ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'),
  820. 'cache07': ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'),
  821. # Allow first argument to be a variable.
  822. 'cache08': ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'),
  823. 'cache09': ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'),
  824. 'cache10': ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'),
  825. # Raise exception if we don't have at least 2 args, first one integer.
  826. 'cache11': ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError),
  827. 'cache12': ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError),
  828. 'cache13': ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError),
  829. 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError),
  830. 'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError),
  831. # Regression test for #7460.
  832. 'cache16': ('{% load cache %}{% cache 1 foo bar %}{% endcache %}', {'foo': 'foo', 'bar': 'with spaces'}, ''),
  833. # Regression test for #11270.
  834. 'cache17': ('{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}', {'poem': 'Oh freddled gruntbuggly/Thy micturations are to me/As plurdled gabbleblotchits/On a lurgid bee/That mordiously hath bitled out/Its earted jurtles/Into a rancid festering/Or else I shall rend thee in the gobberwarts with my blurglecruncheon/See if I dont.'}, 'Some Content'),
  835. ### AUTOESCAPE TAG ##############################################
  836. 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),
  837. 'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"),
  838. 'autoescape-tag03': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "&lt;b&gt;hello&lt;/b&gt;"),
  839. # Autoescape disabling and enabling nest in a predictable way.
  840. 'autoescape-tag04': ("{% autoescape off %}{{ first }} {% autoescape on%}{{ first }}{% endautoescape %}{% endautoescape %}", {"first": "<a>"}, "<a> &lt;a&gt;"),
  841. 'autoescape-tag05': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": "<b>first</b>"}, "&lt;b&gt;first&lt;/b&gt;"),
  842. # Strings (ASCII or unicode) already marked as "safe" are not
  843. # auto-escaped
  844. 'autoescape-tag06': ("{{ first }}", {"first": mark_safe("<b>first</b>")}, "<b>first</b>"),
  845. 'autoescape-tag07': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": mark_safe(u"<b>Apple</b>")}, u"<b>Apple</b>"),
  846. # Literal string arguments to filters, if used in the result, are
  847. # safe.
  848. 'autoescape-tag08': (r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}', {"var": None}, ' endquote" hah'),
  849. # Objects which return safe strings as their __unicode__ method
  850. # won't get double-escaped.
  851. 'autoescape-tag09': (r'{{ unsafe }}', {'unsafe': filters.UnsafeClass()}, 'you &amp; me'),
  852. 'autoescape-tag10': (r'{{ safe }}', {'safe': filters.SafeClass()}, 'you &gt; me'),
  853. # The "safe" and "escape" filters cannot work due to internal
  854. # implementation details (fortunately, the (no)autoescape block
  855. # tags can be used in those cases)
  856. 'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError),
  857. }
  858. if __name__ == "__main__":
  859. unittest.main()