Django

Code

root/django/trunk/tests/regressiontests/defaultfilters/tests.py

Revision 8599, 12.7 kB (checked in by mtredinnick, 1 month ago)

Cleaned up some tests I broke as a result of the escapejs changes in [8577].

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2
3 r"""
4 >>> floatformat(7.7)
5 u'7.7'
6 >>> floatformat(7.0)
7 u'7'
8 >>> floatformat(0.7)
9 u'0.7'
10 >>> floatformat(0.07)
11 u'0.1'
12 >>> floatformat(0.007)
13 u'0.0'
14 >>> floatformat(0.0)
15 u'0'
16 >>> floatformat(7.7,3)
17 u'7.700'
18 >>> floatformat(6.000000,3)
19 u'6.000'
20 >>> floatformat(6.200000, 3)
21 u'6.200'
22 >>> floatformat(6.200000, -3)
23 u'6.200'
24 >>> floatformat(13.1031,-3)
25 u'13.103'
26 >>> floatformat(11.1197, -2)
27 u'11.12'
28 >>> floatformat(11.0000, -2)
29 u'11'
30 >>> floatformat(11.000001, -2)
31 u'11.00'
32 >>> floatformat(8.2798, 3)
33 u'8.280'
34 >>> floatformat(u'foo')
35 u''
36 >>> floatformat(13.1031, u'bar')
37 u'13.1031'
38 >>> floatformat(u'foo', u'bar')
39 u''
40 >>> floatformat(None)
41 u''
42
43 >>> addslashes(u'"double quotes" and \'single quotes\'')
44 u'\\"double quotes\\" and \\\'single quotes\\\''
45
46 >>> addslashes(ur'\ : backslashes, too')
47 u'\\\\ : backslashes, too'
48
49 >>> capfirst(u'hello world')
50 u'Hello world'
51
52 >>> escapejs(u'"double quotes" and \'single quotes\'')
53 u'\\x22double quotes\\x22 and \\x27single quotes\\x27'
54
55 >>> escapejs(ur'\ : backslashes, too')
56 u'\\x5C : backslashes, too'
57
58 >>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b')
59 u'and lots of whitespace: \\x0D\\x0A\\x09\\x0B\\x0C\\x08'
60
61 >>> escapejs(ur'<script>and this</script>')
62 u'\\x3Cscript\\x3Eand this\\x3C/script\\x3E'
63
64 >>> fix_ampersands(u'Jack & Jill & Jeroboam')
65 u'Jack &amp; Jill &amp; Jeroboam'
66
67 >>> linenumbers(u'line 1\nline 2')
68 u'1. line 1\n2. line 2'
69
70 >>> linenumbers(u'\n'.join([u'x'] * 10))
71 u'01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. x\n08. x\n09. x\n10. x'
72
73 >>> lower('TEST')
74 u'test'
75
76 >>> lower(u'\xcb') # uppercase E umlaut
77 u'\xeb'
78
79 >>> make_list('abc')
80 [u'a', u'b', u'c']
81
82 >>> make_list(1234)
83 [u'1', u'2', u'3', u'4']
84
85 >>> slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/')
86 u'jack-jill-like-numbers-123-and-4-and-silly-characters'
87
88 >>> slugify(u"Un \xe9l\xe9phant \xe0 l'or\xe9e du bois")
89 u'un-elephant-a-loree-du-bois'
90
91 >>> stringformat(1, u'03d')
92 u'001'
93
94 >>> stringformat(1, u'z')
95 u''
96
97 >>> title('a nice title, isn\'t it?')
98 u"A Nice Title, Isn't It?"
99
100 >>> title(u'discoth\xe8que')
101 u'Discoth\xe8que'
102
103 >>> truncatewords(u'A sentence with a few words in it', 1)
104 u'A ...'
105
106 >>> truncatewords(u'A sentence with a few words in it', 5)
107 u'A sentence with a few ...'
108
109 >>> truncatewords(u'A sentence with a few words in it', 100)
110 u'A sentence with a few words in it'
111
112 >>> truncatewords(u'A sentence with a few words in it', 'not a number')
113 u'A sentence with a few words in it'
114
115 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 0)
116 u''
117
118 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 2)
119 u'<p>one <a href="#">two ...</a></p>'
120
121 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 4)
122 u'<p>one <a href="#">two - three <br>four ...</a></p>'
123
124 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 5)
125 u'<p>one <a href="#">two - three <br>four</a> five</p>'
126
127 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 100)
128 u'<p>one <a href="#">two - three <br>four</a> five</p>'
129
130 >>> truncatewords_html(u'\xc5ngstr\xf6m was here', 1)
131 u'\xc5ngstr\xf6m ...'
132
133 >>> upper(u'Mixed case input')
134 u'MIXED CASE INPUT'
135
136 >>> upper(u'\xeb') # lowercase e umlaut
137 u'\xcb'
138
139
140 >>> urlencode(u'fran\xe7ois & jill')
141 u'fran%C3%A7ois%20%26%20jill'
142 >>> urlencode(1)
143 u'1'
144 >>> iriencode(u'S\xf8r-Tr\xf8ndelag')
145 u'S%C3%B8r-Tr%C3%B8ndelag'
146 >>> iriencode(urlencode(u'fran\xe7ois & jill'))
147 u'fran%C3%A7ois%20%26%20jill'
148
149 >>> urlizetrunc(u'http://short.com/', 20)
150 u'<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
151
152 >>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
153 u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
154
155 >>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
156 u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
157
158 # Check truncating of URIs which are the exact length
159 >>> uri = 'http://31characteruri.com/test/'
160 >>> len(uri)
161 31
162 >>> urlizetrunc(uri, 31)
163 u'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/test/</a>'
164 >>> urlizetrunc(uri, 30)
165 u'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/t...</a>'
166 >>> urlizetrunc(uri, 2)
167 u'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
168
169 # Check normal urlize
170 >>> urlize('http://google.com')
171 u'<a href="http://google.com" rel="nofollow">http://google.com</a>'
172
173 >>> urlize('http://google.com/')
174 u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
175
176 >>> urlize('www.google.com')
177 u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
178
179 >>> urlize('djangoproject.org')
180 u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
181
182 >>> urlize('info@djangoproject.org')
183 u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'
184
185 # Check urlize with https addresses
186 >>> urlize('https://google.com')
187 u'<a href="https://google.com" rel="nofollow">https://google.com</a>'
188
189
190 >>> wordcount('')
191 0
192
193 >>> wordcount(u'oneword')
194 1
195
196 >>> wordcount(u'lots of words')
197 3
198
199 >>> wordwrap(u'this is a long paragraph of text that really needs to be wrapped I\'m afraid', 14)
200 u"this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI'm afraid"
201
202 >>> wordwrap(u'this is a short paragraph of text.\n  But this line should be indented',14)
203 u'this is a\nshort\nparagraph of\ntext.\n  But this\nline should be\nindented'
204
205 >>> wordwrap(u'this is a short paragraph of text.\n  But this line should be indented',15)
206 u'this is a short\nparagraph of\ntext.\n  But this line\nshould be\nindented'
207
208 >>> ljust(u'test', 10)
209 u'test      '
210
211 >>> ljust(u'test', 3)
212 u'test'
213
214 >>> rjust(u'test', 10)
215 u'      test'
216
217 >>> rjust(u'test', 3)
218 u'test'
219
220 >>> center(u'test', 6)
221 u' test '
222
223 >>> cut(u'a string to be mangled', 'a')
224 u' string to be mngled'
225
226 >>> cut(u'a string to be mangled', 'ng')
227 u'a stri to be maled'
228
229 >>> cut(u'a string to be mangled', 'strings')
230 u'a string to be mangled'
231
232 >>> force_escape(u'<some html & special characters > here')
233 u'&lt;some html &amp; special characters &gt; here'
234
235 >>> force_escape(u'<some html & special characters > here ĐÅ€£')
236 u'&lt;some html &amp; special characters &gt; here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3'
237
238 >>> linebreaks(u'line 1')
239 u'<p>line 1</p>'
240
241 >>> linebreaks(u'line 1\nline 2')
242 u'<p>line 1<br />line 2</p>'
243
244 >>> removetags(u'some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img')
245 u'some <b>html</b> with alert("You smell") disallowed  tags'
246
247 >>> striptags(u'some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags')
248 u'some html with alert("You smell") disallowed  tags'
249
250 >>> sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'},
251 ...                          {'age': 63, 'name': 'Ra Ra Rasputin'},
252 ...                          {'name': 'Jonny B Goode', 'age': 18}], 'age')
253 >>> [sorted(dict.items()) for dict in sorted_dicts]
254 [[('age', 18), ('name', 'Jonny B Goode')], [('age', 23), ('name', 'Barbara-Ann')], [('age', 63), ('name', 'Ra Ra Rasputin')]]
255
256 >>> sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
257 ...                                  {'age': 63, 'name': 'Ra Ra Rasputin'},
258 ...                                  {'name': 'Jonny B Goode', 'age': 18}], 'age')
259 >>> [sorted(dict.items()) for dict in sorted_dicts]
260 [[('age', 63), ('name', 'Ra Ra Rasputin')], [('age', 23), ('name', 'Barbara-Ann')], [('age', 18), ('name', 'Jonny B Goode')]]
261
262 >>> first([0,1,2])
263 0
264
265 >>> first(u'')
266 u''
267
268 >>> first(u'test')
269 u't'
270
271 >>> join([0,1,2], u'glue')
272 u'0glue1glue2'
273
274 >>> length(u'1234')
275 4
276
277 >>> length([1,2,3,4])
278 4
279
280 >>> length_is([], 0)
281 True
282
283 >>> length_is([], 1)
284 False
285
286 >>> length_is('a', 1)
287 True
288
289 >>> length_is(u'a', 10)
290 False
291
292 >>> slice_(u'abcdefg', u'0')
293 u''
294
295 >>> slice_(u'abcdefg', u'1')
296 u'a'
297
298 >>> slice_(u'abcdefg', u'-1')
299 u'abcdef'
300
301 >>> slice_(u'abcdefg', u'1:2')
302 u'b'
303
304 >>> slice_(u'abcdefg', u'1:3')
305 u'bc'
306
307 >>> slice_(u'abcdefg', u'0::2')
308 u'aceg'
309
310 >>> unordered_list([u'item 1', u'item 2'])
311 u'\t<li>item 1</li>\n\t<li>item 2</li>'
312
313 >>> unordered_list([u'item 1', [u'item 1.1']])
314 u'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>'
315
316 >>> unordered_list([u'item 1', [u'item 1.1', u'item1.2'], u'item 2'])
317 u'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t\t<li>item1.2</li>\n\t</ul>\n\t</li>\n\t<li>item 2</li>'
318
319 >>> unordered_list([u'item 1', [u'item 1.1', [u'item 1.1.1', [u'item 1.1.1.1']]]])
320 u'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1\n\t\t<ul>\n\t\t\t<li>item 1.1.1\n\t\t\t<ul>\n\t\t\t\t<li>item 1.1.1.1</li>\n\t\t\t</ul>\n\t\t\t</li>\n\t\t</ul>\n\t\t</li>\n\t</ul>\n\t</li>'
321
322 >>> unordered_list(['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']])
323 u'\t<li>States\n\t<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>Lawrence</li>\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>Illinois</li>\n\t</ul>\n\t</li>'
324
325 # Old format for unordered lists should still work
326 >>> unordered_list([u'item 1', []])
327 u'\t<li>item 1</li>'
328
329 >>> unordered_list([u'item 1', [[u'item 1.1', []]]])
330 u'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>'
331
332 >>> unordered_list([u'item 1', [[u'item 1.1', []], [u'item 1.2', []]]])
333 u'\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t\t<li>item 1.2</li>\n\t</ul>\n\t</li>'
334
335 >>> unordered_list(['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]])
336 u'\t<li>States\n\t<ul>\n\t\t<li>Kansas\n\t\t<ul>\n\t\t\t<li>Lawrence</li>\n\t\t\t<li>Topeka</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>Illinois</li>\n\t</ul>\n\t</li>'
337
338 >>> add(u'1', u'2')
339 3
340
341 >>> get_digit(123, 1)
342 3
343
344 >>> get_digit(123, 2)
345 2
346
347 >>> get_digit(123, 3)
348 1
349
350 >>> get_digit(123, 4)
351 0
352
353 >>> get_digit(123, 0)
354 123
355
356 >>> get_digit(u'xyz', 0)
357 u'xyz'
358
359 # real testing of date() is in dateformat.py
360 >>> date(datetime.datetime(2005, 12, 29), u"d F Y")
361 u'29 December 2005'
362 >>> date(datetime.datetime(2005, 12, 29), ur'jS o\f F')
363 u'29th of December'
364
365 # real testing of time() is done in dateformat.py
366 >>> time(datetime.time(13), u"h")
367 u'01'
368
369 >>> time(datetime.time(0), u"h")
370 u'12'
371
372 # real testing is done in timesince.py, where we can provide our own 'now'
373 >>> timesince(datetime.datetime.now() - datetime.timedelta(1))
374 u'1 day'
375
376 >>> timesince(datetime.datetime(2005, 12, 29), datetime.datetime(2005, 12, 30))
377 u'1 day'
378
379 >>> timeuntil(datetime.datetime.now() + datetime.timedelta(1))
380 u'1 day'
381
382 >>> timeuntil(datetime.datetime(2005, 12, 30), datetime.datetime(2005, 12, 29))
383 u'1 day'
384
385 >>> default(u"val", u"default")
386 u'val'
387
388 >>> default(None, u"default")
389 u'default'
390
391 >>> default(u'', u"default")
392 u'default'
393
394 >>> default_if_none(u"val", u"default")
395 u'val'
396
397 >>> default_if_none(None, u"default")
398 u'default'
399
400 >>> default_if_none(u'', u"default")
401 u''
402
403 >>> divisibleby(4, 2)
404 True
405
406 >>> divisibleby(4, 3)
407 False
408
409 >>> yesno(True)
410 u'yes'
411
412 >>> yesno(False)
413 u'no'
414
415 >>> yesno(None)
416 u'maybe'
417
418 >>> yesno(True, u'certainly,get out of town,perhaps')
419 u'certainly'
420
421 >>> yesno(False, u'certainly,get out of town,perhaps')
422 u'get out of town'
423
424 >>> yesno(None, u'certainly,get out of town,perhaps')
425 u'perhaps'
426
427 >>> yesno(None, u'certainly,get out of town')
428 u'get out of town'
429
430 >>> filesizeformat(1023)
431 u'1023 bytes'
432
433 >>> filesizeformat(1024)
434 u'1.0 KB'
435
436 >>> filesizeformat(10*1024)
437 u'10.0 KB'
438
439 >>> filesizeformat(1024*1024-1)
440 u'1024.0 KB'
441
442 >>> filesizeformat(1024*1024)
443 u'1.0 MB'
444
445 >>> filesizeformat(1024*1024*50)
446 u'50.0 MB'
447
448 >>> filesizeformat(1024*1024*1024-1)
449 u'1024.0 MB'
450
451 >>> filesizeformat(1024*1024*1024)
452 u'1.0 GB'
453
454 >>> pluralize(1)
455 u''
456
457 >>> pluralize(0)
458 u's'
459
460 >>> pluralize(2)
461 u's'
462
463 >>> pluralize([1])
464 u''
465
466 >>> pluralize([])
467 u's'
468
469 >>> pluralize([1,2,3])
470 u's'
471
472 >>> pluralize(1,u'es')
473 u''
474
475 >>> pluralize(0,u'es')
476 u'es'
477
478 >>> pluralize(2,u'es')
479 u'es'
480
481 >>> pluralize(1,u'y,ies')
482 u'y'
483
484 >>> pluralize(0,u'y,ies')
485 u'ies'
486
487 >>> pluralize(2,u'y,ies')
488 u'ies'
489
490 >>> pluralize(0,u'y,ies,error')
491 u''
492
493 >>> phone2numeric(u'0800 flowers')
494 u'0800 3569377'
495
496 # Filters shouldn't break if passed non-strings
497 >>> addslashes(123)
498 u'123'
499 >>> linenumbers(123)
500 u'1. 123'
501 >>> lower(123)
502 u'123'
503 >>> make_list(123)
504 [u'1', u'2', u'3']
505 >>> slugify(123)
506 u'123'
507 >>> title(123)
508 u'123'
509 >>> truncatewords(123, 2)
510 u'123'
511 >>> upper(123)
512 u'123'
513 >>> urlencode(123)
514 u'123'
515 >>> urlize(123)
516 u'123'
517 >>> urlizetrunc(123, 1)
518 u'123'
519 >>> wordcount(123)
520 1
521 >>> wordwrap(123, 2)
522 u'123'
523 >>> ljust('123', 4)
524 u'123 '
525 >>> rjust('123', 4)
526 u' 123'
527 >>> center('123', 5)
528 u' 123 '
529 >>> center('123', 6)
530 u' 123  '
531 >>> cut(123, '2')
532 u'13'
533 >>> escape(123)
534 u'123'
535 >>> linebreaks(123)
536 u'<p>123</p>'
537 >>> linebreaksbr(123)
538 u'123'
539 >>> removetags(123, 'a')
540 u'123'
541 >>> striptags(123)
542 u'123'
543
544 """
545
546 from django.template.defaultfilters import *
547 import datetime
548
549 # Python 2.3 doesn't have sorted()
550 try:
551     sorted
552 except NameError:
553     from django.utils.itercompat import sorted
554
555 if __name__ == '__main__':
556     import doctest
557     doctest.testmod()
Note: See TracBrowser for help on using the browser.