diff --git a/patterns/chapter_06_command/receivers.py b/patterns/chapter_06_command/receivers.py index 518c322..05004c0 100644 --- a/patterns/chapter_06_command/receivers.py +++ b/patterns/chapter_06_command/receivers.py @@ -21,37 +21,56 @@ def __repr__(self) -> str: return self.__class__.__name__ -class KitchenLight(Receiver): +class Device(Receiver): + """Device as Receiver class implementation.""" + + def __init__(self) -> None: + """Initialize with the default status.""" + self._is_on: bool = False + + @property + def is_on(self) -> bool: + """Return the status of the device.""" + return self._is_on + + +class KitchenLight(Device): """Kitchen light as receiver class implementation.""" def on(self) -> None: """Turn the kitchen light on.""" + self._is_on = True print('Kitchen light is turned on.') def off(self) -> None: """Turn the kitchen light off.""" + self._is_on = False print('Kitchen light is turned off.') -class BedroomLight(Receiver): +class BedroomLight(Device): """Bedroom light as receiver class implementation.""" def on(self) -> None: """Turn the bedroom light on.""" + self._is_on = True print('Bedroom light is turned on.') def off(self) -> None: """Turn the bedroom light off.""" + self._is_on = False print('Bedroom light is turned off.') -class TV(Receiver): +class TV(Device): """TV as receiver class implementation.""" def on(self) -> None: """Turn the TV on.""" + self._is_on = True print('TV is turned on.') def off(self) -> None: """Turn the TV off.""" + self._is_on = False print('TV is turned off.') diff --git a/pyproject.toml b/pyproject.toml index bf65a32..20626da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,6 @@ addopts = [ [tool.coverage.run] omit = [ "*/main.py", - "patterns/chapter_06_command/*", ] parallel = true diff --git a/tests/test_chapter_06/test_command_pattern.py b/tests/test_chapter_06/test_command_pattern.py new file mode 100644 index 0000000..eec71a0 --- /dev/null +++ b/tests/test_chapter_06/test_command_pattern.py @@ -0,0 +1,44 @@ +"""Module for testing pattern "Command".""" + + +import pytest + +from patterns.chapter_06_command.commands import ( + Command, + TurnDeviceOffCommand, + TurnDeviceOnCommand, +) +from patterns.chapter_06_command.invokers import Invoker +from patterns.chapter_06_command.receivers import ( + TV, + BedroomLight, + Device, + KitchenLight, +) + +pytestmark = pytest.mark.parametrize( + ('command', 'receiver', 'expected_receiver_status'), + [ + (TurnDeviceOnCommand, KitchenLight(), True), + (TurnDeviceOffCommand, KitchenLight(), False), + (TurnDeviceOnCommand, BedroomLight(), True), + (TurnDeviceOffCommand, TV(), False), + ], +) + + +def test_set_command( + command: type[Command], + receiver: Device, + expected_receiver_status: bool, +) -> None: + """Test set command pattern. + + Commands set by Invoker should change status of Receiver. + """ + remote_control: Invoker = Invoker() + + remote_control.set_command(command(receiver)) + remote_control.execute_command() + + assert receiver.is_on == expected_receiver_status