From d577648732c8d55b5d426e013dcd2a10a0b47f89 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 28 Sep 2010 08:19:04 +0000 Subject: [PATCH] Migrated defaultfilters doctests. Thanks to Stephan Jaekel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13937 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/defaultfilters/tests.py | 1086 ++++++++--------- 1 file changed, 485 insertions(+), 601 deletions(-) diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index 50aaf212ed..27014a7813 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -1,605 +1,489 @@ # -*- 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'') -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'

one two - three
four
five

', 0) -u'' - ->>> truncatewords_html(u'

one two - three
four
five

', 2) -u'

one two ...

' - ->>> truncatewords_html(u'

one two - three
four
five

', 4) -u'

one two - three
four ...

' - ->>> truncatewords_html(u'

one two - three
four
five

', 5) -u'

one two - three
four
five

' - ->>> truncatewords_html(u'

one two - three
four
five

', 100) -u'

one two - three
four
five

' - ->>> 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'http://short.com/' - ->>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) -u'http://www.google...' - ->>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) -u'http://www.google...' - -# Check truncating of URIs which are the exact length ->>> uri = 'http://31characteruri.com/test/' ->>> len(uri) -31 ->>> urlizetrunc(uri, 31) -u'http://31characteruri.com/test/' ->>> urlizetrunc(uri, 30) -u'http://31characteruri.com/t...' ->>> urlizetrunc(uri, 2) -u'...' - -# Check normal urlize ->>> urlize('http://google.com') -u'http://google.com' - ->>> urlize('http://google.com/') -u'http://google.com/' - ->>> urlize('www.google.com') -u'www.google.com' - ->>> urlize('djangoproject.org') -u'djangoproject.org' - ->>> urlize('info@djangoproject.org') -u'info@djangoproject.org' - -# Check urlize with https addresses ->>> urlize('https://google.com') -u'https://google.com' - - ->>> 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' here') -u'<some html & special characters > here' - ->>> force_escape(u' here ĐÅ€£') -u'<some html & special characters > here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3' - ->>> linebreaks(u'line 1') -u'

line 1

' - ->>> linebreaks(u'line 1\nline 2') -u'

line 1
line 2

