Issue #26712: Unify (r)split, (l/r)strip tests into string_tests

This eliminates a few redundant test cases.
This commit is contained in:
Martin Panter 2016-04-10 08:45:26 +00:00
parent d979b2cfcf
commit 0d0db6cc1e
3 changed files with 35 additions and 96 deletions

View File

@ -368,6 +368,8 @@ class BaseTest:
sys.maxsize-2) sys.maxsize-2)
self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2) self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
self.checkequal(['abcd'], 'abcd', 'split', '|')
self.checkequal([''], '', 'split', '|')
self.checkequal(['endcase ', ''], 'endcase |', 'split', '|') self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
self.checkequal(['', ' startcase'], '| startcase', 'split', '|') self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|') self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
@ -435,6 +437,8 @@ class BaseTest:
sys.maxsize-100) sys.maxsize-100)
self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0) self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2) self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
self.checkequal(['abcd'], 'abcd', 'rsplit', '|')
self.checkequal([''], '', 'rsplit', '|')
self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|') self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|') self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|') self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
@ -714,16 +718,21 @@ class BaseTest:
self.checkequal(['a'], ' a ', 'split') self.checkequal(['a'], ' a ', 'split')
self.checkequal(['a', 'b'], ' a b ', 'split') self.checkequal(['a', 'b'], ' a b ', 'split')
self.checkequal(['a', 'b '], ' a b ', 'split', None, 1) self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
self.checkequal(['a b c '], ' a b c ', 'split', None, 0)
self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1) self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2) self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
self.checkequal(['a', 'b', 'c'], ' a b c ', 'split', None, 3)
self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split') self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
aaa = ' a '*20 aaa = ' a '*20
self.checkequal(['a']*20, aaa, 'split') self.checkequal(['a']*20, aaa, 'split')
self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1) self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19) self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
# mixed use of str and unicode for b in ('arf\tbarf', 'arf\nbarf', 'arf\rbarf',
self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2) 'arf\fbarf', 'arf\vbarf'):
self.checkequal(['arf', 'barf'], b, 'split')
self.checkequal(['arf', 'barf'], b, 'split', None)
self.checkequal(['arf', 'barf'], b, 'split', None, 2)
def test_additional_rsplit(self): def test_additional_rsplit(self):
self.checkequal(['this', 'is', 'the', 'rsplit', 'function'], self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
@ -745,36 +754,53 @@ class BaseTest:
self.checkequal(['a'], ' a ', 'rsplit') self.checkequal(['a'], ' a ', 'rsplit')
self.checkequal(['a', 'b'], ' a b ', 'rsplit') self.checkequal(['a', 'b'], ' a b ', 'rsplit')
self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1) self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
self.checkequal([' a b c'], ' a b c ', 'rsplit',
None, 0)
self.checkequal([' a b','c'], ' a b c ', 'rsplit', self.checkequal([' a b','c'], ' a b c ', 'rsplit',
None, 1) None, 1)
self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit', self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
None, 2) None, 2)
self.checkequal(['a', 'b', 'c'], ' a b c ', 'rsplit',
None, 3)
self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88) self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
aaa = ' a '*20 aaa = ' a '*20
self.checkequal(['a']*20, aaa, 'rsplit') self.checkequal(['a']*20, aaa, 'rsplit')
self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1) self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18) self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
# mixed use of str and unicode for b in ('arf\tbarf', 'arf\nbarf', 'arf\rbarf',
self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2) 'arf\fbarf', 'arf\vbarf'):
self.checkequal(['arf', 'barf'], b, 'rsplit')
self.checkequal(['arf', 'barf'], b, 'rsplit', None)
self.checkequal(['arf', 'barf'], b, 'rsplit', None, 2)
def test_strip(self): def test_strip_whitespace(self):
self.checkequal('hello', ' hello ', 'strip') self.checkequal('hello', ' hello ', 'strip')
self.checkequal('hello ', ' hello ', 'lstrip') self.checkequal('hello ', ' hello ', 'lstrip')
self.checkequal(' hello', ' hello ', 'rstrip') self.checkequal(' hello', ' hello ', 'rstrip')
self.checkequal('hello', 'hello', 'strip') self.checkequal('hello', 'hello', 'strip')
b = ' \t\n\r\f\vabc \t\n\r\f\v'
self.checkequal('abc', b, 'strip')
self.checkequal('abc \t\n\r\f\v', b, 'lstrip')
self.checkequal(' \t\n\r\f\vabc', b, 'rstrip')
# strip/lstrip/rstrip with None arg # strip/lstrip/rstrip with None arg
self.checkequal('hello', ' hello ', 'strip', None) self.checkequal('hello', ' hello ', 'strip', None)
self.checkequal('hello ', ' hello ', 'lstrip', None) self.checkequal('hello ', ' hello ', 'lstrip', None)
self.checkequal(' hello', ' hello ', 'rstrip', None) self.checkequal(' hello', ' hello ', 'rstrip', None)
self.checkequal('hello', 'hello', 'strip', None) self.checkequal('hello', 'hello', 'strip', None)
def test_strip(self):
# strip/lstrip/rstrip with str arg # strip/lstrip/rstrip with str arg
self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz') self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz') self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz') self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
self.checkequal('hello', 'hello', 'strip', 'xyz') self.checkequal('hello', 'hello', 'strip', 'xyz')
self.checkequal('', 'mississippi', 'strip', 'mississippi')
# only trim the start and end; does not strip internal characters
self.checkequal('mississipp', 'mississippi', 'strip', 'i')
self.checkraises(TypeError, 'hello', 'strip', 42, 42) self.checkraises(TypeError, 'hello', 'strip', 42, 42)
self.checkraises(TypeError, 'hello', 'lstrip', 42, 42) self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)

