def exercise_seek_and_read(self): self.create_file_object(mode="rb") instrumented_file = mock.Mock(spec=self.file_object, wraps=self.file_object) words = ext.read_and_seek(streambuf(instrumented_file)) assert words == b"should, should, uld, ding, fun, [ eof ]" n = streambuf.default_buffer_size soughts = instrumented_file.seek.call_args_list # stringent tests carefully crafted to make sure the seek-in-buffer # optimisation works as expected # C.f. the comment in the C++ function actual_write_test assert soughts[-1] == mock.call(-4, 2) soughts = soughts[:-1] if n >= 14: assert soughts == [] else: assert soughts[0] == mock.call(6, 0) soughts = soughts[1:] if 8 <= n <= 13: assert len(soughts) == 1 and self.only_seek_cur(soughts) elif n == 7: assert len(soughts) == 2 and self.only_seek_cur(soughts) else: assert soughts[0] == mock.call(6, 0) soughts = soughts[1:] assert self.only_seek_cur(soughts) if n == 4: assert len(soughts) == 1 else: assert len(soughts) == 2 self.file_object.close()
def exercise_partial_read(self): self.create_file_object(mode="rb") words = ext.partial_read(streambuf(self.file_object)) assert words == b"Coding, should, " trailing = self.file_object.read() assert trailing == b" be fun" self.file_object.close()
def exercise_read_failure(self): self.create_file_object(mode="rb") self.file_object.close() with pytest.raises(ValueError): ext.read_word(streambuf(self.file_object)) self.file_object.close()
def exercise_read(self): self.create_file_object(mode="rb") words = ext.read_word(streambuf(self.file_object)) assert words == b"Coding, should, be, fun, [ fail, eof ]" self.file_object.close()
def exercise_write_failure(self): self.create_file_object(mode="rb") with pytest.raises(IOError): ext.write_word(streambuf(self.file_object)) self.file_object.close()