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