diff --git a/Lib/test/buffer_tests.py b/Lib/test/buffer_tests.py deleted file mode 100644 index 8bef7e8bc9c..00000000000 --- a/Lib/test/buffer_tests.py +++ /dev/null @@ -1,176 +0,0 @@ -# Tests that work for bytearray objects. Could be merged into string_tests. -# See PEP 3137. - -class MixinBytesBufferCommonTests(object): - """Tests that work for bytearray objects. - See PEP 3137. - """ - - def marshal(self, x): - """Convert x into the appropriate type for these tests.""" - raise RuntimeError('test class must provide a marshal method') - - def test_islower(self): - self.assertFalse(self.marshal(b'').islower()) - self.assertTrue(self.marshal(b'a').islower()) - self.assertFalse(self.marshal(b'A').islower()) - self.assertFalse(self.marshal(b'\n').islower()) - self.assertTrue(self.marshal(b'abc').islower()) - self.assertFalse(self.marshal(b'aBc').islower()) - self.assertTrue(self.marshal(b'abc\n').islower()) - self.assertRaises(TypeError, self.marshal(b'abc').islower, 42) - - def test_isupper(self): - self.assertFalse(self.marshal(b'').isupper()) - self.assertFalse(self.marshal(b'a').isupper()) - self.assertTrue(self.marshal(b'A').isupper()) - self.assertFalse(self.marshal(b'\n').isupper()) - self.assertTrue(self.marshal(b'ABC').isupper()) - self.assertFalse(self.marshal(b'AbC').isupper()) - self.assertTrue(self.marshal(b'ABC\n').isupper()) - self.assertRaises(TypeError, self.marshal(b'abc').isupper, 42) - - def test_istitle(self): - self.assertFalse(self.marshal(b'').istitle()) - self.assertFalse(self.marshal(b'a').istitle()) - self.assertTrue(self.marshal(b'A').istitle()) - self.assertFalse(self.marshal(b'\n').istitle()) - self.assertTrue(self.marshal(b'A Titlecased Line').istitle()) - self.assertTrue(self.marshal(b'A\nTitlecased Line').istitle()) - self.assertTrue(self.marshal(b'A Titlecased, Line').istitle()) - self.assertFalse(self.marshal(b'Not a capitalized String').istitle()) - self.assertFalse(self.marshal(b'Not\ta Titlecase String').istitle()) - self.assertFalse(self.marshal(b'Not--a Titlecase String').istitle()) - self.assertFalse(self.marshal(b'NOT').istitle()) - self.assertRaises(TypeError, self.marshal(b'abc').istitle, 42) - - def test_isspace(self): - self.assertFalse(self.marshal(b'').isspace()) - self.assertFalse(self.marshal(b'a').isspace()) - self.assertTrue(self.marshal(b' ').isspace()) - self.assertTrue(self.marshal(b'\t').isspace()) - self.assertTrue(self.marshal(b'\r').isspace()) - self.assertTrue(self.marshal(b'\n').isspace()) - self.assertTrue(self.marshal(b' \t\r\n').isspace()) - self.assertFalse(self.marshal(b' \t\r\na').isspace()) - self.assertRaises(TypeError, self.marshal(b'abc').isspace, 42) - - def test_isalpha(self): - self.assertFalse(self.marshal(b'').isalpha()) - self.assertTrue(self.marshal(b'a').isalpha()) - self.assertTrue(self.marshal(b'A').isalpha()) - self.assertFalse(self.marshal(b'\n').isalpha()) - self.assertTrue(self.marshal(b'abc').isalpha()) - self.assertFalse(self.marshal(b'aBc123').isalpha()) - self.assertFalse(self.marshal(b'abc\n').isalpha()) - self.assertRaises(TypeError, self.marshal(b'abc').isalpha, 42) - - def test_isalnum(self): - self.assertFalse(self.marshal(b'').isalnum()) - self.assertTrue(self.marshal(b'a').isalnum()) - self.assertTrue(self.marshal(b'A').isalnum()) - self.assertFalse(self.marshal(b'\n').isalnum()) - self.assertTrue(self.marshal(b'123abc456').isalnum()) - self.assertTrue(self.marshal(b'a1b3c').isalnum()) - self.assertFalse(self.marshal(b'aBc000 ').isalnum()) - self.assertFalse(self.marshal(b'abc\n').isalnum()) - self.assertRaises(TypeError, self.marshal(b'abc').isalnum, 42) - - def test_isdigit(self): - self.assertFalse(self.marshal(b'').isdigit()) - self.assertFalse(self.marshal(b'a').isdigit()) - self.assertTrue(self.marshal(b'0').isdigit()) - self.assertTrue(self.marshal(b'0123456789').isdigit()) - self.assertFalse(self.marshal(b'0123456789a').isdigit()) - - self.assertRaises(TypeError, self.marshal(b'abc').isdigit, 42) - - def test_capitalize(self): - self.assertEqual(b' hello ', self.marshal(b' hello ').capitalize()) - self.assertEqual(b'Hello ', self.marshal(b'Hello ').capitalize()) - self.assertEqual(b'Hello ', self.marshal(b'hello ').capitalize()) - self.assertEqual(b'Aaaa', self.marshal(b'aaaa').capitalize()) - self.assertEqual(b'Aaaa', self.marshal(b'AaAa').capitalize()) - - self.assertRaises(TypeError, self.marshal(b'hello').capitalize, 42) - - def test_ljust(self): - self.assertEqual(b'abc ', self.marshal(b'abc').ljust(10)) - self.assertEqual(b'abc ', self.marshal(b'abc').ljust(6)) - self.assertEqual(b'abc', self.marshal(b'abc').ljust(3)) - self.assertEqual(b'abc', self.marshal(b'abc').ljust(2)) - self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, b'*')) - self.assertRaises(TypeError, self.marshal(b'abc').ljust) - - def test_rjust(self): - self.assertEqual(b' abc', self.marshal(b'abc').rjust(10)) - self.assertEqual(b' abc', self.marshal(b'abc').rjust(6)) - self.assertEqual(b'abc', self.marshal(b'abc').rjust(3)) - self.assertEqual(b'abc', self.marshal(b'abc').rjust(2)) - self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, b'*')) - self.assertRaises(TypeError, self.marshal(b'abc').rjust) - - def test_center(self): - self.assertEqual(b' abc ', self.marshal(b'abc').center(10)) - self.assertEqual(b' abc ', self.marshal(b'abc').center(6)) - self.assertEqual(b'abc', self.marshal(b'abc').center(3)) - self.assertEqual(b'abc', self.marshal(b'abc').center(2)) - self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, b'*')) - self.assertRaises(TypeError, self.marshal(b'abc').center) - - def test_swapcase(self): - self.assertEqual(b'hEllO CoMPuTErS', - self.marshal(b'HeLLo cOmpUteRs').swapcase()) - - self.assertRaises(TypeError, self.marshal(b'hello').swapcase, 42) - - def test_zfill(self): - self.assertEqual(b'123', self.marshal(b'123').zfill(2)) - self.assertEqual(b'123', self.marshal(b'123').zfill(3)) - self.assertEqual(b'0123', self.marshal(b'123').zfill(4)) - self.assertEqual(b'+123', self.marshal(b'+123').zfill(3)) - self.assertEqual(b'+123', self.marshal(b'+123').zfill(4)) - self.assertEqual(b'+0123', self.marshal(b'+123').zfill(5)) - self.assertEqual(b'-123', self.marshal(b'-123').zfill(3)) - self.assertEqual(b'-123', self.marshal(b'-123').zfill(4)) - self.assertEqual(b'-0123', self.marshal(b'-123').zfill(5)) - self.assertEqual(b'000', self.marshal(b'').zfill(3)) - self.assertEqual(b'34', self.marshal(b'34').zfill(1)) - self.assertEqual(b'0034', self.marshal(b'34').zfill(4)) - - self.assertRaises(TypeError, self.marshal(b'123').zfill) - - def test_title(self): - self.assertEqual(b' Hello ', self.marshal(b' hello ').title()) - self.assertEqual(b'Hello ', self.marshal(b'hello ').title()) - self.assertEqual(b'Hello ', self.marshal(b'Hello ').title()) - self.assertEqual(b'Format This As Title String', - self.marshal(b'fOrMaT thIs aS titLe String').title()) - self.assertEqual(b'Format,This-As*Title;String', - self.marshal(b'fOrMaT,thIs-aS*titLe;String').title()) - self.assertEqual(b'Getint', self.marshal(b'getInt').title()) - self.assertRaises(TypeError, self.marshal(b'hello').title, 42) - - def test_splitlines(self): - self.assertEqual([b'abc', b'def', b'', b'ghi'], - self.marshal(b'abc\ndef\n\rghi').splitlines()) - self.assertEqual([b'abc', b'def', b'', b'ghi'], - self.marshal(b'abc\ndef\n\r\nghi').splitlines()) - self.assertEqual([b'abc', b'def', b'ghi'], - self.marshal(b'abc\ndef\r\nghi').splitlines()) - self.assertEqual([b'abc', b'def', b'ghi'], - self.marshal(b'abc\ndef\r\nghi\n').splitlines()) - self.assertEqual([b'abc', b'def', b'ghi', b''], - self.marshal(b'abc\ndef\r\nghi\n\r').splitlines()) - self.assertEqual([b'', b'abc', b'def', b'ghi', b''], - self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines()) - self.assertEqual([b'', b'abc', b'def', b'ghi', b''], - self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(False)) - self.assertEqual([b'\n', b'abc\n', b'def\r\n', b'ghi\n', b'\r'], - self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(True)) - self.assertEqual([b'', b'abc', b'def', b'ghi', b''], - self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(keepends=False)) - self.assertEqual([b'\n', b'abc\n', b'def\r\n', b'ghi\n', b'\r'], - self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(keepends=True)) - - self.assertRaises(TypeError, self.marshal(b'abc').splitlines, 42, 42) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index da79ffa4192..b21fa81ef3d 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -51,6 +51,9 @@ class BaseTest: else: return obj + def test_fixtype(self): + self.assertIs(type(self.fixtype("123")), self.type2test) + # check that obj.method(*args) returns result def checkequal(self, result, obj, methodname, *args, **kwargs): result = self.fixtype(result) @@ -682,21 +685,6 @@ class BaseTest: self.checkraises(OverflowError, A2_16, "replace", "A", A2_16) self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16) - - -class CommonTest(BaseTest): - # This testcase contains tests that can be used in all - # stringlike classes. Currently this is str and UserString. - - def test_hash(self): - # SF bug 1054139: += optimization was not invalidating cached hash value - a = self.type2test('DNSSEC') - b = self.type2test('') - for c in a: - b += c - hash(b) - self.assertEqual(hash(a), hash(b)) - def test_capitalize(self): self.checkequal(' hello ', ' hello ', 'capitalize') self.checkequal('Hello ', 'Hello ','capitalize') @@ -704,23 +692,6 @@ class CommonTest(BaseTest): self.checkequal('Aaaa', 'aaaa', 'capitalize') self.checkequal('Aaaa', 'AaAa', 'capitalize') - # check that titlecased chars are lowered correctly - # \u1ffc is the titlecased char - self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3', - '\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize') - # check with cased non-letter chars - self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd', - '\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize') - self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd', - '\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize') - self.checkequal('\u2160\u2171\u2172', - '\u2160\u2161\u2162', 'capitalize') - self.checkequal('\u2160\u2171\u2172', - '\u2170\u2171\u2172', 'capitalize') - # check with Ll chars with no upper - nothing changes here - self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7', - '\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize') - self.checkraises(TypeError, 'hello', 'capitalize', 42) def test_additional_split(self): @@ -854,10 +825,6 @@ class CommonTest(BaseTest): self.checkraises(TypeError, '123', 'zfill') -class MixinStrUnicodeUserStringTest: - # additional tests that only work for - # stringlike objects, i.e. str, UserString - def test_islower(self): self.checkequal(False, '', 'islower') self.checkequal(True, 'a', 'islower') @@ -960,6 +927,43 @@ class MixinStrUnicodeUserStringTest: self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) + +class CommonTest(BaseTest): + # This testcase contains tests that can be used in all + # stringlike classes. Currently this is str and UserString. + + def test_hash(self): + # SF bug 1054139: += optimization was not invalidating cached hash value + a = self.type2test('DNSSEC') + b = self.type2test('') + for c in a: + b += c + hash(b) + self.assertEqual(hash(a), hash(b)) + + def test_capitalize_nonascii(self): + # check that titlecased chars are lowered correctly + # \u1ffc is the titlecased char + self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3', + '\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize') + # check with cased non-letter chars + self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd', + '\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize') + self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd', + '\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize') + self.checkequal('\u2160\u2171\u2172', + '\u2160\u2161\u2162', 'capitalize') + self.checkequal('\u2160\u2171\u2172', + '\u2170\u2171\u2172', 'capitalize') + # check with Ll chars with no upper - nothing changes here + self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7', + '\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize') + + +class MixinStrUnicodeUserStringTest: + # additional tests that only work for + # stringlike objects, i.e. str, UserString + def test_startswith(self): self.checkequal(True, 'hello', 'startswith', 'he') self.checkequal(True, 'hello', 'startswith', 'hello') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 01ba5e51c82..6bcec23fc99 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -1,8 +1,7 @@ """Unit tests for the bytes and bytearray types. -XXX This is a mess. Common tests should be moved to buffer_tests.py, -which itself ought to be unified with string_tests.py (and the latter -should be modernized). +XXX This is a mess. Common tests should be unified with string_tests.py (and +the latter should be modernized). """ import os @@ -16,7 +15,6 @@ import unittest import test.support import test.string_tests -import test.buffer_tests import test.list_tests from test.support import bigaddrspacetest, MAX_Py_ssize_t @@ -1480,8 +1478,7 @@ class AssortedBytesTest(unittest.TestCase): # the rest that make sense (the code can be cleaned up to use modern # unittest methods at the same time). -class BytearrayPEP3137Test(unittest.TestCase, - test.buffer_tests.MixinBytesBufferCommonTests): +class BytearrayPEP3137Test(unittest.TestCase): def marshal(self, x): return bytearray(x) @@ -1511,16 +1508,16 @@ class BytearrayPEP3137Test(unittest.TestCase, class FixedStringTest(test.string_tests.BaseTest): def fixtype(self, obj): if isinstance(obj, str): - return obj.encode("utf-8") + return self.type2test(obj.encode("utf-8")) return super().fixtype(obj) + contains_bytes = True + class ByteArrayAsStringTest(FixedStringTest, unittest.TestCase): type2test = bytearray - contains_bytes = True class BytesAsStringTest(FixedStringTest, unittest.TestCase): type2test = bytes - contains_bytes = True class SubclassTest: diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index c2811468bae..1a415073d02 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -565,7 +565,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertTrue('\ud800\udc02' < '\ud84d\udc56') def test_islower(self): - string_tests.MixinStrUnicodeUserStringTest.test_islower(self) + super().test_islower() self.checkequalnofix(False, '\u1FFc', 'islower') self.assertFalse('\u2167'.islower()) self.assertTrue('\u2177'.islower()) @@ -580,7 +580,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertFalse('\U0001F46F'.islower()) def test_isupper(self): - string_tests.MixinStrUnicodeUserStringTest.test_isupper(self) + super().test_isupper() if not sys.platform.startswith('java'): self.checkequalnofix(False, '\u1FFc', 'isupper') self.assertTrue('\u2167'.isupper()) @@ -596,7 +596,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertFalse('\U0001F46F'.isupper()) def test_istitle(self): - string_tests.MixinStrUnicodeUserStringTest.test_istitle(self) + super().test_istitle() self.checkequalnofix(True, '\u1FFc', 'istitle') self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle') @@ -608,7 +608,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertFalse(ch.istitle(), '{!a} is not title'.format(ch)) def test_isspace(self): - string_tests.MixinStrUnicodeUserStringTest.test_isspace(self) + super().test_isspace() self.checkequalnofix(True, '\u2000', 'isspace') self.checkequalnofix(True, '\u200a', 'isspace') self.checkequalnofix(False, '\u2014', 'isspace') @@ -618,13 +618,13 @@ class UnicodeTest(string_tests.CommonTest, self.assertFalse(ch.isspace(), '{!a} is not space.'.format(ch)) def test_isalnum(self): - string_tests.MixinStrUnicodeUserStringTest.test_isalnum(self) + super().test_isalnum() for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', '\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']: self.assertTrue(ch.isalnum(), '{!a} is alnum.'.format(ch)) def test_isalpha(self): - string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self) + super().test_isalpha() self.checkequalnofix(True, '\u1FFc', 'isalpha') # non-BMP, cased self.assertTrue('\U00010401'.isalpha()) @@ -654,7 +654,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertTrue(ch.isdecimal(), '{!a} is decimal.'.format(ch)) def test_isdigit(self): - string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self) + super().test_isdigit() self.checkequalnofix(True, '\u2460', 'isdigit') self.checkequalnofix(False, '\xbc', 'isdigit') self.checkequalnofix(True, '\u0660', 'isdigit') @@ -807,7 +807,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2') def test_title(self): - string_tests.MixinStrUnicodeUserStringTest.test_title(self) + super().test_title() self.assertEqual('\U0001044F'.title(), '\U00010427') self.assertEqual('\U0001044F\U0001044F'.title(), '\U00010427\U0001044F')