[Python-checkins] cpython (2.7): Issue #26257: Eliminate buffer_tests.py and fix ByteArrayAsStringTest

martin.panter python-checkins at python.org
Fri Apr 8 01:32:34 EDT 2016


https://hg.python.org/cpython/rev/26f1543e806c
changeset:   100868:26f1543e806c
branch:      2.7
parent:      100859:7197809a7428
user:        Martin Panter <vadmium+py at gmail.com>
date:        Wed Apr 06 06:37:17 2016 +0000
summary:
  Issue #26257: Eliminate buffer_tests.py and fix ByteArrayAsStringTest

ByteArrayAsStringTest.fixtype() was converting test data to bytes, not byte-
array, therefore many of the test cases inherited in this class were not
actually being run on the bytearray type.

The tests in buffer_tests.py were redundant with methods in string_tests
.MixinStrUnicodeUserStringTest and string_tests.CommonTest. Moved some tests
into a new base class string_tests.NonStringModuleTest, and run them for
bytearray.

files:
  Lib/test/buffer_tests.py |  206 ---------------------------
  Lib/test/string_tests.py |   47 ++++-
  Lib/test/test_bytes.py   |   22 +--
  3 files changed, 41 insertions(+), 234 deletions(-)


diff --git a/Lib/test/buffer_tests.py b/Lib/test/buffer_tests.py
deleted file mode 100644
--- a/Lib/test/buffer_tests.py
+++ /dev/null
@@ -1,206 +0,0 @@
-# Tests that work for both bytes and buffer objects.
-# See PEP 3137.
-
-import struct
-import sys
-
-class MixinBytesBufferCommonTests(object):
-    """Tests that work for both bytes and buffer 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_lower(self):
-        self.assertEqual(b'hello', self.marshal(b'HeLLo').lower())
-        self.assertEqual(b'hello', self.marshal(b'hello').lower())
-        self.assertRaises(TypeError, self.marshal(b'hello').lower, 42)
-
-    def test_upper(self):
-        self.assertEqual(b'HELLO', self.marshal(b'HeLLo').upper())
-        self.assertEqual(b'HELLO', self.marshal(b'HELLO').upper())
-        self.assertRaises(TypeError, self.marshal(b'hello').upper, 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, '*'))
-        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, '*'))
-        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, '*'))
-        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_expandtabs(self):
-        self.assertEqual(b'abc\rab      def\ng       hi',
-                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
-        self.assertEqual(b'abc\rab      def\ng       hi',
-                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
-        self.assertEqual(b'abc\rab  def\ng   hi',
-                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(4))
-        self.assertEqual(b'abc\r\nab  def\ng   hi',
-                         self.marshal(b'abc\r\nab\tdef\ng\thi').expandtabs(4))
-        self.assertEqual(b'abc\rab      def\ng       hi',
-                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
-        self.assertEqual(b'abc\rab      def\ng       hi',
-                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
-        self.assertEqual(b'abc\r\nab\r\ndef\ng\r\nhi',
-            self.marshal(b'abc\r\nab\r\ndef\ng\r\nhi').expandtabs(4))
-        self.assertEqual(b'  a\n b', self.marshal(b' \ta\n\tb').expandtabs(1))
-
-        self.assertRaises(TypeError, self.marshal(b'hello').expandtabs, 42, 42)
-        # This test is only valid when sizeof(int) == sizeof(void*) == 4.
-        if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
-            self.assertRaises(OverflowError,
-                              self.marshal(b'\ta\n\tb').expandtabs, sys.maxint)
-
-    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'\n', b'abc\n', b'def\r\n', b'ghi\n', b'\r'],
-                         self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(1))
-
-        self.assertRaises(TypeError, self.marshal(b'abc').splitlines, 42, 42)
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -45,6 +45,9 @@
         else:
             return obj
 
+    def test_fixtype(self):
+        self.assertIs(type(self.fixtype("123")), self.type2test)
+
     # check that object.method(*args) returns result
     def checkequal(self, result, object, methodname, *args):
         result = self.fixtype(result)
@@ -404,7 +407,9 @@
                         'split', 'BLAH', 18)
 
         # mixed use of str and unicode
-        self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
+        if self.type2test is not bytearray:
+            result = [u'a', u'b', u'c d']
+            self.checkequal(result, 'a b c d', 'split', u' ', 2)
 
         # argument type
         self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
@@ -494,7 +499,9 @@
                         'rsplit', 'BLAH', 18)
 
         # mixed use of str and unicode
-        self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
+        if self.type2test is not bytearray:
+            result = [u'a b', u'c', u'd']
+            self.checkequal(result, 'a b c d', 'rsplit', u' ', 2)
 
         # argument type
         self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
@@ -522,7 +529,7 @@
         self.checkequal('hello', 'hello', 'strip', 'xyz')
 
         # strip/lstrip/rstrip with unicode arg
-        if test_support.have_unicode:
+        if self.type2test is not bytearray and test_support.have_unicode:
             self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
                  'strip', unicode('xyz', 'ascii'))
             self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
@@ -542,7 +549,11 @@
         self.checkequal('abc   ', 'abc', 'ljust', 6)
         self.checkequal('abc', 'abc', 'ljust', 3)
         self.checkequal('abc', 'abc', 'ljust', 2)
-        self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
+        if self.type2test is bytearray:
+            # Special case because bytearray argument is not accepted
+            self.assertEqual(b'abc*******', bytearray(b'abc').ljust(10, '*'))
+        else:
+            self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
         self.checkraises(TypeError, 'abc', 'ljust')
 
     def test_rjust(self):
@@ -550,7 +561,11 @@
         self.checkequal('   abc', 'abc', 'rjust', 6)
         self.checkequal('abc', 'abc', 'rjust', 3)
         self.checkequal('abc', 'abc', 'rjust', 2)
-        self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
+        if self.type2test is bytearray:
+            # Special case because bytearray argument is not accepted
+            self.assertEqual(b'*******abc', bytearray(b'abc').rjust(10, '*'))
+        else:
+            self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
         self.checkraises(TypeError, 'abc', 'rjust')
 
     def test_center(self):
@@ -558,7 +573,12 @@
         self.checkequal(' abc  ', 'abc', 'center', 6)
         self.checkequal('abc', 'abc', 'center', 3)
         self.checkequal('abc', 'abc', 'center', 2)
-        self.checkequal('***abc****', 'abc', 'center', 10, '*')
+        if self.type2test is bytearray:
+            # Special case because bytearray argument is not accepted
+            result = bytearray(b'abc').center(10, '*')
+            self.assertEqual(b'***abc****', result)
+        else:
+            self.checkequal('***abc****', 'abc', 'center', 10, '*')
         self.checkraises(TypeError, 'abc', 'center')
 
     def test_swapcase(self):
@@ -772,13 +792,10 @@
 
         self.checkraises(TypeError, '123', 'zfill')
 
-# XXX alias for py3k forward compatibility
-BaseTest = CommonTest
 
-class MixinStrUnicodeUserStringTest:
-    # additional tests that only work for
-    # stringlike objects, i.e. str, unicode, UserString
-    # (but not the string module)
+class NonStringModuleTest:
+    # additional test cases for all string classes from bytearray to
+    # UserString, but not valid for the "string" module
 
     def test_islower(self):
         self.checkequal(False, '', 'islower')
@@ -875,6 +892,12 @@
 
         self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
 
+
+class MixinStrUnicodeUserStringTest(NonStringModuleTest):
+    # additional tests that only work for
+    # stringlike objects, i.e. str, unicode, UserString
+    # (but not the string module)
+
     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
--- 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
@@ -15,7 +14,6 @@
 import unittest
 import test.test_support
 import test.string_tests
-import test.buffer_tests
 
 
 if sys.flags.bytes_warning:
@@ -1030,8 +1028,7 @@
     # 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)
 
@@ -1053,12 +1050,10 @@
             self.assertTrue(val is not newval,
                             expr+' returned val on a mutable object')
 
-class FixedStringTest(test.string_tests.BaseTest):
 
-    def fixtype(self, obj):
-        if isinstance(obj, str):
-            return obj.encode("utf-8")
-        return super(FixedStringTest, self).fixtype(obj)
+class ByteArrayAsStringTest(test.string_tests.CommonTest,
+        test.string_tests.NonStringModuleTest):
+    type2test = bytearray
 
     # Currently the bytes containment testing uses a single integer
     # value. This may not be the final design, but until then the
@@ -1076,10 +1071,6 @@
         pass
 
 
-class ByteArrayAsStringTest(FixedStringTest):
-    type2test = bytearray
-
-
 class ByteArraySubclass(bytearray):
     pass
 
@@ -1160,7 +1151,6 @@
 def test_main():
     #test.test_support.run_unittest(BytesTest)
     #test.test_support.run_unittest(AssortedBytesTest)
-    #test.test_support.run_unittest(BytesAsStringTest)
     test.test_support.run_unittest(
         ByteArrayTest,
         ByteArrayAsStringTest,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list