2 # UserString is a wrapper around the native builtin string type.
3 # UserString instances should behave similar to builtin string objects.
6 from test
import test_support
, string_tests
7 from UserString
import UserString
, MutableString
11 string_tests
.CommonTest
,
12 string_tests
.MixinStrUnicodeUserStringTest
,
13 string_tests
.MixinStrStringUserStringTest
,
14 string_tests
.MixinStrUserStringTest
17 type2test
= UserString
19 # Overwrite the three testing methods, because UserString
20 # can't cope with arguments propagated to UserString
21 # (and we don't test with subclasses)
22 def checkequal(self
, result
, object, methodname
, *args
):
23 result
= self
.fixtype(result
)
24 object = self
.fixtype(object)
25 # we don't fix the arguments, because UserString can't cope with it
26 realresult
= getattr(object, methodname
)(*args
)
32 def checkraises(self
, exc
, object, methodname
, *args
):
33 object = self
.fixtype(object)
34 # we don't fix the arguments, because UserString can't cope with it
37 getattr(object, methodname
),
41 def checkcall(self
, object, methodname
, *args
):
42 object = self
.fixtype(object)
43 # we don't fix the arguments, because UserString can't cope with it
44 getattr(object, methodname
)(*args
)
46 class MutableStringTest(UserStringTest
):
47 type2test
= MutableString
49 # MutableStrings can be hashed => deactivate test
53 def test_setitem(self
):
54 s
= self
.type2test("foo")
55 self
.assertRaises(IndexError, s
.__setitem
__, -4, "bar")
56 self
.assertRaises(IndexError, s
.__setitem
__, 3, "bar")
58 self
.assertEqual(s
, "fobar")
60 self
.assertEqual(s
, "barobar")
62 def test_delitem(self
):
63 s
= self
.type2test("foo")
64 self
.assertRaises(IndexError, s
.__delitem
__, -4)
65 self
.assertRaises(IndexError, s
.__delitem
__, 3)
67 self
.assertEqual(s
, "fo")
69 self
.assertEqual(s
, "o")
71 self
.assertEqual(s
, "")
73 def test_setslice(self
):
74 s
= self
.type2test("foo")
76 self
.assertEqual(s
, "bar")
78 self
.assertEqual(s
, "bfoor")
79 s
[1:-1] = UserString("a")
80 self
.assertEqual(s
, "bar")
82 self
.assertEqual(s
, "42")
84 def test_delslice(self
):
85 s
= self
.type2test("foobar")
87 self
.assertEqual(s
, "foo")
89 self
.assertEqual(s
, "fo")
91 def test_extended_set_del_slice(self
):
92 indices
= (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
93 orig
= string
.ascii_letters
+ string
.digits
96 # Use indices[1:] when MutableString can handle real
98 for step
in (None, 1, -1):
99 s
= self
.type2test(orig
)
101 # Make sure we have a slice of exactly the right length,
102 # but with (hopefully) different data.
103 data
= L
[start
:stop
:step
]
105 L
[start
:stop
:step
] = data
106 s
[start
:stop
:step
] = "".join(data
)
107 self
.assertEquals(s
, "".join(L
))
109 del L
[start
:stop
:step
]
110 del s
[start
:stop
:step
]
111 self
.assertEquals(s
, "".join(L
))
113 def test_immutable(self
):
114 s
= self
.type2test("foobar")
116 self
.assertEqual(s
, s2
)
117 self
.assertTrue(isinstance(s2
, UserString
))
120 s
= self
.type2test("foo")
122 self
.assertEqual(s
, "foobar")
123 s
+= UserString("baz")
124 self
.assertEqual(s
, "foobarbaz")
126 self
.assertEqual(s
, "foobarbaz42")
129 s
= self
.type2test("foo")
131 self
.assertEqual(s
, "foo")
133 self
.assertEqual(s
, "foofoo")
135 self
.assertEqual(s
, "")
138 with warnings
.catch_warnings():
139 warnings
.filterwarnings("ignore", ".*MutableString",
141 test_support
.run_unittest(UserStringTest
, MutableStringTest
)
143 if __name__
== "__main__":