' - ->>> removetags(u'some html with disallowed tags', 'script img') -u'some html with alert("You smell") disallowed tags' - ->>> striptags(u'some html with disallowed 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
  • item 1
  • \n\t
  • item 2
  • ' - ->>> unordered_list([u'item 1', [u'item 1.1']]) -u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t
    \n\t
  • ' - ->>> unordered_list([u'item 1', [u'item 1.1', u'item1.2'], u'item 2']) -u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t\t
    • item1.2
    • \n\t
    \n\t
  • \n\t
  • item 2
  • ' - ->>> unordered_list([u'item 1', [u'item 1.1', [u'item 1.1.1', [u'item 1.1.1.1']]]]) -u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1\n\t\t
        \n\t\t\t
      • item 1.1.1\n\t\t\t
          \n\t\t\t\t
        • item 1.1.1.1
        • \n\t\t\t
        \n\t\t\t
      • \n\t\t
      \n\t\t
    • \n\t
    \n\t
  • ' - ->>> unordered_list(['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]) -u'\t
  • States\n\t
      \n\t\t
    • Kansas\n\t\t
        \n\t\t\t
      • Lawrence
      • \n\t\t\t
      • Topeka
      • \n\t\t
      \n\t\t
    • \n\t\t
    • Illinois
    • \n\t
    \n\t
  • ' - ->>> 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
  • ulitem-a
  • \n\t
  • ulitem-b
  • ' - -# Old format for unordered lists should still work ->>> unordered_list([u'item 1', []]) -u'\t
  • item 1
  • ' - ->>> unordered_list([u'item 1', [[u'item 1.1', []]]]) -u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t
    \n\t
  • ' - ->>> unordered_list([u'item 1', [[u'item 1.1', []], [u'item 1.2', []]]]) -u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t\t
    • item 1.2
    • \n\t
    \n\t
  • ' - ->>> unordered_list(['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]) -u'\t
  • States\n\t
      \n\t\t
    • Kansas\n\t\t
        \n\t\t\t
      • Lawrence
      • \n\t\t\t
      • Topeka
      • \n\t\t
      \n\t\t
    • \n\t\t
    • Illinois
    • \n\t
    \n\t
  • ' - ->>> 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(1024*1024*1024*1024) -u'1.0 TB' - ->>> filesizeformat(1024*1024*1024*1024*1024) -u'1.0 PB' - ->>> filesizeformat(1024*1024*1024*1024*1024*2000) -u'2000.0 PB' - ->>> 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'

    123

    ' ->>> linebreaksbr(123) -u'123' ->>> removetags(123, 'a') -u'123' ->>> striptags(123) -u'123' - -""" +import datetime +import unittest from django.template.defaultfilters import * -import datetime -if __name__ == '__main__': - import doctest - doctest.testmod() +class DefaultFiltersTests(unittest.TestCase): + + def test_floatformat(self): + self.assertEqual(floatformat(7.7), u'7.7') + self.assertEqual(floatformat(7.0), u'7') + self.assertEqual(floatformat(0.7), u'0.7') + self.assertEqual(floatformat(0.07), u'0.1') + self.assertEqual(floatformat(0.007), u'0.0') + self.assertEqual(floatformat(0.0), u'0') + self.assertEqual(floatformat(7.7, 3), u'7.700') + self.assertEqual(floatformat(6.000000, 3), u'6.000') + self.assertEqual(floatformat(6.200000, 3), u'6.200') + self.assertEqual(floatformat(6.200000, -3), u'6.200') + self.assertEqual(floatformat(13.1031, -3), u'13.103') + self.assertEqual(floatformat(11.1197, -2), u'11.12') + self.assertEqual(floatformat(11.0000, -2), u'11') + self.assertEqual(floatformat(11.000001, -2), u'11.00') + self.assertEqual(floatformat(8.2798, 3), u'8.280') + self.assertEqual(floatformat(u'foo'), u'') + self.assertEqual(floatformat(13.1031, u'bar'), u'13.1031') + self.assertEqual(floatformat(18.125, 2), u'18.13') + self.assertEqual(floatformat(u'foo', u'bar'), u'') + self.assertEqual(floatformat(u'¿Cómo esta usted?'), u'') + self.assertEqual(floatformat(None), u'') + + pos_inf = float(1e30000) + self.assertEqual(floatformat(pos_inf), unicode(pos_inf)) + + neg_inf = float(-1e30000) + self.assertEqual(floatformat(neg_inf), unicode(neg_inf)) + + nan = pos_inf / pos_inf + self.assertEqual(floatformat(nan), unicode(nan)) + + class FloatWrapper(object): + def __init__(self, value): + self.value = value + def __float__(self): + return self.value + + self.assertEqual(floatformat(FloatWrapper(11.000001), -2), u'11.00') + + def test_addslashes(self): + self.assertEqual(addslashes(u'"double quotes" and \'single quotes\''), + u'\\"double quotes\\" and \\\'single quotes\\\'') + + self.assertEqual(addslashes(ur'\ : backslashes, too'), + u'\\\\ : backslashes, too') + + def test_capfirst(self): + self.assertEqual(capfirst(u'hello world'), u'Hello world') + + def test_escapejs(self): + self.assertEqual(escapejs(u'"double quotes" and \'single quotes\''), + u'\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027') + self.assertEqual(escapejs(ur'\ : backslashes, too'), + u'\\u005C : backslashes, too') + self.assertEqual(escapejs(u'and lots of whitespace: \r\n\t\v\f\b'), + u'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008') + self.assertEqual(escapejs(ur''), + u'\\u003Cscript\\u003Eand this\\u003C/script\\u003E') + self.assertEqual( + escapejs(u'paragraph separator:\u2029and line separator:\u2028'), + u'paragraph separator:\\u2029and line separator:\\u2028') + + def test_fix_ampersands(self): + self.assertEqual(fix_ampersands(u'Jack & Jill & Jeroboam'), + u'Jack & Jill & Jeroboam') + + def test_linenumbers(self): + self.assertEqual(linenumbers(u'line 1\nline 2'), + u'1. line 1\n2. line 2') + self.assertEqual(linenumbers(u'\n'.join([u'x'] * 10)), + u'01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. '\ + u'x\n08. x\n09. x\n10. x') + + def test_lower(self): + self.assertEqual(lower('TEST'), u'test') + + # uppercase E umlaut + self.assertEqual(lower(u'\xcb'), u'\xeb') + + def test_make_list(self): + self.assertEqual(make_list('abc'), [u'a', u'b', u'c']) + self.assertEqual(make_list(1234), [u'1', u'2', u'3', u'4']) + + def test_slugify(self): + self.assertEqual(slugify(' Jack & Jill like numbers 1,2,3 and 4 and'\ + ' silly characters ?%.$!/'), + u'jack-jill-like-numbers-123-and-4-and-silly-characters') + + self.assertEqual(slugify(u"Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"), + u'un-elephant-a-loree-du-bois') + + def test_stringformat(self): + self.assertEqual(stringformat(1, u'03d'), u'001') + self.assertEqual(stringformat(1, u'z'), u'') + + def test_title(self): + self.assertEqual(title('a nice title, isn\'t it?'), + u"A Nice Title, Isn't It?") + self.assertEqual(title(u'discoth\xe8que'), u'Discoth\xe8que') + + def test_truncatewords(self): + self.assertEqual( + truncatewords(u'A sentence with a few words in it', 1), u'A ...') + self.assertEqual( + truncatewords(u'A sentence with a few words in it', 5), + u'A sentence with a few ...') + self.assertEqual( + truncatewords(u'A sentence with a few words in it', 100), + u'A sentence with a few words in it') + self.assertEqual( + truncatewords(u'A sentence with a few words in it', + 'not a number'), u'A sentence with a few words in it') + + def test_truncatewords_html(self): + self.assertEqual(truncatewords_html( + u'

    one two - three
    four
    five

    ', 0), u'') + self.assertEqual(truncatewords_html(u'

    one two - '\ + u'three
    four
    five

    ', 2), + u'

    one two ...

    ') + self.assertEqual(truncatewords_html( + u'

    one two - three
    four
    five

    ', 4), + u'

    one two - three
    four ...

    ') + self.assertEqual(truncatewords_html( + u'

    one two - three
    four
    five

    ', 5), + u'

    one two - three
    four
    five

    ') + self.assertEqual(truncatewords_html( + u'

    one two - three
    four
    five

    ', 100), + u'

    one two - three
    four
    five

    ') + self.assertEqual(truncatewords_html( + u'\xc5ngstr\xf6m was here', 1), u'\xc5ngstr\xf6m ...') + + def test_upper(self): + self.assertEqual(upper(u'Mixed case input'), u'MIXED CASE INPUT') + # lowercase e umlaut + self.assertEqual(upper(u'\xeb'), u'\xcb') + + def test_urlencode(self): + self.assertEqual(urlencode(u'fran\xe7ois & jill'), + u'fran%C3%A7ois%20%26%20jill') + self.assertEqual(urlencode(1), u'1') + + def test_iriencode(self): + self.assertEqual(iriencode(u'S\xf8r-Tr\xf8ndelag'), + u'S%C3%B8r-Tr%C3%B8ndelag') + self.assertEqual(iriencode(urlencode(u'fran\xe7ois & jill')), + u'fran%C3%A7ois%20%26%20jill') + + def test_urlizetrunc(self): + self.assertEqual(urlizetrunc(u'http://short.com/', 20), u'http://short.com/') + + self.assertEqual(urlizetrunc(u'http://www.google.co.uk/search?hl=en'\ + u'&q=some+long+url&btnG=Search&meta=', 20), u'http://www.google...') + + self.assertEqual(urlizetrunc('http://www.google.co.uk/search?hl=en'\ + u'&q=some+long+url&btnG=Search&meta=', 20), u'http://www.google...') + + # Check truncating of URIs which are the exact length + uri = 'http://31characteruri.com/test/' + self.assertEqual(len(uri), 31) + + self.assertEqual(urlizetrunc(uri, 31), + u''\ + u'http://31characteruri.com/test/') + + self.assertEqual(urlizetrunc(uri, 30), + u''\ + u'http://31characteruri.com/t...') + + self.assertEqual(urlizetrunc(uri, 2), + u'...') + + def test_urlize(self): + # Check normal urlize + self.assertEqual(urlize('http://google.com'), + u'http://google.com') + self.assertEqual(urlize('http://google.com/'), + u'http://google.com/') + self.assertEqual(urlize('www.google.com'), + u'www.google.com') + self.assertEqual(urlize('djangoproject.org'), + u'djangoproject.org') + self.assertEqual(urlize('info@djangoproject.org'), + u'info@djangoproject.org') + + # Check urlize with https addresses + self.assertEqual(urlize('https://google.com'), + u'https://google.com') + + def test_wordcount(self): + self.assertEqual(wordcount(''), 0) + self.assertEqual(wordcount(u'oneword'), 1) + self.assertEqual(wordcount(u'lots of words'), 3) + + self.assertEqual(wordwrap(u'this is a long paragraph of text that '\ + u'really needs to be wrapped I\'m afraid', 14), + u"this is a long\nparagraph of\ntext that\nreally needs\nto be "\ + u"wrapped\nI'm afraid") + + self.assertEqual(wordwrap(u'this is a short paragraph of text.\n '\ + u'But this line should be indented', 14), + u'this is a\nshort\nparagraph of\ntext.\n But this\nline '\ + u'should be\nindented') + + self.assertEqual(wordwrap(u'this is a short paragraph of text.\n '\ + u'But this line should be indented',15), u'this is a short\n'\ + u'paragraph of\ntext.\n But this line\nshould be\nindented') + + def test_rjust(self): + self.assertEqual(ljust(u'test', 10), u'test ') + self.assertEqual(ljust(u'test', 3), u'test') + self.assertEqual(rjust(u'test', 10), u' test') + self.assertEqual(rjust(u'test', 3), u'test') + + def test_center(self): + self.assertEqual(center(u'test', 6), u' test ') + + def test_cut(self): + self.assertEqual(cut(u'a string to be mangled', 'a'), + u' string to be mngled') + self.assertEqual(cut(u'a string to be mangled', 'ng'), + u'a stri to be maled') + self.assertEqual(cut(u'a string to be mangled', 'strings'), + u'a string to be mangled') + + def test_force_escape(self): + self.assertEqual( + force_escape(u' here'), + u'<some html & special characters > here') + self.assertEqual( + force_escape(u' here ĐÅ€£'), + u'<some html & special characters > here'\ + u' \u0110\xc5\u20ac\xa3') + + def test_linebreaks(self): + self.assertEqual(linebreaks(u'line 1'), u'

    line 1

    ') + self.assertEqual(linebreaks(u'line 1\nline 2'), + u'

    line 1
    line 2

    ') + + def test_removetags(self): + self.assertEqual(removetags(u'some html with disallowed tags', 'script img'), + u'some html with alert("You smell") disallowed tags') + self.assertEqual(striptags(u'some html with disallowed tags'), + u'some html with alert("You smell") disallowed tags') + + def test_dictsort(self): + sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'}, + {'age': 63, 'name': 'Ra Ra Rasputin'}, + {'name': 'Jonny B Goode', 'age': 18}], 'age') + + self.assertEqual([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')]]) + + def test_dictsortreversed(self): + sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'}, + {'age': 63, 'name': 'Ra Ra Rasputin'}, + {'name': 'Jonny B Goode', 'age': 18}], + 'age') + + self.assertEqual([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')]]) + + def test_first(self): + self.assertEqual(first([0,1,2]), 0) + self.assertEqual(first(u''), u'') + self.assertEqual(first(u'test'), u't') + + def test_join(self): + self.assertEqual(join([0,1,2], u'glue'), u'0glue1glue2') + + def test_length(self): + self.assertEqual(length(u'1234'), 4) + self.assertEqual(length([1,2,3,4]), 4) + self.assertEqual(length_is([], 0), True) + self.assertEqual(length_is([], 1), False) + self.assertEqual(length_is('a', 1), True) + self.assertEqual(length_is(u'a', 10), False) + + def test_slice(self): + self.assertEqual(slice_(u'abcdefg', u'0'), u'') + self.assertEqual(slice_(u'abcdefg', u'1'), u'a') + self.assertEqual(slice_(u'abcdefg', u'-1'), u'abcdef') + self.assertEqual(slice_(u'abcdefg', u'1:2'), u'b') + self.assertEqual(slice_(u'abcdefg', u'1:3'), u'bc') + self.assertEqual(slice_(u'abcdefg', u'0::2'), u'aceg') + + def test_unordered_list(self): + self.assertEqual(unordered_list([u'item 1', u'item 2']), + u'\t
  • item 1
  • \n\t
  • item 2
  • ') + self.assertEqual(unordered_list([u'item 1', [u'item 1.1']]), + u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t
    \n\t
  • ') + + self.assertEqual( + unordered_list([u'item 1', [u'item 1.1', u'item1.2'], u'item 2']), + u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t\t
    • item1.2'\ + u'
    • \n\t
    \n\t
  • \n\t
  • item 2
  • ') + + self.assertEqual( + unordered_list([u'item 1', [u'item 1.1', [u'item 1.1.1', + [u'item 1.1.1.1']]]]), + u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1\n\t\t
        \n\t\t\t
      • '\ + u'item 1.1.1\n\t\t\t
          \n\t\t\t\t
        • item 1.1.1.1
        • \n\t\t\t'\ + u'
        \n\t\t\t
      • \n\t\t
      \n\t\t
    • \n\t
    \n\t
  • ') + + self.assertEqual(unordered_list( + ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]), + u'\t
  • States\n\t
      \n\t\t
    • Kansas\n\t\t
        \n\t\t\t
      • '\ + u'Lawrence
      • \n\t\t\t
      • Topeka
      • \n\t\t
      \n\t\t
    • '\ + u'\n\t\t
    • Illinois
    • \n\t
    \n\t
  • ') + + 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') + self.assertEqual(unordered_list([a,b]), + u'\t
  • ulitem-a
  • \n\t
  • ulitem-b
  • ') + + # Old format for unordered lists should still work + self.assertEqual(unordered_list([u'item 1', []]), u'\t
  • item 1
  • ') + + self.assertEqual(unordered_list([u'item 1', [[u'item 1.1', []]]]), + u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1
    • \n\t
    \n\t
  • ') + + self.assertEqual(unordered_list([u'item 1', [[u'item 1.1', []], + [u'item 1.2', []]]]), u'\t
  • item 1\n\t
      \n\t\t
    • item 1.1'\ + u'
    • \n\t\t
    • item 1.2
    • \n\t
    \n\t
  • ') + + self.assertEqual(unordered_list(['States', [['Kansas', [['Lawrence', + []], ['Topeka', []]]], ['Illinois', []]]]), u'\t
  • States\n\t'\ + u'
      \n\t\t
    • Kansas\n\t\t
        \n\t\t\t
      • Lawrence
      • '\ + u'\n\t\t\t
      • Topeka
      • \n\t\t
      \n\t\t
    • \n\t\t
    • '\ + u'Illinois
    • \n\t
    \n\t
  • ') + + def test_add(self): + self.assertEqual(add(u'1', u'2'), 3) + + def test_get_digit(self): + self.assertEqual(get_digit(123, 1), 3) + self.assertEqual(get_digit(123, 2), 2) + self.assertEqual(get_digit(123, 3), 1) + self.assertEqual(get_digit(123, 4), 0) + self.assertEqual(get_digit(123, 0), 123) + self.assertEqual(get_digit(u'xyz', 0), u'xyz') + + def test_date(self): + # real testing of date() is in dateformat.py + self.assertEqual(date(datetime.datetime(2005, 12, 29), u"d F Y"), + u'29 December 2005') + self.assertEqual(date(datetime.datetime(2005, 12, 29), ur'jS o\f F'), + u'29th of December') + + def test_time(self): + # real testing of time() is done in dateformat.py + self.assertEqual(time(datetime.time(13), u"h"), u'01') + self.assertEqual(time(datetime.time(0), u"h"), u'12') + + def test_timesince(self): + # real testing is done in timesince.py, where we can provide our own 'now' + self.assertEqual( + timesince(datetime.datetime.now() - datetime.timedelta(1)), + u'1 day') + + self.assertEqual( + timesince(datetime.datetime(2005, 12, 29), + datetime.datetime(2005, 12, 30)), + u'1 day') + + def test_timeuntil(self): + self.assertEqual( + timeuntil(datetime.datetime.now() + datetime.timedelta(1)), + u'1 day') + + self.assertEqual(timeuntil(datetime.datetime(2005, 12, 30), + datetime.datetime(2005, 12, 29)), + u'1 day') + + def test_default(self): + self.assertEqual(default(u"val", u"default"), u'val') + self.assertEqual(default(None, u"default"), u'default') + self.assertEqual(default(u'', u"default"), u'default') + + def test_if_none(self): + self.assertEqual(default_if_none(u"val", u"default"), u'val') + self.assertEqual(default_if_none(None, u"default"), u'default') + self.assertEqual(default_if_none(u'', u"default"), u'') + + def test_divisibleby(self): + self.assertEqual(divisibleby(4, 2), True) + self.assertEqual(divisibleby(4, 3), False) + + def test_yesno(self): + self.assertEqual(yesno(True), u'yes') + self.assertEqual(yesno(False), u'no') + self.assertEqual(yesno(None), u'maybe') + self.assertEqual(yesno(True, u'certainly,get out of town,perhaps'), + u'certainly') + self.assertEqual(yesno(False, u'certainly,get out of town,perhaps'), + u'get out of town') + self.assertEqual(yesno(None, u'certainly,get out of town,perhaps'), + u'perhaps') + self.assertEqual(yesno(None, u'certainly,get out of town'), + u'get out of town') + + def test_filesizeformat(self): + self.assertEqual(filesizeformat(1023), u'1023 bytes') + self.assertEqual(filesizeformat(1024), u'1.0 KB') + self.assertEqual(filesizeformat(10*1024), u'10.0 KB') + self.assertEqual(filesizeformat(1024*1024-1), u'1024.0 KB') + self.assertEqual(filesizeformat(1024*1024), u'1.0 MB') + self.assertEqual(filesizeformat(1024*1024*50), u'50.0 MB') + self.assertEqual(filesizeformat(1024*1024*1024-1), u'1024.0 MB') + self.assertEqual(filesizeformat(1024*1024*1024), u'1.0 GB') + self.assertEqual(filesizeformat(1024*1024*1024*1024), u'1.0 TB') + self.assertEqual(filesizeformat(1024*1024*1024*1024*1024), u'1.0 PB') + self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000), + u'2000.0 PB') + self.assertEqual(filesizeformat(complex(1,-1)), u'0 bytes') + self.assertEqual(filesizeformat(""), u'0 bytes') + self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"), + u'0 bytes') + + def test_pluralize(self): + self.assertEqual(pluralize(1), u'') + self.assertEqual(pluralize(0), u's') + self.assertEqual(pluralize(2), u's') + self.assertEqual(pluralize([1]), u'') + self.assertEqual(pluralize([]), u's') + self.assertEqual(pluralize([1,2,3]), u's') + self.assertEqual(pluralize(1,u'es'), u'') + self.assertEqual(pluralize(0,u'es'), u'es') + self.assertEqual(pluralize(2,u'es'), u'es') + self.assertEqual(pluralize(1,u'y,ies'), u'y') + self.assertEqual(pluralize(0,u'y,ies'), u'ies') + self.assertEqual(pluralize(2,u'y,ies'), u'ies') + self.assertEqual(pluralize(0,u'y,ies,error'), u'') + + def test_phone2numeric(self): + self.assertEqual(phone2numeric(u'0800 flowers'), u'0800 3569377') + + def test_non_string_input(self): + # Filters shouldn't break if passed non-strings + self.assertEqual(addslashes(123), u'123') + self.assertEqual(linenumbers(123), u'1. 123') + self.assertEqual(lower(123), u'123') + self.assertEqual(make_list(123), [u'1', u'2', u'3']) + self.assertEqual(slugify(123), u'123') + self.assertEqual(title(123), u'123') + self.assertEqual(truncatewords(123, 2), u'123') + self.assertEqual(upper(123), u'123') + self.assertEqual(urlencode(123), u'123') + self.assertEqual(urlize(123), u'123') + self.assertEqual(urlizetrunc(123, 1), u'123') + self.assertEqual(wordcount(123), 1) + self.assertEqual(wordwrap(123, 2), u'123') + self.assertEqual(ljust('123', 4), u'123 ') + self.assertEqual(rjust('123', 4), u' 123') + self.assertEqual(center('123', 5), u' 123 ') + self.assertEqual(center('123', 6), u' 123 ') + self.assertEqual(cut(123, '2'), u'13') + self.assertEqual(escape(123), u'123') + self.assertEqual(linebreaks(123), u'

    123

    ') + self.assertEqual(linebreaksbr(123), u'123') + self.assertEqual(removetags(123, 'a'), u'123') + self.assertEqual(striptags(123), u'123') +