Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Migrated defaultfilters doctests. Thanks to Stephan Jaekel.

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

0 notes on commit d577648

Please sign in to comment.
Something went wrong with that request. Please try again.