File: test_binary_encode.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (90 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pytest

from textual._binary_encode import DecodeError, dump, load


@pytest.mark.parametrize(
    "data",
    [
        None,
        False,
        True,
        -10,
        -1,
        0,
        1,
        100,
        "",
        "💩",
        "Hello",
        b"World",
        b"",
        [],
        (),
        [None],
        [1, 2, 3],
        (0, "foo"),
        (1, "foo"),
        (0, ""),
        ("", "💩", "💩💩"),
        (""),
        ["hello", "world"],
        ["hello", b"world"],
        ("hello", "world"),
        ("hello", b"world"),
        ("foo", "bar", "baz"),
        ("foo " * 1000, "bar " * 100, "baz " * 500),
        (1, "foo", "bar", "baz"),
        {},
        {"foo": "bar"},
        {"foo": "bar", b"egg": b"baz"},
        {"foo": "bar", b"egg": b"baz", "list_of_things": [1, 2, 3, "Paul", "Jessica"]},
        [{}],
        [[1]],
        [(1, 2), (3, 4)],
    ],
)
def test_round_trip(data: object) -> None:
    """Test the data may be encoded then decoded"""
    encoded = dump(data)
    assert isinstance(encoded, bytes)
    decoded = load(encoded)
    assert data == decoded


@pytest.mark.parametrize(
    "data",
    [
        b"",
        b"100:hello",
        b"i",
        b"i1",
        b"i10",
        b"li1e",
        b"x100",
    ],
)
def test_bad_encoding(data: bytes) -> None:
    with pytest.raises(DecodeError):
        load(data)


@pytest.mark.parametrize(
    "data",
    [
        set(),
        float,
        ...,
        [float],
    ],
)
def test_dump_invalid_type(data):
    with pytest.raises(TypeError):
        dump(data)


def test_load_wrong_type():
    with pytest.raises(TypeError):
        load(None)
    with pytest.raises(TypeError):
        load("foo")