-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace ERROR NamedInts by IntEnum (#2645)
* refactoring(logitech_receiver/notifications): change to enums PairingError and BoltPairingError * refactoring(logitech_receiver/notifications): change to enums PairingError and BoltPairingError (Fix pre-commit checks) * refactor(logitech_receiver/base.py): create unit tests for ping function before replacing ERRORNamedInts by IntEnum * refactor(logitech_receiver/base.py): create unit tests for request function before replacing ERROR NamedInts by IntEnum * refactor(logitech_receiver/base.py): create unit tests for ping function before replacing ERRORNamedInts by IntEnum (add exclusion for macOS) * refactor(logitech_receiver/base.py): create unit tests for ping function before replacing ERRORNamedInts by IntEnum (fix for python < 3.10) * refactor(solaar/cli./probe.py): create unit tests for run function before replacing ERROR NamedInts by IntEnum (focusing on the call order when receiving errors) * refactor(solaar/cli./probe.py): refactor register processing to handle short and long registers in a single loop structure for improved readability and reduced code duplication. * refactor(logitech_receiver/hidpp10_constants.py): replace ERROR NamedInt by IntEnum. * refactor(logitech_receiver/hidpp10_constants.py): distinguish hidpp10 and hidpp20 errors in the code for readibility. * refactor(logitech_receiver/hidpp20_constants.py): replace ERROR NamedInt by IntEnum. * refactor(logitech_receiver/hidpp20_constants.py): replace ERROR NamedInt by IntEnum. (fix problem with | operator when typing with python 3.8) * feature(hide on startup option): Visual test (not binded yet) DRAFT * refactor(solaar/cli./probe.py): create unit tests for run function before replacing ERROR NamedInts by IntEnum (focusing on the call order when receiving errors) * refactor(solaar/cli./probe.py): refactor register processing to handle short and long registers in a single loop structure for improved readability and reduced code duplication. * refactor(logitech_receiver/hidpp10_constants.py): replace ERROR NamedInt by IntEnum. * refactor(logitech_receiver/hidpp20_constants.py): replace ERROR NamedInt by IntEnum. * refactor(logitech_receiver/hidpp20_constants.py): replace ERROR NamedInt by IntEnum. (fix problem with | operator when typing with python 3.8) * feature(hide on startup option): Visual test (not binded yet) DRAFT * Merge: Refactor: hidpp20 to use enum * Merge: Refactor: hidpp20 to use enum (fix test) --------- Co-authored-by: some_developer <[email protected]>
- Loading branch information
Showing
5 changed files
with
105 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest import mock | ||
|
||
from logitech_receiver.hidpp10_constants import ErrorCode | ||
from logitech_receiver.hidpp10_constants import Registers | ||
from solaar.cli.probe import run | ||
|
||
|
||
# Mock receiver class | ||
class MockReceiver: | ||
handle = 1 | ||
isDevice = False | ||
|
||
def read_register(self, register, *args): | ||
return 0 if register == Registers.RECEIVER_INFO else b"\x01\x03" | ||
|
||
|
||
def test_run_register_errors(): | ||
mock_args = mock.Mock() | ||
mock_args.receiver = False | ||
|
||
mock_receiver = MockReceiver() | ||
|
||
# Define expected addresses to be called in order | ||
expected_addresses = [] | ||
|
||
for reg in range(0, 0xFF): | ||
expected_addresses.append((0x8100 | reg, 0)) # First short call, returns invalid_value (continue) | ||
expected_addresses.append((0x8100 | reg, 1)) # Second short call, returns invalid_address (stop here) | ||
|
||
expected_addresses.append((0x8100 | (0x200 + reg), 0)) # First long call, returns invalid_value (continue) | ||
expected_addresses.append((0x8100 | (0x200 + reg), 1)) # Second long call, returns invalid_address (stop here) | ||
|
||
# To record the actual addresses called | ||
called_addresses = [] | ||
|
||
def mock_base_request(handle, devnumber, reg, sub, return_error=False): | ||
called_addresses.append((reg, sub)) | ||
if sub == 0: | ||
return ErrorCode.INVALID_VALUE | ||
elif sub == 1: | ||
return ErrorCode.INVALID_ADDRESS | ||
return b"\x01\x02" | ||
|
||
with mock.patch("logitech_receiver.base.request", side_effect=mock_base_request), mock.patch( | ||
"solaar.cli.probe._print_receiver", return_value=None | ||
): | ||
# Call the run function with mocked receivers and args (passing real find_receiver function) | ||
run([mock_receiver], mock_args, None, None) | ||
|
||
# Evaluate that the addresses called match the expected addresses | ||
assert ( | ||
called_addresses == expected_addresses | ||
), f"Called addresses {called_addresses} do not match expected {expected_addresses}" |