Browse Source

raise ValueError instead of AssertionError on invalid remote control address

Fabian Peter Hammerle 3 years ago
parent
commit
ccd1a8a3f0
3 changed files with 21 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 14 1
      intertechno_cc1101/__init__.py
  3. 6 0
      tests/test_remote_control.py

+ 1 - 0
CHANGELOG.md

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

+ 14 - 1
intertechno_cc1101/__init__.py

@@ -42,7 +42,20 @@ def _encode_message(message: int) -> bytes:
 
 class RemoteControl:
     def __init__(self, address: int) -> None:
-        assert 0 <= address < 2 ** _ADDRESS_LENGTH_BITS, address
+        if not isinstance(address, int):
+            raise ValueError(
+                "expected {}-bit unsigned integer as address, got {!r}".format(
+                    _ADDRESS_LENGTH_BITS, address
+                )
+            )
+        if address < 0:
+            raise ValueError("address must not be negative (got {!r})".format(address))
+        if address >= 2 ** _ADDRESS_LENGTH_BITS:
+            raise ValueError(
+                "address must not exceed {} ({}-bit unsigned integer), got {!r}".format(
+                    2 ** _ADDRESS_LENGTH_BITS, _ADDRESS_LENGTH_BITS, address
+                )
+            )
         self._address = address
 
     def _send_command(self, command: int, button_index: int) -> None:

+ 6 - 0
tests/test_remote_control.py

@@ -7,6 +7,12 @@ import intertechno_cc1101
 # pylint: disable=protected-access
 
 
+@pytest.mark.parametrize("address", (-1, 2 ** 26, 21.42))
+def test___init___invalid_address(address):
+    with pytest.raises(ValueError):
+        intertechno_cc1101.RemoteControl(address=address)
+
+
 @pytest.mark.parametrize(
     ("address", "button_index", "command", "expected_message"),
     ((12345678, 0, 0b00, 790123392), (2 ** 26 - 1, 0b1111, 0b01, 4294967263)),