Skip to content

Commit ddb1485

Browse files
asquihugovk
andauthored
gh-81005: Refactor str tests to reflect that str and unicode are merged in Python 3 (#13172)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 76170f5 commit ddb1485

File tree

5 files changed

+31
-39
lines changed

5 files changed

+31
-39
lines changed

Lib/test/string_tests.py

+9-17
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,20 @@
88
from collections import UserList
99
import random
1010

11+
1112
class Sequence:
1213
def __init__(self, seq='wxyz'): self.seq = seq
1314
def __len__(self): return len(self.seq)
1415
def __getitem__(self, i): return self.seq[i]
1516

16-
class BadSeq1(Sequence):
17-
def __init__(self): self.seq = [7, 'hello', 123]
18-
def __str__(self): return '{0} {1} {2}'.format(*self.seq)
19-
20-
class BadSeq2(Sequence):
21-
def __init__(self): self.seq = ['a', 'b', 'c']
22-
def __len__(self): return 8
2317

2418
class BaseTest:
2519
# These tests are for buffers of values (bytes) and not
2620
# specific to character interpretation, used for bytes objects
2721
# and various string implementations
2822

2923
# The type to be tested
30-
# Change in subclasses to change the behaviour of fixtesttype()
24+
# Change in subclasses to change the behaviour of fixtype()
3125
type2test = None
3226

3327
# Whether the "contained items" of the container are integers in
@@ -36,7 +30,7 @@ class BaseTest:
3630
contains_bytes = False
3731

3832
# All tests pass their arguments to the testing methods
39-
# as str objects. fixtesttype() can be used to propagate
33+
# as str objects. fixtype() can be used to propagate
4034
# these arguments to the appropriate type
4135
def fixtype(self, obj):
4236
if isinstance(obj, str):
@@ -1096,7 +1090,7 @@ def test_splitlines(self):
10961090
self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
10971091

10981092

1099-
class CommonTest(BaseTest):
1093+
class StringLikeTest(BaseTest):
11001094
# This testcase contains tests that can be used in all
11011095
# stringlike classes. Currently this is str and UserString.
11021096

@@ -1127,11 +1121,6 @@ def test_capitalize_nonascii(self):
11271121
self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7',
11281122
'\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
11291123

1130-
1131-
class MixinStrUnicodeUserStringTest:
1132-
# additional tests that only work for
1133-
# stringlike objects, i.e. str, UserString
1134-
11351124
def test_startswith(self):
11361125
self.checkequal(True, 'hello', 'startswith', 'he')
11371126
self.checkequal(True, 'hello', 'startswith', 'hello')
@@ -1313,8 +1302,11 @@ def test_join(self):
13131302
self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
13141303
('a' * i,) * i)
13151304

1316-
#self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
1317-
self.checkequal('a b c', ' ', 'join', BadSeq2())
1305+
class LiesAboutLengthSeq(Sequence):
1306+
def __init__(self): self.seq = ['a', 'b', 'c']
1307+
def __len__(self): return 8
1308+
1309+
self.checkequal('a b c', ' ', 'join', LiesAboutLengthSeq())
13181310

13191311
self.checkraises(TypeError, ' ', 'join')
13201312
self.checkraises(TypeError, ' ', 'join', None)

Lib/test/test_builtin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ def test_setattr(self):
16141614
msg = r"^attribute name must be string, not 'int'$"
16151615
self.assertRaisesRegex(TypeError, msg, setattr, sys, 1, 'spam')
16161616

1617-
# test_str(): see test_unicode.py and test_bytes.py for str() tests.
1617+
# test_str(): see test_str.py and test_bytes.py for str() tests.
16181618

16191619
def test_sum(self):
16201620
self.assertEqual(sum([]), 0)

Lib/test/test_unicode.py renamed to Lib/test/test_str.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ def duplicate_string(text):
5555
class StrSubclass(str):
5656
pass
5757

