blob: cb8900d03f3f17ecd12c19758d453b85ae530fd1 [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001"""
2Common tests shared by test_str, test_unicode, test_userstring and test_string.
3"""
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00004
Guido van Rossum360e4b82007-05-14 22:51:27 +00005import unittest, string, sys, struct
Walter Dörwald0fd583c2003-02-21 12:53:50 +00006from test import test_support
Jeremy Hylton20f41b62000-07-11 03:31:55 +00007from UserList import UserList
8
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00009class Sequence:
Walter Dörwald0fd583c2003-02-21 12:53:50 +000010 def __init__(self, seq='wxyz'): self.seq = seq
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000011 def __len__(self): return len(self.seq)
12 def __getitem__(self, i): return self.seq[i]
13
14class BadSeq1(Sequence):
Guido van Rossume2a383d2007-01-15 16:59:06 +000015 def __init__(self): self.seq = [7, 'hello', 123]
Guido van Rossumf1044292007-09-27 18:01:22 +000016 def __str__(self): return '{0} {1} {2}'.format(*self.seq)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000017
18class BadSeq2(Sequence):
19 def __init__(self): self.seq = ['a', 'b', 'c']
20 def __len__(self): return 8
21
Georg Brandlc7885542007-03-06 19:16:20 +000022class BaseTest(unittest.TestCase):
23 # These tests are for buffers of values (bytes) and not
24 # specific to character interpretation, used for bytes objects
25 # and various string implementations
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000026
Walter Dörwald0fd583c2003-02-21 12:53:50 +000027 # The type to be tested
28 # Change in subclasses to change the behaviour of fixtesttype()
29 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000030
Walter Dörwald0fd583c2003-02-21 12:53:50 +000031 # All tests pass their arguments to the testing methods
32 # as str objects. fixtesttype() can be used to propagate
33 # these arguments to the appropriate type
34 def fixtype(self, obj):
35 if isinstance(obj, str):
36 return self.__class__.type2test(obj)
37 elif isinstance(obj, list):
38 return [self.fixtype(x) for x in obj]
39 elif isinstance(obj, tuple):
40 return tuple([self.fixtype(x) for x in obj])
41 elif isinstance(obj, dict):
42 return dict([
43 (self.fixtype(key), self.fixtype(value))
Guido van Rossumcc2b0162007-02-11 06:12:03 +000044 for (key, value) in obj.items()
Walter Dörwald0fd583c2003-02-21 12:53:50 +000045 ])
46 else:
47 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000048
Guido van Rossum09549f42007-08-27 20:40:10 +000049 # check that obj.method(*args) returns result
50 def checkequal(self, result, obj, methodname, *args):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000051 result = self.fixtype(result)
Guido van Rossum09549f42007-08-27 20:40:10 +000052 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000053 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000054 realresult = getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000055 self.assertEqual(
56 result,
57 realresult
58 )
59 # if the original is returned make sure that
60 # this doesn't happen with subclasses
Guido van Rossum09549f42007-08-27 20:40:10 +000061 if obj is realresult:
62 try:
63 class subtype(self.__class__.type2test):
64 pass
65 except TypeError:
66 pass # Skip this if we can't subclass
67 else:
68 obj = subtype(obj)
69 realresult = getattr(obj, methodname)(*args)
70 self.assert_(obj is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000071
Guido van Rossum09549f42007-08-27 20:40:10 +000072 # check that obj.method(*args) raises exc
73 def checkraises(self, exc, obj, methodname, *args):
74 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000075 args = self.fixtype(args)
76 self.assertRaises(
77 exc,
Guido van Rossum09549f42007-08-27 20:40:10 +000078 getattr(obj, methodname),
Walter Dörwald0fd583c2003-02-21 12:53:50 +000079 *args
80 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000081
Guido van Rossum09549f42007-08-27 20:40:10 +000082 # call obj.method(*args) without any checks
83 def checkcall(self, obj, methodname, *args):
84 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000085 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000086 getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000087
Walter Dörwald0fd583c2003-02-21 12:53:50 +000088 def test_count(self):
89 self.checkequal(3, 'aaa', 'count', 'a')
90 self.checkequal(0, 'aaa', 'count', 'b')
91 self.checkequal(3, 'aaa', 'count', 'a')
92 self.checkequal(0, 'aaa', 'count', 'b')
93 self.checkequal(3, 'aaa', 'count', 'a')
94 self.checkequal(0, 'aaa', 'count', 'b')
95 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000096 self.checkequal(2, 'aaa', 'count', 'a', 1)
97 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000098 self.checkequal(1, 'aaa', 'count', 'a', -1)
99 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000100 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
101 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000102 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
103 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000104 self.checkequal(3, 'aaa', 'count', '', 1)
105 self.checkequal(1, 'aaa', 'count', '', 3)
106 self.checkequal(0, 'aaa', 'count', '', 10)
107 self.checkequal(2, 'aaa', 'count', '', -1)
108 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000109
110 self.checkraises(TypeError, 'hello', 'count')
111 self.checkraises(TypeError, 'hello', 'count', 42)
112
Raymond Hettinger57e74472005-02-20 09:54:53 +0000113 # For a variety of combinations,
114 # verify that str.count() matches an equivalent function
115 # replacing all occurrences and then differencing the string lengths
116 charset = ['', 'a', 'b']
117 digits = 7
118 base = len(charset)
119 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000120 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000121 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000122 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000123 i, m = divmod(i, base)
124 entry.append(charset[m])
125 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000126 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger57e74472005-02-20 09:54:53 +0000127 for i in teststrings:
Raymond Hettinger57e74472005-02-20 09:54:53 +0000128 n = len(i)
129 for j in teststrings:
130 r1 = i.count(j)
131 if j:
Guido van Rossum09549f42007-08-27 20:40:10 +0000132 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
133 len(j))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000134 else:
135 r2, rem = len(i)+1, 0
136 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
138 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000139
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000140 def test_find(self):
141 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
142 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
143 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
144
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000145 self.checkequal(0, 'abc', 'find', '', 0)
146 self.checkequal(3, 'abc', 'find', '', 3)
147 self.checkequal(-1, 'abc', 'find', '', 4)
148
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000149 self.checkraises(TypeError, 'hello', 'find')
150 self.checkraises(TypeError, 'hello', 'find', 42)
151
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000152 # For a variety of combinations,
153 # verify that str.find() matches __contains__
154 # and that the found substring is really at that location
155 charset = ['', 'a', 'b', 'c']
156 digits = 5
157 base = len(charset)
158 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000159 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000160 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000161 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000162 i, m = divmod(i, base)
163 entry.append(charset[m])
164 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000165 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000166 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000167 for j in teststrings:
168 loc = i.find(j)
169 r1 = (loc != -1)
170 r2 = j in i
171 if r1 != r2:
172 self.assertEqual(r1, r2)
173 if loc != -1:
174 self.assertEqual(i[loc:loc+len(j)], j)
175
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000176 def test_rfind(self):
177 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
178 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
179 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
180 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
181
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000182 self.checkequal(3, 'abc', 'rfind', '', 0)
183 self.checkequal(3, 'abc', 'rfind', '', 3)
184 self.checkequal(-1, 'abc', 'rfind', '', 4)
185
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000186 self.checkraises(TypeError, 'hello', 'rfind')
187 self.checkraises(TypeError, 'hello', 'rfind', 42)
188
189 def test_index(self):
190 self.checkequal(0, 'abcdefghiabc', 'index', '')
191 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
192 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
193 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
194
195 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
196 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
197 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
198 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
199
200 self.checkraises(TypeError, 'hello', 'index')
201 self.checkraises(TypeError, 'hello', 'index', 42)
202
203 def test_rindex(self):
204 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
205 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
206 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
207 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
208
209 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
210 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
211 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
212 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
213 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
214
215 self.checkraises(TypeError, 'hello', 'rindex')
216 self.checkraises(TypeError, 'hello', 'rindex', 42)
217
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000218 def test_lower(self):
219 self.checkequal('hello', 'HeLLo', 'lower')
220 self.checkequal('hello', 'hello', 'lower')
221 self.checkraises(TypeError, 'hello', 'lower', 42)
222
223 def test_upper(self):
224 self.checkequal('HELLO', 'HeLLo', 'upper')
225 self.checkequal('HELLO', 'HELLO', 'upper')
226 self.checkraises(TypeError, 'hello', 'upper', 42)
227
228 def test_expandtabs(self):
229 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
230 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
231 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
232 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
233 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
234 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
235 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
236 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
237
238 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
239 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
240 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
241 self.checkraises(OverflowError,
242 '\ta\n\tb', 'expandtabs', sys.maxint)
243
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000244 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000245 # by a char
246 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000247 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000248 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
249 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
250 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
251 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000252 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
253 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000254 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
255 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
256 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000257 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
258 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000259 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
260
Thomas Wouters477c8d52006-05-27 19:21:47 +0000261 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
262 self.checkequal(['a']*15 +['a|a|a|a|a'],
263 ('a|'*20)[:-1], 'split', '|', 15)
264
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000265 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000266 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000267 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
268 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
269 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
270 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000271 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
272 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000273 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
274 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000275 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
277 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
278 'split', 'test')
279 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
280 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
281 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
282 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
283 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
284 self.checkequal([''], '', 'split', 'aaa')
285 self.checkequal(['aa'], 'aa', 'split', 'aaa')
286 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
287 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
288
289 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
290 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
291 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
292 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000293
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000294 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000295 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
296
Thomas Wouters477c8d52006-05-27 19:21:47 +0000297 # null case
298 self.checkraises(ValueError, 'hello', 'split', '')
299 self.checkraises(ValueError, 'hello', 'split', '', 0)
300
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000301 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000302 # by a char
303 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
304 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
305 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
306 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
307 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000308 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
309 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000310 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
311 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
312 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
314 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
315
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000316 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
317
Thomas Wouters477c8d52006-05-27 19:21:47 +0000318 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
319 self.checkequal(['a|a|a|a|a']+['a']*15,
320 ('a|'*20)[:-1], 'rsplit', '|', 15)
321
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000322 # by string
323 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
324 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
325 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
326 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
327 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
329 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000330 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
331 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
332 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000333 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
334 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
335 'rsplit', 'test')
336 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
337 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
338 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
339 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
340 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
341 self.checkequal([''], '', 'rsplit', 'aaa')
342 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
343 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
344 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
345
346 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
347 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
348 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
349 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000350
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000351 # argument type
352 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000353
Thomas Wouters477c8d52006-05-27 19:21:47 +0000354 # null case
355 self.checkraises(ValueError, 'hello', 'rsplit', '')
356 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
357
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000358 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000359 EQ = self.checkequal
360
361 # Operations on the empty string
362 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000363 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364 EQ("", "", "replace", "A", "")
365 EQ("", "", "replace", "A", "A")
366 EQ("", "", "replace", "", "", 100)
367 EQ("", "", "replace", "", "", sys.maxint)
368
369 # interleave (from=="", 'to' gets inserted everywhere)
370 EQ("A", "A", "replace", "", "")
371 EQ("*A*", "A", "replace", "", "*")
372 EQ("*1A*1", "A", "replace", "", "*1")
373 EQ("*-#A*-#", "A", "replace", "", "*-#")
374 EQ("*-A*-A*-", "AA", "replace", "", "*-")
375 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
376 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
377 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
378 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
379 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
380 EQ("*-AA", "AA", "replace", "", "*-", 1)
381 EQ("AA", "AA", "replace", "", "*-", 0)
382
383 # single character deletion (from=="A", to=="")
384 EQ("", "A", "replace", "A", "")
385 EQ("", "AAA", "replace", "A", "")
386 EQ("", "AAA", "replace", "A", "", -1)
387 EQ("", "AAA", "replace", "A", "", sys.maxint)
388 EQ("", "AAA", "replace", "A", "", 4)
389 EQ("", "AAA", "replace", "A", "", 3)
390 EQ("A", "AAA", "replace", "A", "", 2)
391 EQ("AA", "AAA", "replace", "A", "", 1)
392 EQ("AAA", "AAA", "replace", "A", "", 0)
393 EQ("", "AAAAAAAAAA", "replace", "A", "")
394 EQ("BCD", "ABACADA", "replace", "A", "")
395 EQ("BCD", "ABACADA", "replace", "A", "", -1)
396 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
397 EQ("BCD", "ABACADA", "replace", "A", "", 5)
398 EQ("BCD", "ABACADA", "replace", "A", "", 4)
399 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
400 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
401 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
402 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
403 EQ("BCD", "ABCAD", "replace", "A", "")
404 EQ("BCD", "ABCADAA", "replace", "A", "")
405 EQ("BCD", "BCD", "replace", "A", "")
406 EQ("*************", "*************", "replace", "A", "")
407 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
408
409 # substring deletion (from=="the", to=="")
410 EQ("", "the", "replace", "the", "")
411 EQ("ater", "theater", "replace", "the", "")
412 EQ("", "thethe", "replace", "the", "")
413 EQ("", "thethethethe", "replace", "the", "")
414 EQ("aaaa", "theatheatheathea", "replace", "the", "")
415 EQ("that", "that", "replace", "the", "")
416 EQ("thaet", "thaet", "replace", "the", "")
417 EQ("here and re", "here and there", "replace", "the", "")
418 EQ("here and re and re", "here and there and there",
419 "replace", "the", "", sys.maxint)
420 EQ("here and re and re", "here and there and there",
421 "replace", "the", "", -1)
422 EQ("here and re and re", "here and there and there",
423 "replace", "the", "", 3)
424 EQ("here and re and re", "here and there and there",
425 "replace", "the", "", 2)
426 EQ("here and re and there", "here and there and there",
427 "replace", "the", "", 1)
428 EQ("here and there and there", "here and there and there",
429 "replace", "the", "", 0)
430 EQ("here and re and re", "here and there and there", "replace", "the", "")
431
432 EQ("abc", "abc", "replace", "the", "")
433 EQ("abcdefg", "abcdefg", "replace", "the", "")
434
435 # substring deletion (from=="bob", to=="")
436 EQ("bob", "bbobob", "replace", "bob", "")
437 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
438 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
439 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
440
441 # single character replace in place (len(from)==len(to)==1)
442 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
443 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
444 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
445 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
446 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
447 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
448 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
449 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
450
451 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
452 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
453 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
454 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
455 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
456
457 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
458
459 # substring replace in place (len(from)==len(to) > 1)
460 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
461 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
462 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
463 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
464 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
465 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
466 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
467 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
468 EQ("cobob", "bobob", "replace", "bob", "cob")
469 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
470 EQ("bobob", "bobob", "replace", "bot", "bot")
471
472 # replace single character (len(from)==1, len(to)>1)
473 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
474 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
475 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
476 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
477 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
478 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
479 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
480
481 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
482
483 # replace substring (len(from)>1, len(to)!=len(from))
484 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
485 "replace", "spam", "ham")
486 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
487 "replace", "spam", "ham", sys.maxint)
488 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
489 "replace", "spam", "ham", -1)
490 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
491 "replace", "spam", "ham", 4)
492 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
493 "replace", "spam", "ham", 3)
494 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
495 "replace", "spam", "ham", 2)
496 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
497 "replace", "spam", "ham", 1)
498 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
499 "replace", "spam", "ham", 0)
500
501 EQ("bobob", "bobobob", "replace", "bobob", "bob")
502 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
503 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
504
Guido van Rossum39478e82007-08-27 17:23:59 +0000505 # XXX Commented out. Is there any reason to support buffer objects
506 # as arguments for str.replace()? GvR
507## ba = buffer('a')
508## bb = buffer('b')
509## EQ("bbc", "abc", "replace", ba, bb)
510## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000513 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
514 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
515 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
516 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
517 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
518 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
519 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
520 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
521 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
522 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
523 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
524 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
525 self.checkequal('', '', 'replace', '', '')
526 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
527 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
528 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
529 # MemoryError due to empty result (platform malloc issue when requesting
530 # 0 bytes).
531 self.checkequal('', '123', 'replace', '123', '')
532 self.checkequal('', '123123', 'replace', '123', '')
533 self.checkequal('x', '123x123', 'replace', '123', '')
534
535 self.checkraises(TypeError, 'hello', 'replace')
536 self.checkraises(TypeError, 'hello', 'replace', 42)
537 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
538 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
539
Thomas Wouters477c8d52006-05-27 19:21:47 +0000540 def test_replace_overflow(self):
541 # Check for overflow checking on 32 bit machines
Guido van Rossum360e4b82007-05-14 22:51:27 +0000542 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000543 return
544 A2_16 = "A" * (2**16)
545 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
546 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
547 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
548
Georg Brandlc7885542007-03-06 19:16:20 +0000549
550
551class CommonTest(BaseTest):
552 # This testcase contains test that can be used in all
553 # stringlike classes. Currently this is str, unicode
554 # UserString and the string module.
555
556 def test_hash(self):
557 # SF bug 1054139: += optimization was not invalidating cached hash value
558 a = self.type2test('DNSSEC')
559 b = self.type2test('')
560 for c in a:
561 b += c
562 hash(b)
563 self.assertEqual(hash(a), hash(b))
564
565 def test_capitalize(self):
566 self.checkequal(' hello ', ' hello ', 'capitalize')
567 self.checkequal('Hello ', 'Hello ','capitalize')
568 self.checkequal('Hello ', 'hello ','capitalize')
569 self.checkequal('Aaaa', 'aaaa', 'capitalize')
570 self.checkequal('Aaaa', 'AaAa', 'capitalize')
571
572 self.checkraises(TypeError, 'hello', 'capitalize', 42)
573
574 def test_lower(self):
575 self.checkequal('hello', 'HeLLo', 'lower')
576 self.checkequal('hello', 'hello', 'lower')
577 self.checkraises(TypeError, 'hello', 'lower', 42)
578
579 def test_upper(self):
580 self.checkequal('HELLO', 'HeLLo', 'upper')
581 self.checkequal('HELLO', 'HELLO', 'upper')
582 self.checkraises(TypeError, 'hello', 'upper', 42)
583
584 def test_expandtabs(self):
585 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
586 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
587 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
588 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
589 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
590 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
591 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
592
593 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
594
595 def test_additional_split(self):
596 self.checkequal(['this', 'is', 'the', 'split', 'function'],
597 'this is the split function', 'split')
598
599 # by whitespace
600 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
601 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
602 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
603 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
604 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
605 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
606 sys.maxint-1)
607 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
608 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
609 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
610
611 self.checkequal([], ' ', 'split')
612 self.checkequal(['a'], ' a ', 'split')
613 self.checkequal(['a', 'b'], ' a b ', 'split')
614 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
615 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
616 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
617 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
618 aaa = ' a '*20
619 self.checkequal(['a']*20, aaa, 'split')
620 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
621 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
622
623 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000624 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000625
626 def test_additional_rsplit(self):
627 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
628 'this is the rsplit function', 'rsplit')
629
630 # by whitespace
631 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
632 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
633 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
634 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
635 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
636 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
637 sys.maxint-20)
638 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
639 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
640 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
641
642 self.checkequal([], ' ', 'rsplit')
643 self.checkequal(['a'], ' a ', 'rsplit')
644 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
645 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
646 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
647 None, 1)
648 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
649 None, 2)
650 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
651 aaa = ' a '*20
652 self.checkequal(['a']*20, aaa, 'rsplit')
653 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
654 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
655
656 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000657 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000658
659 def test_strip(self):
660 self.checkequal('hello', ' hello ', 'strip')
661 self.checkequal('hello ', ' hello ', 'lstrip')
662 self.checkequal(' hello', ' hello ', 'rstrip')
663 self.checkequal('hello', 'hello', 'strip')
664
665 # strip/lstrip/rstrip with None arg
666 self.checkequal('hello', ' hello ', 'strip', None)
667 self.checkequal('hello ', ' hello ', 'lstrip', None)
668 self.checkequal(' hello', ' hello ', 'rstrip', None)
669 self.checkequal('hello', 'hello', 'strip', None)
670
671 # strip/lstrip/rstrip with str arg
672 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
673 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
674 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
675 self.checkequal('hello', 'hello', 'strip', 'xyz')
676
Georg Brandlc7885542007-03-06 19:16:20 +0000677 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
678 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
679 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
680
681 def test_ljust(self):
682 self.checkequal('abc ', 'abc', 'ljust', 10)
683 self.checkequal('abc ', 'abc', 'ljust', 6)
684 self.checkequal('abc', 'abc', 'ljust', 3)
685 self.checkequal('abc', 'abc', 'ljust', 2)
686 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
687 self.checkraises(TypeError, 'abc', 'ljust')
688
689 def test_rjust(self):
690 self.checkequal(' abc', 'abc', 'rjust', 10)
691 self.checkequal(' abc', 'abc', 'rjust', 6)
692 self.checkequal('abc', 'abc', 'rjust', 3)
693 self.checkequal('abc', 'abc', 'rjust', 2)
694 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
695 self.checkraises(TypeError, 'abc', 'rjust')
696
697 def test_center(self):
698 self.checkequal(' abc ', 'abc', 'center', 10)
699 self.checkequal(' abc ', 'abc', 'center', 6)
700 self.checkequal('abc', 'abc', 'center', 3)
701 self.checkequal('abc', 'abc', 'center', 2)
702 self.checkequal('***abc****', 'abc', 'center', 10, '*')
703 self.checkraises(TypeError, 'abc', 'center')
704
705 def test_swapcase(self):
706 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
707
708 self.checkraises(TypeError, 'hello', 'swapcase', 42)
709
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000710 def test_zfill(self):
711 self.checkequal('123', '123', 'zfill', 2)
712 self.checkequal('123', '123', 'zfill', 3)
713 self.checkequal('0123', '123', 'zfill', 4)
714 self.checkequal('+123', '+123', 'zfill', 3)
715 self.checkequal('+123', '+123', 'zfill', 4)
716 self.checkequal('+0123', '+123', 'zfill', 5)
717 self.checkequal('-123', '-123', 'zfill', 3)
718 self.checkequal('-123', '-123', 'zfill', 4)
719 self.checkequal('-0123', '-123', 'zfill', 5)
720 self.checkequal('000', '', 'zfill', 3)
721 self.checkequal('34', '34', 'zfill', 1)
722 self.checkequal('0034', '34', 'zfill', 4)
723
724 self.checkraises(TypeError, '123', 'zfill')
725
726class MixinStrUnicodeUserStringTest:
727 # additional tests that only work for
728 # stringlike objects, i.e. str, unicode, UserString
729 # (but not the string module)
730
731 def test_islower(self):
732 self.checkequal(False, '', 'islower')
733 self.checkequal(True, 'a', 'islower')
734 self.checkequal(False, 'A', 'islower')
735 self.checkequal(False, '\n', 'islower')
736 self.checkequal(True, 'abc', 'islower')
737 self.checkequal(False, 'aBc', 'islower')
738 self.checkequal(True, 'abc\n', 'islower')
739 self.checkraises(TypeError, 'abc', 'islower', 42)
740
741 def test_isupper(self):
742 self.checkequal(False, '', 'isupper')
743 self.checkequal(False, 'a', 'isupper')
744 self.checkequal(True, 'A', 'isupper')
745 self.checkequal(False, '\n', 'isupper')
746 self.checkequal(True, 'ABC', 'isupper')
747 self.checkequal(False, 'AbC', 'isupper')
748 self.checkequal(True, 'ABC\n', 'isupper')
749 self.checkraises(TypeError, 'abc', 'isupper', 42)
750
751 def test_istitle(self):
752 self.checkequal(False, '', 'istitle')
753 self.checkequal(False, 'a', 'istitle')
754 self.checkequal(True, 'A', 'istitle')
755 self.checkequal(False, '\n', 'istitle')
756 self.checkequal(True, 'A Titlecased Line', 'istitle')
757 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
758 self.checkequal(True, 'A Titlecased, Line', 'istitle')
759 self.checkequal(False, 'Not a capitalized String', 'istitle')
760 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
761 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
762 self.checkequal(False, 'NOT', 'istitle')
763 self.checkraises(TypeError, 'abc', 'istitle', 42)
764
765 def test_isspace(self):
766 self.checkequal(False, '', 'isspace')
767 self.checkequal(False, 'a', 'isspace')
768 self.checkequal(True, ' ', 'isspace')
769 self.checkequal(True, '\t', 'isspace')
770 self.checkequal(True, '\r', 'isspace')
771 self.checkequal(True, '\n', 'isspace')
772 self.checkequal(True, ' \t\r\n', 'isspace')
773 self.checkequal(False, ' \t\r\na', 'isspace')
774 self.checkraises(TypeError, 'abc', 'isspace', 42)
775
776 def test_isalpha(self):
777 self.checkequal(False, '', 'isalpha')
778 self.checkequal(True, 'a', 'isalpha')
779 self.checkequal(True, 'A', 'isalpha')
780 self.checkequal(False, '\n', 'isalpha')
781 self.checkequal(True, 'abc', 'isalpha')
782 self.checkequal(False, 'aBc123', 'isalpha')
783 self.checkequal(False, 'abc\n', 'isalpha')
784 self.checkraises(TypeError, 'abc', 'isalpha', 42)
785
786 def test_isalnum(self):
787 self.checkequal(False, '', 'isalnum')
788 self.checkequal(True, 'a', 'isalnum')
789 self.checkequal(True, 'A', 'isalnum')
790 self.checkequal(False, '\n', 'isalnum')
791 self.checkequal(True, '123abc456', 'isalnum')
792 self.checkequal(True, 'a1b3c', 'isalnum')
793 self.checkequal(False, 'aBc000 ', 'isalnum')
794 self.checkequal(False, 'abc\n', 'isalnum')
795 self.checkraises(TypeError, 'abc', 'isalnum', 42)
796
797 def test_isdigit(self):
798 self.checkequal(False, '', 'isdigit')
799 self.checkequal(False, 'a', 'isdigit')
800 self.checkequal(True, '0', 'isdigit')
801 self.checkequal(True, '0123456789', 'isdigit')
802 self.checkequal(False, '0123456789a', 'isdigit')
803
804 self.checkraises(TypeError, 'abc', 'isdigit', 42)
805
806 def test_title(self):
807 self.checkequal(' Hello ', ' hello ', 'title')
808 self.checkequal('Hello ', 'hello ', 'title')
809 self.checkequal('Hello ', 'Hello ', 'title')
810 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
811 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
812 self.checkequal('Getint', "getInt", 'title')
813 self.checkraises(TypeError, 'hello', 'title', 42)
814
815 def test_splitlines(self):
816 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
817 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
818 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
819 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
820 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
821 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
822 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
823
824 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
825
826 def test_startswith(self):
827 self.checkequal(True, 'hello', 'startswith', 'he')
828 self.checkequal(True, 'hello', 'startswith', 'hello')
829 self.checkequal(False, 'hello', 'startswith', 'hello world')
830 self.checkequal(True, 'hello', 'startswith', '')
831 self.checkequal(False, 'hello', 'startswith', 'ello')
832 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
833 self.checkequal(True, 'hello', 'startswith', 'o', 4)
834 self.checkequal(False, 'hello', 'startswith', 'o', 5)
835 self.checkequal(True, 'hello', 'startswith', '', 5)
836 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
837 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
838 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
839 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
840
841 # test negative indices
842 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
843 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
844 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
845 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
846 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
847 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
848 self.checkequal(False, 'hello', 'startswith', 'o', -2)
849 self.checkequal(True, 'hello', 'startswith', 'o', -1)
850 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
851 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
852
853 self.checkraises(TypeError, 'hello', 'startswith')
854 self.checkraises(TypeError, 'hello', 'startswith', 42)
855
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000856 # test tuple arguments
857 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
858 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
859 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
860 self.checkequal(False, 'hello', 'startswith', ())
861 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
862 'rld', 'lowo'), 3)
863 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
864 'rld'), 3)
865 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
866 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
867 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
868
869 self.checkraises(TypeError, 'hello', 'startswith', (42,))
870
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000871 def test_endswith(self):
872 self.checkequal(True, 'hello', 'endswith', 'lo')
873 self.checkequal(False, 'hello', 'endswith', 'he')
874 self.checkequal(True, 'hello', 'endswith', '')
875 self.checkequal(False, 'hello', 'endswith', 'hello world')
876 self.checkequal(False, 'helloworld', 'endswith', 'worl')
877 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
878 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
879 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
880 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
881 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
882 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
883 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
884 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
885 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
886
887 # test negative indices
888 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
889 self.checkequal(False, 'hello', 'endswith', 'he', -2)
890 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
891 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
892 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
893 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
894 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
895 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
896 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
897 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
898 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
899 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
900 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
901
902 self.checkraises(TypeError, 'hello', 'endswith')
903 self.checkraises(TypeError, 'hello', 'endswith', 42)
904
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000905 # test tuple arguments
906 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
907 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
908 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
909 self.checkequal(False, 'hello', 'endswith', ())
910 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
911 'rld', 'lowo'), 3)
912 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
913 'rld'), 3, -1)
914 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
915 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
916 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
917
918 self.checkraises(TypeError, 'hello', 'endswith', (42,))
919
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000920 def test___contains__(self):
921 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
922 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
923 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
924 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
925 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
926 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
927 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
928 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
929 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
930
931 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000932 self.checkequal('a', 'abc', '__getitem__', 0)
933 self.checkequal('c', 'abc', '__getitem__', -1)
934 self.checkequal('a', 'abc', '__getitem__', 0)
935 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
936 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
937 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
938 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000939
940 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
941
942 def test_slice(self):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000943 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
944 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
945 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
946 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
947 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
948 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
949 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
950 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
951 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000952
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000953 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000954
Thomas Woutersed03b412007-08-28 21:37:11 +0000955 def test_extended_getslice(self):
956 # Test extended slicing by comparing with list slicing.
957 s = string.ascii_letters + string.digits
958 indices = (0, None, 1, 3, 41, -1, -2, -37)
959 for start in indices:
960 for stop in indices:
961 # Skip step 0 (invalid)
962 for step in indices[1:]:
963 L = list(s)[start:stop:step]
964 self.checkequal("".join(L), s, '__getitem__',
965 slice(start, stop, step))
966
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000967 def test_mul(self):
968 self.checkequal('', 'abc', '__mul__', -1)
969 self.checkequal('', 'abc', '__mul__', 0)
970 self.checkequal('abc', 'abc', '__mul__', 1)
971 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
972 self.checkraises(TypeError, 'abc', '__mul__')
973 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +0000974 # XXX: on a 64-bit system, this doesn't raise an overflow error,
975 # but either raises a MemoryError, or succeeds (if you have 54TiB)
976 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000977
978 def test_join(self):
979 # join now works with any sequence type
980 # moved here, because the argument order is
981 # different in string.join (see the test in
982 # test.test_string.StringTest.test_join)
983 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
984 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000985 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
986 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000987 self.checkequal('w x y z', ' ', 'join', Sequence())
988 self.checkequal('abc', 'a', 'join', ('abc',))
989 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +0000990 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
Guido van Rossumf1044292007-09-27 18:01:22 +0000991 self.checkequal('a.b.3', '.', 'join', ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000992 for i in [5, 25, 125]:
993 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
994 ['a' * i] * i)
995 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
996 ('a' * i,) * i)
997
Guido van Rossumf1044292007-09-27 18:01:22 +0000998 self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000999 self.checkequal('a b c', ' ', 'join', BadSeq2())
1000
1001 self.checkraises(TypeError, ' ', 'join')
1002 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossumf1044292007-09-27 18:01:22 +00001003 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001004 try:
1005 def f():
1006 yield 4 + ""
1007 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001008 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001009 if '+' not in str(e):
1010 self.fail('join() ate exception message')
1011 else:
1012 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001013
1014 def test_formatting(self):
1015 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1016 self.checkequal('+10+', '+%d+', '__mod__', 10)
1017 self.checkequal('a', "%c", '__mod__', "a")
1018 self.checkequal('a', "%c", '__mod__', "a")
1019 self.checkequal('"', "%c", '__mod__', 34)
1020 self.checkequal('$', "%c", '__mod__', 36)
1021 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001022 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001023
1024 for ordinal in (-100, 0x200000):
1025 # unicode raises ValueError, str raises OverflowError
1026 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1027
1028 self.checkequal(' 42', '%3ld', '__mod__', 42)
1029 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001030 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001031
1032 self.checkraises(TypeError, 'abc', '__mod__')
1033 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1034 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1035 self.checkraises(TypeError, '%c', '__mod__', (None,))
1036 self.checkraises(ValueError, '%(foo', '__mod__', {})
1037 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1038
1039 # argument names with properly nested brackets are supported
1040 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1041
1042 # 100 is a magic number in PyUnicode_Format, this forces a resize
1043 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1044
1045 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1046 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1047 self.checkraises(ValueError, '%10', '__mod__', (42,))
1048
1049 def test_floatformatting(self):
1050 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001051 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001052 format = '%%.%if' % prec
1053 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001054 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001055 value = value * 3.141592655 / 3.0 * 10.0
1056 # The formatfloat() code in stringobject.c and
1057 # unicodeobject.c uses a 120 byte buffer and switches from
1058 # 'f' formatting to 'g' at precision 50, so we expect
1059 # OverflowErrors for the ranges x < 50 and prec >= 67.
1060 if x < 50 and prec >= 67:
1061 self.checkraises(OverflowError, format, "__mod__", value)
1062 else:
1063 self.checkcall(format, "__mod__", value)
1064
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065 def test_inplace_rewrites(self):
1066 # Check that strings don't copy and modify cached single-character strings
1067 self.checkequal('a', 'A', 'lower')
1068 self.checkequal(True, 'A', 'isupper')
1069 self.checkequal('A', 'a', 'upper')
1070 self.checkequal(True, 'a', 'islower')
1071
1072 self.checkequal('a', 'A', 'replace', 'A', 'a')
1073 self.checkequal(True, 'A', 'isupper')
1074
1075 self.checkequal('A', 'a', 'capitalize')
1076 self.checkequal(True, 'a', 'islower')
1077
1078 self.checkequal('A', 'a', 'swapcase')
1079 self.checkequal(True, 'a', 'islower')
1080
1081 self.checkequal('A', 'a', 'title')
1082 self.checkequal(True, 'a', 'islower')
1083
1084 def test_partition(self):
1085
1086 self.checkequal(('this is the par', 'ti', 'tion method'),
1087 'this is the partition method', 'partition', 'ti')
1088
1089 # from raymond's original specification
1090 S = 'http://www.python.org'
1091 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1092 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1093 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1094 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1095
1096 self.checkraises(ValueError, S, 'partition', '')
1097 self.checkraises(TypeError, S, 'partition', None)
1098
1099 def test_rpartition(self):
1100
1101 self.checkequal(('this is the rparti', 'ti', 'on method'),
1102 'this is the rpartition method', 'rpartition', 'ti')
1103
1104 # from raymond's original specification
1105 S = 'http://www.python.org'
1106 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001108 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1109 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1110
1111 self.checkraises(ValueError, S, 'rpartition', '')
1112 self.checkraises(TypeError, S, 'rpartition', None)
1113
Walter Dörwald57d88e52004-08-26 16:53:04 +00001114
Walter Dörwald57d88e52004-08-26 16:53:04 +00001115class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001116 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001117
1118 def test_bug1001011(self):
1119 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001120 # involving a subclass.
1121 # Make sure that it is of the appropriate type.
1122 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001123 t = self.type2test
1124 class subclass(t):
1125 pass
1126 s1 = subclass("abcd")
1127 s2 = t().join([s1])
1128 self.assert_(s1 is not s2)
1129 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001130
1131 s1 = t("abcd")
1132 s2 = t().join([s1])
1133 self.assert_(s1 is s2)
1134
1135 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001136 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001137 s1 = subclass("abcd")
1138 s2 = "".join([s1])
1139 self.assert_(s1 is not s2)
1140 self.assert_(type(s2) is t)
1141
1142 s1 = t("abcd")
1143 s2 = "".join([s1])
1144 self.assert_(s1 is s2)
1145
Walter Dörwald4c271fe2007-06-07 13:52:37 +00001146 elif t is str8:
Tim Peters108f1372004-08-27 05:36:07 +00001147 s1 = subclass("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001148 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001149 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001150 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001151
1152 s1 = t("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001153 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001154 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001155 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001156
1157 else:
1158 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)