Migrated text doctests. Thanks to Stephan Jaekel.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13894 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
36ac6f1fb9
commit
1bcde8d6b4
|
@ -1,64 +1,81 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
r"""
|
from django.test import TestCase
|
||||||
# Tests for stuff in django.utils.text and other text munging util functions.
|
|
||||||
|
|
||||||
>>> from django.utils.text import *
|
from django.utils.text import *
|
||||||
|
from django.utils.http import urlquote, urlquote_plus, cookie_date, http_date
|
||||||
|
from django.utils.encoding import iri_to_uri
|
||||||
|
|
||||||
### smart_split ###########################################################
|
class TextTests(TestCase):
|
||||||
>>> list(smart_split(r'''This is "a person" test.'''))
|
"""
|
||||||
[u'This', u'is', u'"a person"', u'test.']
|
Tests for stuff in django.utils.text and other text munging util functions.
|
||||||
>>> print list(smart_split(r'''This is "a person's" test.'''))[2]
|
"""
|
||||||
"a person's"
|
|
||||||
>>> print list(smart_split(r'''This is "a person\"s" test.'''))[2]
|
|
||||||
"a person\"s"
|
|
||||||
>>> list(smart_split('''"a 'one'''))
|
|
||||||
[u'"a', u"'one"]
|
|
||||||
>>> print list(smart_split(r'''all friends' tests'''))[1]
|
|
||||||
friends'
|
|
||||||
>>> list(smart_split(u'url search_page words="something else"'))
|
|
||||||
[u'url', u'search_page', u'words="something else"']
|
|
||||||
>>> list(smart_split(u"url search_page words='something else'"))
|
|
||||||
[u'url', u'search_page', u"words='something else'"]
|
|
||||||
>>> list(smart_split(u'url search_page words "something else"'))
|
|
||||||
[u'url', u'search_page', u'words', u'"something else"']
|
|
||||||
>>> list(smart_split(u'url search_page words-"something else"'))
|
|
||||||
[u'url', u'search_page', u'words-"something else"']
|
|
||||||
>>> list(smart_split(u'url search_page words=hello'))
|
|
||||||
[u'url', u'search_page', u'words=hello']
|
|
||||||
>>> list(smart_split(u'url search_page words="something else'))
|
|
||||||
[u'url', u'search_page', u'words="something', u'else']
|
|
||||||
>>> list(smart_split("cut:','|cut:' '"))
|
|
||||||
[u"cut:','|cut:' '"]
|
|
||||||
|
|
||||||
### urlquote #############################################################
|
def test_smart_split(self):
|
||||||
>>> from django.utils.http import urlquote, urlquote_plus
|
|
||||||
>>> urlquote(u'Paris & Orl\xe9ans')
|
|
||||||
u'Paris%20%26%20Orl%C3%A9ans'
|
|
||||||
>>> urlquote(u'Paris & Orl\xe9ans', safe="&")
|
|
||||||
u'Paris%20&%20Orl%C3%A9ans'
|
|
||||||
>>> urlquote_plus(u'Paris & Orl\xe9ans')
|
|
||||||
u'Paris+%26+Orl%C3%A9ans'
|
|
||||||
>>> urlquote_plus(u'Paris & Orl\xe9ans', safe="&")
|
|
||||||
u'Paris+&+Orl%C3%A9ans'
|
|
||||||
|
|
||||||
### cookie_date, http_date ###############################################
|
self.assertEquals(list(smart_split(r'''This is "a person" test.''')),
|
||||||
>>> from django.utils.http import cookie_date, http_date
|
[u'This', u'is', u'"a person"', u'test.'])
|
||||||
>>> t = 1167616461.0
|
|
||||||
>>> cookie_date(t)
|
|
||||||
'Mon, 01-Jan-2007 01:54:21 GMT'
|
|
||||||
>>> http_date(t)
|
|
||||||
'Mon, 01 Jan 2007 01:54:21 GMT'
|
|
||||||
|
|
||||||
### iri_to_uri ###########################################################
|
self.assertEquals(list(smart_split(r'''This is "a person's" test.'''))[2],
|
||||||
>>> from django.utils.encoding import iri_to_uri
|
u'"a person\'s"')
|
||||||
>>> iri_to_uri(u'red%09ros\xe9#red')
|
|
||||||
'red%09ros%C3%A9#red'
|
|
||||||
>>> iri_to_uri(u'/blog/for/J\xfcrgen M\xfcnster/')
|
|
||||||
'/blog/for/J%C3%BCrgen%20M%C3%BCnster/'
|
|
||||||
>>> iri_to_uri(u'locations/%s' % urlquote_plus(u'Paris & Orl\xe9ans'))
|
|
||||||
'locations/Paris+%26+Orl%C3%A9ans'
|
|
||||||
|
|
||||||
iri_to_uri() is idempotent:
|
self.assertEquals(list(smart_split(r'''This is "a person\"s" test.'''))[2],
|
||||||
>>> iri_to_uri(iri_to_uri(u'red%09ros\xe9#red'))
|
u'"a person\\"s"')
|
||||||
'red%09ros%C3%A9#red'
|
|
||||||
"""
|
self.assertEquals(list(smart_split('''"a 'one''')), [u'"a', u"'one"])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(r'''all friends' tests'''))[1],
|
||||||
|
"friends'")
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u'url search_page words="something else"')),
|
||||||
|
[u'url', u'search_page', u'words="something else"'])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u"url search_page words='something else'")),
|
||||||
|
[u'url', u'search_page', u"words='something else'"])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u'url search_page words "something else"')),
|
||||||
|
[u'url', u'search_page', u'words', u'"something else"'])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u'url search_page words-"something else"')),
|
||||||
|
[u'url', u'search_page', u'words-"something else"'])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u'url search_page words=hello')),
|
||||||
|
[u'url', u'search_page', u'words=hello'])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split(u'url search_page words="something else')),
|
||||||
|
[u'url', u'search_page', u'words="something', u'else'])
|
||||||
|
|
||||||
|
self.assertEquals(list(smart_split("cut:','|cut:' '")),
|
||||||
|
[u"cut:','|cut:' '"])
|
||||||
|
|
||||||
|
def test_urlquote(self):
|
||||||
|
|
||||||
|
self.assertEquals(urlquote(u'Paris & Orl\xe9ans'),
|
||||||
|
u'Paris%20%26%20Orl%C3%A9ans')
|
||||||
|
self.assertEquals(urlquote(u'Paris & Orl\xe9ans', safe="&"),
|
||||||
|
u'Paris%20&%20Orl%C3%A9ans')
|
||||||
|
self.assertEquals(urlquote_plus(u'Paris & Orl\xe9ans'),
|
||||||
|
u'Paris+%26+Orl%C3%A9ans')
|
||||||
|
self.assertEquals(urlquote_plus(u'Paris & Orl\xe9ans', safe="&"),
|
||||||
|
u'Paris+&+Orl%C3%A9ans')
|
||||||
|
|
||||||
|
def test_cookie_date(self):
|
||||||
|
t = 1167616461.0
|
||||||
|
self.assertEquals(cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT')
|
||||||
|
|
||||||
|
def test_http_date(self):
|
||||||
|
t = 1167616461.0
|
||||||
|
self.assertEquals(http_date(t), 'Mon, 01 Jan 2007 01:54:21 GMT')
|
||||||
|
|
||||||
|
def test_iri_to_uri(self):
|
||||||
|
self.assertEquals(iri_to_uri(u'red%09ros\xe9#red'),
|
||||||
|
'red%09ros%C3%A9#red')
|
||||||
|
|
||||||
|
self.assertEquals(iri_to_uri(u'/blog/for/J\xfcrgen M\xfcnster/'),
|
||||||
|
'/blog/for/J%C3%BCrgen%20M%C3%BCnster/')
|
||||||
|
|
||||||
|
self.assertEquals(iri_to_uri(u'locations/%s' % urlquote_plus(u'Paris & Orl\xe9ans')),
|
||||||
|
'locations/Paris+%26+Orl%C3%A9ans')
|
||||||
|
|
||||||
|
def test_iri_to_uri_idempotent(self):
|
||||||
|
self.assertEquals(iri_to_uri(iri_to_uri(u'red%09ros\xe9#red')),
|
||||||
|
'red%09ros%C3%A9#red')
|
||||||
|
|
Loading…
Reference in New Issue