58-
class UnicodeTest(string_tests.CommonTest,
59-
string_tests.MixinStrUnicodeUserStringTest,
58+
class StrTest(string_tests.StringLikeTest,
6059
string_tests.MixinStrUnicodeTest,
6160
unittest.TestCase):
6261

@@ -213,7 +212,7 @@ def test_pickle_iterator(self):
213212
self.assertEqual(case, pickled)
214213

215214
def test_count(self):
216-
string_tests.CommonTest.test_count(self)
215+
string_tests.StringLikeTest.test_count(self)
217216
# check mixed argument types
218217
self.checkequalnofix(3, 'aaa', 'count', 'a')
219218
self.checkequalnofix(0, 'aaa', 'count', 'b')
@@ -243,7 +242,7 @@ class MyStr(str):
243242
self.checkequal(3, MyStr('aaa'), 'count', 'a')
244243

245244
def test_find(self):
246-
string_tests.CommonTest.test_find(self)
245+
string_tests.StringLikeTest.test_find(self)
247246
# test implementation details of the memchr fast path
248247
self.checkequal(100, 'a' * 100 + '\u0102', 'find', '\u0102')
249248
self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0201')
@@ -288,7 +287,7 @@ def test_find(self):
288287
self.checkequal(-1, '\u0102' * 100, 'find', '\u0102\U00100304')
289288

290289
def test_rfind(self):
291-
string_tests.CommonTest.test_rfind(self)
290+
string_tests.StringLikeTest.test_rfind(self)
292291
# test implementation details of the memrchr fast path
293292
self.checkequal(0, '\u0102' + 'a' * 100 , 'rfind', '\u0102')
294293
self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0201')
@@ -329,7 +328,7 @@ def test_rfind(self):
329328
self.checkequal(-1, '\u0102' * 100, 'rfind', '\U00100304\u0102')
330329

331330
def test_index(self):
332-
string_tests.CommonTest.test_index(self)
331+
string_tests.StringLikeTest.test_index(self)
333332
self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
334333
self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
335334
self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
@@ -353,7 +352,7 @@ def test_index(self):
353352
self.assertRaises(ValueError, ('\u0102' * 100).index, '\u0102\U00100304')
354353

355354
def test_rindex(self):
356-
string_tests.CommonTest.test_rindex(self)
355+
string_tests.StringLikeTest.test_rindex(self)
357356
self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
358357
self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
359358
self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
@@ -449,7 +448,7 @@ def test_maketrans_translate(self):
449448
self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
450449

451450
def test_split(self):
452-
string_tests.CommonTest.test_split(self)
451+
string_tests.StringLikeTest.test_split(self)
453452

454453
# test mixed kinds
455454
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
@@ -466,7 +465,7 @@ def test_split(self):
466465
left + delim * 2 + right, 'split', delim *2)
467466

468467
def test_rsplit(self):
469-
string_tests.CommonTest.test_rsplit(self)
468+
string_tests.StringLikeTest.test_rsplit(self)
470469
# test mixed kinds
471470
for left, right in ('ba', 'юё', '\u0101\u0100', '\U00010301\U00010300'):
472471
left *= 9
@@ -486,7 +485,7 @@ def test_rsplit(self):
486485
left + right, 'rsplit', None)
487486

488487
def test_partition(self):
489-
string_tests.MixinStrUnicodeUserStringTest.test_partition(self)
488+
string_tests.StringLikeTest.test_partition(self)
490489
# test mixed kinds
491490
self.checkequal(('ABCDEFGH', '', ''), 'ABCDEFGH', 'partition', '\u4200')
492491
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
@@ -503,7 +502,7 @@ def test_partition(self):
503502
left + delim * 2 + right, 'partition', delim * 2)
504503

505504
def test_rpartition(self):
506-
string_tests.MixinStrUnicodeUserStringTest.test_rpartition(self)
505+
string_tests.StringLikeTest.test_rpartition(self)
507506
# test mixed kinds
508507
self.checkequal(('', '', 'ABCDEFGH'), 'ABCDEFGH', 'rpartition', '\u4200')
509508
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
@@ -520,7 +519,7 @@ def test_rpartition(self):
520519
left + delim * 2 + right, 'rpartition', delim * 2)
521520

