|
1 # -*- coding: utf-8 -*- |
|
2 from django.conf import settings |
|
3 |
|
4 if __name__ == '__main__': |
|
5 # When running this file in isolation, we need to set up the configuration |
|
6 # before importing 'template'. |
|
7 settings.configure() |
|
8 |
|
9 from datetime import datetime, timedelta |
|
10 import time |
|
11 import os |
|
12 import sys |
|
13 import traceback |
|
14 import unittest |
|
15 |
|
16 from django import template |
|
17 from django.core import urlresolvers |
|
18 from django.template import loader |
|
19 from django.template.loaders import app_directories, filesystem, cached |
|
20 from django.utils.translation import activate, deactivate, ugettext as _ |
|
21 from django.utils.safestring import mark_safe |
|
22 from django.utils.tzinfo import LocalTimezone |
|
23 |
|
24 from context import ContextTests |
|
25 from custom import CustomTests |
|
26 from parser import ParserTests |
|
27 from unicode import UnicodeTests |
|
28 from nodelist import NodelistTest |
|
29 from smartif import * |
|
30 |
|
31 try: |
|
32 from loaders import * |
|
33 except ImportError: |
|
34 pass # If setuptools isn't installed, that's fine. Just move on. |
|
35 |
|
36 import filters |
|
37 |
|
38 ################################# |
|
39 # Custom template tag for tests # |
|
40 ################################# |
|
41 |
|
42 register = template.Library() |
|
43 |
|
44 class EchoNode(template.Node): |
|
45 def __init__(self, contents): |
|
46 self.contents = contents |
|
47 |
|
48 def render(self, context): |
|
49 return " ".join(self.contents) |
|
50 |
|
51 def do_echo(parser, token): |
|
52 return EchoNode(token.contents.split()[1:]) |
|
53 |
|
54 register.tag("echo", do_echo) |
|
55 |
|
56 template.libraries['testtags'] = register |
|
57 |
|
58 ##################################### |
|
59 # Helper objects for template tests # |
|
60 ##################################### |
|
61 |
|
62 class SomeException(Exception): |
|
63 silent_variable_failure = True |
|
64 |
|
65 class SomeOtherException(Exception): |
|
66 pass |
|
67 |
|
68 class ContextStackException(Exception): |
|
69 pass |
|
70 |
|
71 class SomeClass: |
|
72 def __init__(self): |
|
73 self.otherclass = OtherClass() |
|
74 |
|
75 def method(self): |
|
76 return "SomeClass.method" |
|
77 |
|
78 def method2(self, o): |
|
79 return o |
|
80 |
|
81 def method3(self): |
|
82 raise SomeException |
|
83 |
|
84 def method4(self): |
|
85 raise SomeOtherException |
|
86 |
|
87 class OtherClass: |
|
88 def method(self): |
|
89 return "OtherClass.method" |
|
90 |
|
91 class TestObj(object): |
|
92 def is_true(self): |
|
93 return True |
|
94 |
|
95 def is_false(self): |
|
96 return False |
|
97 |
|
98 def is_bad(self): |
|
99 time.sleep(0.3) |
|
100 return True |
|
101 |
|
102 class SilentGetItemClass(object): |
|
103 def __getitem__(self, key): |
|
104 raise SomeException |
|
105 |
|
106 class SilentAttrClass(object): |
|
107 def b(self): |
|
108 raise SomeException |
|
109 b = property(b) |
|
110 |
|
111 class UTF8Class: |
|
112 "Class whose __str__ returns non-ASCII data" |
|
113 def __str__(self): |
|
114 return u'ŠĐĆŽćžšđ'.encode('utf-8') |
|
115 |
|
116 class Templates(unittest.TestCase): |
|
117 def test_loaders_security(self): |
|
118 ad_loader = app_directories.Loader() |
|
119 fs_loader = filesystem.Loader() |
|
120 def test_template_sources(path, template_dirs, expected_sources): |
|
121 if isinstance(expected_sources, list): |
|
122 # Fix expected sources so they are normcased and abspathed |
|
123 expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources] |
|
124 # Test the two loaders (app_directores and filesystem). |
|
125 func1 = lambda p, t: list(ad_loader.get_template_sources(p, t)) |
|
126 func2 = lambda p, t: list(fs_loader.get_template_sources(p, t)) |
|
127 for func in (func1, func2): |
|
128 if isinstance(expected_sources, list): |
|
129 self.assertEqual(func(path, template_dirs), expected_sources) |
|
130 else: |
|
131 self.assertRaises(expected_sources, func, path, template_dirs) |
|
132 |
|
133 template_dirs = ['/dir1', '/dir2'] |
|
134 test_template_sources('index.html', template_dirs, |
|
135 ['/dir1/index.html', '/dir2/index.html']) |
|
136 test_template_sources('/etc/passwd', template_dirs, []) |
|
137 test_template_sources('etc/passwd', template_dirs, |
|
138 ['/dir1/etc/passwd', '/dir2/etc/passwd']) |
|
139 test_template_sources('../etc/passwd', template_dirs, []) |
|
140 test_template_sources('../../../etc/passwd', template_dirs, []) |
|
141 test_template_sources('/dir1/index.html', template_dirs, |
|
142 ['/dir1/index.html']) |
|
143 test_template_sources('../dir2/index.html', template_dirs, |
|
144 ['/dir2/index.html']) |
|
145 test_template_sources('/dir1blah', template_dirs, []) |
|
146 test_template_sources('../dir1blah', template_dirs, []) |
|
147 |
|
148 # UTF-8 bytestrings are permitted. |
|
149 test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs, |
|
150 [u'/dir1/Ångström', u'/dir2/Ångström']) |
|
151 # Unicode strings are permitted. |
|
152 test_template_sources(u'Ångström', template_dirs, |
|
153 [u'/dir1/Ångström', u'/dir2/Ångström']) |
|
154 test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström']) |
|
155 test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'], |
|
156 [u'/Straße/Ångström']) |
|
157 # Invalid UTF-8 encoding in bytestrings is not. Should raise a |
|
158 # semi-useful error message. |
|
159 test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError) |
|
160 |
|
161 # Case insensitive tests (for win32). Not run unless we're on |
|
162 # a case insensitive operating system. |
|
163 if os.path.normcase('/TEST') == os.path.normpath('/test'): |
|
164 template_dirs = ['/dir1', '/DIR2'] |
|
165 test_template_sources('index.html', template_dirs, |
|
166 ['/dir1/index.html', '/dir2/index.html']) |
|
167 test_template_sources('/DIR1/index.HTML', template_dirs, |
|
168 ['/dir1/index.html']) |
|
169 |
|
170 def test_loader_debug_origin(self): |
|
171 # Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with |
|
172 # the compiled templates. |
|
173 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True |
|
174 old_loaders = loader.template_source_loaders |
|
175 |
|
176 try: |
|
177 loader.template_source_loaders = (filesystem.Loader(),) |
|
178 |
|
179 # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to |
|
180 # point to a directory containing a 404.html file. Also that |
|
181 # the file system and app directories loaders both inherit the |
|
182 # load_template method from the BaseLoader class, so we only need |
|
183 # to test one of them. |
|
184 load_name = '404.html' |
|
185 template = loader.get_template(load_name) |
|
186 template_name = template.nodelist[0].source[0].name |
|
187 self.assertTrue(template_name.endswith(load_name), |
|
188 'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name) |
|
189 |
|
190 # Aso test the cached loader, since it overrides load_template |
|
191 cache_loader = cached.Loader(('',)) |
|
192 cache_loader._cached_loaders = loader.template_source_loaders |
|
193 loader.template_source_loaders = (cache_loader,) |
|
194 |
|
195 template = loader.get_template(load_name) |
|
196 template_name = template.nodelist[0].source[0].name |
|
197 self.assertTrue(template_name.endswith(load_name), |
|
198 'Template loaded through cached loader has incorrect name for debug page: %s' % template_name) |
|
199 |
|
200 template = loader.get_template(load_name) |
|
201 template_name = template.nodelist[0].source[0].name |
|
202 self.assertTrue(template_name.endswith(load_name), |
|
203 'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name) |
|
204 finally: |
|
205 loader.template_source_loaders = old_loaders |
|
206 settings.TEMPLATE_DEBUG = old_td |
|
207 |
|
208 def test_extends_include_missing_baseloader(self): |
|
209 """ |
|
210 Tests that the correct template is identified as not existing |
|
211 when {% extends %} specifies a template that does exist, but |
|
212 that template has an {% include %} of something that does not |
|
213 exist. See #12787. |
|
214 """ |
|
215 |
|
216 # TEMPLATE_DEBUG must be true, otherwise the exception raised |
|
217 # during {% include %} processing will be suppressed. |
|
218 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True |
|
219 old_loaders = loader.template_source_loaders |
|
220 |
|
221 try: |
|
222 # Test the base loader class via the app loader. load_template |
|
223 # from base is used by all shipped loaders excepting cached, |
|
224 # which has its own test. |
|
225 loader.template_source_loaders = (app_directories.Loader(),) |
|
226 |
|
227 load_name = 'test_extends_error.html' |
|
228 tmpl = loader.get_template(load_name) |
|
229 r = None |
|
230 try: |
|
231 r = tmpl.render(template.Context({})) |
|
232 except template.TemplateSyntaxError, e: |
|
233 settings.TEMPLATE_DEBUG = old_td |
|
234 self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html') |
|
235 self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) |
|
236 finally: |
|
237 loader.template_source_loaders = old_loaders |
|
238 settings.TEMPLATE_DEBUG = old_td |
|
239 |
|
240 def test_extends_include_missing_cachedloader(self): |
|
241 """ |
|
242 Same as test_extends_include_missing_baseloader, only tests |
|
243 behavior of the cached loader instead of BaseLoader. |
|
244 """ |
|
245 |
|
246 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True |
|
247 old_loaders = loader.template_source_loaders |
|
248 |
|
249 try: |
|
250 cache_loader = cached.Loader(('',)) |
|
251 cache_loader._cached_loaders = (app_directories.Loader(),) |
|
252 loader.template_source_loaders = (cache_loader,) |
|
253 |
|
254 load_name = 'test_extends_error.html' |
|
255 tmpl = loader.get_template(load_name) |
|
256 r = None |
|
257 try: |
|
258 r = tmpl.render(template.Context({})) |
|
259 except template.TemplateSyntaxError, e: |
|
260 self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html') |
|
261 self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) |
|
262 |
|
263 # For the cached loader, repeat the test, to ensure the first attempt did not cache a |
|
264 # result that behaves incorrectly on subsequent attempts. |
|
265 tmpl = loader.get_template(load_name) |
|
266 try: |
|
267 tmpl.render(template.Context({})) |
|
268 except template.TemplateSyntaxError, e: |
|
269 self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html') |
|
270 self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) |
|
271 finally: |
|
272 loader.template_source_loaders = old_loaders |
|
273 settings.TEMPLATE_DEBUG = old_td |
|
274 |
|
275 def test_token_smart_split(self): |
|
276 # Regression test for #7027 |
|
277 token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")') |
|
278 split = token.split_contents() |
|
279 self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")']) |
|
280 |
|
281 def test_url_reverse_no_settings_module(self): |
|
282 # Regression test for #9005 |
|
283 from django.template import Template, Context, TemplateSyntaxError |
|
284 |
|
285 old_settings_module = settings.SETTINGS_MODULE |
|
286 old_template_debug = settings.TEMPLATE_DEBUG |
|
287 |
|
288 settings.SETTINGS_MODULE = None |
|
289 settings.TEMPLATE_DEBUG = True |
|
290 |
|
291 t = Template('{% url will_not_match %}') |
|
292 c = Context() |
|
293 try: |
|
294 rendered = t.render(c) |
|
295 except TemplateSyntaxError, e: |
|
296 # Assert that we are getting the template syntax error and not the |
|
297 # string encoding error. |
|
298 self.assertEquals(e.args[0], "Caught NoReverseMatch while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.") |
|
299 |
|
300 settings.SETTINGS_MODULE = old_settings_module |
|
301 settings.TEMPLATE_DEBUG = old_template_debug |
|
302 |
|
303 def test_invalid_block_suggestion(self): |
|
304 # See #7876 |
|
305 from django.template import Template, TemplateSyntaxError |
|
306 try: |
|
307 t = Template("{% if 1 %}lala{% endblock %}{% endif %}") |
|
308 except TemplateSyntaxError, e: |
|
309 self.assertEquals(e.args[0], "Invalid block tag: 'endblock', expected 'else' or 'endif'") |
|
310 |
|
311 def test_templates(self): |
|
312 template_tests = self.get_template_tests() |
|
313 filter_tests = filters.get_filter_tests() |
|
314 |
|
315 # Quickly check that we aren't accidentally using a name in both |
|
316 # template and filter tests. |
|
317 overlapping_names = [name for name in filter_tests if name in template_tests] |
|
318 assert not overlapping_names, 'Duplicate test name(s): %s' % ', '.join(overlapping_names) |
|
319 |
|
320 template_tests.update(filter_tests) |
|
321 |
|
322 # Register our custom template loader. |
|
323 def test_template_loader(template_name, template_dirs=None): |
|
324 "A custom template loader that loads the unit-test templates." |
|
325 try: |
|
326 return (template_tests[template_name][0] , "test:%s" % template_name) |
|
327 except KeyError: |
|
328 raise template.TemplateDoesNotExist, template_name |
|
329 |
|
330 cache_loader = cached.Loader(('test_template_loader',)) |
|
331 cache_loader._cached_loaders = (test_template_loader,) |
|
332 |
|
333 old_template_loaders = loader.template_source_loaders |
|
334 loader.template_source_loaders = [cache_loader] |
|
335 |
|
336 failures = [] |
|
337 tests = template_tests.items() |
|
338 tests.sort() |
|
339 |
|
340 # Turn TEMPLATE_DEBUG off, because tests assume that. |
|
341 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False |
|
342 |
|
343 # Set TEMPLATE_STRING_IF_INVALID to a known string. |
|
344 old_invalid = settings.TEMPLATE_STRING_IF_INVALID |
|
345 expected_invalid_str = 'INVALID' |
|
346 |
|
347 # Warm the URL reversing cache. This ensures we don't pay the cost |
|
348 # warming the cache during one of the tests. |
|
349 urlresolvers.reverse('regressiontests.templates.views.client_action', |
|
350 kwargs={'id':0,'action':"update"}) |
|
351 |
|
352 for name, vals in tests: |
|
353 if isinstance(vals[2], tuple): |
|
354 normal_string_result = vals[2][0] |
|
355 invalid_string_result = vals[2][1] |
|
356 if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result: |
|
357 expected_invalid_str = 'INVALID %s' |
|
358 invalid_string_result = invalid_string_result % vals[2][2] |
|
359 template.invalid_var_format_string = True |
|
360 else: |
|
361 normal_string_result = vals[2] |
|
362 invalid_string_result = vals[2] |
|
363 |
|
364 if 'LANGUAGE_CODE' in vals[1]: |
|
365 activate(vals[1]['LANGUAGE_CODE']) |
|
366 else: |
|
367 activate('en-us') |
|
368 |
|
369 for invalid_str, result in [('', normal_string_result), |
|
370 (expected_invalid_str, invalid_string_result)]: |
|
371 settings.TEMPLATE_STRING_IF_INVALID = invalid_str |
|
372 for is_cached in (False, True): |
|
373 try: |
|
374 start = datetime.now() |
|
375 test_template = loader.get_template(name) |
|
376 end = datetime.now() |
|
377 if end-start > timedelta(seconds=0.2): |
|
378 failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Took too long to parse test" % (is_cached, invalid_str, name)) |
|
379 |
|
380 start = datetime.now() |
|
381 output = self.render(test_template, vals) |
|
382 end = datetime.now() |
|
383 if end-start > timedelta(seconds=0.2): |
|
384 failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Took too long to render test" % (is_cached, invalid_str, name)) |
|
385 except ContextStackException: |
|
386 failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, name)) |
|
387 continue |
|
388 except Exception: |
|
389 exc_type, exc_value, exc_tb = sys.exc_info() |
|
390 if exc_type != result: |
|
391 tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb)) |
|
392 failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, name, exc_type, exc_value, tb)) |
|
393 continue |
|
394 if output != result: |
|
395 failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, name, result, output)) |
|
396 cache_loader.reset() |
|
397 |
|
398 if 'LANGUAGE_CODE' in vals[1]: |
|
399 deactivate() |
|
400 |
|
401 if template.invalid_var_format_string: |
|
402 expected_invalid_str = 'INVALID' |
|
403 template.invalid_var_format_string = False |
|
404 |
|
405 loader.template_source_loaders = old_template_loaders |
|
406 deactivate() |
|
407 settings.TEMPLATE_DEBUG = old_td |
|
408 settings.TEMPLATE_STRING_IF_INVALID = old_invalid |
|
409 |
|
410 self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % |
|
411 ('-'*70, ("\n%s\n" % ('-'*70)).join(failures))) |
|
412 |
|
413 def render(self, test_template, vals): |
|
414 context = template.Context(vals[1]) |
|
415 before_stack_size = len(context.dicts) |
|
416 output = test_template.render(context) |
|
417 if len(context.dicts) != before_stack_size: |
|
418 raise ContextStackException |
|
419 return output |
|
420 |
|
421 def get_template_tests(self): |
|
422 # SYNTAX -- |
|
423 # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class) |
|
424 return { |
|
425 ### BASIC SYNTAX ################################################ |
|
426 |
|
427 # Plain text should go through the template parser untouched |
|
428 'basic-syntax01': ("something cool", {}, "something cool"), |
|
429 |
|
430 # Variables should be replaced with their value in the current |
|
431 # context |
|
432 'basic-syntax02': ("{{ headline }}", {'headline':'Success'}, "Success"), |
|
433 |
|
434 # More than one replacement variable is allowed in a template |
|
435 'basic-syntax03': ("{{ first }} --- {{ second }}", {"first" : 1, "second" : 2}, "1 --- 2"), |
|
436 |
|
437 # Fail silently when a variable is not found in the current context |
|
438 'basic-syntax04': ("as{{ missing }}df", {}, ("asdf","asINVALIDdf")), |
|
439 |
|
440 # A variable may not contain more than one word |
|
441 'basic-syntax06': ("{{ multi word variable }}", {}, template.TemplateSyntaxError), |
|
442 |
|
443 # Raise TemplateSyntaxError for empty variable tags |
|
444 'basic-syntax07': ("{{ }}", {}, template.TemplateSyntaxError), |
|
445 'basic-syntax08': ("{{ }}", {}, template.TemplateSyntaxError), |
|
446 |
|
447 # Attribute syntax allows a template to call an object's attribute |
|
448 'basic-syntax09': ("{{ var.method }}", {"var": SomeClass()}, "SomeClass.method"), |
|
449 |
|
450 # Multiple levels of attribute access are allowed |
|
451 'basic-syntax10': ("{{ var.otherclass.method }}", {"var": SomeClass()}, "OtherClass.method"), |
|
452 |
|
453 # Fail silently when a variable's attribute isn't found |
|
454 'basic-syntax11': ("{{ var.blech }}", {"var": SomeClass()}, ("","INVALID")), |
|
455 |
|
456 # Raise TemplateSyntaxError when trying to access a variable beginning with an underscore |
|
457 'basic-syntax12': ("{{ var.__dict__ }}", {"var": SomeClass()}, template.TemplateSyntaxError), |
|
458 |
|
459 # Raise TemplateSyntaxError when trying to access a variable containing an illegal character |
|
460 'basic-syntax13': ("{{ va>r }}", {}, template.TemplateSyntaxError), |
|
461 'basic-syntax14': ("{{ (var.r) }}", {}, template.TemplateSyntaxError), |
|
462 'basic-syntax15': ("{{ sp%am }}", {}, template.TemplateSyntaxError), |
|
463 'basic-syntax16': ("{{ eggs! }}", {}, template.TemplateSyntaxError), |
|
464 'basic-syntax17': ("{{ moo? }}", {}, template.TemplateSyntaxError), |
|
465 |
|
466 # Attribute syntax allows a template to call a dictionary key's value |
|
467 'basic-syntax18': ("{{ foo.bar }}", {"foo" : {"bar" : "baz"}}, "baz"), |
|
468 |
|
469 # Fail silently when a variable's dictionary key isn't found |
|
470 'basic-syntax19': ("{{ foo.spam }}", {"foo" : {"bar" : "baz"}}, ("","INVALID")), |
|
471 |
|
472 # Fail silently when accessing a non-simple method |
|
473 'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")), |
|
474 |
|
475 # Don't get confused when parsing something that is almost, but not |
|
476 # quite, a template tag. |
|
477 'basic-syntax21': ("a {{ moo %} b", {}, "a {{ moo %} b"), |
|
478 'basic-syntax22': ("{{ moo #}", {}, "{{ moo #}"), |
|
479 |
|
480 # Will try to treat "moo #} {{ cow" as the variable. Not ideal, but |
|
481 # costly to work around, so this triggers an error. |
|
482 'basic-syntax23': ("{{ moo #} {{ cow }}", {"cow": "cow"}, template.TemplateSyntaxError), |
|
483 |
|
484 # Embedded newlines make it not-a-tag. |
|
485 'basic-syntax24': ("{{ moo\n }}", {}, "{{ moo\n }}"), |
|
486 |
|
487 # Literal strings are permitted inside variables, mostly for i18n |
|
488 # purposes. |
|
489 'basic-syntax25': ('{{ "fred" }}', {}, "fred"), |
|
490 'basic-syntax26': (r'{{ "\"fred\"" }}', {}, "\"fred\""), |
|
491 'basic-syntax27': (r'{{ _("\"fred\"") }}', {}, "\"fred\""), |
|
492 |
|
493 # regression test for ticket #12554 |
|
494 # make sure a silent_variable_failure Exception is supressed |
|
495 # on dictionary and attribute lookup |
|
496 'basic-syntax28': ("{{ a.b }}", {'a': SilentGetItemClass()}, ('', 'INVALID')), |
|
497 'basic-syntax29': ("{{ a.b }}", {'a': SilentAttrClass()}, ('', 'INVALID')), |
|
498 |
|
499 # Something that starts like a number but has an extra lookup works as a lookup. |
|
500 'basic-syntax30': ("{{ 1.2.3 }}", {"1": {"2": {"3": "d"}}}, "d"), |
|
501 'basic-syntax31': ("{{ 1.2.3 }}", {"1": {"2": ("a", "b", "c", "d")}}, "d"), |
|
502 'basic-syntax32': ("{{ 1.2.3 }}", {"1": (("x", "x", "x", "x"), ("y", "y", "y", "y"), ("a", "b", "c", "d"))}, "d"), |
|
503 'basic-syntax33': ("{{ 1.2.3 }}", {"1": ("xxxx", "yyyy", "abcd")}, "d"), |
|
504 'basic-syntax34': ("{{ 1.2.3 }}", {"1": ({"x": "x"}, {"y": "y"}, {"z": "z", "3": "d"})}, "d"), |
|
505 |
|
506 # Numbers are numbers even if their digits are in the context. |
|
507 'basic-syntax35': ("{{ 1 }}", {"1": "abc"}, "1"), |
|
508 'basic-syntax36': ("{{ 1.2 }}", {"1": "abc"}, "1.2"), |
|
509 |
|
510 # List-index syntax allows a template to access a certain item of a subscriptable object. |
|
511 'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"), |
|
512 |
|
513 # Fail silently when the list index is out of range. |
|
514 'list-index02': ("{{ var.5 }}", {"var": ["first item", "second item"]}, ("", "INVALID")), |
|
515 |
|
516 # Fail silently when the variable is not a subscriptable object. |
|
517 'list-index03': ("{{ var.1 }}", {"var": None}, ("", "INVALID")), |
|
518 |
|
519 # Fail silently when variable is a dict without the specified key. |
|
520 'list-index04': ("{{ var.1 }}", {"var": {}}, ("", "INVALID")), |
|
521 |
|
522 # Dictionary lookup wins out when dict's key is a string. |
|
523 'list-index05': ("{{ var.1 }}", {"var": {'1': "hello"}}, "hello"), |
|
524 |
|
525 # But list-index lookup wins out when dict's key is an int, which |
|
526 # behind the scenes is really a dictionary lookup (for a dict) |
|
527 # after converting the key to an int. |
|
528 'list-index06': ("{{ var.1 }}", {"var": {1: "hello"}}, "hello"), |
|
529 |
|
530 # Dictionary lookup wins out when there is a string and int version of the key. |
|
531 'list-index07': ("{{ var.1 }}", {"var": {'1': "hello", 1: "world"}}, "hello"), |
|
532 |
|
533 # Basic filter usage |
|
534 'filter-syntax01': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"), |
|
535 |
|
536 # Chained filters |
|
537 'filter-syntax02': ("{{ var|upper|lower }}", {"var": "Django is the greatest!"}, "django is the greatest!"), |
|
538 |
|
539 # Raise TemplateSyntaxError for space between a variable and filter pipe |
|
540 'filter-syntax03': ("{{ var |upper }}", {}, template.TemplateSyntaxError), |
|
541 |
|
542 # Raise TemplateSyntaxError for space after a filter pipe |
|
543 'filter-syntax04': ("{{ var| upper }}", {}, template.TemplateSyntaxError), |
|
544 |
|
545 # Raise TemplateSyntaxError for a nonexistent filter |
|
546 'filter-syntax05': ("{{ var|does_not_exist }}", {}, template.TemplateSyntaxError), |
|
547 |
|
548 # Raise TemplateSyntaxError when trying to access a filter containing an illegal character |
|
549 'filter-syntax06': ("{{ var|fil(ter) }}", {}, template.TemplateSyntaxError), |
|
550 |
|
551 # Raise TemplateSyntaxError for invalid block tags |
|
552 'filter-syntax07': ("{% nothing_to_see_here %}", {}, template.TemplateSyntaxError), |
|
553 |
|
554 # Raise TemplateSyntaxError for empty block tags |
|
555 'filter-syntax08': ("{% %}", {}, template.TemplateSyntaxError), |
|
556 |
|
557 # Chained filters, with an argument to the first one |
|
558 'filter-syntax09': ('{{ var|removetags:"b i"|upper|lower }}', {"var": "<b><i>Yes</i></b>"}, "yes"), |
|
559 |
|
560 # Literal string as argument is always "safe" from auto-escaping.. |
|
561 'filter-syntax10': (r'{{ var|default_if_none:" endquote\" hah" }}', |
|
562 {"var": None}, ' endquote" hah'), |
|
563 |
|
564 # Variable as argument |
|
565 'filter-syntax11': (r'{{ var|default_if_none:var2 }}', {"var": None, "var2": "happy"}, 'happy'), |
|
566 |
|
567 # Default argument testing |
|
568 'filter-syntax12': (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'), |
|
569 |
|
570 # Fail silently for methods that raise an exception with a |
|
571 # "silent_variable_failure" attribute |
|
572 'filter-syntax13': (r'1{{ var.method3 }}2', {"var": SomeClass()}, ("12", "1INVALID2")), |
|
573 |
|
574 # In methods that raise an exception without a |
|
575 # "silent_variable_attribute" set to True, the exception propagates |
|
576 'filter-syntax14': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException), |
|
577 |
|
578 # Escaped backslash in argument |
|
579 'filter-syntax15': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'), |
|
580 |
|
581 # Escaped backslash using known escape char |
|
582 'filter-syntax16': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'), |
|
583 |
|
584 # Empty strings can be passed as arguments to filters |
|
585 'filter-syntax17': (r'{{ var|join:"" }}', {'var': ['a', 'b', 'c']}, 'abc'), |
|
586 |
|
587 # Make sure that any unicode strings are converted to bytestrings |
|
588 # in the final output. |
|
589 'filter-syntax18': (r'{{ var }}', {'var': UTF8Class()}, u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'), |
|
590 |
|
591 # Numbers as filter arguments should work |
|
592 'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."), |
|
593 |
|
594 #filters should accept empty string constants |
|
595 'filter-syntax20': ('{{ ""|default_if_none:"was none" }}', {}, ""), |
|
596 |
|
597 ### COMMENT SYNTAX ######################################################## |
|
598 'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"), |
|
599 'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"), |
|
600 |
|
601 # Comments can contain invalid stuff. |
|
602 'comment-syntax03': ("foo{# {% if %} #}", {}, "foo"), |
|
603 'comment-syntax04': ("foo{# {% endblock %} #}", {}, "foo"), |
|
604 'comment-syntax05': ("foo{# {% somerandomtag %} #}", {}, "foo"), |
|
605 'comment-syntax06': ("foo{# {% #}", {}, "foo"), |
|
606 'comment-syntax07': ("foo{# %} #}", {}, "foo"), |
|
607 'comment-syntax08': ("foo{# %} #}bar", {}, "foobar"), |
|
608 'comment-syntax09': ("foo{# {{ #}", {}, "foo"), |
|
609 'comment-syntax10': ("foo{# }} #}", {}, "foo"), |
|
610 'comment-syntax11': ("foo{# { #}", {}, "foo"), |
|
611 'comment-syntax12': ("foo{# } #}", {}, "foo"), |
|
612 |
|
613 ### COMMENT TAG ########################################################### |
|
614 'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"), |
|
615 'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"), |
|
616 |
|
617 # Comment tag can contain invalid stuff. |
|
618 'comment-tag03': ("foo{% comment %} {% if %} {% endcomment %}", {}, "foo"), |
|
619 'comment-tag04': ("foo{% comment %} {% endblock %} {% endcomment %}", {}, "foo"), |
|
620 'comment-tag05': ("foo{% comment %} {% somerandomtag %} {% endcomment %}", {}, "foo"), |
|
621 |
|
622 ### CYCLE TAG ############################################################# |
|
623 'cycle01': ('{% cycle a %}', {}, template.TemplateSyntaxError), |
|
624 'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'), |
|
625 'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'), |
|
626 'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'), |
|
627 'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError), |
|
628 'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError), |
|
629 'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError), |
|
630 'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'), |
|
631 'cycle09': ("{% for i in test %}{% cycle a,b %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'), |
|
632 'cycle10': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}", {}, 'ab'), |
|
633 'cycle11': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}", {}, 'abc'), |
|
634 'cycle12': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}", {}, 'abca'), |
|
635 'cycle13': ("{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'), |
|
636 'cycle14': ("{% cycle one two as foo %}{% cycle foo %}", {'one': '1','two': '2'}, '12'), |
|
637 'cycle15': ("{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}", {'test': range(5), 'aye': 'a', 'bee': 'b'}, 'a0,b1,a2,b3,a4,'), |
|
638 'cycle16': ("{% cycle one|lower two as foo %}{% cycle foo %}", {'one': 'A','two': '2'}, 'a2'), |
|
639 |
|
640 ### EXCEPTIONS ############################################################ |
|
641 |
|
642 # Raise exception for invalid template name |
|
643 'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist), |
|
644 |
|
645 # Raise exception for invalid template name (in variable) |
|
646 'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)), |
|
647 |
|
648 # Raise exception for extra {% extends %} tags |
|
649 'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError), |
|
650 |
|
651 # Raise exception for custom tags used in child with {% load %} tag in parent, not in child |
|
652 'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError), |
|
653 |
|
654 ### FILTER TAG ############################################################ |
|
655 'filter01': ('{% filter upper %}{% endfilter %}', {}, ''), |
|
656 'filter02': ('{% filter upper %}django{% endfilter %}', {}, 'DJANGO'), |
|
657 'filter03': ('{% filter upper|lower %}django{% endfilter %}', {}, 'django'), |
|
658 'filter04': ('{% filter cut:remove %}djangospam{% endfilter %}', {'remove': 'spam'}, 'django'), |
|
659 |
|
660 ### FIRSTOF TAG ########################################################### |
|
661 'firstof01': ('{% firstof a b c %}', {'a':0,'b':0,'c':0}, ''), |
|
662 'firstof02': ('{% firstof a b c %}', {'a':1,'b':0,'c':0}, '1'), |
|
663 'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'), |
|
664 'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'), |
|
665 'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'), |
|
666 'firstof06': ('{% firstof a b c %}', {'b':0,'c':3}, '3'), |
|
667 'firstof07': ('{% firstof a b "c" %}', {'a':0}, 'c'), |
|
668 'firstof08': ('{% firstof a b "c and d" %}', {'a':0,'b':0}, 'c and d'), |
|
669 'firstof09': ('{% firstof %}', {}, template.TemplateSyntaxError), |
|
670 |
|
671 ### FOR TAG ############################################################### |
|
672 'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"), |
|
673 'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"), |
|
674 'for-tag-vars01': ("{% for val in values %}{{ forloop.counter }}{% endfor %}", {"values": [6, 6, 6]}, "123"), |
|
675 'for-tag-vars02': ("{% for val in values %}{{ forloop.counter0 }}{% endfor %}", {"values": [6, 6, 6]}, "012"), |
|
676 'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"), |
|
677 'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"), |
|
678 'for-tag-vars05': ("{% for val in values %}{% if forloop.first %}f{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "fxx"), |
|
679 'for-tag-vars06': ("{% for val in values %}{% if forloop.last %}l{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "xxl"), |
|
680 'for-tag-unpack01': ("{% for key,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), |
|
681 'for-tag-unpack03': ("{% for key, value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), |
|
682 'for-tag-unpack04': ("{% for key , value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), |
|
683 'for-tag-unpack05': ("{% for key ,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), |
|
684 'for-tag-unpack06': ("{% for key value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError), |
|
685 'for-tag-unpack07': ("{% for key,,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError), |
|
686 'for-tag-unpack08': ("{% for key,value, in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, template.TemplateSyntaxError), |
|
687 # Ensure that a single loopvar doesn't truncate the list in val. |
|
688 'for-tag-unpack09': ("{% for val in items %}{{ val.0 }}:{{ val.1 }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), |
|
689 # Otherwise, silently truncate if the length of loopvars differs to the length of each set of items. |
|
690 'for-tag-unpack10': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'orange'))}, "one:1/two:2/"), |
|
691 '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/")), |
|
692 '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/")), |
|
693 '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/")), |
|
694 'for-tag-unpack14': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (1, 2)}, (":/:/", "INVALID:INVALID/INVALID:INVALID/")), |
|
695 'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"), |
|
696 'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"), |
|
697 'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"), |
|
698 |
|
699 ### IF TAG ################################################################ |
|
700 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"), |
|
701 'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"), |
|
702 'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"), |
|
703 |
|
704 # Filters |
|
705 'if-tag-filter01': ("{% if foo|length == 5 %}yes{% else %}no{% endif %}", {'foo': 'abcde'}, "yes"), |
|
706 'if-tag-filter02': ("{% if foo|upper == 'ABC' %}yes{% else %}no{% endif %}", {}, "no"), |
|
707 |
|
708 # Equality |
|
709 'if-tag-eq01': ("{% if foo == bar %}yes{% else %}no{% endif %}", {}, "yes"), |
|
710 'if-tag-eq02': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1}, "no"), |
|
711 'if-tag-eq03': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1, 'bar': 1}, "yes"), |
|
712 'if-tag-eq04': ("{% if foo == bar %}yes{% else %}no{% endif %}", {'foo': 1, 'bar': 2}, "no"), |
|
713 'if-tag-eq05': ("{% if foo == '' %}yes{% else %}no{% endif %}", {}, "no"), |
|
714 |
|
715 # Comparison |
|
716 'if-tag-gt-01': ("{% if 2 > 1 %}yes{% else %}no{% endif %}", {}, "yes"), |
|
717 'if-tag-gt-02': ("{% if 1 > 1 %}yes{% else %}no{% endif %}", {}, "no"), |
|
718 'if-tag-gte-01': ("{% if 1 >= 1 %}yes{% else %}no{% endif %}", {}, "yes"), |
|
719 'if-tag-gte-02': ("{% if 1 >= 2 %}yes{% else %}no{% endif %}", {}, "no"), |
|
720 'if-tag-lt-01': ("{% if 1 < 2 %}yes{% else %}no{% endif %}", {}, "yes"), |
|
721 'if-tag-lt-02': ("{% if 1 < 1 %}yes{% else %}no{% endif %}", {}, "no"), |
|
722 'if-tag-lte-01': ("{% if 1 <= 1 %}yes{% else %}no{% endif %}", {}, "yes"), |
|
723 'if-tag-lte-02': ("{% if 2 <= 1 %}yes{% else %}no{% endif %}", {}, "no"), |
|
724 |
|
725 # Contains |
|
726 'if-tag-in-01': ("{% if 1 in x %}yes{% else %}no{% endif %}", {'x':[1]}, "yes"), |
|
727 'if-tag-in-02': ("{% if 2 in x %}yes{% else %}no{% endif %}", {'x':[1]}, "no"), |
|
728 'if-tag-not-in-01': ("{% if 1 not in x %}yes{% else %}no{% endif %}", {'x':[1]}, "no"), |
|
729 'if-tag-not-in-02': ("{% if 2 not in x %}yes{% else %}no{% endif %}", {'x':[1]}, "yes"), |
|
730 |
|
731 # AND |
|
732 'if-tag-and01': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'), |
|
733 'if-tag-and02': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'), |
|
734 'if-tag-and03': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'), |
|
735 'if-tag-and04': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'), |
|
736 'if-tag-and05': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': False}, 'no'), |
|
737 'if-tag-and06': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'bar': False}, 'no'), |
|
738 'if-tag-and07': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'foo': True}, 'no'), |
|
739 'if-tag-and08': ("{% if foo and bar %}yes{% else %}no{% endif %}", {'bar': True}, 'no'), |
|
740 |
|
741 # OR |
|
742 'if-tag-or01': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'), |
|
743 'if-tag-or02': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'), |
|
744 'if-tag-or03': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'), |
|
745 'if-tag-or04': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'), |
|
746 'if-tag-or05': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': False}, 'no'), |
|
747 'if-tag-or06': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'bar': False}, 'no'), |
|
748 'if-tag-or07': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'foo': True}, 'yes'), |
|
749 'if-tag-or08': ("{% if foo or bar %}yes{% else %}no{% endif %}", {'bar': True}, 'yes'), |
|
750 |
|
751 # multiple ORs |
|
752 'if-tag-or09': ("{% if foo or bar or baz %}yes{% else %}no{% endif %}", {'baz': True}, 'yes'), |
|
753 |
|
754 # NOT |
|
755 'if-tag-not01': ("{% if not foo %}no{% else %}yes{% endif %}", {'foo': True}, 'yes'), |
|
756 'if-tag-not02': ("{% if not not foo %}no{% else %}yes{% endif %}", {'foo': True}, 'no'), |
|
757 # not03 to not05 removed, now TemplateSyntaxErrors |
|
758 |
|
759 'if-tag-not06': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {}, 'no'), |
|
760 'if-tag-not07': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'), |
|
761 'if-tag-not08': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'), |
|
762 'if-tag-not09': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'), |
|
763 'if-tag-not10': ("{% if foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'), |
|
764 |
|
765 'if-tag-not11': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {}, 'no'), |
|
766 'if-tag-not12': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'), |
|
767 'if-tag-not13': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'), |
|
768 'if-tag-not14': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'), |
|
769 'if-tag-not15': ("{% if not foo and bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'no'), |
|
770 |
|
771 'if-tag-not16': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {}, 'yes'), |
|
772 'if-tag-not17': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'), |
|
773 'if-tag-not18': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'), |
|
774 'if-tag-not19': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'), |
|
775 'if-tag-not20': ("{% if foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'), |
|
776 |
|
777 'if-tag-not21': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {}, 'yes'), |
|
778 'if-tag-not22': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'yes'), |
|
779 'if-tag-not23': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'), |
|
780 'if-tag-not24': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'), |
|
781 'if-tag-not25': ("{% if not foo or bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'), |
|
782 |
|
783 'if-tag-not26': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {}, 'yes'), |
|
784 'if-tag-not27': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'), |
|
785 'if-tag-not28': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'no'), |
|
786 'if-tag-not29': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'no'), |
|
787 'if-tag-not30': ("{% if not foo and not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'), |
|
788 |
|
789 'if-tag-not31': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {}, 'yes'), |
|
790 'if-tag-not32': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': True}, 'no'), |
|
791 'if-tag-not33': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': True, 'bar': False}, 'yes'), |
|
792 'if-tag-not34': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': True}, 'yes'), |
|
793 'if-tag-not35': ("{% if not foo or not bar %}yes{% else %}no{% endif %}", {'foo': False, 'bar': False}, 'yes'), |
|
794 |
|
795 # Various syntax errors |
|
796 'if-tag-error01': ("{% if %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
797 'if-tag-error02': ("{% if foo and %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError), |
|
798 'if-tag-error03': ("{% if foo or %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError), |
|
799 'if-tag-error04': ("{% if not foo and %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError), |
|
800 'if-tag-error05': ("{% if not foo or %}yes{% else %}no{% endif %}", {'foo': True}, template.TemplateSyntaxError), |
|
801 'if-tag-error06': ("{% if abc def %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
802 'if-tag-error07': ("{% if not %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
803 'if-tag-error08': ("{% if and %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
804 'if-tag-error09': ("{% if or %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
805 'if-tag-error10': ("{% if == %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
806 'if-tag-error11': ("{% if 1 == %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
807 'if-tag-error12': ("{% if a not b %}yes{% endif %}", {}, template.TemplateSyntaxError), |
|
808 |
|
809 # If evaluations are shortcircuited where possible |
|
810 # These tests will fail by taking too long to run. When the if clause |
|
811 # is shortcircuiting correctly, the is_bad() function shouldn't be |
|
812 # evaluated, and the deliberate sleep won't happen. |
|
813 'if-tag-shortcircuit01': ('{% if x.is_true or x.is_bad %}yes{% else %}no{% endif %}', {'x': TestObj()}, "yes"), |
|
814 'if-tag-shortcircuit02': ('{% if x.is_false and x.is_bad %}yes{% else %}no{% endif %}', {'x': TestObj()}, "no"), |
|
815 |
|
816 # Non-existent args |
|
817 'if-tag-badarg01':("{% if x|default_if_none:y %}yes{% endif %}", {}, ''), |
|
818 'if-tag-badarg02':("{% if x|default_if_none:y %}yes{% endif %}", {'y': 0}, ''), |
|
819 'if-tag-badarg03':("{% if x|default_if_none:y %}yes{% endif %}", {'y': 1}, 'yes'), |
|
820 'if-tag-badarg04':("{% if x|default_if_none:y %}yes{% else %}no{% endif %}", {}, 'no'), |
|
821 |
|
822 # Additional, more precise parsing tests are in SmartIfTests |
|
823 |
|
824 ### IFCHANGED TAG ######################################################### |
|
825 'ifchanged01': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,2,3)}, '123'), |
|
826 'ifchanged02': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,1,3)}, '13'), |
|
827 'ifchanged03': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', {'num': (1,1,1)}, '1'), |
|
828 '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'), |
|
829 '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'), |
|
830 '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'), |
|
831 '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'), |
|
832 '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'), |
|
833 |
|
834 # Test one parameter given to ifchanged. |
|
835 'ifchanged-param01': ('{% for n in num %}{% ifchanged n %}..{% endifchanged %}{{ n }}{% endfor %}', { 'num': (1,2,3) }, '..1..2..3'), |
|
836 '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'), |
|
837 |
|
838 # Test multiple parameters to ifchanged. |
|
839 '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'), |
|
840 |
|
841 # Test a date+hour like construct, where the hour of the last day |
|
842 # is the same but the date had changed, so print the hour anyway. |
|
843 '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'), |
|
844 |
|
845 # Logically the same as above, just written with explicit |
|
846 # ifchanged for the day. |
|
847 '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'), |
|
848 |
|
849 # Test the else clause of ifchanged. |
|
850 '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,'), |
|
851 |
|
852 '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,'), |
|
853 '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,'), |
|
854 |
|
855 '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'), |
|
856 |
|
857 ### IFEQUAL TAG ########################################################### |
|
858 'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), |
|
859 'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"), |
|
860 'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"), |
|
861 'ifequal04': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 1}, "yes"), |
|
862 'ifequal05': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "test"}, "yes"), |
|
863 'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"), |
|
864 'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"), |
|
865 'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"), |
|
866 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), |
|
867 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), |
|
868 |
|
869 # SMART SPLITTING |
|
870 'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, "no"), |
|
871 'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"), |
|
872 'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"), |
|
873 'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"), |
|
874 'ifequal-split05': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': ''}, "no"), |
|
875 'ifequal-split06': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i "love" you'}, "yes"), |
|
876 'ifequal-split07': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i love you'}, "no"), |
|
877 'ifequal-split08': (r"{% ifequal a 'I\'m happy' %}yes{% else %}no{% endifequal %}", {'a': "I'm happy"}, "yes"), |
|
878 'ifequal-split09': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slash\man"}, "yes"), |
|
879 'ifequal-split10': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slashman"}, "no"), |
|
880 |
|
881 # NUMERIC RESOLUTION |
|
882 'ifequal-numeric01': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': '5'}, ''), |
|
883 'ifequal-numeric02': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': 5}, 'yes'), |
|
884 'ifequal-numeric03': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5}, ''), |
|
885 'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'), |
|
886 'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'), |
|
887 'ifequal-numeric06': ('{% ifequal x .2 %}yes{% endifequal %}', {'x': .2}, 'yes'), |
|
888 'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ''), |
|
889 'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''), |
|
890 'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'), |
|
891 'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'), |
|
892 'ifequal-numeric11': ('{% ifequal x -5.2 %}yes{% endifequal %}', {'x': -5.2}, 'yes'), |
|
893 'ifequal-numeric12': ('{% ifequal x +5 %}yes{% endifequal %}', {'x': 5}, 'yes'), |
|
894 |
|
895 # FILTER EXPRESSIONS AS ARGUMENTS |
|
896 'ifequal-filter01': ('{% ifequal a|upper "A" %}x{% endifequal %}', {'a': 'a'}, 'x'), |
|
897 'ifequal-filter02': ('{% ifequal "A" a|upper %}x{% endifequal %}', {'a': 'a'}, 'x'), |
|
898 'ifequal-filter03': ('{% ifequal a|upper b|upper %}x{% endifequal %}', {'a': 'x', 'b': 'X'}, 'x'), |
|
899 'ifequal-filter04': ('{% ifequal x|slice:"1" "a" %}x{% endifequal %}', {'x': 'aaa'}, 'x'), |
|
900 'ifequal-filter05': ('{% ifequal x|slice:"1"|upper "A" %}x{% endifequal %}', {'x': 'aaa'}, 'x'), |
|
901 |
|
902 ### IFNOTEQUAL TAG ######################################################## |
|
903 'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), |
|
904 'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""), |
|
905 'ifnotequal03': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), |
|
906 'ifnotequal04': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 1}, "no"), |
|
907 |
|
908 ### INCLUDE TAG ########################################################### |
|
909 'include01': ('{% include "basic-syntax01" %}', {}, "something cool"), |
|
910 'include02': ('{% include "basic-syntax02" %}', {'headline': 'Included'}, "Included"), |
|
911 'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"), |
|
912 'include04': ('a{% include "nonexistent" %}b', {}, "ab"), |
|
913 'include 05': ('template with a space', {}, 'template with a space'), |
|
914 'include06': ('{% include "include 05"%}', {}, 'template with a space'), |
|
915 |
|
916 ### NAMED ENDBLOCKS ####################################################### |
|
917 |
|
918 # Basic test |
|
919 'namedendblocks01': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock first %}3", {}, '1_2_3'), |
|
920 |
|
921 # Unbalanced blocks |
|
922 'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}_{% endblock second %}3", {}, template.TemplateSyntaxError), |
|
923 'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock second %}3", {}, template.TemplateSyntaxError), |
|
924 'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock third %}3", {}, template.TemplateSyntaxError), |
|
925 'namedendblocks05': ("1{% block first %}_{% block second %}2{% endblock first %}", {}, template.TemplateSyntaxError), |
|
926 |
|
927 # Mixed named and unnamed endblocks |
|
928 'namedendblocks06': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'), |
|
929 'namedendblocks07': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'), |
|
930 |
|
931 ### INHERITANCE ########################################################### |
|
932 |
|
933 # Standard template with no inheritance |
|
934 'inheritance01': ("1{% block first %}&{% endblock %}3{% block second %}_{% endblock %}", {}, '1&3_'), |
|
935 |
|
936 # Standard two-level inheritance |
|
937 'inheritance02': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'), |
|
938 |
|
939 # Three-level with no redefinitions on third level |
|
940 'inheritance03': ("{% extends 'inheritance02' %}", {}, '1234'), |
|
941 |
|
942 # Two-level with no redefinitions on second level |
|
943 'inheritance04': ("{% extends 'inheritance01' %}", {}, '1&3_'), |
|
944 |
|
945 # Two-level with double quotes instead of single quotes |
|
946 'inheritance05': ('{% extends "inheritance02" %}', {}, '1234'), |
|
947 |
|
948 # Three-level with variable parent-template name |
|
949 'inheritance06': ("{% extends foo %}", {'foo': 'inheritance02'}, '1234'), |
|
950 |
|
951 # Two-level with one block defined, one block not defined |
|
952 'inheritance07': ("{% extends 'inheritance01' %}{% block second %}5{% endblock %}", {}, '1&35'), |
|
953 |
|
954 # Three-level with one block defined on this level, two blocks defined next level |
|
955 'inheritance08': ("{% extends 'inheritance02' %}{% block second %}5{% endblock %}", {}, '1235'), |
|
956 |
|
957 # Three-level with second and third levels blank |
|
958 'inheritance09': ("{% extends 'inheritance04' %}", {}, '1&3_'), |
|
959 |
|
960 # Three-level with space NOT in a block -- should be ignored |
|
961 'inheritance10': ("{% extends 'inheritance04' %} ", {}, '1&3_'), |
|
962 |
|
963 # Three-level with both blocks defined on this level, but none on second level |
|
964 'inheritance11': ("{% extends 'inheritance04' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'), |
|
965 |
|
966 # Three-level with this level providing one and second level providing the other |
|
967 'inheritance12': ("{% extends 'inheritance07' %}{% block first %}2{% endblock %}", {}, '1235'), |
|
968 |
|
969 # Three-level with this level overriding second level |
|
970 'inheritance13': ("{% extends 'inheritance02' %}{% block first %}a{% endblock %}{% block second %}b{% endblock %}", {}, '1a3b'), |
|
971 |
|
972 # A block defined only in a child template shouldn't be displayed |
|
973 'inheritance14': ("{% extends 'inheritance01' %}{% block newblock %}NO DISPLAY{% endblock %}", {}, '1&3_'), |
|
974 |
|
975 # A block within another block |
|
976 'inheritance15': ("{% extends 'inheritance01' %}{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}", {}, '12inner3_'), |
|
977 |
|
978 # A block within another block (level 2) |
|
979 'inheritance16': ("{% extends 'inheritance15' %}{% block inner %}out{% endblock %}", {}, '12out3_'), |
|
980 |
|
981 # {% load %} tag (parent -- setup for exception04) |
|
982 'inheritance17': ("{% load testtags %}{% block first %}1234{% endblock %}", {}, '1234'), |
|
983 |
|
984 # {% load %} tag (standard usage, without inheritance) |
|
985 'inheritance18': ("{% load testtags %}{% echo this that theother %}5678", {}, 'this that theother5678'), |
|
986 |
|
987 # {% load %} tag (within a child template) |
|
988 'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'), |
|
989 |
|
990 # Two-level inheritance with {{ block.super }} |
|
991 'inheritance20': ("{% extends 'inheritance01' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1&a3_'), |
|
992 |
|
993 # Three-level inheritance with {{ block.super }} from parent |
|
994 'inheritance21': ("{% extends 'inheritance02' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '12a34'), |
|
995 |
|
996 # Three-level inheritance with {{ block.super }} from grandparent |
|
997 'inheritance22': ("{% extends 'inheritance04' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1&a3_'), |
|
998 |
|
999 # Three-level inheritance with {{ block.super }} from parent and grandparent |
|
1000 'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1&ab3_'), |
|
1001 |
|
1002 # Inheritance from local context without use of template loader |
|
1003 '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'), |
|
1004 |
|
1005 # Inheritance from local context with variable parent template |
|
1006 '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'), |
|
1007 |
|
1008 # Set up a base template to extend |
|
1009 'inheritance26': ("no tags", {}, 'no tags'), |
|
1010 |
|
1011 # Inheritance from a template that doesn't have any blocks |
|
1012 'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'), |
|
1013 |
|
1014 # Set up a base template with a space in it. |
|
1015 'inheritance 28': ("{% block first %}!{% endblock %}", {}, '!'), |
|
1016 |
|
1017 # Inheritance from a template with a space in its name should work. |
|
1018 'inheritance29': ("{% extends 'inheritance 28' %}", {}, '!'), |
|
1019 |
|
1020 # Base template, putting block in a conditional {% if %} tag |
|
1021 'inheritance30': ("1{% if optional %}{% block opt %}2{% endblock %}{% endif %}3", {'optional': True}, '123'), |
|
1022 |
|
1023 # Inherit from a template with block wrapped in an {% if %} tag (in parent), still gets overridden |
|
1024 'inheritance31': ("{% extends 'inheritance30' %}{% block opt %}two{% endblock %}", {'optional': True}, '1two3'), |
|
1025 'inheritance32': ("{% extends 'inheritance30' %}{% block opt %}two{% endblock %}", {}, '13'), |
|
1026 |
|
1027 # Base template, putting block in a conditional {% ifequal %} tag |
|
1028 'inheritance33': ("1{% ifequal optional 1 %}{% block opt %}2{% endblock %}{% endifequal %}3", {'optional': 1}, '123'), |
|
1029 |
|
1030 # Inherit from a template with block wrapped in an {% ifequal %} tag (in parent), still gets overridden |
|
1031 'inheritance34': ("{% extends 'inheritance33' %}{% block opt %}two{% endblock %}", {'optional': 1}, '1two3'), |
|
1032 'inheritance35': ("{% extends 'inheritance33' %}{% block opt %}two{% endblock %}", {'optional': 2}, '13'), |
|
1033 |
|
1034 # Base template, putting block in a {% for %} tag |
|
1035 'inheritance36': ("{% for n in numbers %}_{% block opt %}{{ n }}{% endblock %}{% endfor %}_", {'numbers': '123'}, '_1_2_3_'), |
|
1036 |
|
1037 # Inherit from a template with block wrapped in an {% for %} tag (in parent), still gets overridden |
|
1038 'inheritance37': ("{% extends 'inheritance36' %}{% block opt %}X{% endblock %}", {'numbers': '123'}, '_X_X_X_'), |
|
1039 'inheritance38': ("{% extends 'inheritance36' %}{% block opt %}X{% endblock %}", {}, '_'), |
|
1040 |
|
1041 # The super block will still be found. |
|
1042 'inheritance39': ("{% extends 'inheritance30' %}{% block opt %}new{{ block.super }}{% endblock %}", {'optional': True}, '1new23'), |
|
1043 'inheritance40': ("{% extends 'inheritance33' %}{% block opt %}new{{ block.super }}{% endblock %}", {'optional': 1}, '1new23'), |
|
1044 'inheritance41': ("{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}", {'numbers': '123'}, '_new1_new2_new3_'), |
|
1045 |
|
1046 ### I18N ################################################################## |
|
1047 |
|
1048 # {% spaceless %} tag |
|
1049 'spaceless01': ("{% spaceless %} <b> <i> text </i> </b> {% endspaceless %}", {}, "<b><i> text </i></b>"), |
|
1050 'spaceless02': ("{% spaceless %} <b> \n <i> text </i> \n </b> {% endspaceless %}", {}, "<b><i> text </i></b>"), |
|
1051 'spaceless03': ("{% spaceless %}<b><i>text</i></b>{% endspaceless %}", {}, "<b><i>text</i></b>"), |
|
1052 |
|
1053 # simple translation of a string delimited by ' |
|
1054 'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"), |
|
1055 |
|
1056 # simple translation of a string delimited by " |
|
1057 'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"), |
|
1058 |
|
1059 # simple translation of a variable |
|
1060 'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u"Å"), |
|
1061 |
|
1062 # simple translation of a variable and filter |
|
1063 'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u'å'), |
|
1064 |
|
1065 # simple translation of a string with interpolation |
|
1066 'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"), |
|
1067 |
|
1068 # simple translation of a string to german |
|
1069 'i18n06': ('{% load i18n %}{% trans "Page not found" %}', {'LANGUAGE_CODE': 'de'}, "Seite nicht gefunden"), |
|
1070 |
|
1071 # translation of singular form |
|
1072 'i18n07': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 1}, "singular"), |
|
1073 |
|
1074 # translation of plural form |
|
1075 'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"), |
|
1076 |
|
1077 # simple non-translation (only marking) of a string to german |
|
1078 'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), |
|
1079 |
|
1080 # translation of a variable with a translated filter |
|
1081 'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'), |
|
1082 |
|
1083 # translation of a variable with a non-translated filter |
|
1084 'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), |
|
1085 |
|
1086 # usage of the get_available_languages tag |
|
1087 'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), |
|
1088 |
|
1089 # translation of constant strings |
|
1090 'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'), |
|
1091 'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), |
|
1092 'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), |
|
1093 'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), |
|
1094 |
|
1095 # Escaping inside blocktrans and trans works as if it was directly in the |
|
1096 # template. |
|
1097 'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), |
|
1098 'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), |
|
1099 'i18n19': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': 'a & b'}, u'a & b'), |
|
1100 'i18n20': ('{% load i18n %}{% trans andrew %}', {'andrew': 'a & b'}, u'a & b'), |
|
1101 'i18n21': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': mark_safe('a & b')}, u'a & b'), |
|
1102 'i18n22': ('{% load i18n %}{% trans andrew %}', {'andrew': mark_safe('a & b')}, u'a & b'), |
|
1103 |
|
1104 # Use filters with the {% trans %} tag, #5972 |
|
1105 'i18n23': ('{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}', {'LANGUAGE_CODE': 'de'}, u'nicht gefunden'), |
|
1106 'i18n24': ("{% load i18n %}{% trans 'Page not found'|upper %}", {'LANGUAGE_CODE': 'de'}, u'SEITE NICHT GEFUNDEN'), |
|
1107 'i18n25': ('{% load i18n %}{% trans somevar|upper %}', {'somevar': 'Page not found', 'LANGUAGE_CODE': 'de'}, u'SEITE NICHT GEFUNDEN'), |
|
1108 |
|
1109 # translation of plural form with extra field in singular form (#13568) |
|
1110 'i18n26': ('{% load i18n %}{% blocktrans with myextra_field as extra_field count number as counter %}singular {{ extra_field }}{% plural %}plural{% endblocktrans %}', {'number': 1, 'myextra_field': 'test'}, "singular test"), |
|
1111 |
|
1112 # translation of singular form in russian (#14126) |
|
1113 'i18n27': ('{% load i18n %}{% blocktrans count number as counter %}1 result{% plural %}{{ counter }} results{% endblocktrans %}', {'number': 1, 'LANGUAGE_CODE': 'ru'}, u'1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442'), |
|
1114 |
|
1115 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### |
|
1116 |
|
1117 'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')), |
|
1118 'invalidstr02': ('{{ var|default_if_none:"Foo" }}', {}, ('','INVALID')), |
|
1119 'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''), |
|
1120 'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'), |
|
1121 'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'), |
|
1122 'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')), |
|
1123 'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')), |
|
1124 |
|
1125 ### MULTILINE ############################################################# |
|
1126 |
|
1127 'multiline01': (""" |
|
1128 Hello, |
|
1129 boys. |
|
1130 How |
|
1131 are |
|
1132 you |
|
1133 gentlemen. |
|
1134 """, |
|
1135 {}, |
|
1136 """ |
|
1137 Hello, |
|
1138 boys. |
|
1139 How |
|
1140 are |
|
1141 you |
|
1142 gentlemen. |
|
1143 """), |
|
1144 |
|
1145 ### REGROUP TAG ########################################################### |
|
1146 'regroup01': ('{% regroup data by bar as grouped %}' + \ |
|
1147 '{% for group in grouped %}' + \ |
|
1148 '{{ group.grouper }}:' + \ |
|
1149 '{% for item in group.list %}' + \ |
|
1150 '{{ item.foo }}' + \ |
|
1151 '{% endfor %},' + \ |
|
1152 '{% endfor %}', |
|
1153 {'data': [ {'foo':'c', 'bar':1}, |
|
1154 {'foo':'d', 'bar':1}, |
|
1155 {'foo':'a', 'bar':2}, |
|
1156 {'foo':'b', 'bar':2}, |
|
1157 {'foo':'x', 'bar':3} ]}, |
|
1158 '1:cd,2:ab,3:x,'), |
|
1159 |
|
1160 # Test for silent failure when target variable isn't found |
|
1161 'regroup02': ('{% regroup data by bar as grouped %}' + \ |
|
1162 '{% for group in grouped %}' + \ |
|
1163 '{{ group.grouper }}:' + \ |
|
1164 '{% for item in group.list %}' + \ |
|
1165 '{{ item.foo }}' + \ |
|
1166 '{% endfor %},' + \ |
|
1167 '{% endfor %}', |
|
1168 {}, ''), |
|
1169 |
|
1170 ### TEMPLATETAG TAG ####################################################### |
|
1171 'templatetag01': ('{% templatetag openblock %}', {}, '{%'), |
|
1172 'templatetag02': ('{% templatetag closeblock %}', {}, '%}'), |
|
1173 'templatetag03': ('{% templatetag openvariable %}', {}, '{{'), |
|
1174 'templatetag04': ('{% templatetag closevariable %}', {}, '}}'), |
|
1175 'templatetag05': ('{% templatetag %}', {}, template.TemplateSyntaxError), |
|
1176 'templatetag06': ('{% templatetag foo %}', {}, template.TemplateSyntaxError), |
|
1177 'templatetag07': ('{% templatetag openbrace %}', {}, '{'), |
|
1178 'templatetag08': ('{% templatetag closebrace %}', {}, '}'), |
|
1179 'templatetag09': ('{% templatetag openbrace %}{% templatetag openbrace %}', {}, '{{'), |
|
1180 'templatetag10': ('{% templatetag closebrace %}{% templatetag closebrace %}', {}, '}}'), |
|
1181 'templatetag11': ('{% templatetag opencomment %}', {}, '{#'), |
|
1182 'templatetag12': ('{% templatetag closecomment %}', {}, '#}'), |
|
1183 |
|
1184 ### WIDTHRATIO TAG ######################################################## |
|
1185 'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'), |
|
1186 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''), |
|
1187 'widthratio03': ('{% widthratio a b 100 %}', {'a':0,'b':100}, '0'), |
|
1188 'widthratio04': ('{% widthratio a b 100 %}', {'a':50,'b':100}, '50'), |
|
1189 'widthratio05': ('{% widthratio a b 100 %}', {'a':100,'b':100}, '100'), |
|
1190 |
|
1191 # 62.5 should round to 63 |
|
1192 'widthratio06': ('{% widthratio a b 100 %}', {'a':50,'b':80}, '63'), |
|
1193 |
|
1194 # 71.4 should round to 71 |
|
1195 'widthratio07': ('{% widthratio a b 100 %}', {'a':50,'b':70}, '71'), |
|
1196 |
|
1197 # Raise exception if we don't have 3 args, last one an integer |
|
1198 'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError), |
|
1199 'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError), |
|
1200 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, '50'), |
|
1201 |
|
1202 # #10043: widthratio should allow max_width to be a variable |
|
1203 'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'), |
|
1204 |
|
1205 ### WITH TAG ######################################################## |
|
1206 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), |
|
1207 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), |
|
1208 |
|
1209 'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), |
|
1210 'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), |
|
1211 |
|
1212 ### NOW TAG ######################################################## |
|
1213 # Simple case |
|
1214 'now01': ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)), |
|
1215 |
|
1216 # Check parsing of escaped and special characters |
|
1217 'now02': ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError), |
|
1218 # 'now03': ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), |
|
1219 # 'now04': ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year)) |
|
1220 |
|
1221 ### URL TAG ######################################################## |
|
1222 # Successes |
|
1223 'legacyurl02': ('{% url regressiontests.templates.views.client_action id=client.id,action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1224 'legacyurl02a': ('{% url regressiontests.templates.views.client_action client.id,"update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1225 'legacyurl02b': ("{% url regressiontests.templates.views.client_action id=client.id,action='update' %}", {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1226 'legacyurl02c': ("{% url regressiontests.templates.views.client_action client.id,'update' %}", {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1227 'legacyurl10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'), |
|
1228 'legacyurl13': ('{% url regressiontests.templates.views.client_action id=client.id, action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'), |
|
1229 'legacyurl14': ('{% url regressiontests.templates.views.client_action client.id, arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'), |
|
1230 'legacyurl16': ('{% url regressiontests.templates.views.client_action action="update",id="1" %}', {}, '/url_tag/client/1/update/'), |
|
1231 'legacyurl16a': ("{% url regressiontests.templates.views.client_action action='update',id='1' %}", {}, '/url_tag/client/1/update/'), |
|
1232 'legacyurl17': ('{% url regressiontests.templates.views.client_action client_id=client.my_id,action=action %}', {'client': {'my_id': 1}, 'action': 'update'}, '/url_tag/client/1/update/'), |
|
1233 |
|
1234 'url01': ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'), |
|
1235 'url02': ('{% url regressiontests.templates.views.client_action id=client.id action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1236 'url02a': ('{% url regressiontests.templates.views.client_action client.id "update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1237 'url02b': ("{% url regressiontests.templates.views.client_action id=client.id action='update' %}", {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1238 'url02c': ("{% url regressiontests.templates.views.client_action client.id 'update' %}", {'client': {'id': 1}}, '/url_tag/client/1/update/'), |
|
1239 'url03': ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'), |
|
1240 'url04': ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'), |
|
1241 'url05': (u'{% url метка_оператора v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), |
|
1242 'url06': (u'{% url метка_оператора_2 tag=v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), |
|
1243 '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/'), |
|
1244 'url08': (u'{% url метка_оператора v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), |
|
1245 'url09': (u'{% url метка_оператора_2 tag=v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), |
|
1246 'url10': ('{% url regressiontests.templates.views.client_action id=client.id action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'), |
|
1247 'url11': ('{% url regressiontests.templates.views.client_action id=client.id action="==" %}', {'client': {'id': 1}}, '/url_tag/client/1/==/'), |
|
1248 'url12': ('{% url regressiontests.templates.views.client_action id=client.id action="," %}', {'client': {'id': 1}}, '/url_tag/client/1/,/'), |
|
1249 'url13': ('{% url regressiontests.templates.views.client_action id=client.id action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'), |
|
1250 'url14': ('{% url regressiontests.templates.views.client_action client.id arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'), |
|
1251 'url15': ('{% url regressiontests.templates.views.client_action 12 "test" %}', {}, '/url_tag/client/12/test/'), |
|
1252 'url18': ('{% url regressiontests.templates.views.client "1,2" %}', {}, '/url_tag/client/1,2/'), |
|
1253 |
|
1254 # Failures |
|
1255 'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError), |
|
1256 'url-fail02': ('{% url no_such_view %}', {}, urlresolvers.NoReverseMatch), |
|
1257 'url-fail03': ('{% url regressiontests.templates.views.client %}', {}, urlresolvers.NoReverseMatch), |
|
1258 'url-fail04': ('{% url view id, %}', {}, template.TemplateSyntaxError), |
|
1259 'url-fail05': ('{% url view id= %}', {}, template.TemplateSyntaxError), |
|
1260 'url-fail06': ('{% url view a.id=id %}', {}, template.TemplateSyntaxError), |
|
1261 'url-fail07': ('{% url view a.id!id %}', {}, template.TemplateSyntaxError), |
|
1262 'url-fail08': ('{% url view id="unterminatedstring %}', {}, template.TemplateSyntaxError), |
|
1263 'url-fail09': ('{% url view id=", %}', {}, template.TemplateSyntaxError), |
|
1264 |
|
1265 # {% url ... as var %} |
|
1266 'url-asvar01': ('{% url regressiontests.templates.views.index as url %}', {}, ''), |
|
1267 'url-asvar02': ('{% url regressiontests.templates.views.index as url %}{{ url }}', {}, '/url_tag/'), |
|
1268 'url-asvar03': ('{% url no_such_view as url %}{{ url }}', {}, ''), |
|
1269 |
|
1270 ### CACHE TAG ###################################################### |
|
1271 'cache01': ('{% load cache %}{% cache -1 test %}cache01{% endcache %}', {}, 'cache01'), |
|
1272 'cache02': ('{% load cache %}{% cache -1 test %}cache02{% endcache %}', {}, 'cache02'), |
|
1273 'cache03': ('{% load cache %}{% cache 2 test %}cache03{% endcache %}', {}, 'cache03'), |
|
1274 'cache04': ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), |
|
1275 'cache05': ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), |
|
1276 'cache06': ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), |
|
1277 'cache07': ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'), |
|
1278 |
|
1279 # Allow first argument to be a variable. |
|
1280 'cache08': ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'), |
|
1281 'cache09': ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'), |
|
1282 'cache10': ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'), |
|
1283 |
|
1284 # Raise exception if we don't have at least 2 args, first one integer. |
|
1285 'cache11': ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), |
|
1286 'cache12': ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), |
|
1287 'cache13': ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), |
|
1288 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), |
|
1289 'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), |
|
1290 |
|
1291 # Regression test for #7460. |
|
1292 'cache16': ('{% load cache %}{% cache 1 foo bar %}{% endcache %}', {'foo': 'foo', 'bar': 'with spaces'}, ''), |
|
1293 |
|
1294 # Regression test for #11270. |
|
1295 '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'), |
|
1296 |
|
1297 |
|
1298 ### AUTOESCAPE TAG ############################################## |
|
1299 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"), |
|
1300 'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"), |
|
1301 'autoescape-tag03': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"), |
|
1302 |
|
1303 # Autoescape disabling and enabling nest in a predictable way. |
|
1304 'autoescape-tag04': ("{% autoescape off %}{{ first }} {% autoescape on%}{{ first }}{% endautoescape %}{% endautoescape %}", {"first": "<a>"}, "<a> <a>"), |
|
1305 |
|
1306 'autoescape-tag05': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": "<b>first</b>"}, "<b>first</b>"), |
|
1307 |
|
1308 # Strings (ASCII or unicode) already marked as "safe" are not |
|
1309 # auto-escaped |
|
1310 'autoescape-tag06': ("{{ first }}", {"first": mark_safe("<b>first</b>")}, "<b>first</b>"), |
|
1311 'autoescape-tag07': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": mark_safe(u"<b>Apple</b>")}, u"<b>Apple</b>"), |
|
1312 |
|
1313 # Literal string arguments to filters, if used in the result, are |
|
1314 # safe. |
|
1315 'autoescape-tag08': (r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}', {"var": None}, ' endquote" hah'), |
|
1316 |
|
1317 # Objects which return safe strings as their __unicode__ method |
|
1318 # won't get double-escaped. |
|
1319 'autoescape-tag09': (r'{{ unsafe }}', {'unsafe': filters.UnsafeClass()}, 'you & me'), |
|
1320 'autoescape-tag10': (r'{{ safe }}', {'safe': filters.SafeClass()}, 'you > me'), |
|
1321 |
|
1322 # The "safe" and "escape" filters cannot work due to internal |
|
1323 # implementation details (fortunately, the (no)autoescape block |
|
1324 # tags can be used in those cases) |
|
1325 'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError), |
|
1326 |
|
1327 # ifqeual compares unescaped vales. |
|
1328 'autoescape-ifequal01': ('{% ifequal var "this & that" %}yes{% endifequal %}', { "var": "this & that" }, "yes" ), |
|
1329 |
|
1330 # Arguments to filters are 'safe' and manipulate their input unescaped. |
|
1331 'autoescape-filters01': ('{{ var|cut:"&" }}', { "var": "this & that" }, "this that" ), |
|
1332 'autoescape-filters02': ('{{ var|join:" & \" }}', { "var": ("Tom", "Dick", "Harry") }, "Tom & Dick & Harry" ), |
|
1333 |
|
1334 # Literal strings are safe. |
|
1335 'autoescape-literals01': ('{{ "this & that" }}',{}, "this & that" ), |
|
1336 |
|
1337 # Iterating over strings outputs safe characters. |
|
1338 'autoescape-stringiterations01': ('{% for l in var %}{{ l }},{% endfor %}', {'var': 'K&R'}, "K,&,R," ), |
|
1339 |
|
1340 # Escape requirement survives lookup. |
|
1341 'autoescape-lookup01': ('{{ var.key }}', { "var": {"key": "this & that" }}, "this & that" ), |
|
1342 |
|
1343 } |
|
1344 |
|
1345 |
|
1346 class TemplateTagLoading(unittest.TestCase): |
|
1347 |
|
1348 def setUp(self): |
|
1349 self.old_path = sys.path[:] |
|
1350 self.old_apps = settings.INSTALLED_APPS |
|
1351 self.egg_dir = '%s/eggs' % os.path.dirname(__file__) |
|
1352 self.old_tag_modules = template.templatetags_modules |
|
1353 template.templatetags_modules = [] |
|
1354 |
|
1355 def tearDown(self): |
|
1356 settings.INSTALLED_APPS = self.old_apps |
|
1357 sys.path = self.old_path |
|
1358 template.templatetags_modules = self.old_tag_modules |
|
1359 |
|
1360 def test_load_error(self): |
|
1361 ttext = "{% load broken_tag %}" |
|
1362 self.assertRaises(template.TemplateSyntaxError, template.Template, ttext) |
|
1363 try: |
|
1364 template.Template(ttext) |
|
1365 except template.TemplateSyntaxError, e: |
|
1366 self.assertTrue('ImportError' in e.args[0]) |
|
1367 self.assertTrue('Xtemplate' in e.args[0]) |
|
1368 |
|
1369 def test_load_error_egg(self): |
|
1370 ttext = "{% load broken_egg %}" |
|
1371 egg_name = '%s/tagsegg.egg' % self.egg_dir |
|
1372 sys.path.append(egg_name) |
|
1373 settings.INSTALLED_APPS = ('tagsegg',) |
|
1374 self.assertRaises(template.TemplateSyntaxError, template.Template, ttext) |
|
1375 try: |
|
1376 template.Template(ttext) |
|
1377 except template.TemplateSyntaxError, e: |
|
1378 self.assertTrue('ImportError' in e.args[0]) |
|
1379 self.assertTrue('Xtemplate' in e.args[0]) |
|
1380 |
|
1381 def test_load_working_egg(self): |
|
1382 ttext = "{% load working_egg %}" |
|
1383 egg_name = '%s/tagsegg.egg' % self.egg_dir |
|
1384 sys.path.append(egg_name) |
|
1385 settings.INSTALLED_APPS = ('tagsegg',) |
|
1386 t = template.Template(ttext) |
|
1387 |
|
1388 if __name__ == "__main__": |
|
1389 unittest.main() |