Example #1
0
def test_chain(values, width, links, bs):
    assert list(rw.chain(*links).read(bio(bs))) == list(values)
    assert rw.chain(*links).write(
        values, BytesIO()
    ).getvalue() == bytearray(bs)

    assert rw.chain(*links).width() == width
Example #2
0
def test_chain_with_list():
    assert rw.chain(
        [rw.number(1), rw.number(2)]
    ).read(bio([1, 2, 3])) == [1, 515]
Example #3
0
])
def test_switch(switch_rw, cases, width, value, bs):
    s_rw = rw.switch(switch_rw, cases)
    assert s_rw.read(bio(bs)) == value
    assert s_rw.write(value, BytesIO()).getvalue() == bytearray(bs)

    assert s_rw.width() == width


@pytest.mark.parametrize('t_rw, bs', [
    (rw.number(1), []),
    (rw.number(2), [1]),
    (rw.number(4), [1, 2, 3]),
    (rw.number(8), range(7)),
    (rw.len_prefixed_string(rw.number(1)), [10, 97]),
    (rw.chain(rw.number(1), rw.number(2)), [1, 2]),
    (rw.switch(rw.number(1), {0: rw.number(2)}), [0, 1]),
])
def test_stream_too_short(t_rw, bs):
    with pytest.raises(ReadError):
        t_rw.read(bio(bs))


class TestDelegatingReadWriter(object):

    def _mk_rw(self, t_rw):
        return type(
            'SomeClass',
            (rw.DelegatingReadWriter,),
            {'__rw__': t_rw}
        )()