summaryrefslogtreecommitdiff
path: root/tests/regressiontests/text/tests.py
blob: e61c14d10315885671747e0061f81335ef63d27e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# coding: utf-8
from __future__ import unicode_literals

from django.test import TestCase
from django.utils.encoding import iri_to_uri
from django.utils.http import (cookie_date, http_date,
    urlquote, urlquote_plus, urlunquote, urlunquote_plus)
from django.utils.text import get_text_list, smart_split
from django.utils.translation import override


class TextTests(TestCase):
    """
    Tests for stuff in django.utils.text and other text munging util functions.
    """

    def test_get_text_list(self):
        self.assertEqual(get_text_list(['a', 'b', 'c', 'd']), 'a, b, c or d')
        self.assertEqual(get_text_list(['a', 'b', 'c'], 'and'), 'a, b and c')
        self.assertEqual(get_text_list(['a', 'b'], 'and'), 'a and b')
        self.assertEqual(get_text_list(['a']), 'a')
        self.assertEqual(get_text_list([]), '')
        with override('ar'):
            self.assertEqual(get_text_list(['a', 'b', 'c']), "a، b أو c")

    def test_smart_split(self):

        self.assertEqual(list(smart_split(r'''This is "a person" test.''')),
            ['This', 'is', '"a person"', 'test.'])

        self.assertEqual(list(smart_split(r'''This is "a person's" test.'''))[2],
            '"a person\'s"')

        self.assertEqual(list(smart_split(r'''This is "a person\"s" test.'''))[2],
            '"a person\\"s"')

        self.assertEqual(list(smart_split('''"a 'one''')), ['"a', "'one"])

        self.assertEqual(list(smart_split(r'''all friends' tests'''))[1],
            "friends'")

        self.assertEqual(list(smart_split('url search_page words="something else"')),
            ['url', 'search_page', 'words="something else"'])

        self.assertEqual(list(smart_split("url search_page words='something else'")),
            ['url', 'search_page', "words='something else'"])

        self.assertEqual(list(smart_split('url search_page words "something else"')),
            ['url', 'search_page', 'words', '"something else"'])

        self.assertEqual(list(smart_split('url search_page words-"something else"')),
            ['url', 'search_page', 'words-"something else"'])

        self.assertEqual(list(smart_split('url search_page words=hello')),
            ['url', 'search_page', 'words=hello'])

        self.assertEqual(list(smart_split('url search_page words="something else')),
            ['url', 'search_page', 'words="something', 'else'])

        self.assertEqual(list(smart_split("cut:','|cut:' '")),
            ["cut:','|cut:' '"])

    def test_urlquote(self):
        self.assertEqual(urlquote('Paris & Orl\xe9ans'),
            'Paris%20%26%20Orl%C3%A9ans')
        self.assertEqual(urlquote('Paris & Orl\xe9ans', safe="&"),
            'Paris%20&%20Orl%C3%A9ans')
        self.assertEqual(
            urlunquote('Paris%20%26%20Orl%C3%A9ans'),
            'Paris & Orl\xe9ans')
        self.assertEqual(
            urlunquote('Paris%20&%20Orl%C3%A9ans'),
            'Paris & Orl\xe9ans')
        self.assertEqual(urlquote_plus('Paris & Orl\xe9ans'),
            'Paris+%26+Orl%C3%A9ans')
        self.assertEqual(urlquote_plus('Paris & Orl\xe9ans', safe="&"),
            'Paris+&+Orl%C3%A9ans')
        self.assertEqual(
            urlunquote_plus('Paris+%26+Orl%C3%A9ans'),
            'Paris & Orl\xe9ans')
        self.assertEqual(
            urlunquote_plus('Paris+&+Orl%C3%A9ans'),
            'Paris & Orl\xe9ans')

    def test_cookie_date(self):
        t = 1167616461.0
        self.assertEqual(cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT')

    def test_http_date(self):
        t = 1167616461.0
        self.assertEqual(http_date(t), 'Mon, 01 Jan 2007 01:54:21 GMT')

    def test_iri_to_uri(self):
        self.assertEqual(iri_to_uri('red%09ros\xe9#red'),
            'red%09ros%C3%A9#red')

        self.assertEqual(iri_to_uri('/blog/for/J\xfcrgen M\xfcnster/'),
            '/blog/for/J%C3%BCrgen%20M%C3%BCnster/')

        self.assertEqual(iri_to_uri('locations/%s' % urlquote_plus('Paris & Orl\xe9ans')),
            'locations/Paris+%26+Orl%C3%A9ans')

    def test_iri_to_uri_idempotent(self):
        self.assertEqual(iri_to_uri(iri_to_uri('red%09ros\xe9#red')),
            'red%09ros%C3%A9#red')