summaryrefslogtreecommitdiffstats
path: root/tests/test_reply_packet_parser.py
blob: 82af1a51fb6ac96c071d1946408c84acfc609969 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pytest

from lirc.exceptions import LircdInvalidReplyPacketError
from lirc.reply_packet_parser import ReplyPacketParser


def test_starting_state_of_reply_packet_parser():
    """
    lirc.reply_packet_parser.ReplyPacketParser.__init__

    Ensure the initial state of the ReplyPacketParser
    is what is expected.
    """
    parser = ReplyPacketParser()  # SUT

    assert not parser.is_finished
    assert not parser.success
    assert parser.data == []


def test_parsing_version_reply_packet():
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that a reply packet from a `VERSION` command
    is parsed correctly.
    """
    parser = ReplyPacketParser()
    packet = ["BEGIN\n", "VERSION\n", "SUCCESS\n", "DATA\n", "1\n", "0.10.1\n", "END\n"]

    for line in packet:
        parser.feed(line)  # SUT

    assert parser.success
    assert parser.is_finished
    assert parser.data == ["0.10.1"]


def test_parsing_remotes_reply_packet():
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that a reply packet from a `LIST` command
    is parsed correctly.
    """
    parser = ReplyPacketParser()
    packet = [
        "BEGIN\n",
        "LIST\n",
        "SUCCESS\n",
        "DATA\n",
        "2\n",
        "some-remote\n",
        "some-other-remote\n",
        "END\n",
    ]

    for line in packet:
        parser.feed(line)  # SUT

    assert parser.success
    assert parser.is_finished
    assert parser.data == ["some-remote", "some-other-remote"]


def test_handling_of_sighup_packet():
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that a reply packet from a `SIGHUP` command
    is parsed correctly and resets the reply parser since
    this just indicates we've parsed
    """
    parser = ReplyPacketParser()
    packet = [
        "BEGIN\n",
        "SIGHUP\n",
        "END\n",
    ]

    for line in packet:
        parser.feed(line)  # SUT

    # The parser should be reset if we recieved a SIGHUP
    # so success would be false and is would not be finished.
    assert not parser.success
    assert not parser.is_finished
    assert parser.data == []


def test_error_with_send_once():
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that an ERROR with a SEND_ONCE command is
    parsed correctly.
    """
    parser = ReplyPacketParser()
    packet = [
        "BEGIN\n",
        "SEND_ONCE remote key_power 1\n",
        "ERROR",
        "DATA",
        "1",
        "hardware does not support sending",
        "END",
    ]

    for line in packet:
        parser.feed(line)  # SUT

    assert not parser.success
    assert parser.is_finished
    assert parser.data == ["hardware does not support sending"]


def test_no_line_passed_to_feed():
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that an empty line or no line (such as None)
    does nothing to advance the parser.
    """
    parser = ReplyPacketParser()

    parser.feed("")  # SUT

    assert not parser.success
    assert not parser.is_finished
    assert parser.data == []


@pytest.mark.parametrize(
    "packet",
    [
        [
            "BEGIN\n",
            "SIGHUP\n",
            "invalid-expected-end\n",
        ],
        [
            "BEGIN\n",
            "VERSION\n",
            "SUCCESS\n",
            "DATA\n",
            "1\n",
            "0.10.1\n",
            "invalid-expected-end\n",
        ],
        ["NOTBEGIN"],
        [
            "BEGIN\n",
            "VERSION\n",
            "not-SUCCESS-or-ERROR\n",
            "DATA\n",
            "1\n",
            "0.10.1\n",
            "END\n",
        ],
        [
            "BEGIN\n",
            "VERSION\n",
            "SUCCESS\n",
            "not-DATA-or-END\n",
            "1\n",
            "0.10.1\n",
            "END\n",
        ],
        [
            "BEGIN\n",
            "VERSION\n",
            "SUCCESS\n",
            "DATA\n",
            "not-a-line-count-left-number\n",
            "0.10.1\n",
            "END\n",
        ],
    ],
)
def test_invalid_end_line_raises_error(packet):
    """
    lirc.reply_packet_parser.ReplyPacketParser.feed

    Ensure that invalid reply packets raise an error.
    """
    parser = ReplyPacketParser()

    with pytest.raises(LircdInvalidReplyPacketError):
        for line in packet:
            parser.feed(line)  # SUT


@pytest.mark.parametrize("line", [None, "", False])
def test_command_state_raises_exception_on_empty_line(line):
    """
    lirc.reply_packet_parser.ReplyPacketParser.__command

    Ensure that the command state raises an error when
    an empty (or falsey) line is passed. This scenario
    is unlikely to happen in practical use since the .feed()
    method simply returns if we recieved an empty line.
    """
    parser = ReplyPacketParser()

    with pytest.raises(LircdInvalidReplyPacketError):
        parser._command(line)