522521
def test_join(self):
523-
string_tests.MixinStrUnicodeUserStringTest.test_join(self)
522+
string_tests.StringLikeTest.test_join(self)
524523

525524
class MyWrapper:
526525
def __init__(self, sval): self.sval = sval
@@ -547,7 +546,7 @@ def test_join_overflow(self):
547546
self.assertRaises(OverflowError, ''.join, seq)
548547

549548
def test_replace(self):
550-
string_tests.CommonTest.test_replace(self)
549+
string_tests.StringLikeTest.test_replace(self)
551550

552551
# method call forwarded from str implementation because of unicode argument
553552
self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
@@ -866,7 +865,7 @@ def test_surrogates(self):
866865

867866

868867
def test_lower(self):
869-
string_tests.CommonTest.test_lower(self)
868+
string_tests.StringLikeTest.test_lower(self)
870869
self.assertEqual('\U00010427'.lower(), '\U0001044F')
871870
self.assertEqual('\U00010427\U00010427'.lower(),
872871
'\U0001044F\U0001044F')
@@ -897,7 +896,7 @@ def test_casefold(self):
897896
self.assertEqual('\u00b5'.casefold(), '\u03bc')
898897

899898
def test_upper(self):
900-
string_tests.CommonTest.test_upper(self)
899+
string_tests.StringLikeTest.test_upper(self)
901900
self.assertEqual('\U0001044F'.upper(), '\U00010427')
902901
self.assertEqual('\U0001044F\U0001044F'.upper(),
903902
'\U00010427\U00010427')
@@ -914,7 +913,7 @@ def test_upper(self):
914913
self.assertEqual('\u2177'.upper(), '\u2167')
915914

916915
def test_capitalize(self):
917-
string_tests.CommonTest.test_capitalize(self)
916+
string_tests.StringLikeTest.test_capitalize(self)
918917
self.assertEqual('\U0001044F'.capitalize(), '\U00010427')
919918
self.assertEqual('\U0001044F\U0001044F'.capitalize(),
920919
'\U00010427\U0001044F')
@@ -948,7 +947,7 @@ def test_title(self):
948947
self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')
949948

950949
def test_swapcase(self):
951-
string_tests.CommonTest.test_swapcase(self)
950+
string_tests.StringLikeTest.test_swapcase(self)
952951
self.assertEqual('\U0001044F'.swapcase(), '\U00010427')
953952
self.assertEqual('\U00010427'.swapcase(), '\U0001044F')
954953
self.assertEqual('\U0001044F\U0001044F'.swapcase(),
@@ -974,7 +973,7 @@ def test_swapcase(self):
974973
self.assertEqual('\u1fd2'.swapcase(), '\u0399\u0308\u0300')
975974

976975
def test_center(self):
977-
string_tests.CommonTest.test_center(self)
976+
string_tests.StringLikeTest.test_center(self)
978977
self.assertEqual('x'.center(2, '\U0010FFFF'),
979978
'x\U0010FFFF')
980979
self.assertEqual('x'.center(3, '\U0010FFFF'),
@@ -1475,7 +1474,7 @@ def __format__(self, spec):
14751474
self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
14761475

14771476
def test_formatting(self):
1478-
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
1477+
string_tests.StringLikeTest.test_formatting(self)
14791478
# Testing Unicode formatting strings...
14801479
self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
14811480
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')

Lib/test/test_userstring.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from collections import UserString
88

99
class UserStringTest(
10-
string_tests.CommonTest,
11-
string_tests.MixinStrUnicodeUserStringTest,
10+
string_tests.StringLikeTest,
1211
unittest.TestCase
1312
):
1413

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
String tests are modified to reflect that ``str`` and ``unicode`` are merged
2+
in Python 3. Patch by Daniel Fortunov.

0 commit comments

Comments
 (0)