Skip to content

Commit

Permalink
Add test for command pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
balancy committed Oct 3, 2023
1 parent a9be0d3 commit 0765735
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
25 changes: 22 additions & 3 deletions patterns/chapter_06_command/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ addopts = [
[tool.coverage.run]
omit = [
"*/main.py",
"patterns/chapter_06_command/*",
]
parallel = true

Expand Down
44 changes: 44 additions & 0 deletions tests/test_chapter_06/test_command_pattern.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0765735

Please sign in to comment.