Browse Source

increase test coverage of context manager

Fabian Peter Hammerle 3 years ago
parent
commit
fbdcc5bcc9
2 changed files with 20 additions and 1 deletions
  1. 3 1
      cc1101/__init__.py
  2. 17 0
      tests/test_spi.py

+ 3 - 1
cc1101/__init__.py

@@ -532,7 +532,9 @@ class CC1101:
         self._configure_defaults()
         marcstate = self.get_main_radio_control_state_machine_state()
         if marcstate != MainRadioControlStateMachineState.IDLE:
-            raise ValueError("expected marcstate idle (actual: {})".format(marcstate))
+            raise ValueError(
+                "expected marcstate idle (actual: {})".format(marcstate.name)
+            )
         return self
 
     def __exit__(self, exc_type, exc_value, traceback):  # -> typing.Literal[False]

+ 17 - 0
tests/test_spi.py

@@ -139,3 +139,20 @@ def test___enter__permission_error(transceiver, bus, chip_select):
     ):
         with transceiver:
             pass
+
+
+def test___enter__non_idle(transceiver):
+    with unittest.mock.patch.object(
+        transceiver,
+        "get_main_radio_control_state_machine_state",
+        return_value=cc1101.MainRadioControlStateMachineState.TX,
+    ), unittest.mock.patch.object(transceiver, "_reset"), unittest.mock.patch.object(
+        transceiver, "_verify_chip"
+    ), unittest.mock.patch.object(
+        transceiver, "_configure_defaults"
+    ):
+        with pytest.raises(
+            ValueError, match=r"^expected marcstate idle \(actual: TX\)$"
+        ):
+            with transceiver:
+                pass