1 r""" |
|
2 >>> floatformat(7.7) |
|
3 '7.7' |
|
4 >>> floatformat(7.0) |
|
5 '7' |
|
6 >>> floatformat(0.7) |
|
7 '0.7' |
|
8 >>> floatformat(0.07) |
|
9 '0.1' |
|
10 >>> floatformat(0.007) |
|
11 '0.0' |
|
12 >>> floatformat(0.0) |
|
13 '0' |
|
14 >>> floatformat(7.7,3) |
|
15 '7.700' |
|
16 >>> floatformat(6.000000,3) |
|
17 '6.000' |
|
18 >>> floatformat(13.1031,-3) |
|
19 '13.103' |
|
20 >>> floatformat(11.1197, -2) |
|
21 '11.12' |
|
22 >>> floatformat(11.0000, -2) |
|
23 '11' |
|
24 >>> floatformat(11.000001, -2) |
|
25 '11.00' |
|
26 >>> floatformat(8.2798, 3) |
|
27 '8.280' |
|
28 >>> floatformat('foo') |
|
29 '' |
|
30 >>> floatformat(13.1031, 'bar') |
|
31 '13.1031' |
|
32 >>> floatformat('foo', 'bar') |
|
33 '' |
|
34 |
|
35 >>> addslashes('"double quotes" and \'single quotes\'') |
|
36 '\\"double quotes\\" and \\\'single quotes\\\'' |
|
37 |
|
38 >>> addslashes(r'\ : backslashes, too') |
|
39 '\\\\ : backslashes, too' |
|
40 |
|
41 >>> capfirst('hello world') |
|
42 'Hello world' |
|
43 |
|
44 >>> fix_ampersands('Jack & Jill & Jeroboam') |
|
45 'Jack & Jill & Jeroboam' |
|
46 |
|
47 >>> linenumbers('line 1\nline 2') |
|
48 '1. line 1\n2. line 2' |
|
49 |
|
50 >>> linenumbers('\n'.join(['x'] * 10)) |
|
51 '01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. x\n08. x\n09. x\n10. x' |
|
52 |
|
53 >>> lower('TEST') |
|
54 'test' |
|
55 |
|
56 >>> lower(u'\xcb') # uppercase E umlaut |
|
57 u'\xeb' |
|
58 |
|
59 >>> make_list('abc') |
|
60 ['a', 'b', 'c'] |
|
61 |
|
62 >>> make_list(1234) |
|
63 ['1', '2', '3', '4'] |
|
64 |
|
65 >>> slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/') |
|
66 'jack-jill-like-numbers-123-and-4-and-silly-characters' |
|
67 |
|
68 >>> stringformat(1, '03d') |
|
69 '001' |
|
70 |
|
71 >>> stringformat(1, 'z') |
|
72 '' |
|
73 |
|
74 >>> title('a nice title, isn\'t it?') |
|
75 "A Nice Title, Isn't It?" |
|
76 |
|
77 |
|
78 >>> truncatewords('A sentence with a few words in it', 1) |
|
79 'A ...' |
|
80 |
|
81 >>> truncatewords('A sentence with a few words in it', 5) |
|
82 'A sentence with a few ...' |
|
83 |
|
84 >>> truncatewords('A sentence with a few words in it', 100) |
|
85 'A sentence with a few words in it' |
|
86 |
|
87 >>> truncatewords('A sentence with a few words in it', 'not a number') |
|
88 'A sentence with a few words in it' |
|
89 |
|
90 >>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0) |
|
91 '' |
|
92 |
|
93 >>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 2) |
|
94 '<p>one <a href="#">two ...</a></p>' |
|
95 |
|
96 >>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 4) |
|
97 '<p>one <a href="#">two - three <br>four ...</a></p>' |
|
98 |
|
99 >>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 5) |
|
100 '<p>one <a href="#">two - three <br>four</a> five</p>' |
|
101 |
|
102 >>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 100) |
|
103 '<p>one <a href="#">two - three <br>four</a> five</p>' |
|
104 |
|
105 >>> upper('Mixed case input') |
|
106 'MIXED CASE INPUT' |
|
107 |
|
108 >>> upper(u'\xeb') # lowercase e umlaut |
|
109 u'\xcb' |
|
110 |
|
111 |
|
112 >>> urlencode('jack & jill') |
|
113 'jack%20%26%20jill' |
|
114 >>> urlencode(1) |
|
115 '1' |
|
116 |
|
117 |
|
118 >>> urlizetrunc('http://short.com/', 20) |
|
119 '<a href="http://short.com/" rel="nofollow">http://short.com/</a>' |
|
120 |
|
121 >>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) |
|
122 '<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>' |
|
123 |
|
124 >>> wordcount('') |
|
125 0 |
|
126 |
|
127 >>> wordcount('oneword') |
|
128 1 |
|
129 |
|
130 >>> wordcount('lots of words') |
|
131 3 |
|
132 |
|
133 >>> wordwrap('this is a long paragraph of text that really needs to be wrapped I\'m afraid', 14) |
|
134 "this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI'm afraid" |
|
135 |
|
136 >>> wordwrap('this is a short paragraph of text.\n But this line should be indented',14) |
|
137 'this is a\nshort\nparagraph of\ntext.\n But this\nline should be\nindented' |
|
138 |
|
139 >>> wordwrap('this is a short paragraph of text.\n But this line should be indented',15) |
|
140 'this is a short\nparagraph of\ntext.\n But this line\nshould be\nindented' |
|
141 |
|
142 >>> ljust('test', 10) |
|
143 'test ' |
|
144 |
|
145 >>> ljust('test', 3) |
|
146 'test' |
|
147 |
|
148 >>> rjust('test', 10) |
|
149 ' test' |
|
150 |
|
151 >>> rjust('test', 3) |
|
152 'test' |
|
153 |
|
154 >>> center('test', 6) |
|
155 ' test ' |
|
156 |
|
157 >>> cut('a string to be mangled', 'a') |
|
158 ' string to be mngled' |
|
159 |
|
160 >>> cut('a string to be mangled', 'ng') |
|
161 'a stri to be maled' |
|
162 |
|
163 >>> cut('a string to be mangled', 'strings') |
|
164 'a string to be mangled' |
|
165 |
|
166 >>> escape('<some html & special characters > here') |
|
167 '<some html & special characters > here' |
|
168 |
|
169 >>> linebreaks('line 1') |
|
170 '<p>line 1</p>' |
|
171 |
|
172 >>> linebreaks('line 1\nline 2') |
|
173 '<p>line 1<br />line 2</p>' |
|
174 |
|
175 >>> removetags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img') |
|
176 'some <b>html</b> with alert("You smell") disallowed tags' |
|
177 |
|
178 >>> striptags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags') |
|
179 'some html with alert("You smell") disallowed tags' |
|
180 |
|
181 >>> dictsort([{'age': 23, 'name': 'Barbara-Ann'}, |
|
182 ... {'age': 63, 'name': 'Ra Ra Rasputin'}, |
|
183 ... {'name': 'Jonny B Goode', 'age': 18}], 'age') |
|
184 [{'age': 18, 'name': 'Jonny B Goode'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 63, 'name': 'Ra Ra Rasputin'}] |
|
185 |
|
186 >>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'}, |
|
187 ... {'age': 63, 'name': 'Ra Ra Rasputin'}, |
|
188 ... {'name': 'Jonny B Goode', 'age': 18}], 'age') |
|
189 [{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 18, 'name': 'Jonny B Goode'}] |
|
190 |
|
191 >>> first([0,1,2]) |
|
192 0 |
|
193 |
|
194 >>> first('') |
|
195 '' |
|
196 |
|
197 >>> first('test') |
|
198 't' |
|
199 |
|
200 >>> join([0,1,2], 'glue') |
|
201 '0glue1glue2' |
|
202 |
|
203 >>> length('1234') |
|
204 4 |
|
205 |
|
206 >>> length([1,2,3,4]) |
|
207 4 |
|
208 |
|
209 >>> length_is([], 0) |
|
210 True |
|
211 |
|
212 >>> length_is([], 1) |
|
213 False |
|
214 |
|
215 >>> length_is('a', 1) |
|
216 True |
|
217 |
|
218 >>> length_is('a', 10) |
|
219 False |
|
220 |
|
221 >>> slice_('abcdefg', '0') |
|
222 '' |
|
223 |
|
224 >>> slice_('abcdefg', '1') |
|
225 'a' |
|
226 |
|
227 >>> slice_('abcdefg', '-1') |
|
228 'abcdef' |
|
229 |
|
230 >>> slice_('abcdefg', '1:2') |
|
231 'b' |
|
232 |
|
233 >>> slice_('abcdefg', '1:3') |
|
234 'bc' |
|
235 |
|
236 >>> slice_('abcdefg', '0::2') |
|
237 'aceg' |
|
238 |
|
239 >>> unordered_list(['item 1', []]) |
|
240 '\t<li>item 1</li>' |
|
241 |
|
242 >>> unordered_list(['item 1', [['item 1.1', []]]]) |
|
243 '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>' |
|
244 |
|
245 >>> unordered_list(['item 1', [['item 1.1', []], ['item 1.2', []]]]) |
|
246 '\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>' |
|
247 |
|
248 >>> add('1', '2') |
|
249 3 |
|
250 |
|
251 >>> get_digit(123, 1) |
|
252 3 |
|
253 |
|
254 >>> get_digit(123, 2) |
|
255 2 |
|
256 |
|
257 >>> get_digit(123, 3) |
|
258 1 |
|
259 |
|
260 >>> get_digit(123, 4) |
|
261 0 |
|
262 |
|
263 >>> get_digit(123, 0) |
|
264 123 |
|
265 |
|
266 >>> get_digit('xyz', 0) |
|
267 'xyz' |
|
268 |
|
269 # real testing of date() is in dateformat.py |
|
270 >>> date(datetime.datetime(2005, 12, 29), "d F Y") |
|
271 '29 December 2005' |
|
272 >>> date(datetime.datetime(2005, 12, 29), r'jS o\f F') |
|
273 '29th of December' |
|
274 |
|
275 # real testing of time() is done in dateformat.py |
|
276 >>> time(datetime.time(13), "h") |
|
277 '01' |
|
278 |
|
279 >>> time(datetime.time(0), "h") |
|
280 '12' |
|
281 |
|
282 # real testing is done in timesince.py, where we can provide our own 'now' |
|
283 >>> timesince(datetime.datetime.now() - datetime.timedelta(1)) |
|
284 '1 day' |
|
285 |
|
286 >>> default("val", "default") |
|
287 'val' |
|
288 |
|
289 >>> default(None, "default") |
|
290 'default' |
|
291 |
|
292 >>> default('', "default") |
|
293 'default' |
|
294 |
|
295 >>> default_if_none("val", "default") |
|
296 'val' |
|
297 |
|
298 >>> default_if_none(None, "default") |
|
299 'default' |
|
300 |
|
301 >>> default_if_none('', "default") |
|
302 '' |
|
303 |
|
304 >>> divisibleby(4, 2) |
|
305 True |
|
306 |
|
307 >>> divisibleby(4, 3) |
|
308 False |
|
309 |
|
310 >>> yesno(True) |
|
311 'yes' |
|
312 |
|
313 >>> yesno(False) |
|
314 'no' |
|
315 |
|
316 >>> yesno(None) |
|
317 'maybe' |
|
318 |
|
319 >>> yesno(True, 'certainly,get out of town,perhaps') |
|
320 'certainly' |
|
321 |
|
322 >>> yesno(False, 'certainly,get out of town,perhaps') |
|
323 'get out of town' |
|
324 |
|
325 >>> yesno(None, 'certainly,get out of town,perhaps') |
|
326 'perhaps' |
|
327 |
|
328 >>> yesno(None, 'certainly,get out of town') |
|
329 'get out of town' |
|
330 |
|
331 >>> filesizeformat(1023) |
|
332 '1023 bytes' |
|
333 |
|
334 >>> filesizeformat(1024) |
|
335 '1.0 KB' |
|
336 |
|
337 >>> filesizeformat(10*1024) |
|
338 '10.0 KB' |
|
339 |
|
340 >>> filesizeformat(1024*1024-1) |
|
341 '1024.0 KB' |
|
342 |
|
343 >>> filesizeformat(1024*1024) |
|
344 '1.0 MB' |
|
345 |
|
346 >>> filesizeformat(1024*1024*50) |
|
347 '50.0 MB' |
|
348 |
|
349 >>> filesizeformat(1024*1024*1024-1) |
|
350 '1024.0 MB' |
|
351 |
|
352 >>> filesizeformat(1024*1024*1024) |
|
353 '1.0 GB' |
|
354 |
|
355 >>> pluralize(1) |
|
356 '' |
|
357 |
|
358 >>> pluralize(0) |
|
359 's' |
|
360 |
|
361 >>> pluralize(2) |
|
362 's' |
|
363 |
|
364 >>> pluralize([1]) |
|
365 '' |
|
366 |
|
367 >>> pluralize([]) |
|
368 's' |
|
369 |
|
370 >>> pluralize([1,2,3]) |
|
371 's' |
|
372 |
|
373 >>> pluralize(1,'es') |
|
374 '' |
|
375 |
|
376 >>> pluralize(0,'es') |
|
377 'es' |
|
378 |
|
379 >>> pluralize(2,'es') |
|
380 'es' |
|
381 |
|
382 >>> pluralize(1,'y,ies') |
|
383 'y' |
|
384 |
|
385 >>> pluralize(0,'y,ies') |
|
386 'ies' |
|
387 |
|
388 >>> pluralize(2,'y,ies') |
|
389 'ies' |
|
390 |
|
391 >>> pluralize(0,'y,ies,error') |
|
392 '' |
|
393 |
|
394 >>> phone2numeric('0800 flowers') |
|
395 '0800 3569377' |
|
396 |
|
397 # Filters shouldn't break if passed non-strings |
|
398 >>> addslashes(123) |
|
399 '123' |
|
400 >>> linenumbers(123) |
|
401 '1. 123' |
|
402 >>> lower(123) |
|
403 '123' |
|
404 >>> make_list(123) |
|
405 ['1', '2', '3'] |
|
406 >>> slugify(123) |
|
407 '123' |
|
408 >>> title(123) |
|
409 '123' |
|
410 >>> truncatewords(123, 2) |
|
411 '123' |
|
412 >>> upper(123) |
|
413 '123' |
|
414 >>> urlencode(123) |
|
415 '123' |
|
416 >>> urlize(123) |
|
417 '123' |
|
418 >>> urlizetrunc(123, 1) |
|
419 '123' |
|
420 >>> wordcount(123) |
|
421 1 |
|
422 >>> wordwrap(123, 2) |
|
423 '123' |
|
424 >>> ljust('123', 4) |
|
425 '123 ' |
|
426 >>> rjust('123', 4) |
|
427 ' 123' |
|
428 >>> center('123', 5) |
|
429 ' 123 ' |
|
430 >>> center('123', 6) |
|
431 ' 123 ' |
|
432 >>> cut(123, '2') |
|
433 '13' |
|
434 >>> escape(123) |
|
435 '123' |
|
436 >>> linebreaks(123) |
|
437 '<p>123</p>' |
|
438 >>> linebreaksbr(123) |
|
439 '123' |
|
440 >>> removetags(123, 'a') |
|
441 '123' |
|
442 >>> striptags(123) |
|
443 '123' |
|
444 |
|
445 """ |
|
446 |
|
447 from django.template.defaultfilters import * |
|
448 import datetime |
|
449 |
|
450 if __name__ == '__main__': |
|
451 import doctest |
|
452 doctest.testmod() |
|