def test_instance_exception():
    some_rw = InstanceDouble('tchannel.rw.ReadWriter')
    c_rw = rw.instance(ClassWithArgs, ('x', some_rw), ('y', rw.number(4)))
    allow(some_rw).read.and_raise(ReadError("great sadness"))

    with pytest.raises(ReadError):
        c_rw.read(bio([1, 2, 3, 4]))
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_instance(obj, width, params, bs):
    i_rw = rw.instance(*params)
    assert i_rw.read(bio(bs)) == obj
    assert i_rw.write(obj, BytesIO()).getvalue() == bytearray(bs)

    assert i_rw.width() == width