blob: 916a98f804e457050a301f642dfc00b6aafc3279 [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]
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000016
17class BadSeq2(Sequence):
18 def __init__(self): self.seq = ['a', 'b', 'c']
19 def __len__(self): return 8
20
Georg Brandlc7885542007-03-06 19:16:20 +000021class BaseTest(unittest.TestCase):
22 # These tests are for buffers of values (bytes) and not
23 # specific to character interpretation, used for bytes objects
24 # and various string implementations
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000025
Walter Dörwald0fd583c2003-02-21 12:53:50 +000026 # The type to be tested
27 # Change in subclasses to change the behaviour of fixtesttype()
28 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000029
Walter Dörwald0fd583c2003-02-21 12:53:50 +000030 # 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))
Guido van Rossumcc2b0162007-02-11 06:12:03 +000043 for (key, value) in obj.items()
Walter Dörwald0fd583c2003-02-21 12:53:50 +000044 ])
45 else:
46 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000047
Guido van Rossum09549f42007-08-27 20:40:10 +000048 # check that obj.method(*args) returns result
49 def checkequal(self, result, obj, methodname, *args):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000050 result = self.fixtype(result)
Guido van Rossum09549f42007-08-27 20:40:10 +000051 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000052 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000053 realresult = getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000054 self.assertEqual(
55 result,
56 realresult
57 )
58 # if the original is returned make sure that
59 # this doesn't happen with subclasses
Guido van Rossum09549f42007-08-27 20:40:10 +000060 if obj is realresult:
61 try:
62 class subtype(self.__class__.type2test):
63 pass
64 except TypeError:
65 pass # Skip this if we can't subclass
66 else:
67 obj = subtype(obj)
68 realresult = getattr(obj, methodname)(*args)
69 self.assert_(obj is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000070
Guido van Rossum09549f42007-08-27 20:40:10 +000071 # check that obj.method(*args) raises exc
72 def checkraises(self, exc, obj, methodname, *args):
73 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000074 args = self.fixtype(args)
75 self.assertRaises(
76 exc,
Guido van Rossum09549f42007-08-27 20:40:10 +000077 getattr(obj, methodname),
Walter Dörwald0fd583c2003-02-21 12:53:50 +000078 *args
79 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000080
Guido van Rossum09549f42007-08-27 20:40:10 +000081 # call obj.method(*args) without any checks
82 def checkcall(self, obj, methodname, *args):
83 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000084 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000085 getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000086
Walter Dörwald0fd583c2003-02-21 12:53:50 +000087 def test_count(self):
88 self.checkequal(3, 'aaa', 'count', 'a')
89 self.checkequal(0, 'aaa', 'count', 'b')
90 self.checkequal(3, 'aaa', 'count', 'a')
91 self.checkequal(0, 'aaa', 'count', 'b')
92 self.checkequal(3, 'aaa', 'count', 'a')
93 self.checkequal(0, 'aaa', 'count', 'b')
94 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000095 self.checkequal(2, 'aaa', 'count', 'a', 1)
96 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000097 self.checkequal(1, 'aaa', 'count', 'a', -1)
98 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000099 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
100 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000101 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
102 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000103 self.checkequal(3, 'aaa', 'count', '', 1)
104 self.checkequal(1, 'aaa', 'count', '', 3)
105 self.checkequal(0, 'aaa', 'count', '', 10)
106 self.checkequal(2, 'aaa', 'count', '', -1)
107 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000108
109 self.checkraises(TypeError, 'hello', 'count')
110 self.checkraises(TypeError, 'hello', 'count', 42)
111
Raymond Hettinger57e74472005-02-20 09:54:53 +0000112 # For a variety of combinations,
113 # verify that str.count() matches an equivalent function
114 # replacing all occurrences and then differencing the string lengths
115 charset = ['', 'a', 'b']
116 digits = 7
117 base = len(charset)
118 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000119 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000120 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000121 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000122 i, m = divmod(i, base)
123 entry.append(charset[m])
124 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000125 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger57e74472005-02-20 09:54:53 +0000126 for i in teststrings:
Raymond Hettinger57e74472005-02-20 09:54:53 +0000127 n = len(i)
128 for j in teststrings:
129 r1 = i.count(j)
130 if j:
Guido van Rossum09549f42007-08-27 20:40:10 +0000131 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
132 len(j))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000133 else:
134 r2, rem = len(i)+1, 0
135 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000136 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
137 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000138
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000139 def test_find(self):
140 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
141 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
142 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
143
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000144 self.checkequal(0, 'abc', 'find', '', 0)
145 self.checkequal(3, 'abc', 'find', '', 3)
146 self.checkequal(-1, 'abc', 'find', '', 4)
147
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000148 self.checkraises(TypeError, 'hello', 'find')
149 self.checkraises(TypeError, 'hello', 'find', 42)
150
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000151 # For a variety of combinations,
152 # verify that str.find() matches __contains__
153 # and that the found substring is really at that location
154 charset = ['', 'a', 'b', 'c']
155 digits = 5
156 base = len(charset)
157 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000158 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000159 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000160 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000161 i, m = divmod(i, base)
162 entry.append(charset[m])
163 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000164 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000165 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000166 for j in teststrings:
167 loc = i.find(j)
168 r1 = (loc != -1)
169 r2 = j in i
170 if r1 != r2:
171 self.assertEqual(r1, r2)
172 if loc != -1:
173 self.assertEqual(i[loc:loc+len(j)], j)
174
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000175 def test_rfind(self):
176 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
177 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
178 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
179 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
180
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000181 self.checkequal(3, 'abc', 'rfind', '', 0)
182 self.checkequal(3, 'abc', 'rfind', '', 3)
183 self.checkequal(-1, 'abc', 'rfind', '', 4)
184
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000185 self.checkraises(TypeError, 'hello', 'rfind')
186 self.checkraises(TypeError, 'hello', 'rfind', 42)
187
188 def test_index(self):
189 self.checkequal(0, 'abcdefghiabc', 'index', '')
190 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
191 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
192 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
193
194 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
195 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
196 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
197 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
198
199 self.checkraises(TypeError, 'hello', 'index')
200 self.checkraises(TypeError, 'hello', 'index', 42)
201
202 def test_rindex(self):
203 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
204 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
205 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
206 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
207
208 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
209 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
210 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
211 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
212 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
213
214 self.checkraises(TypeError, 'hello', 'rindex')
215 self.checkraises(TypeError, 'hello', 'rindex', 42)
216
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000217 def test_lower(self):
218 self.checkequal('hello', 'HeLLo', 'lower')
219 self.checkequal('hello', 'hello', 'lower')
220 self.checkraises(TypeError, 'hello', 'lower', 42)
221
222 def test_upper(self):
223 self.checkequal('HELLO', 'HeLLo', 'upper')
224 self.checkequal('HELLO', 'HELLO', 'upper')
225 self.checkraises(TypeError, 'hello', 'upper', 42)
226
227 def test_expandtabs(self):
228 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
229 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
230 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
231 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
232 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
233 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
234 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
235 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
236
237 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
238 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
239 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
240 self.checkraises(OverflowError,
241 '\ta\n\tb', 'expandtabs', sys.maxint)
242
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000243 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000244 # by a char
245 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000247 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
248 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
249 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
250 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000251 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
252 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000253 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
254 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
255 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000256 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
257 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000258 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
259
Thomas Wouters477c8d52006-05-27 19:21:47 +0000260 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
261 self.checkequal(['a']*15 +['a|a|a|a|a'],
262 ('a|'*20)[:-1], 'split', '|', 15)
263
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000264 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000265 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000266 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
267 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
268 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
269 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000270 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
271 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000272 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
273 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000274 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
276 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
277 'split', 'test')
278 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
279 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
280 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
281 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
282 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
283 self.checkequal([''], '', 'split', 'aaa')
284 self.checkequal(['aa'], 'aa', 'split', 'aaa')
285 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
286 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
287
288 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
289 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
290 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
291 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000292
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000293 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000294 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
295
Thomas Wouters477c8d52006-05-27 19:21:47 +0000296 # null case
297 self.checkraises(ValueError, 'hello', 'split', '')
298 self.checkraises(ValueError, 'hello', 'split', '', 0)
299
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000300 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000301 # by a char
302 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
303 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
304 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
305 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
306 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
308 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000309 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
310 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
311 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
313 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
314
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000315 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
316
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
318 self.checkequal(['a|a|a|a|a']+['a']*15,
319 ('a|'*20)[:-1], 'rsplit', '|', 15)
320
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000321 # by string
322 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
323 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
324 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
325 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
326 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000327 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
328 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000329 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
330 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
331 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
333 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
334 'rsplit', 'test')
335 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
336 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
337 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
338 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
339 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
340 self.checkequal([''], '', 'rsplit', 'aaa')
341 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
342 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
343 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
344
345 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
346 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
347 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
348 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000349
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000350 # argument type
351 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000352
Thomas Wouters477c8d52006-05-27 19:21:47 +0000353 # null case
354 self.checkraises(ValueError, 'hello', 'rsplit', '')
355 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
356
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000357 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000358 EQ = self.checkequal
359
360 # Operations on the empty string
361 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000362 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000363 EQ("", "", "replace", "A", "")
364 EQ("", "", "replace", "A", "A")
365 EQ("", "", "replace", "", "", 100)
366 EQ("", "", "replace", "", "", sys.maxint)
367
368 # interleave (from=="", 'to' gets inserted everywhere)
369 EQ("A", "A", "replace", "", "")
370 EQ("*A*", "A", "replace", "", "*")
371 EQ("*1A*1", "A", "replace", "", "*1")
372 EQ("*-#A*-#", "A", "replace", "", "*-#")
373 EQ("*-A*-A*-", "AA", "replace", "", "*-")
374 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
375 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
376 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
377 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
378 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
379 EQ("*-AA", "AA", "replace", "", "*-", 1)
380 EQ("AA", "AA", "replace", "", "*-", 0)
381
382 # single character deletion (from=="A", to=="")
383 EQ("", "A", "replace", "A", "")
384 EQ("", "AAA", "replace", "A", "")
385 EQ("", "AAA", "replace", "A", "", -1)
386 EQ("", "AAA", "replace", "A", "", sys.maxint)
387 EQ("", "AAA", "replace", "A", "", 4)
388 EQ("", "AAA", "replace", "A", "", 3)
389 EQ("A", "AAA", "replace", "A", "", 2)
390 EQ("AA", "AAA", "replace", "A", "", 1)
391 EQ("AAA", "AAA", "replace", "A", "", 0)
392 EQ("", "AAAAAAAAAA", "replace", "A", "")
393 EQ("BCD", "ABACADA", "replace", "A", "")
394 EQ("BCD", "ABACADA", "replace", "A", "", -1)
395 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
396 EQ("BCD", "ABACADA", "replace", "A", "", 5)
397 EQ("BCD", "ABACADA", "replace", "A", "", 4)
398 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
399 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
400 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
401 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
402 EQ("BCD", "ABCAD", "replace", "A", "")
403 EQ("BCD", "ABCADAA", "replace", "A", "")
404 EQ("BCD", "BCD", "replace", "A", "")
405 EQ("*************", "*************", "replace", "A", "")
406 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
407
408 # substring deletion (from=="the", to=="")
409 EQ("", "the", "replace", "the", "")
410 EQ("ater", "theater", "replace", "the", "")
411 EQ("", "thethe", "replace", "the", "")
412 EQ("", "thethethethe", "replace", "the", "")
413 EQ("aaaa", "theatheatheathea", "replace", "the", "")
414 EQ("that", "that", "replace", "the", "")
415 EQ("thaet", "thaet", "replace", "the", "")
416 EQ("here and re", "here and there", "replace", "the", "")
417 EQ("here and re and re", "here and there and there",
418 "replace", "the", "", sys.maxint)
419 EQ("here and re and re", "here and there and there",
420 "replace", "the", "", -1)
421 EQ("here and re and re", "here and there and there",
422 "replace", "the", "", 3)
423 EQ("here and re and re", "here and there and there",
424 "replace", "the", "", 2)
425 EQ("here and re and there", "here and there and there",
426 "replace", "the", "", 1)
427 EQ("here and there and there", "here and there and there",
428 "replace", "the", "", 0)
429 EQ("here and re and re", "here and there and there", "replace", "the", "")
430
431 EQ("abc", "abc", "replace", "the", "")
432 EQ("abcdefg", "abcdefg", "replace", "the", "")
433
434 # substring deletion (from=="bob", to=="")
435 EQ("bob", "bbobob", "replace", "bob", "")
436 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
437 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
438 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
439
440 # single character replace in place (len(from)==len(to)==1)
441 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
442 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
443 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
444 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
445 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
446 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
447 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
448 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
449
450 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
451 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
452 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
453 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
454 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
455
456 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
457
458 # substring replace in place (len(from)==len(to) > 1)
459 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
460 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
461 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
462 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
463 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
464 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
465 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
466 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
467 EQ("cobob", "bobob", "replace", "bob", "cob")
468 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
469 EQ("bobob", "bobob", "replace", "bot", "bot")
470
471 # replace single character (len(from)==1, len(to)>1)
472 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
473 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
474 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
475 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
476 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
477 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
478 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
479
480 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
481
482 # replace substring (len(from)>1, len(to)!=len(from))
483 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
484 "replace", "spam", "ham")
485 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
486 "replace", "spam", "ham", sys.maxint)
487 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
488 "replace", "spam", "ham", -1)
489 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
490 "replace", "spam", "ham", 4)
491 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
492 "replace", "spam", "ham", 3)
493 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
494 "replace", "spam", "ham", 2)
495 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
496 "replace", "spam", "ham", 1)
497 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
498 "replace", "spam", "ham", 0)
499
500 EQ("bobob", "bobobob", "replace", "bobob", "bob")
501 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
502 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
503
Guido van Rossum39478e82007-08-27 17:23:59 +0000504 # XXX Commented out. Is there any reason to support buffer objects
505 # as arguments for str.replace()? GvR
506## ba = buffer('a')
507## bb = buffer('b')
508## EQ("bbc", "abc", "replace", ba, bb)
509## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510
Thomas Wouters477c8d52006-05-27 19:21:47 +0000511 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000512 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
513 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
514 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
515 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
516 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
517 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
518 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
519 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
520 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
521 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
522 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
523 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
524 self.checkequal('', '', 'replace', '', '')
525 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
526 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
527 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
528 # MemoryError due to empty result (platform malloc issue when requesting
529 # 0 bytes).
530 self.checkequal('', '123', 'replace', '123', '')
531 self.checkequal('', '123123', 'replace', '123', '')
532 self.checkequal('x', '123x123', 'replace', '123', '')
533
534 self.checkraises(TypeError, 'hello', 'replace')
535 self.checkraises(TypeError, 'hello', 'replace', 42)
536 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
537 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
538
Thomas Wouters477c8d52006-05-27 19:21:47 +0000539 def test_replace_overflow(self):
540 # Check for overflow checking on 32 bit machines
Guido van Rossum360e4b82007-05-14 22:51:27 +0000541 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000542 return
543 A2_16 = "A" * (2**16)
544 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
545 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
546 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
547
Georg Brandlc7885542007-03-06 19:16:20 +0000548
549
550class CommonTest(BaseTest):
551 # This testcase contains test that can be used in all
552 # stringlike classes. Currently this is str, unicode
553 # UserString and the string module.
554
555 def test_hash(self):
556 # SF bug 1054139: += optimization was not invalidating cached hash value
557 a = self.type2test('DNSSEC')
558 b = self.type2test('')
559 for c in a:
560 b += c
561 hash(b)
562 self.assertEqual(hash(a), hash(b))
563
564 def test_capitalize(self):
565 self.checkequal(' hello ', ' hello ', 'capitalize')
566 self.checkequal('Hello ', 'Hello ','capitalize')
567 self.checkequal('Hello ', 'hello ','capitalize')
568 self.checkequal('Aaaa', 'aaaa', 'capitalize')
569 self.checkequal('Aaaa', 'AaAa', 'capitalize')
570
571 self.checkraises(TypeError, 'hello', 'capitalize', 42)
572
573 def test_lower(self):
574 self.checkequal('hello', 'HeLLo', 'lower')
575 self.checkequal('hello', 'hello', 'lower')
576 self.checkraises(TypeError, 'hello', 'lower', 42)
577
578 def test_upper(self):
579 self.checkequal('HELLO', 'HeLLo', 'upper')
580 self.checkequal('HELLO', 'HELLO', 'upper')
581 self.checkraises(TypeError, 'hello', 'upper', 42)
582
583 def test_expandtabs(self):
584 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
585 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
586 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
587 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
588 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
589 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
590 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
591
592 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
593
594 def test_additional_split(self):
595 self.checkequal(['this', 'is', 'the', 'split', 'function'],
596 'this is the split function', 'split')
597
598 # by whitespace
599 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
600 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
601 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
602 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
603 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
604 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
605 sys.maxint-1)
606 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
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, 2)
609
610 self.checkequal([], ' ', 'split')
611 self.checkequal(['a'], ' a ', 'split')
612 self.checkequal(['a', 'b'], ' a b ', 'split')
613 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
614 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
615 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
616 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
617 aaa = ' a '*20
618 self.checkequal(['a']*20, aaa, 'split')
619 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
620 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
621
622 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000623 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000624
625 def test_additional_rsplit(self):
626 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
627 'this is the rsplit function', 'rsplit')
628
629 # by whitespace
630 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
631 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
632 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
633 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
634 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
635 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
636 sys.maxint-20)
637 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
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, 2)
640
641 self.checkequal([], ' ', 'rsplit')
642 self.checkequal(['a'], ' a ', 'rsplit')
643 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
644 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
645 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
646 None, 1)
647 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
648 None, 2)
649 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
650 aaa = ' a '*20
651 self.checkequal(['a']*20, aaa, 'rsplit')
652 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
653 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
654
655 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000656 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000657
658 def test_strip(self):
659 self.checkequal('hello', ' hello ', 'strip')
660 self.checkequal('hello ', ' hello ', 'lstrip')
661 self.checkequal(' hello', ' hello ', 'rstrip')
662 self.checkequal('hello', 'hello', 'strip')
663
664 # strip/lstrip/rstrip with None arg
665 self.checkequal('hello', ' hello ', 'strip', None)
666 self.checkequal('hello ', ' hello ', 'lstrip', None)
667 self.checkequal(' hello', ' hello ', 'rstrip', None)
668 self.checkequal('hello', 'hello', 'strip', None)
669
670 # strip/lstrip/rstrip with str arg
671 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
672 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
673 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
674 self.checkequal('hello', 'hello', 'strip', 'xyz')
675
Georg Brandlc7885542007-03-06 19:16:20 +0000676 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
677 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
678 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
679
680 def test_ljust(self):
681 self.checkequal('abc ', 'abc', 'ljust', 10)
682 self.checkequal('abc ', 'abc', 'ljust', 6)
683 self.checkequal('abc', 'abc', 'ljust', 3)
684 self.checkequal('abc', 'abc', 'ljust', 2)
685 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
686 self.checkraises(TypeError, 'abc', 'ljust')
687
688 def test_rjust(self):
689 self.checkequal(' abc', 'abc', 'rjust', 10)
690 self.checkequal(' abc', 'abc', 'rjust', 6)
691 self.checkequal('abc', 'abc', 'rjust', 3)
692 self.checkequal('abc', 'abc', 'rjust', 2)
693 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
694 self.checkraises(TypeError, 'abc', 'rjust')
695
696 def test_center(self):
697 self.checkequal(' abc ', 'abc', 'center', 10)
698 self.checkequal(' abc ', 'abc', 'center', 6)
699 self.checkequal('abc', 'abc', 'center', 3)
700 self.checkequal('abc', 'abc', 'center', 2)
701 self.checkequal('***abc****', 'abc', 'center', 10, '*')
702 self.checkraises(TypeError, 'abc', 'center')
703
704 def test_swapcase(self):
705 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
706
707 self.checkraises(TypeError, 'hello', 'swapcase', 42)
708
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000709 def test_zfill(self):
710 self.checkequal('123', '123', 'zfill', 2)
711 self.checkequal('123', '123', 'zfill', 3)
712 self.checkequal('0123', '123', 'zfill', 4)
713 self.checkequal('+123', '+123', 'zfill', 3)
714 self.checkequal('+123', '+123', 'zfill', 4)
715 self.checkequal('+0123', '+123', 'zfill', 5)
716 self.checkequal('-123', '-123', 'zfill', 3)
717 self.checkequal('-123', '-123', 'zfill', 4)
718 self.checkequal('-0123', '-123', 'zfill', 5)
719 self.checkequal('000', '', 'zfill', 3)
720 self.checkequal('34', '34', 'zfill', 1)
721 self.checkequal('0034', '34', 'zfill', 4)
722
723 self.checkraises(TypeError, '123', 'zfill')
724
725class MixinStrUnicodeUserStringTest:
726 # additional tests that only work for
727 # stringlike objects, i.e. str, unicode, UserString
728 # (but not the string module)
729
730 def test_islower(self):
731 self.checkequal(False, '', 'islower')
732 self.checkequal(True, 'a', 'islower')
733 self.checkequal(False, 'A', 'islower')
734 self.checkequal(False, '\n', 'islower')
735 self.checkequal(True, 'abc', 'islower')
736 self.checkequal(False, 'aBc', 'islower')
737 self.checkequal(True, 'abc\n', 'islower')
738 self.checkraises(TypeError, 'abc', 'islower', 42)
739
740 def test_isupper(self):
741 self.checkequal(False, '', 'isupper')
742 self.checkequal(False, 'a', 'isupper')
743 self.checkequal(True, 'A', 'isupper')
744 self.checkequal(False, '\n', 'isupper')
745 self.checkequal(True, 'ABC', 'isupper')
746 self.checkequal(False, 'AbC', 'isupper')
747 self.checkequal(True, 'ABC\n', 'isupper')
748 self.checkraises(TypeError, 'abc', 'isupper', 42)
749
750 def test_istitle(self):
751 self.checkequal(False, '', 'istitle')
752 self.checkequal(False, 'a', 'istitle')
753 self.checkequal(True, 'A', 'istitle')
754 self.checkequal(False, '\n', 'istitle')
755 self.checkequal(True, 'A Titlecased Line', 'istitle')
756 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
757 self.checkequal(True, 'A Titlecased, Line', 'istitle')
758 self.checkequal(False, 'Not a capitalized String', 'istitle')
759 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
760 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
761 self.checkequal(False, 'NOT', 'istitle')
762 self.checkraises(TypeError, 'abc', 'istitle', 42)
763
764 def test_isspace(self):
765 self.checkequal(False, '', 'isspace')
766 self.checkequal(False, 'a', 'isspace')
767 self.checkequal(True, ' ', 'isspace')
768 self.checkequal(True, '\t', 'isspace')
769 self.checkequal(True, '\r', 'isspace')
770 self.checkequal(True, '\n', 'isspace')
771 self.checkequal(True, ' \t\r\n', 'isspace')
772 self.checkequal(False, ' \t\r\na', 'isspace')
773 self.checkraises(TypeError, 'abc', 'isspace', 42)
774
775 def test_isalpha(self):
776 self.checkequal(False, '', 'isalpha')
777 self.checkequal(True, 'a', 'isalpha')
778 self.checkequal(True, 'A', 'isalpha')
779 self.checkequal(False, '\n', 'isalpha')
780 self.checkequal(True, 'abc', 'isalpha')
781 self.checkequal(False, 'aBc123', 'isalpha')
782 self.checkequal(False, 'abc\n', 'isalpha')
783 self.checkraises(TypeError, 'abc', 'isalpha', 42)
784
785 def test_isalnum(self):
786 self.checkequal(False, '', 'isalnum')
787 self.checkequal(True, 'a', 'isalnum')
788 self.checkequal(True, 'A', 'isalnum')
789 self.checkequal(False, '\n', 'isalnum')
790 self.checkequal(True, '123abc456', 'isalnum')
791 self.checkequal(True, 'a1b3c', 'isalnum')
792 self.checkequal(False, 'aBc000 ', 'isalnum')
793 self.checkequal(False, 'abc\n', 'isalnum')
794 self.checkraises(TypeError, 'abc', 'isalnum', 42)
795
796 def test_isdigit(self):
797 self.checkequal(False, '', 'isdigit')
798 self.checkequal(False, 'a', 'isdigit')
799 self.checkequal(True, '0', 'isdigit')
800 self.checkequal(True, '0123456789', 'isdigit')
801 self.checkequal(False, '0123456789a', 'isdigit')
802
803 self.checkraises(TypeError, 'abc', 'isdigit', 42)
804
805 def test_title(self):
806 self.checkequal(' Hello ', ' hello ', 'title')
807 self.checkequal('Hello ', 'hello ', 'title')
808 self.checkequal('Hello ', 'Hello ', 'title')
809 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
810 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
811 self.checkequal('Getint', "getInt", 'title')
812 self.checkraises(TypeError, 'hello', 'title', 42)
813
814 def test_splitlines(self):
815 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
816 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
817 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
818 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
819 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
820 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
821 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
822
823 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
824
825 def test_startswith(self):
826 self.checkequal(True, 'hello', 'startswith', 'he')
827 self.checkequal(True, 'hello', 'startswith', 'hello')
828 self.checkequal(False, 'hello', 'startswith', 'hello world')
829 self.checkequal(True, 'hello', 'startswith', '')
830 self.checkequal(False, 'hello', 'startswith', 'ello')
831 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
832 self.checkequal(True, 'hello', 'startswith', 'o', 4)
833 self.checkequal(False, 'hello', 'startswith', 'o', 5)
834 self.checkequal(True, 'hello', 'startswith', '', 5)
835 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
836 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
837 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
838 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
839
840 # test negative indices
841 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
842 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
843 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
844 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
845 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
846 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
847 self.checkequal(False, 'hello', 'startswith', 'o', -2)
848 self.checkequal(True, 'hello', 'startswith', 'o', -1)
849 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
850 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
851
852 self.checkraises(TypeError, 'hello', 'startswith')
853 self.checkraises(TypeError, 'hello', 'startswith', 42)
854
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000855 # test tuple arguments
856 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
857 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
858 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
859 self.checkequal(False, 'hello', 'startswith', ())
860 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
861 'rld', 'lowo'), 3)
862 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
863 'rld'), 3)
864 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
865 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
866 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
867
868 self.checkraises(TypeError, 'hello', 'startswith', (42,))
869
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000870 def test_endswith(self):
871 self.checkequal(True, 'hello', 'endswith', 'lo')
872 self.checkequal(False, 'hello', 'endswith', 'he')
873 self.checkequal(True, 'hello', 'endswith', '')
874 self.checkequal(False, 'hello', 'endswith', 'hello world')
875 self.checkequal(False, 'helloworld', 'endswith', 'worl')
876 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
877 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
878 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
879 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
880 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
881 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
882 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
883 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
884 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
885
886 # test negative indices
887 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
888 self.checkequal(False, 'hello', 'endswith', 'he', -2)
889 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
890 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
891 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
892 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
893 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
894 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
895 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
896 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
897 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
898 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
899 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
900
901 self.checkraises(TypeError, 'hello', 'endswith')
902 self.checkraises(TypeError, 'hello', 'endswith', 42)
903
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000904 # test tuple arguments
905 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
906 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
907 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
908 self.checkequal(False, 'hello', 'endswith', ())
909 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
910 'rld', 'lowo'), 3)
911 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
912 'rld'), 3, -1)
913 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
914 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
915 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
916
917 self.checkraises(TypeError, 'hello', 'endswith', (42,))
918
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000919 def test___contains__(self):
920 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
921 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
922 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
923 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
924 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
925 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
926 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
927 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
928 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
929
930 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000931 self.checkequal('a', 'abc', '__getitem__', 0)
932 self.checkequal('c', 'abc', '__getitem__', -1)
933 self.checkequal('a', 'abc', '__getitem__', 0)
934 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
935 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
936 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
937 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000938
939 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
940
941 def test_slice(self):
942 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
943 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
944 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
945 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
946 self.checkequal('b', 'abc', '__getslice__', 1, 2)
947 self.checkequal('', 'abc', '__getslice__', 2, 2)
948 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
949 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
950 self.checkequal('', 'abc', '__getslice__', 2, 1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000951
952 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
953
Thomas Woutersed03b412007-08-28 21:37:11 +0000954 def test_extended_getslice(self):
955 # Test extended slicing by comparing with list slicing.
956 s = string.ascii_letters + string.digits
957 indices = (0, None, 1, 3, 41, -1, -2, -37)
958 for start in indices:
959 for stop in indices:
960 # Skip step 0 (invalid)
961 for step in indices[1:]:
962 L = list(s)[start:stop:step]
963 self.checkequal("".join(L), s, '__getitem__',
964 slice(start, stop, step))
965
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000966 def test_mul(self):
967 self.checkequal('', 'abc', '__mul__', -1)
968 self.checkequal('', 'abc', '__mul__', 0)
969 self.checkequal('abc', 'abc', '__mul__', 1)
970 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
971 self.checkraises(TypeError, 'abc', '__mul__')
972 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +0000973 # XXX: on a 64-bit system, this doesn't raise an overflow error,
974 # but either raises a MemoryError, or succeeds (if you have 54TiB)
975 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000976
977 def test_join(self):
978 # join now works with any sequence type
979 # moved here, because the argument order is
980 # different in string.join (see the test in
981 # test.test_string.StringTest.test_join)
982 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
983 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000984 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
985 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000986 self.checkequal('w x y z', ' ', 'join', Sequence())
987 self.checkequal('abc', 'a', 'join', ('abc',))
988 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +0000989 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
990 self.checkraises(TypeError, '.', 'join', ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000991 for i in [5, 25, 125]:
992 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
993 ['a' * i] * i)
994 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
995 ('a' * i,) * i)
996
997 self.checkraises(TypeError, ' ', 'join', BadSeq1())
998 self.checkequal('a b c', ' ', 'join', BadSeq2())
999
1000 self.checkraises(TypeError, ' ', 'join')
1001 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001002 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123]))
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001003 try:
1004 def f():
1005 yield 4 + ""
1006 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001007 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001008 if '+' not in str(e):
1009 self.fail('join() ate exception message')
1010 else:
1011 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001012
1013 def test_formatting(self):
1014 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1015 self.checkequal('+10+', '+%d+', '__mod__', 10)
1016 self.checkequal('a', "%c", '__mod__', "a")
1017 self.checkequal('a', "%c", '__mod__', "a")
1018 self.checkequal('"', "%c", '__mod__', 34)
1019 self.checkequal('$', "%c", '__mod__', 36)
1020 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001021 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001022
1023 for ordinal in (-100, 0x200000):
1024 # unicode raises ValueError, str raises OverflowError
1025 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1026
1027 self.checkequal(' 42', '%3ld', '__mod__', 42)
1028 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001029 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001030
1031 self.checkraises(TypeError, 'abc', '__mod__')
1032 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1033 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1034 self.checkraises(TypeError, '%c', '__mod__', (None,))
1035 self.checkraises(ValueError, '%(foo', '__mod__', {})
1036 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1037
1038 # argument names with properly nested brackets are supported
1039 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1040
1041 # 100 is a magic number in PyUnicode_Format, this forces a resize
1042 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1043
1044 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1045 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1046 self.checkraises(ValueError, '%10', '__mod__', (42,))
1047
1048 def test_floatformatting(self):
1049 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001050 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001051 format = '%%.%if' % prec
1052 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001053 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001054 value = value * 3.141592655 / 3.0 * 10.0
1055 # The formatfloat() code in stringobject.c and
1056 # unicodeobject.c uses a 120 byte buffer and switches from
1057 # 'f' formatting to 'g' at precision 50, so we expect
1058 # OverflowErrors for the ranges x < 50 and prec >= 67.
1059 if x < 50 and prec >= 67:
1060 self.checkraises(OverflowError, format, "__mod__", value)
1061 else:
1062 self.checkcall(format, "__mod__", value)
1063
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064 def test_inplace_rewrites(self):
1065 # Check that strings don't copy and modify cached single-character strings
1066 self.checkequal('a', 'A', 'lower')
1067 self.checkequal(True, 'A', 'isupper')
1068 self.checkequal('A', 'a', 'upper')
1069 self.checkequal(True, 'a', 'islower')
1070
1071 self.checkequal('a', 'A', 'replace', 'A', 'a')
1072 self.checkequal(True, 'A', 'isupper')
1073
1074 self.checkequal('A', 'a', 'capitalize')
1075 self.checkequal(True, 'a', 'islower')
1076
1077 self.checkequal('A', 'a', 'swapcase')
1078 self.checkequal(True, 'a', 'islower')
1079
1080 self.checkequal('A', 'a', 'title')
1081 self.checkequal(True, 'a', 'islower')
1082
1083 def test_partition(self):
1084
1085 self.checkequal(('this is the par', 'ti', 'tion method'),
1086 'this is the partition method', 'partition', 'ti')
1087
1088 # from raymond's original specification
1089 S = 'http://www.python.org'
1090 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1091 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1092 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1093 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1094
1095 self.checkraises(ValueError, S, 'partition', '')
1096 self.checkraises(TypeError, S, 'partition', None)
1097
1098 def test_rpartition(self):
1099
1100 self.checkequal(('this is the rparti', 'ti', 'on method'),
1101 'this is the rpartition method', 'rpartition', 'ti')
1102
1103 # from raymond's original specification
1104 S = 'http://www.python.org'
1105 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001106 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001107 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1108 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1109
1110 self.checkraises(ValueError, S, 'rpartition', '')
1111 self.checkraises(TypeError, S, 'rpartition', None)
1112
Walter Dörwald57d88e52004-08-26 16:53:04 +00001113
Walter Dörwald57d88e52004-08-26 16:53:04 +00001114class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001115 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001116
1117 def test_bug1001011(self):
1118 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001119 # involving a subclass.
1120 # Make sure that it is of the appropriate type.
1121 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001122 t = self.type2test
1123 class subclass(t):
1124 pass
1125 s1 = subclass("abcd")
1126 s2 = t().join([s1])
1127 self.assert_(s1 is not s2)
1128 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001129
1130 s1 = t("abcd")
1131 s2 = t().join([s1])
1132 self.assert_(s1 is s2)
1133
1134 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001135 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001136 s1 = subclass("abcd")
1137 s2 = "".join([s1])
1138 self.assert_(s1 is not s2)
1139 self.assert_(type(s2) is t)
1140
1141 s1 = t("abcd")
1142 s2 = "".join([s1])
1143 self.assert_(s1 is s2)
1144
Walter Dörwald4c271fe2007-06-07 13:52:37 +00001145 elif t is str8:
Tim Peters108f1372004-08-27 05:36:07 +00001146 s1 = subclass("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001147 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001148 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001149 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001150
1151 s1 = t("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001152 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001153 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001154 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001155
1156 else:
1157 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)