|
1 # coding: utf-8 |
|
2 from django.test import TestCase |
|
3 |
|
4 from django.utils.text import * |
|
5 from django.utils.http import urlquote, urlquote_plus, cookie_date, http_date |
|
6 from django.utils.encoding import iri_to_uri |
|
7 |
|
8 class TextTests(TestCase): |
|
9 """ |
|
10 Tests for stuff in django.utils.text and other text munging util functions. |
|
11 """ |
|
12 |
|
13 def test_smart_split(self): |
|
14 |
|
15 self.assertEquals(list(smart_split(r'''This is "a person" test.''')), |
|
16 [u'This', u'is', u'"a person"', u'test.']) |
|
17 |
|
18 self.assertEquals(list(smart_split(r'''This is "a person's" test.'''))[2], |
|
19 u'"a person\'s"') |
|
20 |
|
21 self.assertEquals(list(smart_split(r'''This is "a person\"s" test.'''))[2], |
|
22 u'"a person\\"s"') |
|
23 |
|
24 self.assertEquals(list(smart_split('''"a 'one''')), [u'"a', u"'one"]) |
|
25 |
|
26 self.assertEquals(list(smart_split(r'''all friends' tests'''))[1], |
|
27 "friends'") |
|
28 |
|
29 self.assertEquals(list(smart_split(u'url search_page words="something else"')), |
|
30 [u'url', u'search_page', u'words="something else"']) |
|
31 |
|
32 self.assertEquals(list(smart_split(u"url search_page words='something else'")), |
|
33 [u'url', u'search_page', u"words='something else'"]) |
|
34 |
|
35 self.assertEquals(list(smart_split(u'url search_page words "something else"')), |
|
36 [u'url', u'search_page', u'words', u'"something else"']) |
|
37 |
|
38 self.assertEquals(list(smart_split(u'url search_page words-"something else"')), |
|
39 [u'url', u'search_page', u'words-"something else"']) |
|
40 |
|
41 self.assertEquals(list(smart_split(u'url search_page words=hello')), |
|
42 [u'url', u'search_page', u'words=hello']) |
|
43 |
|
44 self.assertEquals(list(smart_split(u'url search_page words="something else')), |
|
45 [u'url', u'search_page', u'words="something', u'else']) |
|
46 |
|
47 self.assertEquals(list(smart_split("cut:','|cut:' '")), |
|
48 [u"cut:','|cut:' '"]) |
|
49 |
|
50 def test_urlquote(self): |
|
51 |
|
52 self.assertEquals(urlquote(u'Paris & Orl\xe9ans'), |
|
53 u'Paris%20%26%20Orl%C3%A9ans') |
|
54 self.assertEquals(urlquote(u'Paris & Orl\xe9ans', safe="&"), |
|
55 u'Paris%20&%20Orl%C3%A9ans') |
|
56 self.assertEquals(urlquote_plus(u'Paris & Orl\xe9ans'), |
|
57 u'Paris+%26+Orl%C3%A9ans') |
|
58 self.assertEquals(urlquote_plus(u'Paris & Orl\xe9ans', safe="&"), |
|
59 u'Paris+&+Orl%C3%A9ans') |
|
60 |
|
61 def test_cookie_date(self): |
|
62 t = 1167616461.0 |
|
63 self.assertEquals(cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT') |
|
64 |
|
65 def test_http_date(self): |
|
66 t = 1167616461.0 |
|
67 self.assertEquals(http_date(t), 'Mon, 01 Jan 2007 01:54:21 GMT') |
|
68 |
|
69 def test_iri_to_uri(self): |
|
70 self.assertEquals(iri_to_uri(u'red%09ros\xe9#red'), |
|
71 'red%09ros%C3%A9#red') |
|
72 |
|
73 self.assertEquals(iri_to_uri(u'/blog/for/J\xfcrgen M\xfcnster/'), |
|
74 '/blog/for/J%C3%BCrgen%20M%C3%BCnster/') |
|
75 |
|
76 self.assertEquals(iri_to_uri(u'locations/%s' % urlquote_plus(u'Paris & Orl\xe9ans')), |
|
77 'locations/Paris+%26+Orl%C3%A9ans') |
|
78 |
|
79 def test_iri_to_uri_idempotent(self): |
|
80 self.assertEquals(iri_to_uri(iri_to_uri(u'red%09ros\xe9#red')), |
|
81 'red%09ros%C3%A9#red') |