1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#!/usr/bin/env python
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# UserString is a wrapper around the native builtin string type.
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# UserString instances should behave similar to builtin string objects.
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport string
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support, string_tests
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom UserString import UserString, MutableString
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport warnings
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass UserStringTest(
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    string_tests.CommonTest,
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    string_tests.MixinStrUnicodeUserStringTest,
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    string_tests.MixinStrStringUserStringTest,
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    string_tests.MixinStrUserStringTest
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ):
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    type2test = UserString
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Overwrite the three testing methods, because UserString
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # can't cope with arguments propagated to UserString
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # (and we don't test with subclasses)
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def checkequal(self, result, object, methodname, *args):
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        result = self.fixtype(result)
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        object = self.fixtype(object)
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # we don't fix the arguments, because UserString can't cope with it
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        realresult = getattr(object, methodname)(*args)
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            result,
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            realresult
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        )
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def checkraises(self, exc, object, methodname, *args):
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        object = self.fixtype(object)
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # we don't fix the arguments, because UserString can't cope with it
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            exc,
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            getattr(object, methodname),
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            *args
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        )
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def checkcall(self, object, methodname, *args):
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        object = self.fixtype(object)
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # we don't fix the arguments, because UserString can't cope with it
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        getattr(object, methodname)(*args)
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MutableStringTest(UserStringTest):
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    type2test = MutableString
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # MutableStrings can be hashed => deactivate test
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_hash(self):
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_setitem(self):
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foo")
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(IndexError, s.__setitem__, -4, "bar")
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(IndexError, s.__setitem__, 3, "bar")
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[-1] = "bar"
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "fobar")
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[0] = "bar"
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "barobar")
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_delitem(self):
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foo")
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(IndexError, s.__delitem__, -4)
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(IndexError, s.__delitem__, 3)
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del s[-1]
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "fo")
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del s[0]
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "o")
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del s[0]
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "")
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_setslice(self):
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foo")
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[:] = "bar"
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "bar")
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[1:2] = "foo"
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "bfoor")
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[1:-1] = UserString("a")
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "bar")
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s[0:10] = 42
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "42")
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_delslice(self):
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foobar")
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del s[3:10]
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foo")
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del s[-1:10]
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "fo")
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_extended_set_del_slice(self):
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        orig = string.ascii_letters + string.digits
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for start in indices:
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for stop in indices:
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Use indices[1:] when MutableString can handle real
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # extended slices
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for step in (None, 1, -1):
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    s = self.type2test(orig)
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    L = list(orig)
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Make sure we have a slice of exactly the right length,
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # but with (hopefully) different data.
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data = L[start:stop:step]
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data.reverse()
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    L[start:stop:step] = data
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    s[start:stop:step] = "".join(data)
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(s, "".join(L))
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del L[start:stop:step]
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del s[start:stop:step]
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(s, "".join(L))
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_immutable(self):
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foobar")
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s2 = s.immutable()
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, s2)
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(s2, UserString)
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_iadd(self):
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foo")
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s += "bar"
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foobar")
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s += UserString("baz")
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foobarbaz")
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s += 42
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foobarbaz42")
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_imul(self):
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.type2test("foo")
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s *= 1
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foo")
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s *= 2
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "foofoo")
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s *= -1
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "")
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with warnings.catch_warnings():
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        warnings.filterwarnings("ignore", ".*MutableString has been removed",
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                DeprecationWarning)
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        warnings.filterwarnings("ignore",
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                ".*__(get|set|del)slice__ has been removed",
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                DeprecationWarning)
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.run_unittest(UserStringTest, MutableStringTest)
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
148