def setUp(self): self.bdrain = BBuf() """Buffer accepting binary strings (bytes)""" self.udrain = UBuf() """Buffer accepting unicode strings""" self.mock_stdout = mock_stdout() """Stub of sys.stdout under Python 3"""
class OutputTests(unittest.TestCase): bdata = b('\xfc') udata = u'\xfc' def setUp(self): self.bdrain = BBuf() """Buffer accepting binary strings (bytes)""" self.udrain = UBuf() """Buffer accepting unicode strings""" self.mock_stdout = mock_stdout() """Stub of sys.stdout under Python 3""" def test_write_unicode(self): fo = io.FileOutput(destination=self.udrain, encoding='unicode', autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) def test_write_utf8(self): if sys.version_info >= (3,0): fo = io.FileOutput(destination=self.udrain, encoding='utf8', autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) else: fo = io.FileOutput(destination=self.bdrain, encoding='utf8', autoclose=False) fo.write(self.udata) self.assertEqual(self.bdrain.getvalue(), self.udata.encode('utf8')) # With destination in binary mode, data must be binary string # and is written as-is: def test_write_bytes(self): fo = io.FileOutput(destination=self.bdrain, encoding='utf8', mode='wb', autoclose=False) fo.write(self.bdata) self.assertEqual(self.bdrain.getvalue(), self.bdata) # Test for Python 3 features: if sys.version_info >= (3,0): def test_write_bytes_to_stdout(self): # binary data is written to destination.buffer, if the # destination is sys.stdout or sys.stdin backup = sys.stdout sys.stdout = self.mock_stdout fo = io.FileOutput(destination=sys.stdout, mode='wb', autoclose=False) fo.write(self.bdata) self.assertEqual(self.mock_stdout.buffer.getvalue(), self.bdata) sys.stdout = backup def test_encoding_clash(self): # Raise error, if given and destination encodings differ # TODO: try the `write to .buffer` scheme instead? self.assertRaises(ValueError, io.FileOutput, destination=self.mock_stdout, encoding='latin1')
class OutputTests(unittest.TestCase): bdata = b("\xfc") udata = u"\xfc" def setUp(self): self.bdrain = BBuf() """Buffer accepting binary strings (bytes)""" self.udrain = UBuf() """Buffer accepting unicode strings""" self.mock_stdout = mock_stdout() """Stub of sys.stdout under Python 3""" def test_write_unicode(self): fo = io.FileOutput(destination=self.udrain, encoding="unicode", autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) def test_write_utf8(self): if sys.version_info >= (3, 0): fo = io.FileOutput(destination=self.udrain, encoding="utf8", autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) else: fo = io.FileOutput(destination=self.bdrain, encoding="utf8", autoclose=False) fo.write(self.udata) self.assertEqual(self.bdrain.getvalue(), self.udata.encode("utf8")) # With destination in binary mode, data must be binary string # and is written as-is: def test_write_bytes(self): fo = io.FileOutput(destination=self.bdrain, encoding="utf8", mode="wb", autoclose=False) fo.write(self.bdata) self.assertEqual(self.bdrain.getvalue(), self.bdata) # Test for Python 3 features: if sys.version_info >= (3, 0): def test_write_bytes_to_stdout(self): # try writing data to `destination.buffer`, if data is # instance of `bytes` and writing to `destination` fails: fo = io.FileOutput(destination=self.mock_stdout) fo.write(self.bdata) self.assertEqual(self.mock_stdout.buffer.getvalue(), self.bdata) def test_encoding_clash_resolved(self): fo = io.FileOutput(destination=self.mock_stdout, encoding="latin1", autoclose=False) fo.write(self.udata) self.assertEqual(self.mock_stdout.buffer.getvalue(), self.udata.encode("latin1")) def test_encoding_clash_nonresolvable(self): del (self.mock_stdout.buffer) fo = io.FileOutput(destination=self.mock_stdout, encoding="latin1", autoclose=False) self.assertRaises(ValueError, fo.write, self.udata)
def __init__(self): self.buffer = BBuf() UBuf.__init__(self)
class OutputTests(unittest.TestCase): bdata = b'\xfc' udata = u'\xfc' def setUp(self): self.bdrain = BBuf() """Buffer accepting binary strings (bytes)""" self.udrain = UBuf() """Buffer accepting unicode strings""" self.mock_stdout = mock_stdout() """Stub of sys.stdout under Python 3""" def test_write_unicode(self): fo = io.FileOutput(destination=self.udrain, encoding='unicode', autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) def test_write_utf8(self): if sys.version_info >= (3, 0): fo = io.FileOutput(destination=self.udrain, encoding='utf8', autoclose=False) fo.write(self.udata) self.assertEqual(self.udrain.getvalue(), self.udata) else: fo = io.FileOutput(destination=self.bdrain, encoding='utf8', autoclose=False) fo.write(self.udata) self.assertEqual(self.bdrain.getvalue(), self.udata.encode('utf8')) # With destination in binary mode, data must be binary string # and is written as-is: def test_write_bytes(self): fo = io.FileOutput(destination=self.bdrain, encoding='utf8', mode='wb', autoclose=False) fo.write(self.bdata) self.assertEqual(self.bdrain.getvalue(), self.bdata) # Test for Python 3 features: if sys.version_info >= (3, 0): def test_write_bytes_to_stdout(self): # try writing data to `destination.buffer`, if data is # instance of `bytes` and writing to `destination` fails: fo = io.FileOutput(destination=self.mock_stdout) fo.write(self.bdata) self.assertEqual(self.mock_stdout.buffer.getvalue(), self.bdata) def test_encoding_clash_resolved(self): fo = io.FileOutput(destination=self.mock_stdout, encoding='latin1', autoclose=False) fo.write(self.udata) self.assertEqual(self.mock_stdout.buffer.getvalue(), self.udata.encode('latin1')) def test_encoding_clash_nonresolvable(self): del(self.mock_stdout.buffer) fo = io.FileOutput(destination=self.mock_stdout, encoding='latin1', autoclose=False) self.assertRaises(ValueError, fo.write, self.udata)