PageRenderTime 36ms CodeModel.GetById 29ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/text/tests.py

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