def test_reader_and_writer_noorder(spec, value): """Test serialization and deserialization for types that have no guaranteed order.""" protocol = BinaryProtocol() buffer = WriteBuffer() spec.write_to(protocol.writer(buffer), value) result = spec.read_from(protocol.reader(ReadBuffer(buffer.value))) assert result == value
def test_simple_read(): buff = ReadBuffer(b'abcd') assert buff.take(1) == b'a' assert buff.take(2) == b'bc' with pytest.raises(EndOfInputError): buff.take(2) assert buff.take(1) == b'd'
def test_input_too_short(typ, spec, bs): """Test that EndOfInputError is raised when not enough bytes are available.""" protocol = BinaryProtocol() with pytest.raises(EndOfInputError) as exc_info: s = bytes(bytearray(bs)) protocol.deserialize_value(typ, s) assert 'bytes but got' in str(exc_info) with pytest.raises(EndOfInputError) as exc_info: reader = protocol.reader(ReadBuffer(s)) spec.read_from(reader) assert 'bytes but got' in str(exc_info)
def test_reader_and_writer(typ, bs, spec, value): """Test serialization and deserialization of all samples.""" bs = bytes(bytearray(bs)) protocol = BinaryProtocol() result = protocol.deserialize_value(typ, bs) assert value == result result = protocol.serialize_value(value) assert bs == result buffer = ReadBuffer(bs) deserialized = spec.read_from(protocol.reader(buffer)) buffer = WriteBuffer() spec.write_to(protocol.writer(buffer), deserialized) assert bs == buffer.value
def test_empty_read_buffer(): buff = ReadBuffer(b'') assert buff.take(0) == b'' with pytest.raises(EndOfInputError): buff.take(1)