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 | |
Kristján Valur Jónsson | 170eee9 | 2007-05-03 20:09:56 +0000 | [diff] [blame] | 5 | import unittest, string, sys, struct |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 65 | self.assertTrue(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 |
Benjamin Peterson | 1643d5c | 2014-09-28 12:48:46 -0400 | [diff] [blame] | 68 | def checkraises(self, exc, obj, methodname, *args): |
| 69 | obj = self.fixtype(obj) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 70 | args = self.fixtype(args) |
Benjamin Peterson | 1643d5c | 2014-09-28 12:48:46 -0400 | [diff] [blame] | 71 | with self.assertRaises(exc) as cm: |
| 72 | getattr(obj, methodname)(*args) |
Terry Jan Reedy | c0dc65e | 2014-10-12 22:00:10 -0400 | [diff] [blame^] | 73 | self.assertNotEqual(cm.exception.args[0], '') |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 74 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 75 | # call object.method(*args) without any checks |
| 76 | def checkcall(self, object, methodname, *args): |
| 77 | object = self.fixtype(object) |
| 78 | args = self.fixtype(args) |
| 79 | getattr(object, methodname)(*args) |
| 80 | |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 81 | def test_hash(self): |
| 82 | # SF bug 1054139: += optimization was not invalidating cached hash value |
| 83 | a = self.type2test('DNSSEC') |
| 84 | b = self.type2test('') |
| 85 | for c in a: |
| 86 | b += c |
| 87 | hash(b) |
| 88 | self.assertEqual(hash(a), hash(b)) |
| 89 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 90 | def test_capitalize(self): |
| 91 | self.checkequal(' hello ', ' hello ', 'capitalize') |
| 92 | self.checkequal('Hello ', 'Hello ','capitalize') |
| 93 | self.checkequal('Hello ', 'hello ','capitalize') |
| 94 | self.checkequal('Aaaa', 'aaaa', 'capitalize') |
| 95 | self.checkequal('Aaaa', 'AaAa', 'capitalize') |
| 96 | |
| 97 | self.checkraises(TypeError, 'hello', 'capitalize', 42) |
| 98 | |
| 99 | def test_count(self): |
| 100 | self.checkequal(3, 'aaa', 'count', 'a') |
| 101 | self.checkequal(0, 'aaa', 'count', 'b') |
| 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(0, 'aaa', 'count', 'b') |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame] | 107 | self.checkequal(2, 'aaa', 'count', 'a', 1) |
| 108 | self.checkequal(0, 'aaa', 'count', 'a', 10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 109 | self.checkequal(1, 'aaa', 'count', 'a', -1) |
| 110 | self.checkequal(3, 'aaa', 'count', 'a', -10) |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame] | 111 | self.checkequal(1, 'aaa', 'count', 'a', 0, 1) |
| 112 | self.checkequal(3, 'aaa', 'count', 'a', 0, 10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 113 | self.checkequal(2, 'aaa', 'count', 'a', 0, -1) |
| 114 | self.checkequal(0, 'aaa', 'count', 'a', 0, -10) |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame] | 115 | self.checkequal(3, 'aaa', 'count', '', 1) |
Fredrik Lundh | 9e9ef9f | 2006-05-30 17:39:58 +0000 | [diff] [blame] | 116 | self.checkequal(1, 'aaa', 'count', '', 3) |
| 117 | self.checkequal(0, 'aaa', 'count', '', 10) |
Fredrik Lundh | b51b470 | 2006-05-29 22:42:07 +0000 | [diff] [blame] | 118 | self.checkequal(2, 'aaa', 'count', '', -1) |
| 119 | self.checkequal(4, 'aaa', 'count', '', -10) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 120 | |
Amaury Forgeot d'Arc | fc5ea39 | 2008-09-26 22:34:08 +0000 | [diff] [blame] | 121 | self.checkequal(1, '', 'count', '') |
| 122 | self.checkequal(0, '', 'count', '', 1, 1) |
| 123 | self.checkequal(0, '', 'count', '', sys.maxint, 0) |
| 124 | |
| 125 | self.checkequal(0, '', 'count', 'xx') |
| 126 | self.checkequal(0, '', 'count', 'xx', 1, 1) |
| 127 | self.checkequal(0, '', 'count', 'xx', sys.maxint, 0) |
| 128 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 129 | self.checkraises(TypeError, 'hello', 'count') |
| 130 | self.checkraises(TypeError, 'hello', 'count', 42) |
| 131 | |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 132 | # For a variety of combinations, |
| 133 | # verify that str.count() matches an equivalent function |
| 134 | # replacing all occurrences and then differencing the string lengths |
| 135 | charset = ['', 'a', 'b'] |
| 136 | digits = 7 |
| 137 | base = len(charset) |
| 138 | teststrings = set() |
| 139 | for i in xrange(base ** digits): |
| 140 | entry = [] |
| 141 | for j in xrange(digits): |
| 142 | i, m = divmod(i, base) |
| 143 | entry.append(charset[m]) |
| 144 | teststrings.add(''.join(entry)) |
| 145 | teststrings = list(teststrings) |
| 146 | for i in teststrings: |
| 147 | i = self.fixtype(i) |
| 148 | n = len(i) |
| 149 | for j in teststrings: |
| 150 | r1 = i.count(j) |
| 151 | if j: |
| 152 | r2, rem = divmod(n - len(i.replace(j, '')), len(j)) |
| 153 | else: |
| 154 | r2, rem = len(i)+1, 0 |
| 155 | if rem or r1 != r2: |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 156 | self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i)) |
| 157 | self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i)) |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 158 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 159 | def test_find(self): |
| 160 | self.checkequal(0, 'abcdefghiabc', 'find', 'abc') |
| 161 | self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 162 | self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4) |
| 163 | |
Fredrik Lundh | 93eff6f | 2006-05-30 17:11:48 +0000 | [diff] [blame] | 164 | self.checkequal(0, 'abc', 'find', '', 0) |
| 165 | self.checkequal(3, 'abc', 'find', '', 3) |
| 166 | self.checkequal(-1, 'abc', 'find', '', 4) |
| 167 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 168 | # to check the ability to pass None as defaults |
| 169 | self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a') |
| 170 | self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4) |
| 171 | self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6) |
| 172 | self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None) |
| 173 | self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6) |
| 174 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 175 | self.checkraises(TypeError, 'hello', 'find') |
| 176 | self.checkraises(TypeError, 'hello', 'find', 42) |
| 177 | |
Amaury Forgeot d'Arc | fc5ea39 | 2008-09-26 22:34:08 +0000 | [diff] [blame] | 178 | self.checkequal(0, '', 'find', '') |
| 179 | self.checkequal(-1, '', 'find', '', 1, 1) |
| 180 | self.checkequal(-1, '', 'find', '', sys.maxint, 0) |
| 181 | |
| 182 | self.checkequal(-1, '', 'find', 'xx') |
| 183 | self.checkequal(-1, '', 'find', 'xx', 1, 1) |
| 184 | self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0) |
| 185 | |
Antoine Pitrou | 83f86e8 | 2010-01-02 21:47:10 +0000 | [diff] [blame] | 186 | # issue 7458 |
| 187 | self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0) |
| 188 | |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 189 | # For a variety of combinations, |
| 190 | # verify that str.find() matches __contains__ |
| 191 | # and that the found substring is really at that location |
| 192 | charset = ['', 'a', 'b', 'c'] |
| 193 | digits = 5 |
| 194 | base = len(charset) |
| 195 | teststrings = set() |
| 196 | for i in xrange(base ** digits): |
| 197 | entry = [] |
| 198 | for j in xrange(digits): |
| 199 | i, m = divmod(i, base) |
| 200 | entry.append(charset[m]) |
| 201 | teststrings.add(''.join(entry)) |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 202 | teststrings = list(teststrings) |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 203 | for i in teststrings: |
| 204 | i = self.fixtype(i) |
| 205 | for j in teststrings: |
| 206 | loc = i.find(j) |
| 207 | r1 = (loc != -1) |
| 208 | r2 = j in i |
Antoine Pitrou | b538d54 | 2010-01-02 21:53:44 +0000 | [diff] [blame] | 209 | self.assertEqual(r1, r2) |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 210 | if loc != -1: |
| 211 | self.assertEqual(i[loc:loc+len(j)], j) |
| 212 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 213 | def test_rfind(self): |
| 214 | self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc') |
| 215 | self.checkequal(12, 'abcdefghiabc', 'rfind', '') |
| 216 | self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd') |
| 217 | self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz') |
| 218 | |
Fredrik Lundh | 93eff6f | 2006-05-30 17:11:48 +0000 | [diff] [blame] | 219 | self.checkequal(3, 'abc', 'rfind', '', 0) |
| 220 | self.checkequal(3, 'abc', 'rfind', '', 3) |
| 221 | self.checkequal(-1, 'abc', 'rfind', '', 4) |
| 222 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 223 | # to check the ability to pass None as defaults |
| 224 | self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a') |
| 225 | self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4) |
| 226 | self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6) |
| 227 | self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None) |
| 228 | self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6) |
| 229 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 230 | self.checkraises(TypeError, 'hello', 'rfind') |
| 231 | self.checkraises(TypeError, 'hello', 'rfind', 42) |
| 232 | |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 233 | # For a variety of combinations, |
| 234 | # verify that str.rfind() matches __contains__ |
| 235 | # and that the found substring is really at that location |
| 236 | charset = ['', 'a', 'b', 'c'] |
| 237 | digits = 5 |
| 238 | base = len(charset) |
| 239 | teststrings = set() |
| 240 | for i in xrange(base ** digits): |
| 241 | entry = [] |
| 242 | for j in xrange(digits): |
| 243 | i, m = divmod(i, base) |
| 244 | entry.append(charset[m]) |
| 245 | teststrings.add(''.join(entry)) |
| 246 | teststrings = list(teststrings) |
| 247 | for i in teststrings: |
| 248 | i = self.fixtype(i) |
| 249 | for j in teststrings: |
| 250 | loc = i.rfind(j) |
| 251 | r1 = (loc != -1) |
| 252 | r2 = j in i |
Antoine Pitrou | b538d54 | 2010-01-02 21:53:44 +0000 | [diff] [blame] | 253 | self.assertEqual(r1, r2) |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 254 | if loc != -1: |
Florent Xicluna | c0c0b14 | 2010-09-13 08:53:00 +0000 | [diff] [blame] | 255 | self.assertEqual(i[loc:loc+len(j)], self.fixtype(j)) |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | 83f86e8 | 2010-01-02 21:47:10 +0000 | [diff] [blame] | 257 | # issue 7458 |
| 258 | self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0) |
| 259 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 260 | def test_index(self): |
| 261 | self.checkequal(0, 'abcdefghiabc', 'index', '') |
| 262 | self.checkequal(3, 'abcdefghiabc', 'index', 'def') |
| 263 | self.checkequal(0, 'abcdefghiabc', 'index', 'abc') |
| 264 | self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 265 | |
| 266 | self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib') |
| 267 | self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1) |
| 268 | self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8) |
| 269 | self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1) |
| 270 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 271 | # to check the ability to pass None as defaults |
| 272 | self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a') |
| 273 | self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4) |
| 274 | self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6) |
| 275 | self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None) |
| 276 | self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6) |
| 277 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 278 | self.checkraises(TypeError, 'hello', 'index') |
| 279 | self.checkraises(TypeError, 'hello', 'index', 42) |
| 280 | |
| 281 | def test_rindex(self): |
| 282 | self.checkequal(12, 'abcdefghiabc', 'rindex', '') |
| 283 | self.checkequal(3, 'abcdefghiabc', 'rindex', 'def') |
| 284 | self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc') |
| 285 | self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) |
| 286 | |
| 287 | self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib') |
| 288 | self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1) |
| 289 | self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1) |
| 290 | self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8) |
| 291 | self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1) |
| 292 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 293 | # to check the ability to pass None as defaults |
| 294 | self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a') |
| 295 | self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4) |
| 296 | self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6) |
| 297 | self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None) |
| 298 | self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6) |
| 299 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 300 | self.checkraises(TypeError, 'hello', 'rindex') |
| 301 | self.checkraises(TypeError, 'hello', 'rindex', 42) |
| 302 | |
| 303 | def test_lower(self): |
| 304 | self.checkequal('hello', 'HeLLo', 'lower') |
| 305 | self.checkequal('hello', 'hello', 'lower') |
| 306 | self.checkraises(TypeError, 'hello', 'lower', 42) |
| 307 | |
| 308 | def test_upper(self): |
| 309 | self.checkequal('HELLO', 'HeLLo', 'upper') |
| 310 | self.checkequal('HELLO', 'HELLO', 'upper') |
| 311 | self.checkraises(TypeError, 'hello', 'upper', 42) |
| 312 | |
| 313 | def test_expandtabs(self): |
| 314 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') |
| 315 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) |
| 316 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4) |
| 317 | self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4) |
| 318 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') |
| 319 | self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) |
| 320 | self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4) |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 321 | self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 322 | |
| 323 | self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42) |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 324 | # This test is only valid when sizeof(int) == sizeof(void*) == 4. |
| 325 | if sys.maxint < (1 << 32) and struct.calcsize('P') == 4: |
| 326 | self.checkraises(OverflowError, |
| 327 | '\ta\n\tb', 'expandtabs', sys.maxint) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 328 | |
| 329 | def test_split(self): |
| 330 | self.checkequal(['this', 'is', 'the', 'split', 'function'], |
| 331 | 'this is the split function', 'split') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 332 | |
| 333 | # by whitespace |
| 334 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 335 | self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1) |
| 336 | self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) |
| 337 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3) |
| 338 | 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] | 339 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, |
| 340 | sys.maxint-1) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 341 | 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] | 342 | 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] | 343 | 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] | 344 | |
Andrew Dalke | 984b971 | 2006-05-26 11:11:38 +0000 | [diff] [blame] | 345 | self.checkequal([], ' ', 'split') |
| 346 | self.checkequal(['a'], ' a ', 'split') |
| 347 | self.checkequal(['a', 'b'], ' a b ', 'split') |
| 348 | self.checkequal(['a', 'b '], ' a b ', 'split', None, 1) |
| 349 | self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1) |
| 350 | self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2) |
Andrew Dalke | 03fb444 | 2006-05-26 11:15:22 +0000 | [diff] [blame] | 351 | self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 352 | aaa = ' a '*20 |
| 353 | self.checkequal(['a']*20, aaa, 'split') |
| 354 | self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 355 | self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19) |
Andrew Dalke | 984b971 | 2006-05-26 11:11:38 +0000 | [diff] [blame] | 356 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 357 | # by a char |
| 358 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 359 | 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] | 360 | self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1) |
| 361 | self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2) |
| 362 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3) |
| 363 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4) |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 364 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', |
| 365 | sys.maxint-2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 366 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) |
| 367 | self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2) |
| 368 | self.checkequal(['endcase ', ''], 'endcase |', 'split', '|') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 369 | self.checkequal(['', ' startcase'], '| startcase', 'split', '|') |
| 370 | self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 371 | self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2) |
| 372 | |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 373 | self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|') |
| 374 | self.checkequal(['a']*15 +['a|a|a|a|a'], |
| 375 | ('a|'*20)[:-1], 'split', '|', 15) |
| 376 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 377 | # by string |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 378 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 379 | self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1) |
| 380 | self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2) |
| 381 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3) |
| 382 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4) |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 383 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', |
| 384 | sys.maxint-10) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 385 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0) |
| 386 | 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] | 387 | self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 388 | self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test') |
| 389 | self.checkequal(['', ' bothcase ', ''], 'test bothcase test', |
| 390 | 'split', 'test') |
| 391 | self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 392 | self.checkequal(['', ''], 'aaa', 'split', 'aaa') |
| 393 | self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0) |
| 394 | self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba') |
| 395 | self.checkequal(['aaaa'], 'aaaa', 'split', 'aab') |
| 396 | self.checkequal([''], '', 'split', 'aaa') |
| 397 | self.checkequal(['aa'], 'aa', 'split', 'aaa') |
Andrew Dalke | 5cc6009 | 2006-05-26 12:31:00 +0000 | [diff] [blame] | 398 | self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb') |
| 399 | self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb') |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 400 | |
| 401 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH') |
| 402 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19) |
| 403 | self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4], |
| 404 | 'split', 'BLAH', 18) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 405 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 406 | # mixed use of str and unicode |
| 407 | self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2) |
| 408 | |
| 409 | # argument type |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 410 | self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) |
| 411 | |
Andrew Dalke | 005aee2 | 2006-05-26 12:28:15 +0000 | [diff] [blame] | 412 | # null case |
| 413 | self.checkraises(ValueError, 'hello', 'split', '') |
| 414 | self.checkraises(ValueError, 'hello', 'split', '', 0) |
| 415 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 416 | def test_rsplit(self): |
| 417 | self.checkequal(['this', 'is', 'the', 'rsplit', 'function'], |
| 418 | 'this is the rsplit function', 'rsplit') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 419 | |
| 420 | # by whitespace |
| 421 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit') |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 422 | self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1) |
| 423 | self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) |
| 424 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3) |
| 425 | 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] | 426 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, |
| 427 | sys.maxint-20) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 428 | 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] | 429 | 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] | 430 | 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] | 431 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 432 | self.checkequal([], ' ', 'rsplit') |
| 433 | self.checkequal(['a'], ' a ', 'rsplit') |
| 434 | self.checkequal(['a', 'b'], ' a b ', 'rsplit') |
| 435 | self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1) |
| 436 | self.checkequal([' a b','c'], ' a b c ', 'rsplit', |
| 437 | None, 1) |
| 438 | self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit', |
| 439 | None, 2) |
| 440 | self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88) |
| 441 | aaa = ' a '*20 |
| 442 | self.checkequal(['a']*20, aaa, 'rsplit') |
| 443 | self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1) |
| 444 | self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18) |
| 445 | |
| 446 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 447 | # by a char |
| 448 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|') |
| 449 | self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1) |
| 450 | self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2) |
| 451 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3) |
| 452 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 453 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', |
| 454 | sys.maxint-100) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 455 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0) |
| 456 | self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2) |
| 457 | self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 458 | self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|') |
| 459 | self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|') |
| 460 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 461 | self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2) |
| 462 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 463 | self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|') |
| 464 | self.checkequal(['a|a|a|a|a']+['a']*15, |
| 465 | ('a|'*20)[:-1], 'rsplit', '|', 15) |
| 466 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 467 | # by string |
| 468 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//') |
| 469 | self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1) |
| 470 | self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2) |
| 471 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3) |
| 472 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4) |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 473 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', |
| 474 | sys.maxint-5) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 475 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0) |
| 476 | self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2) |
| 477 | self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test') |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 478 | self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test') |
| 479 | self.checkequal(['', ' bothcase ', ''], 'test bothcase test', |
| 480 | 'rsplit', 'test') |
| 481 | self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb') |
| 482 | self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa') |
| 483 | self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0) |
| 484 | self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba') |
| 485 | self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab') |
| 486 | self.checkequal([''], '', 'rsplit', 'aaa') |
| 487 | self.checkequal(['aa'], 'aa', 'rsplit', 'aaa') |
| 488 | self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb') |
| 489 | self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb') |
| 490 | |
| 491 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH') |
| 492 | self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19) |
| 493 | self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4], |
| 494 | 'rsplit', 'BLAH', 18) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 495 | |
| 496 | # mixed use of str and unicode |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 497 | 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] | 498 | |
| 499 | # argument type |
| 500 | self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 501 | |
Andrew Dalke | 669fa18 | 2006-05-26 13:05:55 +0000 | [diff] [blame] | 502 | # null case |
| 503 | self.checkraises(ValueError, 'hello', 'rsplit', '') |
| 504 | self.checkraises(ValueError, 'hello', 'rsplit', '', 0) |
| 505 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 506 | def test_strip(self): |
| 507 | self.checkequal('hello', ' hello ', 'strip') |
| 508 | self.checkequal('hello ', ' hello ', 'lstrip') |
| 509 | self.checkequal(' hello', ' hello ', 'rstrip') |
| 510 | self.checkequal('hello', 'hello', 'strip') |
| 511 | |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 512 | # strip/lstrip/rstrip with None arg |
| 513 | self.checkequal('hello', ' hello ', 'strip', None) |
| 514 | self.checkequal('hello ', ' hello ', 'lstrip', None) |
| 515 | self.checkequal(' hello', ' hello ', 'rstrip', None) |
| 516 | self.checkequal('hello', 'hello', 'strip', None) |
| 517 | |
| 518 | # strip/lstrip/rstrip with str arg |
| 519 | self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz') |
| 520 | self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz') |
| 521 | self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz') |
| 522 | self.checkequal('hello', 'hello', 'strip', 'xyz') |
| 523 | |
| 524 | # strip/lstrip/rstrip with unicode arg |
| 525 | if test_support.have_unicode: |
| 526 | self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy', |
| 527 | 'strip', unicode('xyz', 'ascii')) |
| 528 | self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy', |
| 529 | 'lstrip', unicode('xyz', 'ascii')) |
| 530 | self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy', |
| 531 | 'rstrip', unicode('xyz', 'ascii')) |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 532 | # XXX |
| 533 | #self.checkequal(unicode('hello', 'ascii'), 'hello', |
| 534 | # 'strip', unicode('xyz', 'ascii')) |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 535 | |
| 536 | self.checkraises(TypeError, 'hello', 'strip', 42, 42) |
| 537 | self.checkraises(TypeError, 'hello', 'lstrip', 42, 42) |
| 538 | self.checkraises(TypeError, 'hello', 'rstrip', 42, 42) |
| 539 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 540 | def test_ljust(self): |
| 541 | self.checkequal('abc ', 'abc', 'ljust', 10) |
| 542 | self.checkequal('abc ', 'abc', 'ljust', 6) |
| 543 | self.checkequal('abc', 'abc', 'ljust', 3) |
| 544 | self.checkequal('abc', 'abc', 'ljust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 545 | self.checkequal('abc*******', 'abc', 'ljust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 546 | self.checkraises(TypeError, 'abc', 'ljust') |
| 547 | |
| 548 | def test_rjust(self): |
| 549 | self.checkequal(' abc', 'abc', 'rjust', 10) |
| 550 | self.checkequal(' abc', 'abc', 'rjust', 6) |
| 551 | self.checkequal('abc', 'abc', 'rjust', 3) |
| 552 | self.checkequal('abc', 'abc', 'rjust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 553 | self.checkequal('*******abc', 'abc', 'rjust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 554 | self.checkraises(TypeError, 'abc', 'rjust') |
| 555 | |
| 556 | def test_center(self): |
| 557 | self.checkequal(' abc ', 'abc', 'center', 10) |
| 558 | self.checkequal(' abc ', 'abc', 'center', 6) |
| 559 | self.checkequal('abc', 'abc', 'center', 3) |
| 560 | self.checkequal('abc', 'abc', 'center', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 561 | self.checkequal('***abc****', 'abc', 'center', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 562 | self.checkraises(TypeError, 'abc', 'center') |
| 563 | |
| 564 | def test_swapcase(self): |
| 565 | self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase') |
| 566 | |
| 567 | self.checkraises(TypeError, 'hello', 'swapcase', 42) |
| 568 | |
| 569 | def test_replace(self): |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 570 | EQ = self.checkequal |
| 571 | |
| 572 | # Operations on the empty string |
| 573 | EQ("", "", "replace", "", "") |
Tim Peters | 80a18f0 | 2006-06-01 13:56:26 +0000 | [diff] [blame] | 574 | EQ("A", "", "replace", "", "A") |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 575 | EQ("", "", "replace", "A", "") |
| 576 | EQ("", "", "replace", "A", "A") |
| 577 | EQ("", "", "replace", "", "", 100) |
| 578 | EQ("", "", "replace", "", "", sys.maxint) |
| 579 | |
| 580 | # interleave (from=="", 'to' gets inserted everywhere) |
| 581 | EQ("A", "A", "replace", "", "") |
| 582 | EQ("*A*", "A", "replace", "", "*") |
| 583 | EQ("*1A*1", "A", "replace", "", "*1") |
| 584 | EQ("*-#A*-#", "A", "replace", "", "*-#") |
| 585 | EQ("*-A*-A*-", "AA", "replace", "", "*-") |
| 586 | EQ("*-A*-A*-", "AA", "replace", "", "*-", -1) |
| 587 | EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint) |
| 588 | EQ("*-A*-A*-", "AA", "replace", "", "*-", 4) |
| 589 | EQ("*-A*-A*-", "AA", "replace", "", "*-", 3) |
| 590 | EQ("*-A*-A", "AA", "replace", "", "*-", 2) |
| 591 | EQ("*-AA", "AA", "replace", "", "*-", 1) |
| 592 | EQ("AA", "AA", "replace", "", "*-", 0) |
| 593 | |
| 594 | # single character deletion (from=="A", to=="") |
| 595 | EQ("", "A", "replace", "A", "") |
| 596 | EQ("", "AAA", "replace", "A", "") |
| 597 | EQ("", "AAA", "replace", "A", "", -1) |
| 598 | EQ("", "AAA", "replace", "A", "", sys.maxint) |
| 599 | EQ("", "AAA", "replace", "A", "", 4) |
| 600 | EQ("", "AAA", "replace", "A", "", 3) |
| 601 | EQ("A", "AAA", "replace", "A", "", 2) |
| 602 | EQ("AA", "AAA", "replace", "A", "", 1) |
| 603 | EQ("AAA", "AAA", "replace", "A", "", 0) |
| 604 | EQ("", "AAAAAAAAAA", "replace", "A", "") |
| 605 | EQ("BCD", "ABACADA", "replace", "A", "") |
| 606 | EQ("BCD", "ABACADA", "replace", "A", "", -1) |
| 607 | EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint) |
| 608 | EQ("BCD", "ABACADA", "replace", "A", "", 5) |
| 609 | EQ("BCD", "ABACADA", "replace", "A", "", 4) |
| 610 | EQ("BCDA", "ABACADA", "replace", "A", "", 3) |
| 611 | EQ("BCADA", "ABACADA", "replace", "A", "", 2) |
| 612 | EQ("BACADA", "ABACADA", "replace", "A", "", 1) |
| 613 | EQ("ABACADA", "ABACADA", "replace", "A", "", 0) |
| 614 | EQ("BCD", "ABCAD", "replace", "A", "") |
| 615 | EQ("BCD", "ABCADAA", "replace", "A", "") |
| 616 | EQ("BCD", "BCD", "replace", "A", "") |
| 617 | EQ("*************", "*************", "replace", "A", "") |
| 618 | EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999) |
| 619 | |
| 620 | # substring deletion (from=="the", to=="") |
| 621 | EQ("", "the", "replace", "the", "") |
| 622 | EQ("ater", "theater", "replace", "the", "") |
| 623 | EQ("", "thethe", "replace", "the", "") |
| 624 | EQ("", "thethethethe", "replace", "the", "") |
| 625 | EQ("aaaa", "theatheatheathea", "replace", "the", "") |
| 626 | EQ("that", "that", "replace", "the", "") |
| 627 | EQ("thaet", "thaet", "replace", "the", "") |
| 628 | EQ("here and re", "here and there", "replace", "the", "") |
| 629 | EQ("here and re and re", "here and there and there", |
| 630 | "replace", "the", "", sys.maxint) |
| 631 | EQ("here and re and re", "here and there and there", |
| 632 | "replace", "the", "", -1) |
| 633 | EQ("here and re and re", "here and there and there", |
| 634 | "replace", "the", "", 3) |
| 635 | EQ("here and re and re", "here and there and there", |
| 636 | "replace", "the", "", 2) |
| 637 | EQ("here and re and there", "here and there and there", |
| 638 | "replace", "the", "", 1) |
| 639 | EQ("here and there and there", "here and there and there", |
| 640 | "replace", "the", "", 0) |
| 641 | EQ("here and re and re", "here and there and there", "replace", "the", "") |
| 642 | |
| 643 | EQ("abc", "abc", "replace", "the", "") |
| 644 | EQ("abcdefg", "abcdefg", "replace", "the", "") |
| 645 | |
| 646 | # substring deletion (from=="bob", to=="") |
| 647 | EQ("bob", "bbobob", "replace", "bob", "") |
| 648 | EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "") |
| 649 | EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "") |
| 650 | EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 651 | |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 652 | # single character replace in place (len(from)==len(to)==1) |
| 653 | EQ("Who goes there?", "Who goes there?", "replace", "o", "o") |
| 654 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O") |
| 655 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint) |
| 656 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1) |
| 657 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3) |
| 658 | EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2) |
| 659 | EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1) |
| 660 | EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0) |
| 661 | |
| 662 | EQ("Who goes there?", "Who goes there?", "replace", "a", "q") |
| 663 | EQ("who goes there?", "Who goes there?", "replace", "W", "w") |
| 664 | EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w") |
| 665 | EQ("Who goes there!", "Who goes there?", "replace", "?", "!") |
| 666 | EQ("Who goes there!!", "Who goes there??", "replace", "?", "!") |
| 667 | |
| 668 | EQ("Who goes there?", "Who goes there?", "replace", ".", "!") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 669 | |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 670 | # substring replace in place (len(from)==len(to) > 1) |
| 671 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**") |
| 672 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint) |
| 673 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1) |
| 674 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4) |
| 675 | EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3) |
| 676 | EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2) |
| 677 | EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1) |
| 678 | EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0) |
| 679 | EQ("cobob", "bobob", "replace", "bob", "cob") |
| 680 | EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob") |
| 681 | EQ("bobob", "bobob", "replace", "bot", "bot") |
| 682 | |
| 683 | # replace single character (len(from)==1, len(to)>1) |
| 684 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK") |
| 685 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1) |
| 686 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint) |
| 687 | EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2) |
| 688 | EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1) |
| 689 | EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0) |
| 690 | EQ("A----B----C----", "A.B.C.", "replace", ".", "----") |
| 691 | |
| 692 | EQ("Reykjavik", "Reykjavik", "replace", "q", "KK") |
| 693 | |
| 694 | # replace substring (len(from)>1, len(to)!=len(from)) |
| 695 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 696 | "replace", "spam", "ham") |
| 697 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 698 | "replace", "spam", "ham", sys.maxint) |
| 699 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 700 | "replace", "spam", "ham", -1) |
| 701 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 702 | "replace", "spam", "ham", 4) |
| 703 | EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam", |
| 704 | "replace", "spam", "ham", 3) |
| 705 | EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam", |
| 706 | "replace", "spam", "ham", 2) |
| 707 | EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam", |
| 708 | "replace", "spam", "ham", 1) |
| 709 | EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam", |
| 710 | "replace", "spam", "ham", 0) |
| 711 | |
| 712 | EQ("bobob", "bobobob", "replace", "bobob", "bob") |
| 713 | EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob") |
| 714 | EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby") |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 715 | |
Florent Xicluna | 6de9e93 | 2010-03-07 12:18:33 +0000 | [diff] [blame] | 716 | with test_support.check_py3k_warnings(): |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 717 | ba = buffer('a') |
| 718 | bb = buffer('b') |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 719 | EQ("bbc", "abc", "replace", ba, bb) |
| 720 | EQ("aac", "abc", "replace", bb, ba) |
| 721 | |
Tim Peters | beaec0c | 2006-05-24 20:27:18 +0000 | [diff] [blame] | 722 | # |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 723 | self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 724 | self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '') |
| 725 | self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2) |
| 726 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3) |
| 727 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4) |
| 728 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0) |
| 729 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@') |
| 730 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@') |
| 731 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2) |
| 732 | self.checkequal('-a-b-c-', 'abc', 'replace', '', '-') |
| 733 | self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3) |
| 734 | self.checkequal('abc', 'abc', 'replace', '', '-', 0) |
| 735 | self.checkequal('', '', 'replace', '', '') |
| 736 | self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0) |
| 737 | self.checkequal('abc', 'abc', 'replace', 'xy', '--') |
| 738 | # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with |
| 739 | # MemoryError due to empty result (platform malloc issue when requesting |
| 740 | # 0 bytes). |
| 741 | self.checkequal('', '123', 'replace', '123', '') |
| 742 | self.checkequal('', '123123', 'replace', '123', '') |
| 743 | self.checkequal('x', '123x123', 'replace', '123', '') |
| 744 | |
| 745 | self.checkraises(TypeError, 'hello', 'replace') |
| 746 | self.checkraises(TypeError, 'hello', 'replace', 42) |
| 747 | self.checkraises(TypeError, 'hello', 'replace', 42, 'h') |
| 748 | self.checkraises(TypeError, 'hello', 'replace', 'h', 42) |
| 749 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 750 | @unittest.skipIf(sys.maxint > (1 << 32) or struct.calcsize('P') != 4, |
| 751 | 'only applies to 32-bit platforms') |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 752 | def test_replace_overflow(self): |
| 753 | # Check for overflow checking on 32 bit machines |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 754 | A2_16 = "A" * (2**16) |
| 755 | self.checkraises(OverflowError, A2_16, "replace", "", A2_16) |
| 756 | self.checkraises(OverflowError, A2_16, "replace", "A", A2_16) |
| 757 | self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16) |
Andrew Dalke | e5488ec | 2006-05-24 18:55:37 +0000 | [diff] [blame] | 758 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 759 | def test_zfill(self): |
| 760 | self.checkequal('123', '123', 'zfill', 2) |
| 761 | self.checkequal('123', '123', 'zfill', 3) |
| 762 | self.checkequal('0123', '123', 'zfill', 4) |
| 763 | self.checkequal('+123', '+123', 'zfill', 3) |
| 764 | self.checkequal('+123', '+123', 'zfill', 4) |
| 765 | self.checkequal('+0123', '+123', 'zfill', 5) |
| 766 | self.checkequal('-123', '-123', 'zfill', 3) |
| 767 | self.checkequal('-123', '-123', 'zfill', 4) |
| 768 | self.checkequal('-0123', '-123', 'zfill', 5) |
| 769 | self.checkequal('000', '', 'zfill', 3) |
| 770 | self.checkequal('34', '34', 'zfill', 1) |
| 771 | self.checkequal('0034', '34', 'zfill', 4) |
| 772 | |
| 773 | self.checkraises(TypeError, '123', 'zfill') |
| 774 | |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 775 | # XXX alias for py3k forward compatibility |
| 776 | BaseTest = CommonTest |
| 777 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 778 | class MixinStrUnicodeUserStringTest: |
| 779 | # additional tests that only work for |
| 780 | # stringlike objects, i.e. str, unicode, UserString |
| 781 | # (but not the string module) |
| 782 | |
| 783 | def test_islower(self): |
| 784 | self.checkequal(False, '', 'islower') |
| 785 | self.checkequal(True, 'a', 'islower') |
| 786 | self.checkequal(False, 'A', 'islower') |
| 787 | self.checkequal(False, '\n', 'islower') |
| 788 | self.checkequal(True, 'abc', 'islower') |
| 789 | self.checkequal(False, 'aBc', 'islower') |
| 790 | self.checkequal(True, 'abc\n', 'islower') |
| 791 | self.checkraises(TypeError, 'abc', 'islower', 42) |
| 792 | |
| 793 | def test_isupper(self): |
| 794 | self.checkequal(False, '', 'isupper') |
| 795 | self.checkequal(False, 'a', 'isupper') |
| 796 | self.checkequal(True, 'A', 'isupper') |
| 797 | self.checkequal(False, '\n', 'isupper') |
| 798 | self.checkequal(True, 'ABC', 'isupper') |
| 799 | self.checkequal(False, 'AbC', 'isupper') |
| 800 | self.checkequal(True, 'ABC\n', 'isupper') |
| 801 | self.checkraises(TypeError, 'abc', 'isupper', 42) |
| 802 | |
| 803 | def test_istitle(self): |
| 804 | self.checkequal(False, '', 'istitle') |
| 805 | self.checkequal(False, 'a', 'istitle') |
| 806 | self.checkequal(True, 'A', 'istitle') |
| 807 | self.checkequal(False, '\n', 'istitle') |
| 808 | self.checkequal(True, 'A Titlecased Line', 'istitle') |
| 809 | self.checkequal(True, 'A\nTitlecased Line', 'istitle') |
| 810 | self.checkequal(True, 'A Titlecased, Line', 'istitle') |
| 811 | self.checkequal(False, 'Not a capitalized String', 'istitle') |
| 812 | self.checkequal(False, 'Not\ta Titlecase String', 'istitle') |
| 813 | self.checkequal(False, 'Not--a Titlecase String', 'istitle') |
| 814 | self.checkequal(False, 'NOT', 'istitle') |
| 815 | self.checkraises(TypeError, 'abc', 'istitle', 42) |
| 816 | |
| 817 | def test_isspace(self): |
| 818 | self.checkequal(False, '', 'isspace') |
| 819 | self.checkequal(False, 'a', 'isspace') |
| 820 | self.checkequal(True, ' ', 'isspace') |
| 821 | self.checkequal(True, '\t', 'isspace') |
| 822 | self.checkequal(True, '\r', 'isspace') |
| 823 | self.checkequal(True, '\n', 'isspace') |
| 824 | self.checkequal(True, ' \t\r\n', 'isspace') |
| 825 | self.checkequal(False, ' \t\r\na', 'isspace') |
| 826 | self.checkraises(TypeError, 'abc', 'isspace', 42) |
| 827 | |
| 828 | def test_isalpha(self): |
| 829 | self.checkequal(False, '', 'isalpha') |
| 830 | self.checkequal(True, 'a', 'isalpha') |
| 831 | self.checkequal(True, 'A', 'isalpha') |
| 832 | self.checkequal(False, '\n', 'isalpha') |
| 833 | self.checkequal(True, 'abc', 'isalpha') |
| 834 | self.checkequal(False, 'aBc123', 'isalpha') |
| 835 | self.checkequal(False, 'abc\n', 'isalpha') |
| 836 | self.checkraises(TypeError, 'abc', 'isalpha', 42) |
| 837 | |
| 838 | def test_isalnum(self): |
| 839 | self.checkequal(False, '', 'isalnum') |
| 840 | self.checkequal(True, 'a', 'isalnum') |
| 841 | self.checkequal(True, 'A', 'isalnum') |
| 842 | self.checkequal(False, '\n', 'isalnum') |
| 843 | self.checkequal(True, '123abc456', 'isalnum') |
| 844 | self.checkequal(True, 'a1b3c', 'isalnum') |
| 845 | self.checkequal(False, 'aBc000 ', 'isalnum') |
| 846 | self.checkequal(False, 'abc\n', 'isalnum') |
| 847 | self.checkraises(TypeError, 'abc', 'isalnum', 42) |
| 848 | |
| 849 | def test_isdigit(self): |
| 850 | self.checkequal(False, '', 'isdigit') |
| 851 | self.checkequal(False, 'a', 'isdigit') |
| 852 | self.checkequal(True, '0', 'isdigit') |
| 853 | self.checkequal(True, '0123456789', 'isdigit') |
| 854 | self.checkequal(False, '0123456789a', 'isdigit') |
| 855 | |
| 856 | self.checkraises(TypeError, 'abc', 'isdigit', 42) |
| 857 | |
| 858 | def test_title(self): |
| 859 | self.checkequal(' Hello ', ' hello ', 'title') |
| 860 | self.checkequal('Hello ', 'hello ', 'title') |
| 861 | self.checkequal('Hello ', 'Hello ', 'title') |
| 862 | self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title') |
| 863 | self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', ) |
| 864 | self.checkequal('Getint', "getInt", 'title') |
| 865 | self.checkraises(TypeError, 'hello', 'title', 42) |
| 866 | |
| 867 | def test_splitlines(self): |
| 868 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines') |
| 869 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines') |
| 870 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines') |
| 871 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines') |
| 872 | self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines') |
| 873 | self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines') |
| 874 | self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1) |
| 875 | |
| 876 | self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) |
| 877 | |
| 878 | def test_startswith(self): |
| 879 | self.checkequal(True, 'hello', 'startswith', 'he') |
| 880 | self.checkequal(True, 'hello', 'startswith', 'hello') |
| 881 | self.checkequal(False, 'hello', 'startswith', 'hello world') |
| 882 | self.checkequal(True, 'hello', 'startswith', '') |
| 883 | self.checkequal(False, 'hello', 'startswith', 'ello') |
| 884 | self.checkequal(True, 'hello', 'startswith', 'ello', 1) |
| 885 | self.checkequal(True, 'hello', 'startswith', 'o', 4) |
| 886 | self.checkequal(False, 'hello', 'startswith', 'o', 5) |
| 887 | self.checkequal(True, 'hello', 'startswith', '', 5) |
| 888 | self.checkequal(False, 'hello', 'startswith', 'lo', 6) |
| 889 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3) |
| 890 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7) |
| 891 | self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6) |
| 892 | |
| 893 | # test negative indices |
| 894 | self.checkequal(True, 'hello', 'startswith', 'he', 0, -1) |
| 895 | self.checkequal(True, 'hello', 'startswith', 'he', -53, -1) |
| 896 | self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1) |
| 897 | self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10) |
| 898 | self.checkequal(False, 'hello', 'startswith', 'ello', -5) |
| 899 | self.checkequal(True, 'hello', 'startswith', 'ello', -4) |
| 900 | self.checkequal(False, 'hello', 'startswith', 'o', -2) |
| 901 | self.checkequal(True, 'hello', 'startswith', 'o', -1) |
| 902 | self.checkequal(True, 'hello', 'startswith', '', -3, -3) |
| 903 | self.checkequal(False, 'hello', 'startswith', 'lo', -9) |
| 904 | |
| 905 | self.checkraises(TypeError, 'hello', 'startswith') |
| 906 | self.checkraises(TypeError, 'hello', 'startswith', 42) |
| 907 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 908 | # test tuple arguments |
| 909 | self.checkequal(True, 'hello', 'startswith', ('he', 'ha')) |
| 910 | self.checkequal(False, 'hello', 'startswith', ('lo', 'llo')) |
| 911 | self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello')) |
| 912 | self.checkequal(False, 'hello', 'startswith', ()) |
| 913 | self.checkequal(True, 'helloworld', 'startswith', ('hellowo', |
| 914 | 'rld', 'lowo'), 3) |
| 915 | self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello', |
| 916 | 'rld'), 3) |
| 917 | self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1) |
| 918 | self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1) |
| 919 | self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2) |
| 920 | |
| 921 | self.checkraises(TypeError, 'hello', 'startswith', (42,)) |
| 922 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 923 | def test_endswith(self): |
| 924 | self.checkequal(True, 'hello', 'endswith', 'lo') |
| 925 | self.checkequal(False, 'hello', 'endswith', 'he') |
| 926 | self.checkequal(True, 'hello', 'endswith', '') |
| 927 | self.checkequal(False, 'hello', 'endswith', 'hello world') |
| 928 | self.checkequal(False, 'helloworld', 'endswith', 'worl') |
| 929 | self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9) |
| 930 | self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12) |
| 931 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7) |
| 932 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7) |
| 933 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7) |
| 934 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7) |
| 935 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8) |
| 936 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1) |
| 937 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0) |
| 938 | |
| 939 | # test negative indices |
| 940 | self.checkequal(True, 'hello', 'endswith', 'lo', -2) |
| 941 | self.checkequal(False, 'hello', 'endswith', 'he', -2) |
| 942 | self.checkequal(True, 'hello', 'endswith', '', -3, -3) |
| 943 | self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2) |
| 944 | self.checkequal(False, 'helloworld', 'endswith', 'worl', -6) |
| 945 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1) |
| 946 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9) |
| 947 | self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12) |
| 948 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3) |
| 949 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3) |
| 950 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3) |
| 951 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4) |
| 952 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2) |
| 953 | |
| 954 | self.checkraises(TypeError, 'hello', 'endswith') |
| 955 | self.checkraises(TypeError, 'hello', 'endswith', 42) |
| 956 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 957 | # test tuple arguments |
| 958 | self.checkequal(False, 'hello', 'endswith', ('he', 'ha')) |
| 959 | self.checkequal(True, 'hello', 'endswith', ('lo', 'llo')) |
| 960 | self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello')) |
| 961 | self.checkequal(False, 'hello', 'endswith', ()) |
| 962 | self.checkequal(True, 'helloworld', 'endswith', ('hellowo', |
| 963 | 'rld', 'lowo'), 3) |
| 964 | self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello', |
| 965 | 'rld'), 3, -1) |
| 966 | self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1) |
| 967 | self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1) |
| 968 | self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4) |
| 969 | |
| 970 | self.checkraises(TypeError, 'hello', 'endswith', (42,)) |
| 971 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 972 | def test___contains__(self): |
Ezio Melotti | 469a05f | 2010-01-24 20:48:35 +0000 | [diff] [blame] | 973 | self.checkequal(True, '', '__contains__', '') |
| 974 | self.checkequal(True, 'abc', '__contains__', '') |
| 975 | self.checkequal(False, 'abc', '__contains__', '\0') |
| 976 | self.checkequal(True, '\0abc', '__contains__', '\0') |
| 977 | self.checkequal(True, 'abc\0', '__contains__', '\0') |
| 978 | self.checkequal(True, '\0abc', '__contains__', 'a') |
| 979 | self.checkequal(True, 'asdf', '__contains__', 'asdf') |
| 980 | self.checkequal(False, 'asd', '__contains__', 'asdf') |
| 981 | self.checkequal(False, '', '__contains__', 'asdf') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 982 | |
| 983 | def test_subscript(self): |
| 984 | self.checkequal(u'a', 'abc', '__getitem__', 0) |
| 985 | self.checkequal(u'c', 'abc', '__getitem__', -1) |
| 986 | self.checkequal(u'a', 'abc', '__getitem__', 0L) |
| 987 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3)) |
| 988 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000)) |
| 989 | self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1)) |
| 990 | self.checkequal(u'', 'abc', '__getitem__', slice(0, 0)) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 991 | |
| 992 | self.checkraises(TypeError, 'abc', '__getitem__', 'def') |
| 993 | |
| 994 | def test_slice(self): |
| 995 | self.checkequal('abc', 'abc', '__getslice__', 0, 1000) |
| 996 | self.checkequal('abc', 'abc', '__getslice__', 0, 3) |
| 997 | self.checkequal('ab', 'abc', '__getslice__', 0, 2) |
| 998 | self.checkequal('bc', 'abc', '__getslice__', 1, 3) |
| 999 | self.checkequal('b', 'abc', '__getslice__', 1, 2) |
| 1000 | self.checkequal('', 'abc', '__getslice__', 2, 2) |
| 1001 | self.checkequal('', 'abc', '__getslice__', 1000, 1000) |
| 1002 | self.checkequal('', 'abc', '__getslice__', 2000, 1000) |
| 1003 | self.checkequal('', 'abc', '__getslice__', 2, 1) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1004 | |
| 1005 | self.checkraises(TypeError, 'abc', '__getslice__', 'def') |
| 1006 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 1007 | def test_extended_getslice(self): |
| 1008 | # Test extended slicing by comparing with list slicing. |
| 1009 | s = string.ascii_letters + string.digits |
| 1010 | indices = (0, None, 1, 3, 41, -1, -2, -37) |
| 1011 | for start in indices: |
| 1012 | for stop in indices: |
| 1013 | # Skip step 0 (invalid) |
| 1014 | for step in indices[1:]: |
| 1015 | L = list(s)[start:stop:step] |
| 1016 | self.checkequal(u"".join(L), s, '__getitem__', |
| 1017 | slice(start, stop, step)) |
| 1018 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1019 | def test_mul(self): |
| 1020 | self.checkequal('', 'abc', '__mul__', -1) |
| 1021 | self.checkequal('', 'abc', '__mul__', 0) |
| 1022 | self.checkequal('abc', 'abc', '__mul__', 1) |
| 1023 | self.checkequal('abcabcabc', 'abc', '__mul__', 3) |
| 1024 | self.checkraises(TypeError, 'abc', '__mul__') |
| 1025 | self.checkraises(TypeError, 'abc', '__mul__', '') |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1026 | # XXX: on a 64-bit system, this doesn't raise an overflow error, |
| 1027 | # but either raises a MemoryError, or succeeds (if you have 54TiB) |
| 1028 | #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1029 | |
| 1030 | def test_join(self): |
| 1031 | # join now works with any sequence type |
| 1032 | # moved here, because the argument order is |
| 1033 | # different in string.join (see the test in |
| 1034 | # test.test_string.StringTest.test_join) |
| 1035 | self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 1036 | self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 1037 | self.checkequal('bd', '', 'join', ('', 'b', '', 'd')) |
| 1038 | self.checkequal('ac', '', 'join', ('a', '', 'c', '')) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1039 | self.checkequal('w x y z', ' ', 'join', Sequence()) |
| 1040 | self.checkequal('abc', 'a', 'join', ('abc',)) |
| 1041 | self.checkequal('z', 'a', 'join', UserList(['z'])) |
| 1042 | if test_support.have_unicode: |
| 1043 | self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c']) |
| 1044 | self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c']) |
| 1045 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c']) |
| 1046 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')]) |
| 1047 | self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3]) |
| 1048 | for i in [5, 25, 125]: |
| 1049 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 1050 | ['a' * i] * i) |
| 1051 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 1052 | ('a' * i,) * i) |
| 1053 | |
| 1054 | self.checkraises(TypeError, ' ', 'join', BadSeq1()) |
| 1055 | self.checkequal('a b c', ' ', 'join', BadSeq2()) |
| 1056 | |
| 1057 | self.checkraises(TypeError, ' ', 'join') |
Benjamin Peterson | 1643d5c | 2014-09-28 12:48:46 -0400 | [diff] [blame] | 1058 | self.checkraises(TypeError, ' ', 'join', None) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1059 | self.checkraises(TypeError, ' ', 'join', 7) |
| 1060 | self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L])) |
Michael W. Hudson | b2308bb | 2005-10-21 11:45:01 +0000 | [diff] [blame] | 1061 | try: |
| 1062 | def f(): |
| 1063 | yield 4 + "" |
| 1064 | self.fixtype(' ').join(f()) |
| 1065 | except TypeError, e: |
| 1066 | if '+' not in str(e): |
| 1067 | self.fail('join() ate exception message') |
| 1068 | else: |
| 1069 | self.fail('exception not raised') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1070 | |
| 1071 | def test_formatting(self): |
| 1072 | self.checkequal('+hello+', '+%s+', '__mod__', 'hello') |
| 1073 | self.checkequal('+10+', '+%d+', '__mod__', 10) |
| 1074 | self.checkequal('a', "%c", '__mod__', "a") |
| 1075 | self.checkequal('a', "%c", '__mod__', "a") |
| 1076 | self.checkequal('"', "%c", '__mod__', 34) |
| 1077 | self.checkequal('$', "%c", '__mod__', 36) |
| 1078 | self.checkequal('10', "%d", '__mod__', 10) |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 1079 | self.checkequal('\x7f', "%c", '__mod__', 0x7f) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1080 | |
| 1081 | for ordinal in (-100, 0x200000): |
| 1082 | # unicode raises ValueError, str raises OverflowError |
| 1083 | self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal) |
| 1084 | |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 1085 | longvalue = sys.maxint + 10L |
| 1086 | slongvalue = str(longvalue) |
| 1087 | if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1] |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1088 | self.checkequal(' 42', '%3ld', '__mod__', 42) |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 1089 | self.checkequal('42', '%d', '__mod__', 42L) |
| 1090 | self.checkequal('42', '%d', '__mod__', 42.0) |
| 1091 | self.checkequal(slongvalue, '%d', '__mod__', longvalue) |
| 1092 | self.checkcall('%d', '__mod__', float(longvalue)) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1093 | self.checkequal('0042.00', '%07.2f', '__mod__', 42) |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 1094 | self.checkequal('0042.00', '%07.2F', '__mod__', 42) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1095 | |
| 1096 | self.checkraises(TypeError, 'abc', '__mod__') |
| 1097 | self.checkraises(TypeError, '%(foo)s', '__mod__', 42) |
| 1098 | self.checkraises(TypeError, '%s%s', '__mod__', (42,)) |
| 1099 | self.checkraises(TypeError, '%c', '__mod__', (None,)) |
| 1100 | self.checkraises(ValueError, '%(foo', '__mod__', {}) |
| 1101 | self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42)) |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 1102 | self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric |
| 1103 | self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1104 | |
| 1105 | # argument names with properly nested brackets are supported |
| 1106 | self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'}) |
| 1107 | |
| 1108 | # 100 is a magic number in PyUnicode_Format, this forces a resize |
| 1109 | self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a') |
| 1110 | |
| 1111 | self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar')) |
| 1112 | self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.)) |
| 1113 | self.checkraises(ValueError, '%10', '__mod__', (42,)) |
| 1114 | |
Benjamin Peterson | 23d49d3 | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 1115 | class X(object): pass |
| 1116 | self.checkraises(TypeError, 'abc', '__mod__', X()) |
Benjamin Peterson | da2c7eb | 2013-03-23 22:32:00 -0500 | [diff] [blame] | 1117 | class X(Exception): |
| 1118 | def __getitem__(self, k): |
| 1119 | return k |
| 1120 | self.checkequal('melon apple', '%(melon)s %(apple)s', '__mod__', X()) |
Benjamin Peterson | 23d49d3 | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 1121 | |
Serhiy Storchaka | 76249ea | 2014-02-07 10:06:05 +0200 | [diff] [blame] | 1122 | @test_support.cpython_only |
| 1123 | def test_formatting_c_limits(self): |
| 1124 | from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX |
| 1125 | SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1 |
| 1126 | width = int(PY_SSIZE_T_MAX + 1) |
| 1127 | if width <= sys.maxint: |
| 1128 | self.checkraises(OverflowError, '%*s', '__mod__', (width, '')) |
| 1129 | prec = int(INT_MAX + 1) |
| 1130 | if prec <= sys.maxint: |
| 1131 | self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7)) |
| 1132 | # Issue 15989 |
| 1133 | width = int(SIZE_MAX + 1) |
| 1134 | if width <= sys.maxint: |
| 1135 | self.checkraises(OverflowError, '%*s', '__mod__', (width, '')) |
| 1136 | prec = int(UINT_MAX + 1) |
| 1137 | if prec <= sys.maxint: |
| 1138 | self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7)) |
| 1139 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1140 | def test_floatformatting(self): |
| 1141 | # float formatting |
| 1142 | for prec in xrange(100): |
| 1143 | format = '%%.%if' % prec |
| 1144 | value = 0.01 |
| 1145 | for x in xrange(60): |
Florent Xicluna | 9b90cd1 | 2010-09-13 07:46:37 +0000 | [diff] [blame] | 1146 | value = value * 3.14159265359 / 3.0 * 10.0 |
Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 1147 | self.checkcall(format, "__mod__", value) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1148 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 1149 | def test_inplace_rewrites(self): |
| 1150 | # Check that strings don't copy and modify cached single-character strings |
| 1151 | self.checkequal('a', 'A', 'lower') |
| 1152 | self.checkequal(True, 'A', 'isupper') |
| 1153 | self.checkequal('A', 'a', 'upper') |
| 1154 | self.checkequal(True, 'a', 'islower') |
Tim Peters | d95d593 | 2006-05-25 21:52:19 +0000 | [diff] [blame] | 1155 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 1156 | self.checkequal('a', 'A', 'replace', 'A', 'a') |
| 1157 | self.checkequal(True, 'A', 'isupper') |
| 1158 | |
| 1159 | self.checkequal('A', 'a', 'capitalize') |
| 1160 | self.checkequal(True, 'a', 'islower') |
Tim Peters | d95d593 | 2006-05-25 21:52:19 +0000 | [diff] [blame] | 1161 | |
Andrew Dalke | 2bddcbf | 2006-05-25 16:30:52 +0000 | [diff] [blame] | 1162 | self.checkequal('A', 'a', 'swapcase') |
| 1163 | self.checkequal(True, 'a', 'islower') |
| 1164 | |
| 1165 | self.checkequal('A', 'a', 'title') |
| 1166 | self.checkequal(True, 'a', 'islower') |
| 1167 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1168 | def test_partition(self): |
| 1169 | |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 1170 | self.checkequal(('this is the par', 'ti', 'tion method'), |
| 1171 | 'this is the partition method', 'partition', 'ti') |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1172 | |
| 1173 | # from raymond's original specification |
| 1174 | S = 'http://www.python.org' |
| 1175 | self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://') |
| 1176 | self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?') |
| 1177 | self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://') |
| 1178 | self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org') |
| 1179 | |
| 1180 | self.checkraises(ValueError, S, 'partition', '') |
| 1181 | self.checkraises(TypeError, S, 'partition', None) |
| 1182 | |
Amaury Forgeot d'Arc | 3571fbf | 2008-09-01 19:52:00 +0000 | [diff] [blame] | 1183 | # mixed use of str and unicode |
| 1184 | self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c')) |
| 1185 | |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 1186 | def test_rpartition(self): |
| 1187 | |
| 1188 | self.checkequal(('this is the rparti', 'ti', 'on method'), |
| 1189 | 'this is the rpartition method', 'rpartition', 'ti') |
| 1190 | |
| 1191 | # from raymond's original specification |
| 1192 | S = 'http://www.python.org' |
| 1193 | self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://') |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 1194 | self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?') |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 1195 | self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://') |
| 1196 | self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org') |
| 1197 | |
| 1198 | self.checkraises(ValueError, S, 'rpartition', '') |
| 1199 | self.checkraises(TypeError, S, 'rpartition', None) |
| 1200 | |
Amaury Forgeot d'Arc | 3571fbf | 2008-09-01 19:52:00 +0000 | [diff] [blame] | 1201 | # mixed use of str and unicode |
| 1202 | self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c')) |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1203 | |
Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 1204 | def test_none_arguments(self): |
| 1205 | # issue 11828 |
| 1206 | s = 'hello' |
| 1207 | self.checkequal(2, s, 'find', 'l', None) |
| 1208 | self.checkequal(3, s, 'find', 'l', -2, None) |
| 1209 | self.checkequal(2, s, 'find', 'l', None, -2) |
| 1210 | self.checkequal(0, s, 'find', 'h', None, None) |
| 1211 | |
| 1212 | self.checkequal(3, s, 'rfind', 'l', None) |
| 1213 | self.checkequal(3, s, 'rfind', 'l', -2, None) |
| 1214 | self.checkequal(2, s, 'rfind', 'l', None, -2) |
| 1215 | self.checkequal(0, s, 'rfind', 'h', None, None) |
| 1216 | |
| 1217 | self.checkequal(2, s, 'index', 'l', None) |
| 1218 | self.checkequal(3, s, 'index', 'l', -2, None) |
| 1219 | self.checkequal(2, s, 'index', 'l', None, -2) |
| 1220 | self.checkequal(0, s, 'index', 'h', None, None) |
| 1221 | |
| 1222 | self.checkequal(3, s, 'rindex', 'l', None) |
| 1223 | self.checkequal(3, s, 'rindex', 'l', -2, None) |
| 1224 | self.checkequal(2, s, 'rindex', 'l', None, -2) |
| 1225 | self.checkequal(0, s, 'rindex', 'h', None, None) |
| 1226 | |
| 1227 | self.checkequal(2, s, 'count', 'l', None) |
| 1228 | self.checkequal(1, s, 'count', 'l', -2, None) |
| 1229 | self.checkequal(1, s, 'count', 'l', None, -2) |
| 1230 | self.checkequal(0, s, 'count', 'x', None, None) |
| 1231 | |
| 1232 | self.checkequal(True, s, 'endswith', 'o', None) |
| 1233 | self.checkequal(True, s, 'endswith', 'lo', -2, None) |
| 1234 | self.checkequal(True, s, 'endswith', 'l', None, -2) |
| 1235 | self.checkequal(False, s, 'endswith', 'x', None, None) |
| 1236 | |
| 1237 | self.checkequal(True, s, 'startswith', 'h', None) |
| 1238 | self.checkequal(True, s, 'startswith', 'l', -2, None) |
| 1239 | self.checkequal(True, s, 'startswith', 'h', None, -2) |
| 1240 | self.checkequal(False, s, 'startswith', 'x', None, None) |
| 1241 | |
| 1242 | def test_find_etc_raise_correct_error_messages(self): |
| 1243 | # issue 11828 |
| 1244 | s = 'hello' |
| 1245 | x = 'x' |
| 1246 | self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find, |
| 1247 | x, None, None, None) |
| 1248 | self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind, |
| 1249 | x, None, None, None) |
| 1250 | self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index, |
| 1251 | x, None, None, None) |
| 1252 | self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex, |
| 1253 | x, None, None, None) |
| 1254 | self.assertRaisesRegexp(TypeError, r'^count\(', s.count, |
| 1255 | x, None, None, None) |
| 1256 | self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith, |
| 1257 | x, None, None, None) |
| 1258 | self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith, |
| 1259 | x, None, None, None) |
| 1260 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1261 | class MixinStrStringUserStringTest: |
| 1262 | # Additional tests for 8bit strings, i.e. str, UserString and |
| 1263 | # the string module |
| 1264 | |
| 1265 | def test_maketrans(self): |
| 1266 | self.assertEqual( |
| 1267 | ''.join(map(chr, xrange(256))).replace('abc', 'xyz'), |
| 1268 | string.maketrans('abc', 'xyz') |
| 1269 | ) |
| 1270 | self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw') |
| 1271 | |
| 1272 | def test_translate(self): |
| 1273 | table = string.maketrans('abc', 'xyz') |
| 1274 | self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def') |
| 1275 | |
| 1276 | table = string.maketrans('a', 'A') |
| 1277 | self.checkequal('Abc', 'abc', 'translate', table) |
| 1278 | self.checkequal('xyz', 'xyz', 'translate', table) |
| 1279 | self.checkequal('yz', 'xyz', 'translate', table, 'x') |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1280 | self.checkequal('yx', 'zyzzx', 'translate', None, 'z') |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 1281 | self.checkequal('zyzzx', 'zyzzx', 'translate', None, '') |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1282 | self.checkequal('zyzzx', 'zyzzx', 'translate', None) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1283 | self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip') |
| 1284 | self.checkraises(ValueError, 'xyz', 'translate', 'too short') |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 1285 | |
| 1286 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1287 | class MixinStrUserStringTest: |
| 1288 | # Additional tests that only work with |
| 1289 | # 8bit compatible object, i.e. str and UserString |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 1290 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 1291 | @unittest.skipUnless(test_support.have_unicode, 'no unicode support') |
| 1292 | def test_encoding_decoding(self): |
| 1293 | codecs = [('rot13', 'uryyb jbeyq'), |
| 1294 | ('base64', 'aGVsbG8gd29ybGQ=\n'), |
| 1295 | ('hex', '68656c6c6f20776f726c64'), |
| 1296 | ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')] |
| 1297 | for encoding, data in codecs: |
| 1298 | self.checkequal(data, 'hello world', 'encode', encoding) |
| 1299 | self.checkequal('hello world', data, 'decode', encoding) |
| 1300 | # zlib is optional, so we make the test optional too... |
| 1301 | try: |
| 1302 | import zlib |
| 1303 | except ImportError: |
| 1304 | pass |
| 1305 | else: |
| 1306 | data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]' |
| 1307 | self.checkequal(data, 'hello world', 'encode', 'zlib') |
| 1308 | self.checkequal('hello world', data, 'decode', 'zlib') |
Walter Dörwald | 97951de | 2003-03-26 14:31:25 +0000 | [diff] [blame] | 1309 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 1310 | self.checkraises(TypeError, 'xyz', 'decode', 42) |
| 1311 | self.checkraises(TypeError, 'xyz', 'encode', 42) |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
| 1314 | class MixinStrUnicodeTest: |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1315 | # Additional tests that only work with str and unicode. |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1316 | |
| 1317 | def test_bug1001011(self): |
| 1318 | # Make sure join returns a NEW object for single item sequences |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1319 | # involving a subclass. |
| 1320 | # Make sure that it is of the appropriate type. |
| 1321 | # Check the optimisation still occurs for standard objects. |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 1322 | t = self.type2test |
| 1323 | class subclass(t): |
| 1324 | pass |
| 1325 | s1 = subclass("abcd") |
| 1326 | s2 = t().join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1327 | self.assertTrue(s1 is not s2) |
| 1328 | self.assertTrue(type(s2) is t) |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1329 | |
| 1330 | s1 = t("abcd") |
| 1331 | s2 = t().join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1332 | self.assertTrue(s1 is s2) |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1333 | |
| 1334 | # Should also test mixed-type join. |
| 1335 | if t is unicode: |
| 1336 | s1 = subclass("abcd") |
| 1337 | s2 = "".join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1338 | self.assertTrue(s1 is not s2) |
| 1339 | self.assertTrue(type(s2) is t) |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1340 | |
| 1341 | s1 = t("abcd") |
| 1342 | s2 = "".join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1343 | self.assertTrue(s1 is s2) |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1344 | |
| 1345 | elif t is str: |
| 1346 | s1 = subclass("abcd") |
| 1347 | s2 = u"".join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1348 | self.assertTrue(s1 is not s2) |
| 1349 | self.assertTrue(type(s2) is unicode) # promotes! |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1350 | |
| 1351 | s1 = t("abcd") |
| 1352 | s2 = u"".join([s1]) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1353 | self.assertTrue(s1 is not s2) |
| 1354 | self.assertTrue(type(s2) is unicode) # promotes! |
Tim Peters | 108f137 | 2004-08-27 05:36:07 +0000 | [diff] [blame] | 1355 | |
| 1356 | else: |
| 1357 | self.fail("unexpected type for MixinStrUnicodeTest %r" % t) |