Browse Source

raise ValueError instead of AssertionError on invalid button index

Fabian Peter Hammerle 3 years ago
parent
commit
5d0ad28103
3 changed files with 27 additions and 2 deletions
  1. 2 1
      CHANGELOG.md
  2. 18 1
      intertechno_cc1101/__init__.py
  3. 7 0
      tests/test_remote_control.py

+ 2 - 1
CHANGELOG.md

@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ## [Unreleased]
 ## [Unreleased]
 ### Changed
 ### Changed
 - command `intertechno-cc1101`: no longer allow abbreviation of options / flags
 - command `intertechno-cc1101`: no longer allow abbreviation of options / flags
-- raise `ValueError` instead of `AssertionError` on invalid remote control address
+- raise `ValueError` instead of `AssertionError` when provided with
+  invalid remote control address or invalid button index
 
 
 ## [0.1.0] - 2021-02-04
 ## [0.1.0] - 2021-02-04
 ### Added
 ### Added

+ 18 - 1
intertechno_cc1101/__init__.py

@@ -60,7 +60,24 @@ class RemoteControl:
 
 
     def _send_command(self, command: int, button_index: int) -> None:
     def _send_command(self, command: int, button_index: int) -> None:
         assert 0 <= command < 2 ** _COMMAND_LENGTH_BITS
         assert 0 <= command < 2 ** _COMMAND_LENGTH_BITS
-        assert 0 <= button_index < 2 ** _BUTTON_INDEX_LENGTH_BITS
+        if not isinstance(button_index, int):
+            raise ValueError(
+                "expected {}-bit unsigned integer as button index, got {!r}".format(
+                    _BUTTON_INDEX_LENGTH_BITS, button_index
+                )
+            )
+        if button_index < 0:
+            raise ValueError(
+                "button index must not be negative (got {!r})".format(button_index)
+            )
+        if button_index >= 2 ** _BUTTON_INDEX_LENGTH_BITS:
+            raise ValueError(
+                "button index must not exceed {} ({}-bit unsigned integer), got {!r}".format(
+                    2 ** _BUTTON_INDEX_LENGTH_BITS,
+                    _BUTTON_INDEX_LENGTH_BITS,
+                    button_index,
+                )
+            )
         _cc1101_transmit(
         _cc1101_transmit(
             _encode_message(
             _encode_message(
                 (self._address << _COMMAND_LENGTH_BITS | command)
                 (self._address << _COMMAND_LENGTH_BITS | command)

+ 7 - 0
tests/test_remote_control.py

@@ -29,6 +29,13 @@ def test__send_command(address, button_index, command, expected_message):
     transmit_mock.assert_called_once_with(b"dummy")
     transmit_mock.assert_called_once_with(b"dummy")
 
 
 
 
+@pytest.mark.parametrize("button_index", (-1, 2 ** 6, 8.15))
+def test__send_command_invalid_button_index(button_index):
+    remote_control = intertechno_cc1101.RemoteControl(address=12345678)
+    with pytest.raises(ValueError):
+        remote_control._send_command(button_index=button_index, command=0b01)
+
+
 @pytest.mark.parametrize("address", [12345678])
 @pytest.mark.parametrize("address", [12345678])
 @pytest.mark.parametrize("button_index", [7])
 @pytest.mark.parametrize("button_index", [7])
 def test_turn_on(address, button_index):
 def test_turn_on(address, button_index):