Commit 86e0d576 authored by Martin Panter's avatar Martin Panter

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.
parent bc62af1b
This diff is collapsed.
...@@ -45,6 +45,9 @@ class CommonTest(unittest.TestCase): ...@@ -45,6 +45,9 @@ class CommonTest(unittest.TestCase):
else: else:
return obj return obj
def test_fixtype(self):
self.assertIs(type(self.fixtype("123")), self.type2test)
# check that object.method(*args) returns result # check that object.method(*args) returns result
def checkequal(self, result, object, methodname, *args): def checkequal(self, result, object, methodname, *args):
result = self.fixtype(result) result = self.fixtype(result)
...@@ -404,7 +407,9 @@ class CommonTest(unittest.TestCase): ...@@ -404,7 +407,9 @@ class CommonTest(unittest.TestCase):
'split', 'BLAH', 18) 'split', 'BLAH', 18)
# mixed use of str and unicode # 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 # argument type
self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
...@@ -494,7 +499,9 @@ class CommonTest(unittest.TestCase): ...@@ -494,7 +499,9 @@ class CommonTest(unittest.TestCase):
'rsplit', 'BLAH', 18) 'rsplit', 'BLAH', 18)
# mixed use of str and unicode # 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 # argument type
self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
...@@ -522,7 +529,7 @@ class CommonTest(unittest.TestCase): ...@@ -522,7 +529,7 @@ class CommonTest(unittest.TestCase):
self.checkequal('hello', 'hello', 'strip', 'xyz') self.checkequal('hello', 'hello', 'strip', 'xyz')
# strip/lstrip/rstrip with unicode arg # 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', self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
'strip', unicode('xyz', 'ascii')) 'strip', unicode('xyz', 'ascii'))
self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy', self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
...@@ -542,7 +549,11 @@ class CommonTest(unittest.TestCase): ...@@ -542,7 +549,11 @@ class CommonTest(unittest.TestCase):
self.checkequal('abc ', 'abc', 'ljust', 6) self.checkequal('abc ', 'abc', 'ljust', 6)
self.checkequal('abc', 'abc', 'ljust', 3) self.checkequal('abc', 'abc', 'ljust', 3)
self.checkequal('abc', 'abc', 'ljust', 2) 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') self.checkraises(TypeError, 'abc', 'ljust')
def test_rjust(self): def test_rjust(self):
...@@ -550,7 +561,11 @@ class CommonTest(unittest.TestCase): ...@@ -550,7 +561,11 @@ class CommonTest(unittest.TestCase):
self.checkequal(' abc', 'abc', 'rjust', 6) self.checkequal(' abc', 'abc', 'rjust', 6)
self.checkequal('abc', 'abc', 'rjust', 3) self.checkequal('abc', 'abc', 'rjust', 3)
self.checkequal('abc', 'abc', 'rjust', 2) 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') self.checkraises(TypeError, 'abc', 'rjust')
def test_center(self): def test_center(self):
...@@ -558,7 +573,12 @@ class CommonTest(unittest.TestCase): ...@@ -558,7 +573,12 @@ class CommonTest(unittest.TestCase):
self.checkequal(' abc ', 'abc', 'center', 6) self.checkequal(' abc ', 'abc', 'center', 6)
self.checkequal('abc', 'abc', 'center', 3) self.checkequal('abc', 'abc', 'center', 3)
self.checkequal('abc', 'abc', 'center', 2) 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') self.checkraises(TypeError, 'abc', 'center')
def test_swapcase(self): def test_swapcase(self):
...@@ -772,13 +792,10 @@ class CommonTest(unittest.TestCase): ...@@ -772,13 +792,10 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, '123', 'zfill') self.checkraises(TypeError, '123', 'zfill')
# XXX alias for py3k forward compatibility
BaseTest = CommonTest
class MixinStrUnicodeUserStringTest: class NonStringModuleTest:
# additional tests that only work for # additional test cases for all string classes from bytearray to
# stringlike objects, i.e. str, unicode, UserString # UserString, but not valid for the "string" module
# (but not the string module)
def test_islower(self): def test_islower(self):
self.checkequal(False, '', 'islower') self.checkequal(False, '', 'islower')
...@@ -875,6 +892,12 @@ class MixinStrUnicodeUserStringTest: ...@@ -875,6 +892,12 @@ class MixinStrUnicodeUserStringTest:
self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) 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): def test_startswith(self):
self.checkequal(True, 'hello', 'startswith', 'he') self.checkequal(True, 'hello', 'startswith', 'he')
self.checkequal(True, 'hello', 'startswith', 'hello') self.checkequal(True, 'hello', 'startswith', 'hello')
......
"""Unit tests for the bytes and bytearray types. """Unit tests for the bytes and bytearray types.
XXX This is a mess. Common tests should be moved to buffer_tests.py, XXX This is a mess. Common tests should be unified with string_tests.py (and
which itself ought to be unified with string_tests.py (and the latter the latter should be modernized).
should be modernized).
""" """
import os import os
...@@ -15,7 +14,6 @@ import tempfile ...@@ -15,7 +14,6 @@ import tempfile
import unittest import unittest
import test.test_support import test.test_support
import test.string_tests import test.string_tests
import test.buffer_tests
if sys.flags.bytes_warning: if sys.flags.bytes_warning:
...@@ -1030,8 +1028,7 @@ class AssortedBytesTest(unittest.TestCase): ...@@ -1030,8 +1028,7 @@ class AssortedBytesTest(unittest.TestCase):
# the rest that make sense (the code can be cleaned up to use modern # the rest that make sense (the code can be cleaned up to use modern
# unittest methods at the same time). # unittest methods at the same time).
class BytearrayPEP3137Test(unittest.TestCase, class BytearrayPEP3137Test(unittest.TestCase):
test.buffer_tests.MixinBytesBufferCommonTests):
def marshal(self, x): def marshal(self, x):
return bytearray(x) return bytearray(x)
...@@ -1053,12 +1050,10 @@ class BytearrayPEP3137Test(unittest.TestCase, ...@@ -1053,12 +1050,10 @@ class BytearrayPEP3137Test(unittest.TestCase,
self.assertTrue(val is not newval, self.assertTrue(val is not newval,
expr+' returned val on a mutable object') expr+' returned val on a mutable object')
class FixedStringTest(test.string_tests.BaseTest):
def fixtype(self, obj): class ByteArrayAsStringTest(test.string_tests.CommonTest,
if isinstance(obj, str): test.string_tests.NonStringModuleTest):
return obj.encode("utf-8") type2test = bytearray
return super(FixedStringTest, self).fixtype(obj)
# Currently the bytes containment testing uses a single integer # Currently the bytes containment testing uses a single integer
# value. This may not be the final design, but until then the # value. This may not be the final design, but until then the
...@@ -1076,10 +1071,6 @@ class FixedStringTest(test.string_tests.BaseTest): ...@@ -1076,10 +1071,6 @@ class FixedStringTest(test.string_tests.BaseTest):
pass pass
class ByteArrayAsStringTest(FixedStringTest):
type2test = bytearray
class ByteArraySubclass(bytearray): class ByteArraySubclass(bytearray):
pass pass
...@@ -1160,7 +1151,6 @@ class ByteArraySubclassTest(unittest.TestCase): ...@@ -1160,7 +1151,6 @@ class ByteArraySubclassTest(unittest.TestCase):
def test_main(): def test_main():
#test.test_support.run_unittest(BytesTest) #test.test_support.run_unittest(BytesTest)
#test.test_support.run_unittest(AssortedBytesTest) #test.test_support.run_unittest(AssortedBytesTest)
#test.test_support.run_unittest(BytesAsStringTest)
test.test_support.run_unittest( test.test_support.run_unittest(
ByteArrayTest, ByteArrayTest,
ByteArrayAsStringTest, ByteArrayAsStringTest,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment