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