diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-12-26 15:01:56 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-12-26 15:01:56 -0600 |
| commit | af6d284734607c6ab0a24af82dd1c0d1170af753 (patch) | |
| tree | 1998483b2f914e855d0f9d3f7575b91796158026 /tests/test_reply_packet_parser.py | |
| parent | Clean up and add tests to lircd connection (diff) | |
| download | lirc-af6d284734607c6ab0a24af82dd1c0d1170af753.zip | |
Add reply packet parser tests
Diffstat (limited to 'tests/test_reply_packet_parser.py')
| -rw-r--r-- | tests/test_reply_packet_parser.py | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/tests/test_reply_packet_parser.py b/tests/test_reply_packet_parser.py new file mode 100644 index 0000000..3eadd28 --- /dev/null +++ b/tests/test_reply_packet_parser.py @@ -0,0 +1,194 @@ +import pytest + +from lirc.reply_packet_parser import ReplyPacketParser +from lirc.exceptions import LircdInvalidReplyPacketError + + +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", + "1\n", + "some-remote\n", + "END\n" + ] + + for line in packet: + parser.feed(line) # SUT + + assert parser.success + assert parser.is_finished + assert parser.data == ["some-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 |