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