|
| 1 | +# mypy: disable-error-code="abstract, method-assign, attr-defined" |
| 2 | +"""This module tests pyfsd.protocol.""" |
| 3 | + |
| 4 | +from unittest import TestCase |
| 5 | +from unittest.mock import Mock, call, patch |
| 6 | + |
| 7 | +from pyfsd.protocol import LineProtocol, LineReceiver |
| 8 | + |
| 9 | + |
| 10 | +class TestLineReceiver(TestCase): |
| 11 | + """tests if linereceiver works.""" |
| 12 | + |
| 13 | + protocol: LineReceiver |
| 14 | + |
| 15 | + @patch.object(LineReceiver, "__abstractmethods__", set()) |
| 16 | + def setUp(self) -> None: |
| 17 | + """Prepare LineReceiver.""" |
| 18 | + self.protocol = LineReceiver() |
| 19 | + self.protocol.buffer_size = 64 |
| 20 | + self.protocol.line_received = Mock() |
| 21 | + self.protocol.buffer_size_exceed = Mock() |
| 22 | + |
| 23 | + def test_line_received(self) -> None: |
| 24 | + """Tests if LineReceiver.line_received works.""" |
| 25 | + self.protocol.data_received(b"1234\r\n5678\r\n") |
| 26 | + self.protocol.line_received.assert_has_calls([call(b"1234"), call(b"5678")]) |
| 27 | + self.protocol.line_received.reset_mock() |
| 28 | + self.protocol.data_received(b"1234\r\n5678") |
| 29 | + self.protocol.line_received.assert_called_once_with(b"1234") |
| 30 | + self.protocol.data_received(b"1234\r\n") |
| 31 | + self.protocol.line_received.assert_called_with(b"56781234") |
| 32 | + self.protocol.line_received.reset_mock() |
| 33 | + self.protocol.data_received(b"abcdefg\r") |
| 34 | + self.protocol.data_received(b"\nhijk") |
| 35 | + self.protocol.line_received.assert_called_once_with(b"abcdefg") |
| 36 | + self.protocol.line_received.reset_mock() |
| 37 | + self.protocol.data_received(b"\r\n") |
| 38 | + self.protocol.line_received.assert_called_once_with(b"hijk") |
| 39 | + |
| 40 | + def test_buffer_size_exceed(self) -> None: |
| 41 | + """Tests if LineReceiver.buffer_size_exceed works.""" |
| 42 | + self.protocol.data_received(b"X" * 65 + b"\r\n") |
| 43 | + self.protocol.buffer_size_exceed.assert_called_with(67) |
| 44 | + self.protocol.buffer_size_exceed.reset_mock() |
| 45 | + self.protocol.buffer = b"" |
| 46 | + self.protocol.data_received(b"X" * 64) |
| 47 | + self.protocol.buffer_size_exceed.assert_not_called() |
| 48 | + self.protocol.data_received(b"X") |
| 49 | + self.protocol.buffer_size_exceed.assert_called_with(65) |
| 50 | + |
| 51 | + |
| 52 | +class TestLineProtocol(TestLineReceiver): |
| 53 | + """Tests if LineProtocol works.""" |
| 54 | + |
| 55 | + @patch.object(LineProtocol, "__abstractmethods__", set()) |
| 56 | + def setUp(self) -> None: |
| 57 | + """Prepare LineProtocol.""" |
| 58 | + self.protocol = LineProtocol() |
| 59 | + self.protocol.buffer_size = 64 |
| 60 | + self.protocol.line_received = Mock() |
| 61 | + self.protocol.buffer_size_exceed = Mock() |
0 commit comments