[issue5734] Fix BufferedRWPair - Code Review (original) (raw)
OLD
NEW
1 """Unit tests for the io module."""
1 """Unit tests for the io module."""
2
2
3 # Tests of io are scattered over the test suite:
3 # Tests of io are scattered over the test suite:
4 # * test_bufio - tests file buffering
4 # * test_bufio - tests file buffering
5 # * test_memoryio - tests BytesIO and StringIO
5 # * test_memoryio - tests BytesIO and StringIO
6 # * test_fileio - tests FileIO
6 # * test_fileio - tests FileIO
7 # * test_file - tests the file interface
7 # * test_file - tests the file interface
8 # * test_io - tests everything else in the io module
8 # * test_io - tests everything else in the io module
9 # * test_univnewlines - tests universal newline support
9 # * test_univnewlines - tests universal newline support
10 # * test_largefile - tests operations on a file greater than 2**32 bytes
10 # * test_largefile - tests operations on a file greater than 2**32 bytes
(...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1044 self.assert_(wr() is None, wr)
1044 self.assert_(wr() is None, wr)
1045 with open(support.TESTFN, "rb") as f:
1045 with open(support.TESTFN, "rb") as f:
1046 self.assertEqual(f.read(), b"123xxx")
1046 self.assertEqual(f.read(), b"123xxx")
1047
1047
1048
1048
1049 class PyBufferedWriterTest(BufferedWriterTest):
1049 class PyBufferedWriterTest(BufferedWriterTest):
1050 tp = pyio.BufferedWriter
1050 tp = pyio.BufferedWriter
1051
1051
1052 class BufferedRWPairTest(unittest.TestCase):
1052 class BufferedRWPairTest(unittest.TestCase):
1053
1053
1054 def test_basic(self):
1054 def test_constructor(self):
1055 r = self.MockRawIO(())
1055 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1056 w = self.MockRawIO()
1057 pair = self.tp(r, w)
1058 self.assertFalse(pair.closed)
1056 self.assertFalse(pair.closed)
1059
1057
1060 def test_max_buffer_size_deprecation(self):
1058 def test_constructor_max_buffer_size_deprecation(self):
1061 with support.check_warnings() as w:
1059 with support.check_warnings() as w:
1062 warnings.simplefilter("always", DeprecationWarning)
1060 warnings.simplefilter("always", DeprecationWarning)
1063 self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12)
1061 self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12)
1064 self.assertEqual(len(w.warnings), 1)
1062 self.assertEqual(len(w.warnings), 1)
1065 warning = w.warnings[0]
1063 warning = w.warnings[0]
1066 self.assertTrue(warning.category is DeprecationWarning)
1064 self.assertTrue(warning.category is DeprecationWarning)
1067 self.assertEqual(str(warning.message),
1065 self.assertEqual(str(warning.message),
1068 "max_buffer_size is deprecated")
1066 "max_buffer_size is deprecated")
1069
1067
1070 # XXX More Tests
1068 def test_constructor_with_not_readable(self):
1069 class NotReadable(MockRawIO):
1070 def readable(self):
1071 return False
1072
1073 self.assertRaises(IOError, self.tp, NotReadable(), self.MockRawIO())
1074
1075 def test_constructor_with_not_writeable(self):
1076 class NotWriteable(MockRawIO):
1077 def writable(self):
1078 return False
1079
1080 self.assertRaises(IOError, self.tp, self.MockRawIO(), NotWriteable())
1081
1082 def test_read(self):
1083 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1084
1085 self.assertEqual(pair.read(3), b"abc")
1086 self.assertEqual(pair.read(1), b"d")
1087 self.assertEqual(pair.read(), b"ef")
1088
1089 def test_read1(self):
1090 # .read1() is delegated to the underlying reader object, so this test
1091 # can be shallow.
1092 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1093
1094 self.assertEqual(pair.read1(3), b"abc")
1095
1096 def test_readinto(self):
1097 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1098
1099 data = bytearray(5)
1100 self.assertEqual(pair.readinto(data), 5)
1101 self.assertEqual(data, b"abcde")
1102
1103 def test_write(self):
1104 w = self.MockRawIO()
1105 pair = self.tp(self.MockRawIO(), w)
1106
1107 pair.write(b"abc")
1108 pair.flush()
1109 pair.write(b"def")
1110 pair.flush()
1111 self.assertEqual(w._write_stack, [b"abc", b"def"])
1112
1113 def test_peek(self):
1114 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1115
1116 self.assertTrue(pair.peek(3).startswith(b"abc"))
1117 self.assertEqual(pair.read(3), b"abc")
1118
1119 def test_readable(self):
1120 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1121 self.assertTrue(pair.readable)
1122
1123 def test_writeable(self):
1124 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1125 self.assertTrue(pair.writable)
1126
1127 # .flush() is delegated to the underlying writer object and has been
1128 # tested in the test_write method.
1129
1130 def test_close_and_closed(self):
1131 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1132 self.assertFalse(pair.closed)
1133 pair.close()
1134 self.assertTrue(pair.closed)
1135
1136 def test_isatty(self):
1137 class SelectableIsAtty(MockRawIO):
1138 def __init__(self, isatty):
1139 MockRawIO.__init__(self)
1140 self._isatty = isatty
1141
1142 def isatty(self):
1143 return self._isatty
1144
1145 pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(False))
1146 self.assertFalse(pair.isatty())
1147
1148 pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(False))
1149 self.assertTrue(pair.isatty())
1150
1151 pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(True))
1152 self.assertTrue(pair.isatty())
1153
1154 pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(True))
1155 self.assertTrue(pair.isatty())
1071
1156
1072 class CBufferedRWPairTest(BufferedRWPairTest):
1157 class CBufferedRWPairTest(BufferedRWPairTest):
1073 tp = io.BufferedRWPair
1158 tp = io.BufferedRWPair
1074
1159
1075 class PyBufferedRWPairTest(BufferedRWPairTest):
1160 class PyBufferedRWPairTest(BufferedRWPairTest):
1076 tp = pyio.BufferedRWPair
1161 tp = pyio.BufferedRWPair
1077
1162
1078
1163
1079 class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
1164 class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
1080 read_mode = "rb+"
1165 read_mode = "rb+"
(...skipping 1077 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
2158 for name, obj in c_io_ns.items():
2243 for name, obj in c_io_ns.items():
2159 setattr(test, name, obj)
2244 setattr(test, name, obj)
2160 elif test.__name__.startswith("Py"):
2245 elif test.__name__.startswith("Py"):
2161 for name, obj in py_io_ns.items():
2246 for name, obj in py_io_ns.items():
2162 setattr(test, name, obj)
2247 setattr(test, name, obj)
2163
2248
2164 support.run_unittest(*tests)
2249 support.run_unittest(*tests)
2165
2250
2166 if __name__ == "__main__":
2251 if __name__ == "__main__":
2167 test_main()
2252 test_main()
OLD
NEW