Example #1
0
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
Example #2
0
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])
Example #3
0
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]))
Example #4
0
def test_headers_with_dict():
    h_rw = rw.headers(
        rw.number(2),
        rw.len_prefixed_string(rw.number(2)),
        rw.len_prefixed_string(rw.number(1))
    )

    headers = {
        'hello': 'world',
        'this': 'is a test',
    }

    buff = h_rw.write(headers, BytesIO()).getvalue()
    assert sorted(h_rw.read(bio(buff)), key=lambda x: x[0]) == [
        ['hello', 'world'],
        ['this', 'is a test']
    ]
Example #5
0
 def test_child_class_must_have_rw(self):
     # Ensure that we require child classes of DelegatingReadWriter to have
     # __rw__
     with pytest.raises(AssertionError):
         type(
             'SomeClass',
             (rw.DelegatingReadWriter,),
             {'__rw': rw.number(1)},
         )
Example #6
0
def test_len_prefixed_string_binary_roundtrip(s, len_width):
    assume(len(s) <= 2 ** len_width - 1)
    assert roundtrip(
        s, rw.len_prefixed_string(rw.number(len_width), is_binary=True)
    ) == s
Example #7
0
def test_len_prefixed_string_roundtrip(s, len_width):
    assume(len(s.encode('utf-8')) <= 2 ** len_width - 1)
    assert roundtrip(s, rw.len_prefixed_string(rw.number(len_width))) == s
Example #8
0
def test_number_roundtrip(num, width):
    num = num % (2 ** width - 1)
    assert roundtrip(num, rw.number(width)) == num
Example #9
0
def test_chain_with_list():
    assert rw.chain(
        [rw.number(1), rw.number(2)]
    ).read(bio([1, 2, 3])) == [1, 515]
Example #10
0
def test_len_prefixed_string_binary(s, len_width, bs):
    s_rw = rw.len_prefixed_string(rw.number(len_width), is_binary=True)
    assert s_rw.read(bio(bs)) == s
    assert s_rw.write(s, BytesIO()).getvalue() == bytearray(bs)

    assert s_rw.width() == len_width
Example #11
0
def test_number(num, width, bs):
    assert rw.number(width).read(bio(bs)) == num
    assert rw.number(width).write(num, BytesIO()).getvalue() == bytearray(bs)
    assert rw.number(width).width() == width
Example #12
0
    stream = bio(bs)
    assert rw.none().read(stream) is None
    stream.read() == 'a b c'

    assert rw.none().width() == 0


def test_none_w():
    stream = BytesIO()
    assert rw.none().write(42, stream) == stream
    assert stream.getvalue() == ''


@pytest.mark.parametrize('other, bs', [
    (rw.none(), []),
    (rw.number(1), [1]),
    (rw.number(4), [1, 2, 3, 4]),
])
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()


@pytest.mark.parametrize('other, v1, v2', [
    (rw.none(), None, None),
    (rw.number(1), 10, 12),
    (rw.number(2), 500, 1),
    (rw.len_prefixed_string(rw.number(1)), "hello world", "test"),