View File

@ -495,68 +495,16 @@ class BaseBytesTest:
self.assertEqual(b.replace(b'i', b'a'), b'massassappa') self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
def test_split(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
self.assertEqual(b.split(b'w'), [b])
# with keyword args
b = self.type2test(b'a|b|c|d')
self.assertEqual(b.split(sep=b'|'), [b'a', b'b', b'c', b'd'])
self.assertEqual(b.split(b'|', maxsplit=1), [b'a', b'b|c|d'])
self.assertEqual(b.split(sep=b'|', maxsplit=1), [b'a', b'b|c|d'])
self.assertEqual(b.split(maxsplit=1, sep=b'|'), [b'a', b'b|c|d'])
b = self.type2test(b'a b c d')
self.assertEqual(b.split(maxsplit=1), [b'a', b'b c d'])
def test_split_whitespace(self):
for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
b'arf\fbarf', b'arf\vbarf'):
b = self.type2test(b)
self.assertEqual(b.split(), [b'arf', b'barf'])
self.assertEqual(b.split(None), [b'arf', b'barf'])
self.assertEqual(b.split(None, 2), [b'arf', b'barf'])
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
b = self.type2test(b)
self.assertEqual(b.split(), [b])
self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c '])
self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c '])
self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c '])
self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c'])
def test_split_string_error(self): def test_split_string_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') self.assertRaises(TypeError, self.type2test(b'a b').split, ' ')
def test_split_unicodewhitespace(self): def test_split_unicodewhitespace(self):
for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
b = self.type2test(b)
self.assertEqual(b.split(), [b])
b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
def test_rsplit(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
self.assertEqual(b.rsplit(b'w'), [b])
# with keyword args
b = self.type2test(b'a|b|c|d')
self.assertEqual(b.rsplit(sep=b'|'), [b'a', b'b', b'c', b'd'])
self.assertEqual(b.rsplit(b'|', maxsplit=1), [b'a|b|c', b'd'])
self.assertEqual(b.rsplit(sep=b'|', maxsplit=1), [b'a|b|c', b'd'])
self.assertEqual(b.rsplit(maxsplit=1, sep=b'|'), [b'a|b|c', b'd'])
b = self.type2test(b'a b c d')
self.assertEqual(b.rsplit(maxsplit=1), [b'a b c', b'd'])
def test_rsplit_whitespace(self):
for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
b'arf\fbarf', b'arf\vbarf'):
b = self.type2test(b)
self.assertEqual(b.rsplit(), [b'arf', b'barf'])
self.assertEqual(b.rsplit(None), [b'arf', b'barf'])
self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf'])
self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c'])
self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c'])
self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c'])
self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c'])
def test_rsplit_string_error(self): def test_rsplit_string_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ')
@ -601,37 +549,6 @@ class BaseBytesTest:
it = pickle.loads(d) it = pickle.loads(d)
self.assertEqual(list(it), data[1:]) self.assertEqual(list(it), data[1:])
def test_strip(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.strip(b'i'), b'mississipp')
self.assertEqual(b.strip(b'm'), b'ississippi')
self.assertEqual(b.strip(b'pi'), b'mississ')
self.assertEqual(b.strip(b'im'), b'ssissipp')
self.assertEqual(b.strip(b'pim'), b'ssiss')
self.assertEqual(b.strip(b), b'')
def test_lstrip(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.lstrip(b'i'), b'mississippi')
self.assertEqual(b.lstrip(b'm'), b'ississippi')
self.assertEqual(b.lstrip(b'pi'), b'mississippi')
self.assertEqual(b.lstrip(b'im'), b'ssissippi')
self.assertEqual(b.lstrip(b'pim'), b'ssissippi')
def test_rstrip(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.rstrip(b'i'), b'mississipp')
self.assertEqual(b.rstrip(b'm'), b'mississippi')
self.assertEqual(b.rstrip(b'pi'), b'mississ')
self.assertEqual(b.rstrip(b'im'), b'mississipp')
self.assertEqual(b.rstrip(b'pim'), b'mississ')
def test_strip_whitespace(self):
b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v')
self.assertEqual(b.strip(), b'abc')
self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v')
self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
def test_strip_bytearray(self): def test_strip_bytearray(self):
self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b') self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b')
self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc') self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc')
@ -1470,7 +1387,7 @@ class AssortedBytesTest(unittest.TestCase):
# XXX More string methods? (Those that don't use character properties) # XXX More string methods? (Those that don't use character properties)
# There are tests in string_tests.py that are more # There are tests in string_tests.py that are more
# comprehensive for things like split, partition, etc. # comprehensive for things like partition, etc.
# Unfortunately they are all bundled with tests that # Unfortunately they are all bundled with tests that
# are not appropriate for bytes # are not appropriate for bytes

View File

@ -380,10 +380,6 @@ class UnicodeTest(string_tests.CommonTest,
def test_split(self): def test_split(self):
string_tests.CommonTest.test_split(self) string_tests.CommonTest.test_split(self)
# Mixed arguments
self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test')
# test mixed kinds # test mixed kinds
for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'): for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
left *= 9 left *= 9