App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / regressiontests / templates / custom.py
blob6bbbe35f03d9cdabd4395bd9fbb40a81877cff99
1 from __future__ import absolute_import
3 from django import template
4 from django.utils.unittest import TestCase
6 from .templatetags import custom
9 class CustomFilterTests(TestCase):
10 def test_filter(self):
11 t = template.Template("{% load custom %}{{ string|trim:5 }}")
12 self.assertEqual(
13 t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
14 u"abcde"
18 class CustomTagTests(TestCase):
19 def verify_tag(self, tag, name):
20 self.assertEqual(tag.__name__, name)
21 self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
22 self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
24 def test_simple_tags(self):
25 c = template.Context({'value': 42})
27 t = template.Template('{% load custom %}{% no_params %}')
28 self.assertEqual(t.render(c), u'no_params - Expected result')
30 t = template.Template('{% load custom %}{% one_param 37 %}')
31 self.assertEqual(t.render(c), u'one_param - Expected result: 37')
33 t = template.Template('{% load custom %}{% explicit_no_context 37 %}')
34 self.assertEqual(t.render(c), u'explicit_no_context - Expected result: 37')
36 t = template.Template('{% load custom %}{% no_params_with_context %}')
37 self.assertEqual(t.render(c), u'no_params_with_context - Expected result (context value: 42)')
39 t = template.Template('{% load custom %}{% params_and_context 37 %}')
40 self.assertEqual(t.render(c), u'params_and_context - Expected result (context value: 42): 37')
42 t = template.Template('{% load custom %}{% simple_two_params 37 42 %}')
43 self.assertEqual(t.render(c), u'simple_two_params - Expected result: 37, 42')
45 t = template.Template('{% load custom %}{% simple_one_default 37 %}')
46 self.assertEqual(t.render(c), u'simple_one_default - Expected result: 37, hi')
48 t = template.Template('{% load custom %}{% simple_one_default 37 two="hello" %}')
49 self.assertEqual(t.render(c), u'simple_one_default - Expected result: 37, hello')
51 t = template.Template('{% load custom %}{% simple_one_default one=99 two="hello" %}')
52 self.assertEqual(t.render(c), u'simple_one_default - Expected result: 99, hello')
54 self.assertRaisesRegexp(template.TemplateSyntaxError,
55 "'simple_one_default' received unexpected keyword argument 'three'",
56 template.Template, '{% load custom %}{% simple_one_default 99 two="hello" three="foo" %}')
58 t = template.Template('{% load custom %}{% simple_one_default 37 42 %}')
59 self.assertEqual(t.render(c), u'simple_one_default - Expected result: 37, 42')
61 t = template.Template('{% load custom %}{% simple_unlimited_args 37 %}')
62 self.assertEqual(t.render(c), u'simple_unlimited_args - Expected result: 37, hi')
64 t = template.Template('{% load custom %}{% simple_unlimited_args 37 42 56 89 %}')
65 self.assertEqual(t.render(c), u'simple_unlimited_args - Expected result: 37, 42, 56, 89')
67 t = template.Template('{% load custom %}{% simple_only_unlimited_args %}')
68 self.assertEqual(t.render(c), u'simple_only_unlimited_args - Expected result: ')
70 t = template.Template('{% load custom %}{% simple_only_unlimited_args 37 42 56 89 %}')
71 self.assertEqual(t.render(c), u'simple_only_unlimited_args - Expected result: 37, 42, 56, 89')
73 self.assertRaisesRegexp(template.TemplateSyntaxError,
74 "'simple_two_params' received too many positional arguments",
75 template.Template, '{% load custom %}{% simple_two_params 37 42 56 %}')
77 self.assertRaisesRegexp(template.TemplateSyntaxError,
78 "'simple_one_default' received too many positional arguments",
79 template.Template, '{% load custom %}{% simple_one_default 37 42 56 %}')
81 t = template.Template('{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}')
82 self.assertEqual(t.render(c), u'simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4')
84 self.assertRaisesRegexp(template.TemplateSyntaxError,
85 "'simple_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)",
86 template.Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}')
88 self.assertRaisesRegexp(template.TemplateSyntaxError,
89 "'simple_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
90 template.Template, '{% load custom %}{% simple_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}')
92 def test_simple_tag_registration(self):
93 # Test that the decorators preserve the decorated function's docstring, name and attributes.
94 self.verify_tag(custom.no_params, 'no_params')
95 self.verify_tag(custom.one_param, 'one_param')
96 self.verify_tag(custom.explicit_no_context, 'explicit_no_context')
97 self.verify_tag(custom.no_params_with_context, 'no_params_with_context')
98 self.verify_tag(custom.params_and_context, 'params_and_context')
99 self.verify_tag(custom.simple_unlimited_args_kwargs, 'simple_unlimited_args_kwargs')
100 self.verify_tag(custom.simple_tag_without_context_parameter, 'simple_tag_without_context_parameter')
102 def test_simple_tag_missing_context(self):
103 # The 'context' parameter must be present when takes_context is True
104 self.assertRaisesRegexp(template.TemplateSyntaxError,
105 "'simple_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
106 template.Template, '{% load custom %}{% simple_tag_without_context_parameter 123 %}')
108 def test_inclusion_tags(self):
109 c = template.Context({'value': 42})
111 t = template.Template('{% load custom %}{% inclusion_no_params %}')
112 self.assertEqual(t.render(c), u'inclusion_no_params - Expected result\n')
114 t = template.Template('{% load custom %}{% inclusion_one_param 37 %}')
115 self.assertEqual(t.render(c), u'inclusion_one_param - Expected result: 37\n')
117 t = template.Template('{% load custom %}{% inclusion_explicit_no_context 37 %}')
118 self.assertEqual(t.render(c), u'inclusion_explicit_no_context - Expected result: 37\n')
120 t = template.Template('{% load custom %}{% inclusion_no_params_with_context %}')
121 self.assertEqual(t.render(c), u'inclusion_no_params_with_context - Expected result (context value: 42)\n')
123 t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
124 self.assertEqual(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
126 t = template.Template('{% load custom %}{% inclusion_two_params 37 42 %}')
127 self.assertEqual(t.render(c), u'inclusion_two_params - Expected result: 37, 42\n')
129 t = template.Template('{% load custom %}{% inclusion_one_default 37 %}')
130 self.assertEqual(t.render(c), u'inclusion_one_default - Expected result: 37, hi\n')
132 t = template.Template('{% load custom %}{% inclusion_one_default 37 two="hello" %}')
133 self.assertEqual(t.render(c), u'inclusion_one_default - Expected result: 37, hello\n')
135 t = template.Template('{% load custom %}{% inclusion_one_default one=99 two="hello" %}')
136 self.assertEqual(t.render(c), u'inclusion_one_default - Expected result: 99, hello\n')
138 self.assertRaisesRegexp(template.TemplateSyntaxError,
139 "'inclusion_one_default' received unexpected keyword argument 'three'",
140 template.Template, '{% load custom %}{% inclusion_one_default 99 two="hello" three="foo" %}')
142 t = template.Template('{% load custom %}{% inclusion_one_default 37 42 %}')
143 self.assertEqual(t.render(c), u'inclusion_one_default - Expected result: 37, 42\n')
145 t = template.Template('{% load custom %}{% inclusion_unlimited_args 37 %}')
146 self.assertEqual(t.render(c), u'inclusion_unlimited_args - Expected result: 37, hi\n')
148 t = template.Template('{% load custom %}{% inclusion_unlimited_args 37 42 56 89 %}')
149 self.assertEqual(t.render(c), u'inclusion_unlimited_args - Expected result: 37, 42, 56, 89\n')
151 t = template.Template('{% load custom %}{% inclusion_only_unlimited_args %}')
152 self.assertEqual(t.render(c), u'inclusion_only_unlimited_args - Expected result: \n')
154 t = template.Template('{% load custom %}{% inclusion_only_unlimited_args 37 42 56 89 %}')
155 self.assertEqual(t.render(c), u'inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89\n')
157 self.assertRaisesRegexp(template.TemplateSyntaxError,
158 "'inclusion_two_params' received too many positional arguments",
159 template.Template, '{% load custom %}{% inclusion_two_params 37 42 56 %}')
161 self.assertRaisesRegexp(template.TemplateSyntaxError,
162 "'inclusion_one_default' received too many positional arguments",
163 template.Template, '{% load custom %}{% inclusion_one_default 37 42 56 %}')
165 self.assertRaisesRegexp(template.TemplateSyntaxError,
166 "'inclusion_one_default' did not receive value\(s\) for the argument\(s\): 'one'",
167 template.Template, '{% load custom %}{% inclusion_one_default %}')
169 self.assertRaisesRegexp(template.TemplateSyntaxError,
170 "'inclusion_unlimited_args' did not receive value\(s\) for the argument\(s\): 'one'",
171 template.Template, '{% load custom %}{% inclusion_unlimited_args %}')
173 t = template.Template('{% load custom %}{% inclusion_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 %}')
174 self.assertEqual(t.render(c), u'inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4\n')
176 self.assertRaisesRegexp(template.TemplateSyntaxError,
177 "'inclusion_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)",
178 template.Template, '{% load custom %}{% inclusion_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 %}')
180 self.assertRaisesRegexp(template.TemplateSyntaxError,
181 "'inclusion_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
182 template.Template, '{% load custom %}{% inclusion_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}')
184 def test_include_tag_missing_context(self):
185 # The 'context' parameter must be present when takes_context is True
186 self.assertRaisesRegexp(template.TemplateSyntaxError,
187 "'inclusion_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
188 template.Template, '{% load custom %}{% inclusion_tag_without_context_parameter 123 %}')
190 def test_inclusion_tags_from_template(self):
191 c = template.Context({'value': 42})
193 t = template.Template('{% load custom %}{% inclusion_no_params_from_template %}')
194 self.assertEqual(t.render(c), u'inclusion_no_params_from_template - Expected result\n')
196 t = template.Template('{% load custom %}{% inclusion_one_param_from_template 37 %}')
197 self.assertEqual(t.render(c), u'inclusion_one_param_from_template - Expected result: 37\n')
199 t = template.Template('{% load custom %}{% inclusion_explicit_no_context_from_template 37 %}')
200 self.assertEqual(t.render(c), u'inclusion_explicit_no_context_from_template - Expected result: 37\n')
202 t = template.Template('{% load custom %}{% inclusion_no_params_with_context_from_template %}')
203 self.assertEqual(t.render(c), u'inclusion_no_params_with_context_from_template - Expected result (context value: 42)\n')
205 t = template.Template('{% load custom %}{% inclusion_params_and_context_from_template 37 %}')
206 self.assertEqual(t.render(c), u'inclusion_params_and_context_from_template - Expected result (context value: 42): 37\n')
208 t = template.Template('{% load custom %}{% inclusion_two_params_from_template 37 42 %}')
209 self.assertEqual(t.render(c), u'inclusion_two_params_from_template - Expected result: 37, 42\n')
211 t = template.Template('{% load custom %}{% inclusion_one_default_from_template 37 %}')
212 self.assertEqual(t.render(c), u'inclusion_one_default_from_template - Expected result: 37, hi\n')
214 t = template.Template('{% load custom %}{% inclusion_one_default_from_template 37 42 %}')
215 self.assertEqual(t.render(c), u'inclusion_one_default_from_template - Expected result: 37, 42\n')
217 t = template.Template('{% load custom %}{% inclusion_unlimited_args_from_template 37 %}')
218 self.assertEqual(t.render(c), u'inclusion_unlimited_args_from_template - Expected result: 37, hi\n')
220 t = template.Template('{% load custom %}{% inclusion_unlimited_args_from_template 37 42 56 89 %}')
221 self.assertEqual(t.render(c), u'inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n')
223 t = template.Template('{% load custom %}{% inclusion_only_unlimited_args_from_template %}')
224 self.assertEqual(t.render(c), u'inclusion_only_unlimited_args_from_template - Expected result: \n')
226 t = template.Template('{% load custom %}{% inclusion_only_unlimited_args_from_template 37 42 56 89 %}')
227 self.assertEqual(t.render(c), u'inclusion_only_unlimited_args_from_template - Expected result: 37, 42, 56, 89\n')
229 def test_inclusion_tag_registration(self):
230 # Test that the decorators preserve the decorated function's docstring, name and attributes.
231 self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
232 self.verify_tag(custom.inclusion_one_param, 'inclusion_one_param')
233 self.verify_tag(custom.inclusion_explicit_no_context, 'inclusion_explicit_no_context')
234 self.verify_tag(custom.inclusion_no_params_with_context, 'inclusion_no_params_with_context')
235 self.verify_tag(custom.inclusion_params_and_context, 'inclusion_params_and_context')
236 self.verify_tag(custom.inclusion_two_params, 'inclusion_two_params')
237 self.verify_tag(custom.inclusion_one_default, 'inclusion_one_default')
238 self.verify_tag(custom.inclusion_unlimited_args, 'inclusion_unlimited_args')
239 self.verify_tag(custom.inclusion_only_unlimited_args, 'inclusion_only_unlimited_args')
240 self.verify_tag(custom.inclusion_tag_without_context_parameter, 'inclusion_tag_without_context_parameter')
241 self.verify_tag(custom.inclusion_tag_use_l10n, 'inclusion_tag_use_l10n')
242 self.verify_tag(custom.inclusion_tag_current_app, 'inclusion_tag_current_app')
243 self.verify_tag(custom.inclusion_unlimited_args_kwargs, 'inclusion_unlimited_args_kwargs')
245 def test_15070_current_app(self):
247 Test that inclusion tag passes down `current_app` of context to the
248 Context of the included/rendered template as well.
250 c = template.Context({})
251 t = template.Template('{% load custom %}{% inclusion_tag_current_app %}')
252 self.assertEqual(t.render(c).strip(), u'None')
254 c.current_app = 'advanced'
255 self.assertEqual(t.render(c).strip(), u'advanced')
257 def test_15070_use_l10n(self):
259 Test that inclusion tag passes down `use_l10n` of context to the
260 Context of the included/rendered template as well.
262 c = template.Context({})
263 t = template.Template('{% load custom %}{% inclusion_tag_use_l10n %}')
264 self.assertEqual(t.render(c).strip(), u'None')
266 c.use_l10n = True
267 self.assertEqual(t.render(c).strip(), u'True')
269 def test_assignment_tags(self):
270 c = template.Context({'value': 42})
272 t = template.Template('{% load custom %}{% assignment_no_params as var %}The result is: {{ var }}')
273 self.assertEqual(t.render(c), u'The result is: assignment_no_params - Expected result')
275 t = template.Template('{% load custom %}{% assignment_one_param 37 as var %}The result is: {{ var }}')
276 self.assertEqual(t.render(c), u'The result is: assignment_one_param - Expected result: 37')
278 t = template.Template('{% load custom %}{% assignment_explicit_no_context 37 as var %}The result is: {{ var }}')
279 self.assertEqual(t.render(c), u'The result is: assignment_explicit_no_context - Expected result: 37')
281 t = template.Template('{% load custom %}{% assignment_no_params_with_context as var %}The result is: {{ var }}')
282 self.assertEqual(t.render(c), u'The result is: assignment_no_params_with_context - Expected result (context value: 42)')
284 t = template.Template('{% load custom %}{% assignment_params_and_context 37 as var %}The result is: {{ var }}')
285 self.assertEqual(t.render(c), u'The result is: assignment_params_and_context - Expected result (context value: 42): 37')
287 t = template.Template('{% load custom %}{% assignment_two_params 37 42 as var %}The result is: {{ var }}')
288 self.assertEqual(t.render(c), u'The result is: assignment_two_params - Expected result: 37, 42')
290 t = template.Template('{% load custom %}{% assignment_one_default 37 as var %}The result is: {{ var }}')
291 self.assertEqual(t.render(c), u'The result is: assignment_one_default - Expected result: 37, hi')
293 t = template.Template('{% load custom %}{% assignment_one_default 37 two="hello" as var %}The result is: {{ var }}')
294 self.assertEqual(t.render(c), u'The result is: assignment_one_default - Expected result: 37, hello')
296 t = template.Template('{% load custom %}{% assignment_one_default one=99 two="hello" as var %}The result is: {{ var }}')
297 self.assertEqual(t.render(c), u'The result is: assignment_one_default - Expected result: 99, hello')
299 self.assertRaisesRegexp(template.TemplateSyntaxError,
300 "'assignment_one_default' received unexpected keyword argument 'three'",
301 template.Template, '{% load custom %}{% assignment_one_default 99 two="hello" three="foo" as var %}')
303 t = template.Template('{% load custom %}{% assignment_one_default 37 42 as var %}The result is: {{ var }}')
304 self.assertEqual(t.render(c), u'The result is: assignment_one_default - Expected result: 37, 42')
306 t = template.Template('{% load custom %}{% assignment_unlimited_args 37 as var %}The result is: {{ var }}')
307 self.assertEqual(t.render(c), u'The result is: assignment_unlimited_args - Expected result: 37, hi')
309 t = template.Template('{% load custom %}{% assignment_unlimited_args 37 42 56 89 as var %}The result is: {{ var }}')
310 self.assertEqual(t.render(c), u'The result is: assignment_unlimited_args - Expected result: 37, 42, 56, 89')
312 t = template.Template('{% load custom %}{% assignment_only_unlimited_args as var %}The result is: {{ var }}')
313 self.assertEqual(t.render(c), u'The result is: assignment_only_unlimited_args - Expected result: ')
315 t = template.Template('{% load custom %}{% assignment_only_unlimited_args 37 42 56 89 as var %}The result is: {{ var }}')
316 self.assertEqual(t.render(c), u'The result is: assignment_only_unlimited_args - Expected result: 37, 42, 56, 89')
318 self.assertRaisesRegexp(template.TemplateSyntaxError,
319 "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
320 template.Template, '{% load custom %}{% assignment_one_param 37 %}The result is: {{ var }}')
322 self.assertRaisesRegexp(template.TemplateSyntaxError,
323 "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
324 template.Template, '{% load custom %}{% assignment_one_param 37 as %}The result is: {{ var }}')
326 self.assertRaisesRegexp(template.TemplateSyntaxError,
327 "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
328 template.Template, '{% load custom %}{% assignment_one_param 37 ass var %}The result is: {{ var }}')
330 self.assertRaisesRegexp(template.TemplateSyntaxError,
331 "'assignment_two_params' received too many positional arguments",
332 template.Template, '{% load custom %}{% assignment_two_params 37 42 56 as var %}The result is: {{ var }}')
334 self.assertRaisesRegexp(template.TemplateSyntaxError,
335 "'assignment_one_default' received too many positional arguments",
336 template.Template, '{% load custom %}{% assignment_one_default 37 42 56 as var %}The result is: {{ var }}')
338 self.assertRaisesRegexp(template.TemplateSyntaxError,
339 "'assignment_one_default' did not receive value\(s\) for the argument\(s\): 'one'",
340 template.Template, '{% load custom %}{% assignment_one_default as var %}The result is: {{ var }}')
342 self.assertRaisesRegexp(template.TemplateSyntaxError,
343 "'assignment_unlimited_args' did not receive value\(s\) for the argument\(s\): 'one'",
344 template.Template, '{% load custom %}{% assignment_unlimited_args as var %}The result is: {{ var }}')
346 t = template.Template('{% load custom %}{% assignment_unlimited_args_kwargs 37 40|add:2 56 eggs="scrambled" four=1|add:3 as var %}The result is: {{ var }}')
347 self.assertEqual(t.render(c), u'The result is: assignment_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4')
349 self.assertRaisesRegexp(template.TemplateSyntaxError,
350 "'assignment_unlimited_args_kwargs' received some positional argument\(s\) after some keyword argument\(s\)",
351 template.Template, '{% load custom %}{% assignment_unlimited_args_kwargs 37 40|add:2 eggs="scrambled" 56 four=1|add:3 as var %}The result is: {{ var }}')
353 self.assertRaisesRegexp(template.TemplateSyntaxError,
354 "'assignment_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
355 template.Template, '{% load custom %}{% assignment_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" as var %}The result is: {{ var }}')
357 def test_assignment_tag_registration(self):
358 # Test that the decorators preserve the decorated function's docstring, name and attributes.
359 self.verify_tag(custom.assignment_no_params, 'assignment_no_params')
360 self.verify_tag(custom.assignment_one_param, 'assignment_one_param')
361 self.verify_tag(custom.assignment_explicit_no_context, 'assignment_explicit_no_context')
362 self.verify_tag(custom.assignment_no_params_with_context, 'assignment_no_params_with_context')
363 self.verify_tag(custom.assignment_params_and_context, 'assignment_params_and_context')
364 self.verify_tag(custom.assignment_one_default, 'assignment_one_default')
365 self.verify_tag(custom.assignment_two_params, 'assignment_two_params')
366 self.verify_tag(custom.assignment_unlimited_args, 'assignment_unlimited_args')
367 self.verify_tag(custom.assignment_only_unlimited_args, 'assignment_only_unlimited_args')
368 self.verify_tag(custom.assignment_unlimited_args, 'assignment_unlimited_args')
369 self.verify_tag(custom.assignment_unlimited_args_kwargs, 'assignment_unlimited_args_kwargs')
370 self.verify_tag(custom.assignment_tag_without_context_parameter, 'assignment_tag_without_context_parameter')
372 def test_assignment_tag_missing_context(self):
373 # The 'context' parameter must be present when takes_context is True
374 self.assertRaisesRegexp(template.TemplateSyntaxError,
375 "'assignment_tag_without_context_parameter' is decorated with takes_context=True so it must have a first argument of 'context'",
376 template.Template, '{% load custom %}{% assignment_tag_without_context_parameter 123 as var %}')