Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1145 -- Added unit tests for default template filters and fixe…

…d two bugs in filters. Thanks, Luke Plant

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1811 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 53aef92bf89f2d655a125d94a0e24203e76af1d6 1 parent 4596b1b
Adrian Holovaty authored January 01, 2006
12  django/core/template/defaultfilters.py
@@ -117,7 +117,8 @@ def urlize(value):
117 117
 
118 118
 def urlizetrunc(value, limit):
119 119
     """
120  
-    Converts URLs into clickable links, truncating URLs to the given character limit
  120
+    Converts URLs into clickable links, truncating URLs to the given character limit,
  121
+    and adding 'rel=nofollow' attribute to discourage spamming.
121 122
 
122 123
     Argument: Length to truncate URLs to.
123 124
     """
@@ -254,7 +255,14 @@ def slice_(value, arg):
254 255
     for an introduction.
255 256
     """
256 257
     try:
257  
-        return value[slice(*[x and int(x) or None for x in arg.split(':')])]
  258
+        bits = []
  259
+        for x in arg.split(':'):
  260
+            if len(x) == 0:
  261
+                bits.append(None)
  262
+            else:
  263
+                bits.append(int(x))
  264
+        return value[slice(*bits)]
  265
+
258 266
     except (ValueError, TypeError):
259 267
         return value # Fail silently.
260 268
 
6  django/utils/timesince.py
@@ -24,14 +24,16 @@ def timesince(d, now=None):
24 24
     else:
25 25
         tz = None
26 26
     now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz)
27  
-    delta = now - d
  27
+    
  28
+    # ignore microsecond part of 'd' since we removed it from 'now'
  29
+    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
28 30
     since = delta.days * 24 * 60 * 60 + delta.seconds
29 31
     for i, (seconds, name) in enumerate(chunks):
30 32
         count = since / seconds
31 33
         if count != 0:
32 34
             break
33 35
     if count < 0:
34  
-        return '%d milliseconds' % math.floor(delta.microseconds / 1000)
  36
+        return '%d milliseconds' % math.floor((now - d).microseconds / 1000)
35 37
     s = '%d %s' % (count, name(count))
36 38
     if i + 1 < len(chunks):
37 39
         # Now get the second item
310  tests/othertests/defaultfilters.py
@@ -11,9 +11,319 @@
11 11
 '0.0'
12 12
 >>> floatformat(0.0)
13 13
 '0'
  14
+
  15
+>>> addslashes('"double quotes" and \\'single quotes\\'')
  16
+'\\\\"double quotes\\\\" and \\\\\\'single quotes\\\\\\''
  17
+
  18
+>>> capfirst('hello world')
  19
+'Hello world'
  20
+
  21
+>>> fix_ampersands('Jack & Jill & Jeroboam')
  22
+'Jack &amp; Jill &amp; Jeroboam'
  23
+
  24
+>>> linenumbers('line 1\\nline 2')
  25
+'1. line 1\\n2. line 2'
  26
+
  27
+>>> linenumbers('\\n'.join(['x'] * 10))
  28
+'01. x\\n02. x\\n03. x\\n04. x\\n05. x\\n06. x\\n07. x\\n08. x\\n09. x\\n10. x'
  29
+
  30
+>>> lower('TEST')
  31
+'test'
  32
+
  33
+>>> lower(u'\\xcb') # uppercase E umlaut
  34
+u'\\xeb'
  35
+
  36
+>>> make_list('abc')
  37
+['a', 'b', 'c']
  38
+
  39
+>>> make_list(1234)
  40
+['1', '2', '3', '4']
  41
+
  42
+>>> slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/')
  43
+'jack-jill-like-numbers-123-and-4-and-silly-characters'
  44
+
  45
+>>> stringformat(1, '03d')
  46
+'001'
  47
+
  48
+>>> stringformat(1, 'z')
  49
+''
  50
+
  51
+>>> title('a nice title, isn\\'t it?')
  52
+"A Nice Title, Isn't It?"
  53
+
  54
+
  55
+>>> truncatewords('A sentence with a few words in it', 1)
  56
+'A ...'
  57
+
  58
+>>> truncatewords('A sentence with a few words in it', 5)
  59
+'A sentence with a few ...'
  60
+
  61
+>>> truncatewords('A sentence with a few words in it', 100)
  62
+'A sentence with a few words in it'
  63
+
  64
+>>> truncatewords('A sentence with a few words in it', 'not a number')
  65
+'A sentence with a few words in it'
  66
+
  67
+
  68
+>>> upper('Mixed case input')
  69
+'MIXED CASE INPUT'
  70
+
  71
+>>> upper(u'\\xeb') # lowercase e umlaut
  72
+u'\\xcb'
  73
+
  74
+
  75
+>>> urlencode('jack & jill')
  76
+'jack%20%26%20jill'
  77
+
  78
+
  79
+>>> urlizetrunc('http://short.com/', 20)
  80
+'<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
  81
+
  82
+>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
  83
+'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>'
  84
+
  85
+>>> wordcount('')
  86
+0
  87
+
  88
+>>> wordcount('oneword')
  89
+1
  90
+
  91
+>>> wordcount('lots of words')
  92
+3
  93
+
  94
+>>> wordwrap('this is a long paragraph of text that really needs to be wrapped I\\'m afraid', 14)
  95
+"this is a long\\nparagraph of\\ntext that\\nreally needs\\nto be wrapped\\nI'm afraid"
  96
+
  97
+>>> ljust('test', 10)
  98
+'test      '
  99
+
  100
+>>> ljust('test', 3)
  101
+'test'
  102
+
  103
+>>> rjust('test', 10)
  104
+'      test'
  105
+
  106
+>>> rjust('test', 3)
  107
+'test'
  108
+
  109
+>>> center('test', 6)
  110
+' test '
  111
+
  112
+>>> cut('a string to be mangled', 'a')
  113
+' string to be mngled'
  114
+
  115
+>>> cut('a string to be mangled', 'ng')
  116
+'a stri to be maled'
  117
+
  118
+>>> cut('a string to be mangled', 'strings')
  119
+'a string to be mangled'
  120
+
  121
+>>> escape('<some html & special characters > here')
  122
+'&lt;some html &amp; special characters &gt; here'
  123
+
  124
+>>> linebreaks('line 1')
  125
+'<p>line 1</p>'
  126
+
  127
+>>> linebreaks('line 1\\nline 2')
  128
+'<p>line 1<br />line 2</p>'
  129
+
  130
+>>> removetags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img')
  131
+'some <b>html</b> with alert("You smell") disallowed  tags'
  132
+
  133
+>>> striptags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags')
  134
+'some html with alert("You smell") disallowed  tags'
  135
+
  136
+>>> dictsort([{'age': 23, 'name': 'Barbara-Ann'},\
  137
+              {'age': 63, 'name': 'Ra Ra Rasputin'},\
  138
+              {'name': 'Jonny B Goode', 'age': 18}], 'age')
  139
+[{'age': 18, 'name': 'Jonny B Goode'},\
  140
+ {'age': 23, 'name': 'Barbara-Ann'},\
  141
+ {'age': 63, 'name': 'Ra Ra Rasputin'}]
  142
+
  143
+>>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},\
  144
+              {'age': 63, 'name': 'Ra Ra Rasputin'},\
  145
+              {'name': 'Jonny B Goode', 'age': 18}], 'age')
  146
+[{'age': 63, 'name': 'Ra Ra Rasputin'},\
  147
+ {'age': 23, 'name': 'Barbara-Ann'},\
  148
+ {'age': 18, 'name': 'Jonny B Goode'}]
  149
+
  150
+>>> first([0,1,2])
  151
+0
  152
+
  153
+>>> first('')
  154
+''
  155
+
  156
+>>> first('test')
  157
+'t'
  158
+
  159
+>>> join([0,1,2], 'glue')
  160
+'0glue1glue2'
  161
+
  162
+>>> length('1234')
  163
+4
  164
+
  165
+>>> length([1,2,3,4])
  166
+4
  167
+
  168
+>>> length_is([], 0)
  169
+True
  170
+
  171
+>>> length_is([], 1)
  172
+False
  173
+
  174
+>>> length_is('a', 1)
  175
+True
  176
+
  177
+>>> length_is('a', 10)
  178
+False
  179
+
  180
+>>> slice_('abcdefg', '0')
  181
+''
  182
+
  183
+>>> slice_('abcdefg', '1')
  184
+'a'
  185
+
  186
+>>> slice_('abcdefg', '-1')
  187
+'abcdef'
  188
+
  189
+>>> slice_('abcdefg', '1:2')
  190
+'b'
  191
+
  192
+>>> slice_('abcdefg', '1:3')
  193
+'bc'
  194
+
  195
+>>> slice_('abcdefg', '0::2')
  196
+'aceg'
  197
+
  198
+>>> unordered_list(['item 1', []])
  199
+'\\t<li>item 1</li>'
  200
+
  201
+>>> unordered_list(['item 1', [['item 1.1', []]]])
  202
+'\\t<li>item 1\\n\\t<ul>\\n\\t\\t<li>item 1.1</li>\\n\\t</ul>\\n\\t</li>'
  203
+
  204
+>>> unordered_list(['item 1', [['item 1.1', []], ['item 1.2', []]]])
  205
+'\\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>'
  206
+
  207
+>>> add('1', '2')
  208
+3
  209
+
  210
+>>> get_digit(123, 1)
  211
+3
  212
+
  213
+>>> get_digit(123, 2)
  214
+2
  215
+
  216
+>>> get_digit(123, 3)
  217
+1
  218
+
  219
+>>> get_digit(123, 4)
  220
+0
  221
+
  222
+>>> get_digit(123, 0)
  223
+123
  224
+
  225
+>>> get_digit('xyz', 0)
  226
+'xyz'
  227
+
  228
+# real testing of date() is in dateformat.py
  229
+>>> date(datetime.datetime(2005, 12, 29), "d F Y")
  230
+'29 December 2005'
  231
+
  232
+# real testing of time() is done in dateformat.py
  233
+>>> time(datetime.time(13), "h")
  234
+'01'
  235
+
  236
+# real testing is done in timesince.py, where we can provide our own 'now'
  237
+>>> timesince(datetime.datetime.now() - datetime.timedelta(1))
  238
+'1 day'
  239
+
  240
+>>> default("val", "default")
  241
+'val'
  242
+
  243
+>>> default(None, "default")
  244
+'default'
  245
+
  246
+>>> default('', "default")
  247
+'default'
  248
+
  249
+>>> default_if_none("val", "default")
  250
+'val'
  251
+
  252
+>>> default_if_none(None, "default")
  253
+'default'
  254
+
  255
+>>> default_if_none('', "default")
  256
+''
  257
+
  258
+>>> divisibleby(4, 2)
  259
+True
  260
+
  261
+>>> divisibleby(4, 3)
  262
+False
  263
+
  264
+>>> yesno(True)
  265
+'yes'
  266
+
  267
+>>> yesno(False)
  268
+'no'
  269
+
  270
+>>> yesno(None)
  271
+'maybe'
  272
+
  273
+>>> yesno(True, 'certainly,get out of town,perhaps')
  274
+'certainly'
  275
+
  276
+>>> yesno(False, 'certainly,get out of town,perhaps')
  277
+'get out of town'
  278
+
  279
+>>> yesno(None, 'certainly,get out of town,perhaps')
  280
+'perhaps'
  281
+
  282
+>>> yesno(None, 'certainly,get out of town')
  283
+'get out of town'
  284
+
  285
+>>> filesizeformat(1023)
  286
+'1023 bytes'
  287
+
  288
+>>> filesizeformat(1024)
  289
+'1.0 KB'
  290
+
  291
+>>> filesizeformat(10*1024)
  292
+'10.0 KB'
  293
+
  294
+>>> filesizeformat(1024*1024-1)
  295
+'1024.0 KB'
  296
+
  297
+>>> filesizeformat(1024*1024)
  298
+'1.0 MB'
  299
+
  300
+>>> filesizeformat(1024*1024*50)
  301
+'50.0 MB'
  302
+
  303
+>>> filesizeformat(1024*1024*1024-1)
  304
+'1024.0 MB'
  305
+
  306
+>>> filesizeformat(1024*1024*1024)
  307
+'1.0 GB'
  308
+
  309
+>>> pluralize(1)
  310
+''
  311
+
  312
+>>> pluralize(0)
  313
+'s'
  314
+
  315
+>>> pluralize(2)
  316
+'s'
  317
+
  318
+>>> phone2numeric('0800 flowers')
  319
+'0800 3569377'
  320
+
  321
+
  322
+
14 323
 """
15 324
 
16 325
 from django.core.template.defaultfilters import *
  326
+import datetime
17 327
 
18 328
 if __name__ == '__main__':
19 329
     import doctest
2  tests/runtests.py
@@ -206,7 +206,7 @@ def run_tests(self):
206 206
     parser.add_option('-v', help='How verbose should the output be? Choices are 0, 1 and 2, where 2 is most verbose. Default is 0.',
207 207
         type='choice', choices=['0', '1', '2'])
208 208
     parser.add_option('--settings',
209  
-        help='Python path to settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
  209
+        help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
210 210
     options, args = parser.parse_args()
211 211
     verbosity_level = 0
212 212
     if options.v:

0 notes on commit 53aef92

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