summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_str.py
blob: be26b242f64228f62b76e1a55d0f3bb6aebc43cb (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
import unittest
from test import test_support, string_tests


class StrTest(
    string_tests.CommonTest,
    string_tests.MixinStrUnicodeUserStringTest,
    string_tests.MixinStrUserStringTest,
    string_tests.MixinStrUnicodeTest,
    ):

    type2test = str

    # We don't need to propagate to str
    def fixtype(self, obj):
        return obj

    def test_formatting(self):
        string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
        self.assertRaises(OverflowError, '%c'.__mod__, 0x12341234)

    def test_iterators(self):
        # Make sure str objects have an __iter__ method
        it = "abc".__iter__()
        self.assertEqual(next(it), "a")
        self.assertEqual(next(it), "b")
        self.assertEqual(next(it), "c")
        self.assertRaises(StopIteration, next, it)

    def test_conversion(self):
        # Make sure __str__() behaves properly

        class Foo1:
            def __str__(self):
                return "foo"

        class Foo2(object):
            def __str__(self):
                return "foo"

        class Foo3(object):
            def __str__(self):
                return "foo"

        class Foo4(str8):
            def __str__(self):
                return "foo"

        class Foo5(str):
            def __unicode__(self):
                return "foo"

        class Foo6(str8):
            def __str__(self):
                return "foos"

            def __unicode__(self):
                return "foou"

        class Foo7(str):
            def __str__(self):
                return "foos"
            def __unicode__(self):
                return "foou"

        class Foo8(str):
            def __new__(cls, content=""):
                return str.__new__(cls, 2*content)
            def __str__(self):
                return self

        class Foo9(str8):
            def __str__(self):
                return "string"
            def __unicode__(self):
                return "not unicode"

        self.assertEqual(str(Foo1()), "foo")
        self.assertEqual(str(Foo2()), "foo")
        self.assertEqual(str(Foo3()), "foo")
        self.assertEqual(str(Foo4("bar")), "foo")
        self.assertEqual(str(Foo5("bar")), "foo")
        self.assertEqual(str8(Foo6("bar")), "foos")
        self.assertEqual(str(Foo6("bar")), "foou")
        self.assertEqual(str8(Foo7("bar")), "foos")
        self.assertEqual(str(Foo7("bar")), "foou")
        self.assertEqual(str(Foo8("foo")), "foofoo")
        self.assertEqual(str8(Foo9("foo")), "string")
        self.assertEqual(str(Foo9("foo")), "not unicode")

def test_main():
    test_support.run_unittest(StrTest)

if __name__ == "__main__":
    test_main()