def test_constant_w(other, v1, v2): # constant(f, x).write(s, *) == f.write(x, *) assert other.write( v1, BytesIO() ).getvalue() == rw.constant(other, v1).write( v2, BytesIO() ).getvalue()
def test_dictionary_ignore_fields(): d_rw = rw.dictionary( ('x', rw.number(1)), (rw.skip, rw.constant(rw.number(2), 42)), ) assert d_rw.read(bio([1, 0, 2])) == {'x': 1} assert d_rw.write( {'x': 1, rw.skip: 2, 'y': 3}, BytesIO() ).getvalue() == bytearray([1, 0, 42]) assert d_rw.width() == 3
def test_instance_ignore(): c_rw = rw.instance( ClassWithArgs, ('x', rw.number(1)), (rw.skip, rw.constant(rw.number(2), 42)), ('y', rw.number(1)), ) assert c_rw.read(bio([1, 2, 3, 4])) == ClassWithArgs(1, 4) assert c_rw.write( ClassWithArgs(1, 2), BytesIO() ).getvalue() == bytearray([1, 0, 42, 2])
def test_constant_r(other, bs): stream = bio(bs) assert rw.constant(other, 42).read(stream) == 42 assert stream.read() == '' assert rw.constant(other, 42).width() == other.width()