Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Lib / test / test_userstring.py
blob53114db28513ae1a53c7bb3491577dcf748372a2
1 #!/usr/bin/env python
2 # UserString is a wrapper around the native builtin string type.
3 # UserString instances should behave similar to builtin string objects.
5 import unittest
6 from test import test_support, string_tests
8 from UserString import UserString, MutableString
10 class UserStringTest(
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)
27 self.assertEqual(
28 result,
29 realresult
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
35 self.assertRaises(
36 exc,
37 getattr(object, methodname),
38 *args
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
50 def test_hash(self):
51 pass
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")
57 s[-1] = "bar"
58 self.assertEqual(s, "fobar")
59 s[0] = "bar"
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)
66 del s[-1]
67 self.assertEqual(s, "fo")
68 del s[0]
69 self.assertEqual(s, "o")
70 del s[0]
71 self.assertEqual(s, "")
73 def test_setslice(self):
74 s = self.type2test("foo")
75 s[:] = "bar"
76 self.assertEqual(s, "bar")
77 s[1:2] = "foo"
78 self.assertEqual(s, "bfoor")
79 s[1:-1] = UserString("a")
80 self.assertEqual(s, "bar")
81 s[0:10] = 42
82 self.assertEqual(s, "42")
84 def test_delslice(self):
85 s = self.type2test("foobar")
86 del s[3:10]
87 self.assertEqual(s, "foo")
88 del s[-1:10]
89 self.assertEqual(s, "fo")
91 def test_immutable(self):
92 s = self.type2test("foobar")
93 s2 = s.immutable()
94 self.assertEqual(s, s2)
95 self.assert_(isinstance(s2, UserString))
97 def test_iadd(self):
98 s = self.type2test("foo")
99 s += "bar"
100 self.assertEqual(s, "foobar")
101 s += UserString("baz")
102 self.assertEqual(s, "foobarbaz")
103 s += 42
104 self.assertEqual(s, "foobarbaz42")
106 def test_imul(self):
107 s = self.type2test("foo")
108 s *= 1
109 self.assertEqual(s, "foo")
110 s *= 2
111 self.assertEqual(s, "foofoo")
112 s *= -1
113 self.assertEqual(s, "")
115 def test_main():
116 test_support.run_unittest(UserStringTest, MutableStringTest)
118 if __name__ == "__main__":
119 test_main()