Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 2 | # UserString is a wrapper around the native builtin string type. |
| 3 | # UserString instances should behave similar to builtin string objects. |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 4 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 5 | import string |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 6 | from test import test_support, string_tests |
Walter Dörwald | 3040b19 | 2005-02-17 18:51:48 +0000 | [diff] [blame] | 7 | from UserString import UserString, MutableString |
Brett Cannon | abb34fe | 2008-05-29 05:08:50 +0000 | [diff] [blame] | 8 | import warnings |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 9 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 10 | class UserStringTest( |
| 11 | string_tests.CommonTest, |
| 12 | string_tests.MixinStrUnicodeUserStringTest, |
| 13 | string_tests.MixinStrStringUserStringTest, |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 14 | string_tests.MixinStrUserStringTest |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 15 | ): |
| 16 | |
| 17 | type2test = UserString |
| 18 | |
| 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 |
| 30 | ) |
| 31 | |
| 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 |
| 39 | ) |
| 40 | |
| 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) |
| 45 | |
Walter Dörwald | 3040b19 | 2005-02-17 18:51:48 +0000 | [diff] [blame] | 46 | class MutableStringTest(UserStringTest): |
| 47 | type2test = MutableString |
| 48 | |
| 49 | # MutableStrings can be hashed => deactivate test |
| 50 | def test_hash(self): |
| 51 | pass |
| 52 | |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 53 | def test_setitem(self): |
| 54 | s = self.type2test("foo") |
Walter Dörwald | af3b39a | 2005-02-18 13:22:43 +0000 | [diff] [blame] | 55 | self.assertRaises(IndexError, s.__setitem__, -4, "bar") |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 56 | self.assertRaises(IndexError, s.__setitem__, 3, "bar") |
Walter Dörwald | af3b39a | 2005-02-18 13:22:43 +0000 | [diff] [blame] | 57 | s[-1] = "bar" |
| 58 | self.assertEqual(s, "fobar") |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 59 | s[0] = "bar" |
Walter Dörwald | af3b39a | 2005-02-18 13:22:43 +0000 | [diff] [blame] | 60 | self.assertEqual(s, "barobar") |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 61 | |
| 62 | def test_delitem(self): |
| 63 | s = self.type2test("foo") |
Walter Dörwald | af3b39a | 2005-02-18 13:22:43 +0000 | [diff] [blame] | 64 | self.assertRaises(IndexError, s.__delitem__, -4) |
Walter Dörwald | 1fd4db9 | 2005-02-17 22:31:28 +0000 | [diff] [blame] | 65 | self.assertRaises(IndexError, s.__delitem__, 3) |
Walter Dörwald | af3b39a | 2005-02-18 13:22:43 +0000 | [diff] [blame] | 66 | del s[-1] |
| 67 | self.assertEqual(s, "fo") |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 68 | del s[0] |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 69 | self.assertEqual(s, "o") |
| 70 | del s[0] |
| 71 | self.assertEqual(s, "") |
| 72 | |
| 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") |
| 83 | |
| 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") |
| 90 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 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 |
| 94 | for start in indices: |
| 95 | for stop in indices: |
| 96 | # Use indices[1:] when MutableString can handle real |
| 97 | # extended slices |
| 98 | for step in (None, 1, -1): |
| 99 | s = self.type2test(orig) |
| 100 | L = list(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] |
| 104 | data.reverse() |
| 105 | L[start:stop:step] = data |
| 106 | s[start:stop:step] = "".join(data) |
| 107 | self.assertEquals(s, "".join(L)) |
| 108 | |
| 109 | del L[start:stop:step] |
| 110 | del s[start:stop:step] |
| 111 | self.assertEquals(s, "".join(L)) |
| 112 | |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 113 | def test_immutable(self): |
| 114 | s = self.type2test("foobar") |
| 115 | s2 = s.immutable() |
| 116 | self.assertEqual(s, s2) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame^] | 117 | self.assertTrue(isinstance(s2, UserString)) |
Walter Dörwald | 7f79152 | 2005-02-17 22:03:31 +0000 | [diff] [blame] | 118 | |
| 119 | def test_iadd(self): |
| 120 | s = self.type2test("foo") |
| 121 | s += "bar" |
| 122 | self.assertEqual(s, "foobar") |
| 123 | s += UserString("baz") |
| 124 | self.assertEqual(s, "foobarbaz") |
| 125 | s += 42 |
| 126 | self.assertEqual(s, "foobarbaz42") |
| 127 | |
| 128 | def test_imul(self): |
| 129 | s = self.type2test("foo") |
| 130 | s *= 1 |
| 131 | self.assertEqual(s, "foo") |
| 132 | s *= 2 |
| 133 | self.assertEqual(s, "foofoo") |
| 134 | s *= -1 |
| 135 | self.assertEqual(s, "") |
| 136 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 137 | def test_main(): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 138 | with warnings.catch_warnings(): |
Brett Cannon | abb34fe | 2008-05-29 05:08:50 +0000 | [diff] [blame] | 139 | warnings.filterwarnings("ignore", ".*MutableString", |
| 140 | DeprecationWarning) |
| 141 | test_support.run_unittest(UserStringTest, MutableStringTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 142 | |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 143 | if __name__ == "__main__": |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 144 | test_main() |