Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1 | """ |
| 2 | Common tests shared by test_str, test_unicode, test_userstring and test_string. |
| 3 | """ |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 5 | import unittest, string, sys |
| 6 | from test import test_support |
Jeremy Hylton | 20f41b6 | 2000-07-11 03:31:55 +0000 | [diff] [blame] | 7 | from UserList import UserList |
| 8 | |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 9 | class Sequence: |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 10 | def __init__(self, seq='wxyz'): self.seq = seq |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 11 | def __len__(self): return len(self.seq) |
| 12 | def __getitem__(self, i): return self.seq[i] |
| 13 | |
| 14 | class BadSeq1(Sequence): |
| 15 | def __init__(self): self.seq = [7, 'hello', 123L] |
| 16 | |
| 17 | class BadSeq2(Sequence): |
| 18 | def __init__(self): self.seq = ['a', 'b', 'c'] |
| 19 | def __len__(self): return 8 |
| 20 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 21 | class CommonTest(unittest.TestCase): |
| 22 | # This testcase contains test that can be used in all |
| 23 | # stringlike classes. Currently this is str, unicode |
| 24 | # UserString and the string module. |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 26 | # The type to be tested |
| 27 | # Change in subclasses to change the behaviour of fixtesttype() |
| 28 | type2test = None |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 29 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 30 | # All tests pass their arguments to the testing methods |
| 31 | # as str objects. fixtesttype() can be used to propagate |
| 32 | # these arguments to the appropriate type |
| 33 | def fixtype(self, obj): |
| 34 | if isinstance(obj, str): |
| 35 | return self.__class__.type2test(obj) |
| 36 | elif isinstance(obj, list): |
| 37 | return [self.fixtype(x) for x in obj] |
| 38 | elif isinstance(obj, tuple): |
| 39 | return tuple([self.fixtype(x) for x in obj]) |
| 40 | elif isinstance(obj, dict): |
| 41 | return dict([ |
| 42 | (self.fixtype(key), self.fixtype(value)) |
| 43 | for (key, value) in obj.iteritems() |
| 44 | ]) |
| 45 | else: |
| 46 | return obj |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 47 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 48 | # check that object.method(*args) returns result |
| 49 | def checkequal(self, result, object, methodname, *args): |
| 50 | result = self.fixtype(result) |
| 51 | object = self.fixtype(object) |
| 52 | args = self.fixtype(args) |
| 53 | realresult = getattr(object, methodname)(*args) |
| 54 | self.assertEqual( |
| 55 | result, |
| 56 | realresult |
| 57 | ) |
| 58 | # if the original is returned make sure that |
| 59 | # this doesn't happen with subclasses |
| 60 | if object == realresult: |
| 61 | class subtype(self.__class__.type2test): |
| 62 | pass |
| 63 | object = subtype(object) |
| 64 | realresult = getattr(object, methodname)(*args) |
| 65 | self.assert_(object is not realresult) |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 66 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 67 | # check that object.method(*args) raises exc |
| 68 | def checkraises(self, exc, object, methodname, *args): |
| 69 | object = self.fixtype(object) |
| 70 | args = self.fixtype(args) |
| 71 | self.assertRaises( |
| 72 | exc, |
| 73 | getattr(object, methodname), |
| 74 | *args |
| 75 | ) |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 76 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 77 | # call object.method(*args) without any checks |
| 78 | def checkcall(self, object, methodname, *args): |
| 79 | object = self.fixtype(object) |
| 80 | args = self.fixtype(args) |
| 81 | getattr(object, methodname)(*args) |
| 82 | |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 83 | def test_hash(self): |
| 84 | # SF bug 1054139: += optimization was not invalidating cached hash value |
| 85 | a = self.type2test('DNSSEC') |
| 86 | b = self.type2test('') |
| 87 | for c in a: |
| 88 | b += c |
| 89 | hash(b) |
| 90 | self.assertEqual(hash(a), hash(b)) |
| 91 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 92 | def test_capitalize(self): |
| 93 | self.checkequal(' hello ', ' hello ', 'capitalize') |
| 94 | self.checkequal('Hello ', 'Hello ','capitalize') |
| 95 | self.checkequal('Hello ', 'hello ','capitalize') |
| 96 | self.checkequal('Aaaa', 'aaaa', 'capitalize') |
| 97 | self.checkequal('Aaaa', 'AaAa', 'capitalize') |
| 98 | |
| 99 | self.checkraises(TypeError, 'hello', 'capitalize', 42) |
| 100 | |
| 101 | def test_count(self): |
| 102 | self.checkequal(3, 'aaa', 'count', 'a') |
| 103 | self.checkequal(0, 'aaa', 'count', 'b') |
| 104 | self.checkequal(3, 'aaa', 'count', 'a') |
| 105 | self.checkequal(0, 'aaa', 'count', 'b') |
| 106 | self.checkequal(3, 'aaa', 'count', 'a') |
| 107 | self.checkequal(0, 'aaa', 'count', 'b') |
| 108 | self.checkequal(0, 'aaa', 'count', 'b') |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame^] | 109 | self.checkequal(2, 'aaa', 'count', 'a', 1) |
| 110 | self.checkequal(0, 'aaa', 'count', 'a', 10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 111 | self.checkequal(1, 'aaa', 'count', 'a', -1) |
| 112 | self.checkequal(3, 'aaa', 'count', 'a', -10) |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame^] | 113 | self.checkequal(1, 'aaa', 'count', 'a', 0, 1) |
| 114 | self.checkequal(3, 'aaa', 'count', 'a', 0, 10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 115 | self.checkequal(2, 'aaa', 'count', 'a', 0, -1) |
| 116 | self.checkequal(0, 'aaa', 'count', 'a', 0, -10) |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame^] | 117 | self.checkequal(3, 'aaa', 'count', '', 1) |
| 118 | self.checkequal(1, 'aaa', 'count', '', 10) |
| 119 | self.checkequal(2, 'aaa', 'count', '', -1) |
| 120 | self.checkequal(4, 'aaa', 'count', '', -10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 121 | |
| 122 | self.checkraises(TypeError, 'hello', 'count') |
| 123 | self.checkraises(TypeError, 'hello', 'count', 42) |
| 124 | |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 125 | # For a variety of combinations, |
| 126 | # verify that str.count() matches an equivalent function |
| 127 | # replacing all occurrences and then differencing the string lengths |
| 128 | charset = ['', 'a', 'b'] |
| 129 | digits = 7 |
| 130 | base = len(charset) |
| 131 | teststrings = set() |
| 132 | for i in xrange(base ** digits): |
| 133 | entry = [] |
| 134 | for j in xrange(digits): |
| 135 | i, m = divmod(i, base) |
| 136 | entry.append(charset[m]) |
| 137 | teststrings.add(''.join(entry)) |
| 138 | teststrings = list(teststrings) |
| 139 | for i in teststrings: |
| 140 | i = self.fixtype(i) |
| 141 | n = len(i) |
| 142 | for j in teststrings: |
| 143 | r1 = i.count(j) |
| 144 | if j: |
| 145 | r2, rem = divmod(n - len(i.replace(j, '')), len(j)) |
| 146 | else: |
| 147 | r2, rem = len(i)+1, 0 |
| 148 | if rem or r1 != r2: |
| 149 | self.assertEqual(rem, 0) |
| 150 | self.assertEqual(r1, r2) |
| 151 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 152 | def test_find(self): |
| 153 | self.checkequal(0, 'abcdefghiabc', 'find', 'abc') |
| 154 | self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 155 | self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4) |
| 156 | |
| 157 | self.checkraises(TypeError, 'hello', 'find') |
| 158 | self.checkraises(TypeError, 'hello', 'find', 42) |
| 159 | |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 160 | # For a variety of combinations, |
| 161 | # verify that str.find() matches __contains__ |
| 162 | # and that the found substring is really at that location |
| 163 | charset = ['', 'a', 'b', 'c'] |
| 164 | digits = 5 |
| 165 | base = len(charset) |
| 166 | teststrings = set() |
| 167 | for i in xrange(base ** digits): |
| 168 | entry = [] |
| 169 | for j in xrange(digits): |
| 170 | i, m = divmod(i, base) |
| 171 | entry.append(charset[m]) |
| 172 | teststrings.add(''.join(entry)) |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 173 | teststrings = list(teststrings) |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 174 | for i in teststrings: |
| 175 | i = self.fixtype(i) |
| 176 | for j in teststrings: |
| 177 | loc = i.find(j) |
| 178 | r1 = (loc != -1) |
| 179 | r2 = j in i |
| 180 | if r1 != r2: |
| 181 | self.assertEqual(r1, r2) |
| 182 | if loc != -1: |
| 183 | self.assertEqual(i[loc:loc+len(j)], j) |
| 184 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 185 | def test_rfind(self): |
| 186 | self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc') |
| 187 | self.checkequal(12, 'abcdefghiabc', 'rfind', '') |
| 188 | self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd') |
| 189 | self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz') |
| 190 | |
| 191 | self.checkraises(TypeError, 'hello', 'rfind') |
| 192 | self.checkraises(TypeError, 'hello', 'rfind', 42) |
| 193 | |
| 194 | def test_index(self): |
| 195 | self.checkequal(0, 'abcdefghiabc', 'index', '') |
| 196 | self.checkequal(3, 'abcdefghiabc', 'index', 'def') |
| 197 | self.checkequal(0, 'abcdefghiabc', 'index', 'abc') |
| 198 | self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 199 | |
| 200 | self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib') |
| 201 | self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1) |
| 202 | self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8) |
| 203 | self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1) |
| 204 | |
| 205 | self.checkraises(TypeError, 'hello', 'index') |
| 206 | self.checkraises(TypeError, 'hello', 'index', 42) |
| 207 | |
| 208 | def test_rindex(self): |
| 209 | self.checkequal(12, 'abcdefghiabc', 'rindex', '') |
| 210 | self.checkequal(3, 'abcdefghiabc', 'rindex', 'def') |
| 211 | self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc') |
| 212 | self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) |
| 213 | |
| 214 | self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib') |
| 215 | self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1) |
| 216 | self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1) |
| 217 | self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8) |
| 218 | self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1) |
| 219 | |
| 220 | self.checkraises(TypeError, 'hello', 'rindex') |
| 221 | self.checkraises(TypeError, 'hello', 'rindex', 42) |
| 222 | |
| 223 | def test_lower(self): |
| 224 | self.checkequal('hello', 'HeLLo', 'lower') |
| 225 | self.checkequal('hello', 'hello', 'lower') |
| 226 | self.checkraises(TypeError, 'hello', 'lower', 42) |
| 227 | |
| 228 | def test_upper(self): |
| 229 | self.checkequal('HELLO', 'HeLLo', 'upper') |
| 230 | self.checkequal('HELLO', 'HELLO', 'upper') |
| 231 | self.checkraises(TypeError, 'hello', 'upper', 42) |
| 232 | |
| 233 | def test_expandtabs(self): |
| 234 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') |
| 235 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) |
| 236 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4) |
| 237 | self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4) |
| 238 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') |
| 239 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) |
| 240 | self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4) |
| 241 | |
| 242 | self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42) |
| 243 | |
| 244 | def test_split(self): |
| 245 | self.checkequal(['this', 'is', 'the', 'split', 'function'], |
| 246 | 'this is the split function', 'split') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 247 | |
| 248 | # by whitespace |
| 249 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 250 | self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1) |
| 251 | self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) |
| 252 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3) |
| 253 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4) |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 254 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, |
| 255 | sys.maxint-1) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 256 | self.checkequal(['a b c d'], 'a b c d', 'split', None, 0) |
Andrew Dalke | 725fe40 | 2006-05-26 16:22:52 +0000 | [diff] [blame] | 257 | self.checkequal(['a b c d'], ' a b c d', 'split', None, 0) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 258 | self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 259 | |
Andrew Dalke | 984b971 | 2006-05-26 11:11:38 +0000 | [diff] [blame] | 260 | self.checkequal([], ' ', 'split') |
| 261 | self.checkequal(['a'], ' a ', 'split') |
| 262 | self.checkequal(['a', 'b'], ' a b ', 'split') |
| 263 | self.checkequal(['a', 'b '], ' a b ', 'split', None, 1) |
| 264 | self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1) |
| 265 | self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2) |
Andrew Dalke | 03fb444 | 2006-05-26 11:15:22 +0000 | [diff] [blame] | 266 | self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 267 | aaa = ' a '*20 |
| 268 | self.checkequal(['a']*20, aaa, 'split') |
| 269 | self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 270 | self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19) |
Andrew Dalke | 984b971 | 2006-05-26 11:11:38 +0000 | [diff] [blame] | 271 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 272 | # by a char |
| 273 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 274 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 275 | self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1) |
| 276 | self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2) |
| 277 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3) |
| 278 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4) |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 279 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', |
| 280 | sys.maxint-2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 281 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) |
| 282 | self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2) |
| 283 | self.checkequal(['endcase ', ''], 'endcase |', 'split', '|') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 284 | self.checkequal(['', ' startcase'], '| startcase', 'split', '|') |
| 285 | self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 286 | self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2) |
| 287 | |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 288 | self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|') |
| 289 | self.checkequal(['a']*15 +['a|a|a|a|a'], |
| 290 | ('a|'*20)[:-1], 'split', '|', 15) |
| 291 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 292 | # by string |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 293 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 294 | self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1) |
| 295 | self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2) |
| 296 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3) |
| 297 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4) |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 298 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', |
| 299 | sys.maxint-10) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 300 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0) |
| 301 | self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 302 | self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 303 | self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test') |
| 304 | self.checkequal(['', ' bothcase ', ''], 'test bothcase test', |
| 305 | 'split', 'test') |
| 306 | self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 307 | self.checkequal(['', ''], 'aaa', 'split', 'aaa') |
| 308 | self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0) |
| 309 | self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba') |
| 310 | self.checkequal(['aaaa'], 'aaaa', 'split', 'aab') |
| 311 | self.checkequal([''], '', 'split', 'aaa') |
| 312 | self.checkequal(['aa'], 'aa', 'split', 'aaa') |
Andrew Dalke | 5cc6009 | 2006-05-26 12:31:00 +0000 | [diff] [blame] | 313 | self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb') |
| 314 | self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 315 | |
| 316 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH') |
| 317 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19) |
| 318 | self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4], |
| 319 | 'split', 'BLAH', 18) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 320 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 321 | # mixed use of str and unicode |
| 322 | self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2) |
| 323 | |
| 324 | # argument type |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 325 | self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) |
| 326 | |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 327 | # null case |
| 328 | self.checkraises(ValueError, 'hello', 'split', '') |
| 329 | self.checkraises(ValueError, 'hello', 'split', '', 0) |
| 330 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 331 | def test_rsplit(self): |
| 332 | self.checkequal(['this', 'is', 'the', 'rsplit', 'function'], |
| 333 | 'this is the rsplit function', 'rsplit') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 334 | |
| 335 | # by whitespace |
| 336 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit') |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 337 | self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1) |
| 338 | self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) |
| 339 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3) |
| 340 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 341 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, |
| 342 | sys.maxint-20) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 343 | self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0) |
Andrew Dalke | 725fe40 | 2006-05-26 16:22:52 +0000 | [diff] [blame] | 344 | self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 345 | self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 346 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 347 | self.checkequal([], ' ', 'rsplit') |
| 348 | self.checkequal(['a'], ' a ', 'rsplit') |
| 349 | self.checkequal(['a', 'b'], ' a b ', 'rsplit') |
| 350 | self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1) |
| 351 | self.checkequal([' a b','c'], ' a b c ', 'rsplit', |
| 352 | None, 1) |
| 353 | self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit', |
| 354 | None, 2) |
| 355 | self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88) |
| 356 | aaa = ' a '*20 |
| 357 | self.checkequal(['a']*20, aaa, 'rsplit') |
| 358 | self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1) |
| 359 | self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18) |
| 360 | |
| 361 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 362 | # by a char |
| 363 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|') |
| 364 | self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1) |
| 365 | self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2) |
| 366 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3) |
| 367 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 368 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', |
| 369 | sys.maxint-100) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 370 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0) |
| 371 | self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2) |
| 372 | self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 373 | self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|') |
| 374 | self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|') |
| 375 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 376 | self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2) |
| 377 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 378 | self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|') |
| 379 | self.checkequal(['a|a|a|a|a']+['a']*15, |
| 380 | ('a|'*20)[:-1], 'rsplit', '|', 15) |
| 381 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 382 | # by string |
| 383 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//') |
| 384 | self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1) |
| 385 | self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2) |
| 386 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3) |
| 387 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 388 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', |
| 389 | sys.maxint-5) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 390 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0) |
| 391 | self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2) |
| 392 | self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 393 | self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test') |
| 394 | self.checkequal(['', ' bothcase ', ''], 'test bothcase test', |
| 395 | 'rsplit', 'test') |
| 396 | self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb') |
| 397 | self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa') |
| 398 | self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0) |
| 399 | self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba') |
| 400 | self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab') |
| 401 | self.checkequal([''], '', 'rsplit', 'aaa') |
| 402 | self.checkequal(['aa'], 'aa', 'rsplit', 'aaa') |
| 403 | self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb') |
| 404 | self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb') |
| 405 | |
| 406 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH') |
| 407 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19) |
| 408 | self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4], |
| 409 | 'rsplit', 'BLAH', 18) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 410 | |
| 411 | # mixed use of str and unicode |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 412 | self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 413 | |
| 414 | # argument type |
| 415 | self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 416 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 417 | # null case |
| 418 | self.checkraises(ValueError, 'hello', 'rsplit', '') |
| 419 | self.checkraises(ValueError, 'hello', 'rsplit', '', 0) |
| 420 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 421 | def test_strip(self): |
| 422 | self.checkequal('hello', ' hello ', 'strip') |
| 423 | self.checkequal('hello ', ' hello ', 'lstrip') |
| 424 | self.checkequal(' hello', ' hello ', 'rstrip') |
| 425 | self.checkequal('hello', 'hello', 'strip') |
| 426 | |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 427 | # strip/lstrip/rstrip with None arg |
| 428 | self.checkequal('hello', ' hello ', 'strip', None) |
| 429 | self.checkequal('hello ', ' hello ', 'lstrip', None) |
| 430 | self.checkequal(' hello', ' hello ', 'rstrip', None) |
| 431 | self.checkequal('hello', 'hello', 'strip', None) |
| 432 | |
| 433 | # strip/lstrip/rstrip with str arg |
| 434 | self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz') |
| 435 | self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz') |
| 436 | self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz') |
| 437 | self.checkequal('hello', 'hello', 'strip', 'xyz') |
| 438 | |
| 439 | # strip/lstrip/rstrip with unicode arg |
| 440 | if test_support.have_unicode: |
| 441 | self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy', |
| 442 | 'strip', unicode('xyz', 'ascii')) |
| 443 | self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy', |
| 444 | 'lstrip', unicode('xyz', 'ascii')) |
| 445 | self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy', |
| 446 | 'rstrip', unicode('xyz', 'ascii')) |
| 447 | self.checkequal(unicode('hello', 'ascii'), 'hello', |
| 448 | 'strip', unicode('xyz', 'ascii')) |
| 449 | |
| 450 | self.checkraises(TypeError, 'hello', 'strip', 42, 42) |
| 451 | self.checkraises(TypeError, 'hello', 'lstrip', 42, 42) |
| 452 | self.checkraises(TypeError, 'hello', 'rstrip', 42, 42) |
| 453 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 454 | def test_ljust(self): |
| 455 | self.checkequal('abc ', 'abc', 'ljust', 10) |
| 456 | self.checkequal('abc ', 'abc', 'ljust', 6) |
| 457 | self.checkequal('abc', 'abc', 'ljust', 3) |
| 458 | self.checkequal('abc', 'abc', 'ljust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 459 | self.checkequal('abc*******', 'abc', 'ljust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 460 | self.checkraises(TypeError, 'abc', 'ljust') |
| 461 | |
| 462 | def test_rjust(self): |
| 463 | self.checkequal(' abc', 'abc', 'rjust', 10) |
| 464 | self.checkequal(' abc', 'abc', 'rjust', 6) |
| 465 | self.checkequal('abc', 'abc', 'rjust', 3) |
| 466 | self.checkequal('abc', 'abc', 'rjust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 467 | self.checkequal('*******abc', 'abc', 'rjust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 468 | self.checkraises(TypeError, 'abc', 'rjust') |
| 469 | |
| 470 | def test_center(self): |
| 471 | self.checkequal(' abc ', 'abc', 'center', 10) |
| 472 | self.checkequal(' abc ', 'abc', 'center', 6) |
| 473 | self.checkequal('abc', 'abc', 'center', 3) |
| 474 | self.checkequal('abc', 'abc', 'center', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 475 | self.checkequal('***abc****', 'abc', 'center', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 476 | self.checkraises(TypeError, 'abc', 'center') |
| 477 | |
| 478 | def test_swapcase(self): |
| 479 | self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase') |
| 480 | |
| 481 | self.checkraises(TypeError, 'hello', 'swapcase', 42) |
| 482 | |
| 483 | def test_replace(self): |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 484 | EQ = self.checkequal |
| 485 | |
| 486 | # Operations on the empty string |
| 487 | EQ("", "", "replace", "", "") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 488 | |
| 489 | #EQ("A", "", "replace", "", "A") |
| 490 | # That was the correct result; this is the result we actually get |
Tim Peters | f404908 | 2006-05-24 21:00:45 +0000 | [diff] [blame] | 491 | # now (for str, but not for unicode): |
| 492 | #EQ("", "", "replace", "", "A") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 493 | |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 494 | EQ("", "", "replace", "A", "") |
| 495 | EQ("", "", "replace", "A", "A") |
| 496 | EQ("", "", "replace", "", "", 100) |
| 497 | EQ("", "", "replace", "", "", sys.maxint) |
| 498 | |
| 499 | # interleave (from=="", 'to' gets inserted everywhere) |
| 500 | EQ("A", "A", "replace", "", "") |
| 501 | EQ("*A*", "A", "replace", "", "*") |
| 502 | EQ("*1A*1", "A", "replace", "", "*1") |
| 503 | EQ("*-#A*-#", "A", "replace", "", "*-#") |
| 504 | EQ("*-A*-A*-", "AA", "replace", "", "*-") |
| 505 | EQ("*-A*-A*-", "AA", "replace", "", "*-", -1) |
| 506 | EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint) |
| 507 | EQ("*-A*-A*-", "AA", "replace", "", "*-", 4) |
| 508 | EQ("*-A*-A*-", "AA", "replace", "", "*-", 3) |
| 509 | EQ("*-A*-A", "AA", "replace", "", "*-", 2) |
| 510 | EQ("*-AA", "AA", "replace", "", "*-", 1) |
| 511 | EQ("AA", "AA", "replace", "", "*-", 0) |
| 512 | |
| 513 | # single character deletion (from=="A", to=="") |
| 514 | EQ("", "A", "replace", "A", "") |
| 515 | EQ("", "AAA", "replace", "A", "") |
| 516 | EQ("", "AAA", "replace", "A", "", -1) |
| 517 | EQ("", "AAA", "replace", "A", "", sys.maxint) |
| 518 | EQ("", "AAA", "replace", "A", "", 4) |
| 519 | EQ("", "AAA", "replace", "A", "", 3) |
| 520 | EQ("A", "AAA", "replace", "A", "", 2) |
| 521 | EQ("AA", "AAA", "replace", "A", "", 1) |
| 522 | EQ("AAA", "AAA", "replace", "A", "", 0) |
| 523 | EQ("", "AAAAAAAAAA", "replace", "A", "") |
| 524 | EQ("BCD", "ABACADA", "replace", "A", "") |
| 525 | EQ("BCD", "ABACADA", "replace", "A", "", -1) |
| 526 | EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint) |
| 527 | EQ("BCD", "ABACADA", "replace", "A", "", 5) |
| 528 | EQ("BCD", "ABACADA", "replace", "A", "", 4) |
| 529 | EQ("BCDA", "ABACADA", "replace", "A", "", 3) |
| 530 | EQ("BCADA", "ABACADA", "replace", "A", "", 2) |
| 531 | EQ("BACADA", "ABACADA", "replace", "A", "", 1) |
| 532 | EQ("ABACADA", "ABACADA", "replace", "A", "", 0) |
| 533 | EQ("BCD", "ABCAD", "replace", "A", "") |
| 534 | EQ("BCD", "ABCADAA", "replace", "A", "") |
| 535 | EQ("BCD", "BCD", "replace", "A", "") |
| 536 | EQ("*************", "*************", "replace", "A", "") |
| 537 | EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999) |
| 538 | |
| 539 | # substring deletion (from=="the", to=="") |
| 540 | EQ("", "the", "replace", "the", "") |
| 541 | EQ("ater", "theater", "replace", "the", "") |
| 542 | EQ("", "thethe", "replace", "the", "") |
| 543 | EQ("", "thethethethe", "replace", "the", "") |
| 544 | EQ("aaaa", "theatheatheathea", "replace", "the", "") |
| 545 | EQ("that", "that", "replace", "the", "") |
| 546 | EQ("thaet", "thaet", "replace", "the", "") |
| 547 | EQ("here and re", "here and there", "replace", "the", "") |
| 548 | EQ("here and re and re", "here and there and there", |
| 549 | "replace", "the", "", sys.maxint) |
| 550 | EQ("here and re and re", "here and there and there", |
| 551 | "replace", "the", "", -1) |
| 552 | EQ("here and re and re", "here and there and there", |
| 553 | "replace", "the", "", 3) |
| 554 | EQ("here and re and re", "here and there and there", |
| 555 | "replace", "the", "", 2) |
| 556 | EQ("here and re and there", "here and there and there", |
| 557 | "replace", "the", "", 1) |
| 558 | EQ("here and there and there", "here and there and there", |
| 559 | "replace", "the", "", 0) |
| 560 | EQ("here and re and re", "here and there and there", "replace", "the", "") |
| 561 | |
| 562 | EQ("abc", "abc", "replace", "the", "") |
| 563 | EQ("abcdefg", "abcdefg", "replace", "the", "") |
| 564 | |
| 565 | # substring deletion (from=="bob", to=="") |
| 566 | EQ("bob", "bbobob", "replace", "bob", "") |
| 567 | EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "") |
| 568 | EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "") |
| 569 | EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 570 | |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 571 | # single character replace in place (len(from)==len(to)==1) |
| 572 | EQ("Who goes there?", "Who goes there?", "replace", "o", "o") |
| 573 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O") |
| 574 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint) |
| 575 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1) |
| 576 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3) |
| 577 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2) |
| 578 | EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1) |
| 579 | EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0) |
| 580 | |
| 581 | EQ("Who goes there?", "Who goes there?", "replace", "a", "q") |
| 582 | EQ("who goes there?", "Who goes there?", "replace", "W", "w") |
| 583 | EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w") |
| 584 | EQ("Who goes there!", "Who goes there?", "replace", "?", "!") |
| 585 | EQ("Who goes there!!", "Who goes there??", "replace", "?", "!") |
| 586 | |
| 587 | EQ("Who goes there?", "Who goes there?", "replace", ".", "!") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 588 | |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 589 | # substring replace in place (len(from)==len(to) > 1) |
| 590 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**") |
| 591 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint) |
| 592 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1) |
| 593 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4) |
| 594 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3) |
| 595 | EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2) |
| 596 | EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1) |
| 597 | EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0) |
| 598 | EQ("cobob", "bobob", "replace", "bob", "cob") |
| 599 | EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob") |
| 600 | EQ("bobob", "bobob", "replace", "bot", "bot") |
| 601 | |
| 602 | # replace single character (len(from)==1, len(to)>1) |
| 603 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK") |
| 604 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1) |
| 605 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint) |
| 606 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2) |
| 607 | EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1) |
| 608 | EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0) |
| 609 | EQ("A----B----C----", "A.B.C.", "replace", ".", "----") |
| 610 | |
| 611 | EQ("Reykjavik", "Reykjavik", "replace", "q", "KK") |
| 612 | |
| 613 | # replace substring (len(from)>1, len(to)!=len(from)) |
| 614 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 615 | "replace", "spam", "ham") |
| 616 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 617 | "replace", "spam", "ham", sys.maxint) |
| 618 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 619 | "replace", "spam", "ham", -1) |
| 620 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 621 | "replace", "spam", "ham", 4) |
| 622 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 623 | "replace", "spam", "ham", 3) |
| 624 | EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam", |
| 625 | "replace", "spam", "ham", 2) |
| 626 | EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam", |
| 627 | "replace", "spam", "ham", 1) |
| 628 | EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam", |
| 629 | "replace", "spam", "ham", 0) |
| 630 | |
| 631 | EQ("bobob", "bobobob", "replace", "bobob", "bob") |
| 632 | EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob") |
| 633 | EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 634 | |
| 635 | # |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 636 | self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 637 | self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '') |
| 638 | self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2) |
| 639 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3) |
| 640 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4) |
| 641 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0) |
| 642 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@') |
| 643 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@') |
| 644 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2) |
| 645 | self.checkequal('-a-b-c-', 'abc', 'replace', '', '-') |
| 646 | self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3) |
| 647 | self.checkequal('abc', 'abc', 'replace', '', '-', 0) |
| 648 | self.checkequal('', '', 'replace', '', '') |
| 649 | self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0) |
| 650 | self.checkequal('abc', 'abc', 'replace', 'xy', '--') |
| 651 | # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with |
| 652 | # MemoryError due to empty result (platform malloc issue when requesting |
| 653 | # 0 bytes). |
| 654 | self.checkequal('', '123', 'replace', '123', '') |
| 655 | self.checkequal('', '123123', 'replace', '123', '') |
| 656 | self.checkequal('x', '123x123', 'replace', '123', '') |
| 657 | |
| 658 | self.checkraises(TypeError, 'hello', 'replace') |
| 659 | self.checkraises(TypeError, 'hello', 'replace', 42) |
| 660 | self.checkraises(TypeError, 'hello', 'replace', 42, 'h') |
| 661 | self.checkraises(TypeError, 'hello', 'replace', 'h', 42) |
| 662 | |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 663 | def test_replace_overflow(self): |
| 664 | # Check for overflow checking on 32 bit machines |
| 665 | if sys.maxint != 2147483647: |
| 666 | return |
| 667 | A2_16 = "A" * (2**16) |
| 668 | self.checkraises(OverflowError, A2_16, "replace", "", A2_16) |
| 669 | self.checkraises(OverflowError, A2_16, "replace", "A", A2_16) |
| 670 | self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16) |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 671 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 672 | def test_zfill(self): |
| 673 | self.checkequal('123', '123', 'zfill', 2) |
| 674 | self.checkequal('123', '123', 'zfill', 3) |
| 675 | self.checkequal('0123', '123', 'zfill', 4) |
| 676 | self.checkequal('+123', '+123', 'zfill', 3) |
| 677 | self.checkequal('+123', '+123', 'zfill', 4) |
| 678 | self.checkequal('+0123', '+123', 'zfill', 5) |
| 679 | self.checkequal('-123', '-123', 'zfill', 3) |
| 680 | self.checkequal('-123', '-123', 'zfill', 4) |
| 681 | self.checkequal('-0123', '-123', 'zfill', 5) |
| 682 | self.checkequal('000', '', 'zfill', 3) |
| 683 | self.checkequal('34', '34', 'zfill', 1) |
| 684 | self.checkequal('0034', '34', 'zfill', 4) |
| 685 | |
| 686 | self.checkraises(TypeError, '123', 'zfill') |
| 687 | |
| 688 | class MixinStrUnicodeUserStringTest: |
| 689 | # additional tests that only work for |
| 690 | # stringlike objects, i.e. str, unicode, UserString |
| 691 | # (but not the string module) |
| 692 | |
| 693 | def test_islower(self): |
| 694 | self.checkequal(False, '', 'islower') |
| 695 | self.checkequal(True, 'a', 'islower') |
| 696 | self.checkequal(False, 'A', 'islower') |
| 697 | self.checkequal(False, '\n', 'islower') |
| 698 | self.checkequal(True, 'abc', 'islower') |
| 699 | self.checkequal(False, 'aBc', 'islower') |
| 700 | self.checkequal(True, 'abc\n', 'islower') |
| 701 | self.checkraises(TypeError, 'abc', 'islower', 42) |
| 702 | |
| 703 | def test_isupper(self): |
| 704 | self.checkequal(False, '', 'isupper') |
| 705 | self.checkequal(False, 'a', 'isupper') |
| 706 | self.checkequal(True, 'A', 'isupper') |
| 707 | self.checkequal(False, '\n', 'isupper') |
| 708 | self.checkequal(True, 'ABC', 'isupper') |
| 709 | self.checkequal(False, 'AbC', 'isupper') |
| 710 | self.checkequal(True, 'ABC\n', 'isupper') |
| 711 | self.checkraises(TypeError, 'abc', 'isupper', 42) |
| 712 | |
| 713 | def test_istitle(self): |
| 714 | self.checkequal(False, '', 'istitle') |
| 715 | self.checkequal(False, 'a', 'istitle') |
| 716 | self.checkequal(True, 'A', 'istitle') |
| 717 | self.checkequal(False, '\n', 'istitle') |
| 718 | self.checkequal(True, 'A Titlecased Line', 'istitle') |
| 719 | self.checkequal(True, 'A\nTitlecased Line', 'istitle') |
| 720 | self.checkequal(True, 'A Titlecased, Line', 'istitle') |
| 721 | self.checkequal(False, 'Not a capitalized String', 'istitle') |
| 722 | self.checkequal(False, 'Not\ta Titlecase String', 'istitle') |
| 723 | self.checkequal(False, 'Not--a Titlecase String', 'istitle') |
| 724 | self.checkequal(False, 'NOT', 'istitle') |
| 725 | self.checkraises(TypeError, 'abc', 'istitle', 42) |
| 726 | |
| 727 | def test_isspace(self): |
| 728 | self.checkequal(False, '', 'isspace') |
| 729 | self.checkequal(False, 'a', 'isspace') |
| 730 | self.checkequal(True, ' ', 'isspace') |
| 731 | self.checkequal(True, '\t', 'isspace') |
| 732 | self.checkequal(True, '\r', 'isspace') |
| 733 | self.checkequal(True, '\n', 'isspace') |
| 734 | self.checkequal(True, ' \t\r\n', 'isspace') |
| 735 | self.checkequal(False, ' \t\r\na', 'isspace') |
| 736 | self.checkraises(TypeError, 'abc', 'isspace', 42) |
| 737 | |
| 738 | def test_isalpha(self): |
| 739 | self.checkequal(False, '', 'isalpha') |
| 740 | self.checkequal(True, 'a', 'isalpha') |
| 741 | self.checkequal(True, 'A', 'isalpha') |
| 742 | self.checkequal(False, '\n', 'isalpha') |
| 743 | self.checkequal(True, 'abc', 'isalpha') |
| 744 | self.checkequal(False, 'aBc123', 'isalpha') |
| 745 | self.checkequal(False, 'abc\n', 'isalpha') |
| 746 | self.checkraises(TypeError, 'abc', 'isalpha', 42) |
| 747 | |
| 748 | def test_isalnum(self): |
| 749 | self.checkequal(False, '', 'isalnum') |
| 750 | self.checkequal(True, 'a', 'isalnum') |
| 751 | self.checkequal(True, 'A', 'isalnum') |
| 752 | self.checkequal(False, '\n', 'isalnum') |
| 753 | self.checkequal(True, '123abc456', 'isalnum') |
| 754 | self.checkequal(True, 'a1b3c', 'isalnum') |
| 755 | self.checkequal(False, 'aBc000 ', 'isalnum') |
| 756 | self.checkequal(False, 'abc\n', 'isalnum') |
| 757 | self.checkraises(TypeError, 'abc', 'isalnum', 42) |
| 758 | |
| 759 | def test_isdigit(self): |
| 760 | self.checkequal(False, '', 'isdigit') |
| 761 | self.checkequal(False, 'a', 'isdigit') |
| 762 | self.checkequal(True, '0', 'isdigit') |
| 763 | self.checkequal(True, '0123456789', 'isdigit') |
| 764 | self.checkequal(False, '0123456789a', 'isdigit') |
| 765 | |
| 766 | self.checkraises(TypeError, 'abc', 'isdigit', 42) |
| 767 | |
| 768 | def test_title(self): |
| 769 | self.checkequal(' Hello ', ' hello ', 'title') |
| 770 | self.checkequal('Hello ', 'hello ', 'title') |
| 771 | self.checkequal('Hello ', 'Hello ', 'title') |
| 772 | self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title') |
| 773 | self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', ) |
| 774 | self.checkequal('Getint', "getInt", 'title') |
| 775 | self.checkraises(TypeError, 'hello', 'title', 42) |
| 776 | |
| 777 | def test_splitlines(self): |
| 778 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines') |
| 779 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines') |
| 780 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines') |
| 781 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines') |
| 782 | self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines') |
| 783 | self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines') |
| 784 | self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1) |
| 785 | |
| 786 | self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) |
| 787 | |
| 788 | def test_startswith(self): |
| 789 | self.checkequal(True, 'hello', 'startswith', 'he') |
| 790 | self.checkequal(True, 'hello', 'startswith', 'hello') |
| 791 | self.checkequal(False, 'hello', 'startswith', 'hello world') |
| 792 | self.checkequal(True, 'hello', 'startswith', '') |
| 793 | self.checkequal(False, 'hello', 'startswith', 'ello') |
| 794 | self.checkequal(True, 'hello', 'startswith', 'ello', 1) |
| 795 | self.checkequal(True, 'hello', 'startswith', 'o', 4) |
| 796 | self.checkequal(False, 'hello', 'startswith', 'o', 5) |
| 797 | self.checkequal(True, 'hello', 'startswith', '', 5) |
| 798 | self.checkequal(False, 'hello', 'startswith', 'lo', 6) |
| 799 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3) |
| 800 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7) |
| 801 | self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6) |
| 802 | |
| 803 | # test negative indices |
| 804 | self.checkequal(True, 'hello', 'startswith', 'he', 0, -1) |
| 805 | self.checkequal(True, 'hello', 'startswith', 'he', -53, -1) |
| 806 | self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1) |
| 807 | self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10) |
| 808 | self.checkequal(False, 'hello', 'startswith', 'ello', -5) |
| 809 | self.checkequal(True, 'hello', 'startswith', 'ello', -4) |
| 810 | self.checkequal(False, 'hello', 'startswith', 'o', -2) |
| 811 | self.checkequal(True, 'hello', 'startswith', 'o', -1) |
| 812 | self.checkequal(True, 'hello', 'startswith', '', -3, -3) |
| 813 | self.checkequal(False, 'hello', 'startswith', 'lo', -9) |
| 814 | |
| 815 | self.checkraises(TypeError, 'hello', 'startswith') |
| 816 | self.checkraises(TypeError, 'hello', 'startswith', 42) |
| 817 | |
| 818 | def test_endswith(self): |
| 819 | self.checkequal(True, 'hello', 'endswith', 'lo') |
| 820 | self.checkequal(False, 'hello', 'endswith', 'he') |
| 821 | self.checkequal(True, 'hello', 'endswith', '') |
| 822 | self.checkequal(False, 'hello', 'endswith', 'hello world') |
| 823 | self.checkequal(False, 'helloworld', 'endswith', 'worl') |
| 824 | self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9) |
| 825 | self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12) |
| 826 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7) |
| 827 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7) |
| 828 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7) |
| 829 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7) |
| 830 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8) |
| 831 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1) |
| 832 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0) |
| 833 | |
| 834 | # test negative indices |
| 835 | self.checkequal(True, 'hello', 'endswith', 'lo', -2) |
| 836 | self.checkequal(False, 'hello', 'endswith', 'he', -2) |
| 837 | self.checkequal(True, 'hello', 'endswith', '', -3, -3) |
| 838 | self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2) |
| 839 | self.checkequal(False, 'helloworld', 'endswith', 'worl', -6) |
| 840 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1) |
| 841 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9) |
| 842 | self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12) |
| 843 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3) |
| 844 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3) |
| 845 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3) |
| 846 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4) |
| 847 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2) |
| 848 | |
| 849 | self.checkraises(TypeError, 'hello', 'endswith') |
| 850 | self.checkraises(TypeError, 'hello', 'endswith', 42) |
| 851 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 852 | def test___contains__(self): |
| 853 | self.checkequal(True, '', '__contains__', '') # vereq('' in '', True) |
| 854 | self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True) |
| 855 | self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False) |
| 856 | self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True) |
| 857 | self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True) |
| 858 | self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True) |
| 859 | self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True) |
| 860 | self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False) |
| 861 | self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False) |
| 862 | |
| 863 | def test_subscript(self): |
| 864 | self.checkequal(u'a', 'abc', '__getitem__', 0) |
| 865 | self.checkequal(u'c', 'abc', '__getitem__', -1) |
| 866 | self.checkequal(u'a', 'abc', '__getitem__', 0L) |
| 867 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3)) |
| 868 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000)) |
| 869 | self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1)) |
| 870 | self.checkequal(u'', 'abc', '__getitem__', slice(0, 0)) |
| 871 | # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice) |
| 872 | |
| 873 | self.checkraises(TypeError, 'abc', '__getitem__', 'def') |
| 874 | |
| 875 | def test_slice(self): |
| 876 | self.checkequal('abc', 'abc', '__getslice__', 0, 1000) |
| 877 | self.checkequal('abc', 'abc', '__getslice__', 0, 3) |
| 878 | self.checkequal('ab', 'abc', '__getslice__', 0, 2) |
| 879 | self.checkequal('bc', 'abc', '__getslice__', 1, 3) |
| 880 | self.checkequal('b', 'abc', '__getslice__', 1, 2) |
| 881 | self.checkequal('', 'abc', '__getslice__', 2, 2) |
| 882 | self.checkequal('', 'abc', '__getslice__', 1000, 1000) |
| 883 | self.checkequal('', 'abc', '__getslice__', 2000, 1000) |
| 884 | self.checkequal('', 'abc', '__getslice__', 2, 1) |
| 885 | # FIXME What about negative indizes? This is handled differently by [] and __getslice__ |
| 886 | |
| 887 | self.checkraises(TypeError, 'abc', '__getslice__', 'def') |
| 888 | |
| 889 | def test_mul(self): |
| 890 | self.checkequal('', 'abc', '__mul__', -1) |
| 891 | self.checkequal('', 'abc', '__mul__', 0) |
| 892 | self.checkequal('abc', 'abc', '__mul__', 1) |
| 893 | self.checkequal('abcabcabc', 'abc', '__mul__', 3) |
| 894 | self.checkraises(TypeError, 'abc', '__mul__') |
| 895 | self.checkraises(TypeError, 'abc', '__mul__', '') |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 896 | # XXX: on a 64-bit system, this doesn't raise an overflow error, |
| 897 | # but either raises a MemoryError, or succeeds (if you have 54TiB) |
| 898 | #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 899 | |
| 900 | def test_join(self): |
| 901 | # join now works with any sequence type |
| 902 | # moved here, because the argument order is |
| 903 | # different in string.join (see the test in |
| 904 | # test.test_string.StringTest.test_join) |
| 905 | self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 906 | self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 907 | self.checkequal('w x y z', ' ', 'join', Sequence()) |
| 908 | self.checkequal('abc', 'a', 'join', ('abc',)) |
| 909 | self.checkequal('z', 'a', 'join', UserList(['z'])) |
| 910 | if test_support.have_unicode: |
| 911 | self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c']) |
| 912 | self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c']) |
| 913 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c']) |
| 914 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')]) |
| 915 | self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3]) |
| 916 | for i in [5, 25, 125]: |
| 917 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 918 | ['a' * i] * i) |
| 919 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 920 | ('a' * i,) * i) |
| 921 | |
| 922 | self.checkraises(TypeError, ' ', 'join', BadSeq1()) |
| 923 | self.checkequal('a b c', ' ', 'join', BadSeq2()) |
| 924 | |
| 925 | self.checkraises(TypeError, ' ', 'join') |
| 926 | self.checkraises(TypeError, ' ', 'join', 7) |
| 927 | self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L])) |
Michael W. Hudson | b2308bb | 2005-10-21 11:45:01 +0000 | [diff] [blame] | 928 | try: |
| 929 | def f(): |
| 930 | yield 4 + "" |
| 931 | self.fixtype(' ').join(f()) |
| 932 | except TypeError, e: |
| 933 | if '+' not in str(e): |
| 934 | self.fail('join() ate exception message') |
| 935 | else: |
| 936 | self.fail('exception not raised') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 937 | |
| 938 | def test_formatting(self): |
| 939 | self.checkequal('+hello+', '+%s+', '__mod__', 'hello') |
| 940 | self.checkequal('+10+', '+%d+', '__mod__', 10) |
| 941 | self.checkequal('a', "%c", '__mod__', "a") |
| 942 | self.checkequal('a', "%c", '__mod__', "a") |
| 943 | self.checkequal('"', "%c", '__mod__', 34) |
| 944 | self.checkequal('$', "%c", '__mod__', 36) |
| 945 | self.checkequal('10', "%d", '__mod__', 10) |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 946 | self.checkequal('\x7f', "%c", '__mod__', 0x7f) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 947 | |
| 948 | for ordinal in (-100, 0x200000): |
| 949 | # unicode raises ValueError, str raises OverflowError |
| 950 | self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal) |
| 951 | |
| 952 | self.checkequal(' 42', '%3ld', '__mod__', 42) |
| 953 | self.checkequal('0042.00', '%07.2f', '__mod__', 42) |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 954 | self.checkequal('0042.00', '%07.2F', '__mod__', 42) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 955 | |
| 956 | self.checkraises(TypeError, 'abc', '__mod__') |
| 957 | self.checkraises(TypeError, '%(foo)s', '__mod__', 42) |
| 958 | self.checkraises(TypeError, '%s%s', '__mod__', (42,)) |
| 959 | self.checkraises(TypeError, '%c', '__mod__', (None,)) |
| 960 | self.checkraises(ValueError, '%(foo', '__mod__', {}) |
| 961 | self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42)) |
| 962 | |
| 963 | # argument names with properly nested brackets are supported |
| 964 | self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'}) |
| 965 | |
| 966 | # 100 is a magic number in PyUnicode_Format, this forces a resize |
| 967 | self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a') |
| 968 | |
| 969 | self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar')) |
| 970 | self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.)) |
| 971 | self.checkraises(ValueError, '%10', '__mod__', (42,)) |
| 972 | |
| 973 | def test_floatformatting(self): |
| 974 | # float formatting |
| 975 | for prec in xrange(100): |
| 976 | format = '%%.%if' % prec |
| 977 | value = 0.01 |
| 978 | for x in xrange(60): |
| 979 | value = value * 3.141592655 / 3.0 * 10.0 |
| 980 | # The formatfloat() code in stringobject.c and |
| 981 | # unicodeobject.c uses a 120 byte buffer and switches from |
| 982 | # 'f' formatting to 'g' at precision 50, so we expect |
| 983 | # OverflowErrors for the ranges x < 50 and prec >= 67. |
| 984 | if x < 50 and prec >= 67: |
| 985 | self.checkraises(OverflowError, format, "__mod__", value) |
| 986 | else: |
| 987 | self.checkcall(format, "__mod__", value) |
| 988 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 989 | def test_inplace_rewrites(self): |
| 990 | # Check that strings don't copy and modify cached single-character strings |
| 991 | self.checkequal('a', 'A', 'lower') |
| 992 | self.checkequal(True, 'A', 'isupper') |
| 993 | self.checkequal('A', 'a', 'upper') |
| 994 | self.checkequal(True, 'a', 'islower') |
Tim Peters | d95d593 | 2006-05-25 21:52:19 +0000 | [diff] [blame] | 995 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 996 | self.checkequal('a', 'A', 'replace', 'A', 'a') |
| 997 | self.checkequal(True, 'A', 'isupper') |
| 998 | |
| 999 | self.checkequal('A', 'a', 'capitalize') |
| 1000 | self.checkequal(True, 'a', 'islower') |
Tim Peters | d95d593 | 2006-05-25 21:52:19 +0000 | [diff] [blame] | 1001 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 1002 | self.checkequal('A', 'a', 'swapcase') |
| 1003 | self.checkequal(True, 'a', 'islower') |
| 1004 | |
| 1005 | self.checkequal('A', 'a', 'title') |
| 1006 | self.checkequal(True, 'a', 'islower') |
| 1007 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1008 | def test_partition(self): |
| 1009 | |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 1010 | self.checkequal(('this is the par', 'ti', 'tion method'), |
| 1011 | 'this is the partition method', 'partition', 'ti') |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1012 | |
| 1013 | # from raymond's original specification |
| 1014 | S = 'http://www.python.org' |
| 1015 | self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://') |
| 1016 | self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?') |
| 1017 | self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://') |
| 1018 | self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org') |
| 1019 | |
| 1020 | self.checkraises(ValueError, S, 'partition', '') |
| 1021 | self.checkraises(TypeError, S, 'partition', None) |
| 1022 | |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 1023 | def test_rpartition(self): |
| 1024 | |
| 1025 | self.checkequal(('this is the rparti', 'ti', 'on method'), |
| 1026 | 'this is the rpartition method', 'rpartition', 'ti') |
| 1027 | |
| 1028 | # from raymond's original specification |
| 1029 | S = 'http://www.python.org' |
| 1030 | self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://') |
| 1031 | self.checkequal(('http://www.python.org', '', ''), S, 'rpartition', '?') |
| 1032 | self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://') |
| 1033 | self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org') |
| 1034 | |
| 1035 | self.checkraises(ValueError, S, 'rpartition', '') |
| 1036 | self.checkraises(TypeError, S, 'rpartition', None) |
| 1037 | |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1038 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1039 | class MixinStrStringUserStringTest: |
| 1040 | # Additional tests for 8bit strings, i.e. str, UserString and |
| 1041 | # the string module |
| 1042 | |
| 1043 | def test_maketrans(self): |
| 1044 | self.assertEqual( |
| 1045 | ''.join(map(chr, xrange(256))).replace('abc', 'xyz'), |
| 1046 | string.maketrans('abc', 'xyz') |
| 1047 | ) |
| 1048 | self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw') |
| 1049 | |
| 1050 | def test_translate(self): |
| 1051 | table = string.maketrans('abc', 'xyz') |
| 1052 | self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def') |
| 1053 | |
| 1054 | table = string.maketrans('a', 'A') |
| 1055 | self.checkequal('Abc', 'abc', 'translate', table) |
| 1056 | self.checkequal('xyz', 'xyz', 'translate', table) |
| 1057 | self.checkequal('yz', 'xyz', 'translate', table, 'x') |
| 1058 | self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip') |
| 1059 | self.checkraises(ValueError, 'xyz', 'translate', 'too short') |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 1060 | |
| 1061 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1062 | class MixinStrUserStringTest: |
| 1063 | # Additional tests that only work with |
| 1064 | # 8bit compatible object, i.e. str and UserString |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 1065 | |
Walter Dörwald | 6eea789 | 2005-07-28 16:49:15 +0000 | [diff] [blame] | 1066 | if test_support.have_unicode: |
| 1067 | def test_encoding_decoding(self): |
| 1068 | codecs = [('rot13', 'uryyb jbeyq'), |
| 1069 | ('base64', 'aGVsbG8gd29ybGQ=\n'), |
| 1070 | ('hex', '68656c6c6f20776f726c64'), |
| 1071 | ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')] |
| 1072 | for encoding, data in codecs: |
| 1073 | self.checkequal(data, 'hello world', 'encode', encoding) |
| 1074 | self.checkequal('hello world', data, 'decode', encoding) |
| 1075 | # zlib is optional, so we make the test optional too... |
| 1076 | try: |
| 1077 | import zlib |
| 1078 | except ImportError: |
| 1079 | pass |
| 1080 | else: |
| 1081 | data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]' |
| 1082 | self.checkequal(data, 'hello world', 'encode', 'zlib') |
| 1083 | self.checkequal('hello world', data, 'decode', 'zlib') |
Walter Dörwald | 97951de | 2003-03-26 14:31:25 +0000 | [diff] [blame] | 1084 | |
Walter Dörwald | 6eea789 | 2005-07-28 16:49:15 +0000 | [diff] [blame] | 1085 | self.checkraises(TypeError, 'xyz', 'decode', 42) |
| 1086 | self.checkraises(TypeError, 'xyz', 'encode', 42) |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1087 | |
| 1088 | |
| 1089 | class MixinStrUnicodeTest: |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1090 | # Additional tests that only work with str and unicode. |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1091 | |
| 1092 | def test_bug1001011(self): |
| 1093 | # Make sure join returns a NEW object for single item sequences |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1094 | # involving a subclass. |
| 1095 | # Make sure that it is of the appropriate type. |
| 1096 | # Check the optimisation still occurs for standard objects. |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1097 | t = self.type2test |
| 1098 | class subclass(t): |
| 1099 | pass |
| 1100 | s1 = subclass("abcd") |
| 1101 | s2 = t().join([s1]) |
| 1102 | self.assert_(s1 is not s2) |
| 1103 | self.assert_(type(s2) is t) |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1104 | |
| 1105 | s1 = t("abcd") |
| 1106 | s2 = t().join([s1]) |
| 1107 | self.assert_(s1 is s2) |
| 1108 | |
| 1109 | # Should also test mixed-type join. |
| 1110 | if t is unicode: |
| 1111 | s1 = subclass("abcd") |
| 1112 | s2 = "".join([s1]) |
| 1113 | self.assert_(s1 is not s2) |
| 1114 | self.assert_(type(s2) is t) |
| 1115 | |
| 1116 | s1 = t("abcd") |
| 1117 | s2 = "".join([s1]) |
| 1118 | self.assert_(s1 is s2) |
| 1119 | |
| 1120 | elif t is str: |
| 1121 | s1 = subclass("abcd") |
| 1122 | s2 = u"".join([s1]) |
| 1123 | self.assert_(s1 is not s2) |
| 1124 | self.assert_(type(s2) is unicode) # promotes! |
| 1125 | |
| 1126 | s1 = t("abcd") |
| 1127 | s2 = u"".join([s1]) |
| 1128 | self.assert_(s1 is not s2) |
| 1129 | self.assert_(type(s2) is unicode) # promotes! |
| 1130 | |
| 1131 | else: |
| 1132 | self.fail("unexpected type for MixinStrUnicodeTest %r" % t) |