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