Browse Source

added tests for `transmit()`

Fabian Peter Hammerle 5 months ago
parent
commit
d3edb13364
1 changed files with 48 additions and 0 deletions
  1. 48 0
      tests/test_transmit.py

+ 48 - 0
tests/test_transmit.py

@@ -4,6 +4,8 @@ import pytest
 
 import cc1101.options
 
+# pylint: disable=protected-access
+
 
 def test_transmit_empty_payload(transceiver):
     with unittest.mock.patch.object(
@@ -62,3 +64,49 @@ def test_transmit_unexpected_payload_len(transceiver, packet_length, payload):
     ):
         with pytest.raises(ValueError, match=r"\bpayload length\b"):
             transceiver.transmit(payload)
+
+
+@pytest.mark.parametrize("payload", (b"\0", b"\xaa\xbb\xcc", bytes(range(42))))
+def test_transmit_fixed(transceiver, payload):
+    transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
+    with unittest.mock.patch.object(
+        transceiver,
+        "get_packet_length_mode",
+        return_value=cc1101.options.PacketLengthMode.FIXED,
+    ), unittest.mock.patch.object(
+        transceiver, "get_packet_length_bytes", return_value=len(payload)
+    ), unittest.mock.patch.object(
+        transceiver,
+        "get_main_radio_control_state_machine_state",
+        return_value=cc1101.MainRadioControlStateMachineState.IDLE,
+    ):
+        transceiver.transmit(payload)
+    assert transceiver._spi.xfer.call_args_list == [
+        unittest.mock.call([0x3B]),  # flush
+        unittest.mock.call([0x3F | 0x40] + list(payload)),
+        unittest.mock.call([0x35]),  # start transmission
+    ]
+
+
+@pytest.mark.parametrize(
+    "payload", (b"\x01\0", b"\x03\xaa\xbb\xcc", b"\x10" + bytes(range(16)))
+)
+def test_transmit_variable(transceiver, payload):
+    transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
+    with unittest.mock.patch.object(
+        transceiver,
+        "get_packet_length_mode",
+        return_value=cc1101.options.PacketLengthMode.VARIABLE,
+    ), unittest.mock.patch.object(
+        transceiver, "get_packet_length_bytes", return_value=255
+    ), unittest.mock.patch.object(
+        transceiver,
+        "get_main_radio_control_state_machine_state",
+        return_value=cc1101.MainRadioControlStateMachineState.IDLE,
+    ):
+        transceiver.transmit(payload)
+    assert transceiver._spi.xfer.call_args_list == [
+        unittest.mock.call([0x3B]),  # flush
+        unittest.mock.call([0x3F | 0x40] + list(payload)),
+        unittest.mock.call([0x35]),  # start transmission
+    ]