Example #1
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 #2
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 #3
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 #4
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 #5
0
    (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"),
])
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()


@pytest.mark.parametrize('num, width, bs', [
    (42, 1, [42]),
    (258, 2, [1, 2]),
    (16909060, 4, [1, 2, 3, 4]),
    (283686952306183, 8, [0, 1, 2, 3, 4, 5, 6, 7]),