aboutsummaryrefslogtreecommitdiffstats
path: root/test/pathod/test_language_writer.py
blob: 7feb985d5b1bb4165a26ce4a625c53a5837f05dc (plain)
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 io
from pathod import language
from pathod.language import writer


def test_send_chunk():
    v = b"foobarfoobar"
    for bs in range(1, len(v) + 2):
        s = io.BytesIO()
        writer.send_chunk(s, v, bs, 0, len(v))
        assert s.getvalue() == v
        for start in range(len(v)):
            for end in range(len(v)):
                s = io.BytesIO()
                writer.send_chunk(s, v, bs, start, end)
                assert s.getvalue() == v[start:end]


def test_write_values_inject():
    tst = b"foo"

    s = io.BytesIO()
    writer.write_values(s, [tst], [(0, "inject", b"aaa")], blocksize=5)
    assert s.getvalue() == b"aaafoo"

    s = io.BytesIO()
    writer.write_values(s, [tst], [(1, "inject", b"aaa")], blocksize=5)
    assert s.getvalue() == b"faaaoo"

    s = io.BytesIO()
    writer.write_values(s, [tst], [(1, "inject", b"aaa")], blocksize=5)
    assert s.getvalue() == b"faaaoo"


def test_write_values_disconnects():
    s = io.BytesIO()
    tst = b"foo" * 100
    writer.write_values(s, [tst], [(0, "disconnect")], blocksize=5)
    assert not s.getvalue()


def test_write_values():
    tst = b"foobarvoing"
    s = io.BytesIO()
    writer.write_values(s, [tst], [])
    assert s.getvalue() == tst

    for bs in range(1, len(tst) + 2):
        for off in range(len(tst)):
            s = io.BytesIO()
            writer.write_values(
                s, [tst], [(off, "disconnect")], blocksize=bs
            )
            assert s.getvalue() == tst[:off]


def test_write_values_pauses():
    tst = "".join(str(i) for i in range(10)).encode()
    for i in range(2, 10):
        s = io.BytesIO()
        writer.write_values(
            s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i
        )
        assert s.getvalue() == tst

    for i in range(2, 10):
        s = io.BytesIO()
        writer.write_values(s, [tst], [(1, "pause", 0)], blocksize=i)
        assert s.getvalue() == tst

    tst = [tst] * 5
    for i in range(2, 10):
        s = io.BytesIO()
        writer.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i)
        assert s.getvalue() == b"".join(tst)


def test_write_values_after():
    s = io.BytesIO()
    r = next(language.parse_pathod("400:da"))
    language.serve(r, s, {})

    s = io.BytesIO()
    r = next(language.parse_pathod("400:pa,0"))
    language.serve(r, s, {})

    s = io.BytesIO()
    r = next(language.parse_pathod("400:ia,'xx'"))
    language.serve(r, s, {})
    assert s.getvalue().endswith(b'xx')