Skip to content

Commit

Permalink
Add test for facade pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
balancy committed Oct 3, 2023
1 parent 552e407 commit 7ee0d1b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
74 changes: 29 additions & 45 deletions patterns/chapter_07_facade/devices.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
"""Devices module."""


class Popper:
"""Popper device class."""
class Device:
"""Device base class."""

def __init__(self) -> None:
"""Initialize device."""
self._is_on: bool = False
print(f'{self.__class__.__name__} is on')

def turn_on(self) -> None:
"""Turn on popper."""
print('Pop-corn: popping')
"""Turn on device."""
self._is_on = True
print(f'{self.__class__.__name__} is on')

def turn_off(self) -> None:
"""Turn off device."""
self._is_on = False
print(f'{self.__class__.__name__} is off')

@property
def is_on(self) -> bool:
"""Check if device is on."""
return self._is_on


class Popper(Device):
"""Popper device class."""

def pop(self) -> None:
"""Pop pop-corn."""
print('Pop-corn: poped')

def turn_off(self) -> None:
"""Turn off popper."""
print('Pop-corn: off')


class Light:
class Light(Device):
"""Light device class."""

def turn_on(self) -> None:
"""Turn on light."""
print('Light: turned on')

def dim(self, value: int) -> None:
"""Dim light."""
print(f'Light: dimmed to {value}')

def turn_off(self) -> None:
"""Turn off light."""
print('Light: turned off')


class Screen:
class Screen(Device):
"""Screen device class."""

def down(self) -> None:
Expand All @@ -45,13 +53,9 @@ def up(self) -> None:
print('Screen: is up')


class Projector:
class Projector(Device):
"""Projector device class."""

def turn_on(self) -> None:
"""Turn on projector."""
print('Projector: turned on')

def set_input(self, input_value: str) -> None:
"""Set input device for projector."""
print(f'Projector: input set to {input_value}')
Expand All @@ -60,18 +64,10 @@ def set_wide_screen_mode(self) -> None:
"""Set wide screen mode for projector."""
print('Projector: switched to wide screen mode')

def turn_off(self) -> None:
"""Turn off projector."""
print('Projector: turned off')


class Amplifier:
class Amplifier(Device):
"""Amplifier device class."""

def turn_on(self) -> None:
"""Turn on amplifier."""
print('Amplifier: turned on')

def set_dvd(self) -> None:
"""Set DVD for amplifier."""
print('Amplifier: DVD is set')
Expand All @@ -84,22 +80,10 @@ def set_volume(self, value: int) -> None:
"""Set volume for amplifier."""
print(f'Amplifier: volume is set to {value}')

def turn_off(self) -> None:
"""Turn off amplifier."""
print('Amplifier: turned off')


class DvdPlayer:
class DvdPlayer(Device):
"""DVD player device class."""

def turn_on(self) -> None:
"""Turn on DVD player."""
print('DVD: turned on')

def play(self, movie: str) -> None:
"""Play movie on DVD player."""
print(f'DVD: playing {movie}')

def turn_off(self) -> None:
"""Turn off DVD player."""
print('DVD: turned off')
15 changes: 14 additions & 1 deletion patterns/chapter_07_facade/facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def watch_movie(self) -> None:
self._light.turn_on()
self._light.dim(10)

self._screen.turn_on()
self._screen.down()

self._projector.turn_on()
Expand Down Expand Up @@ -56,9 +57,21 @@ def end_movie(self) -> None:

self._popper.turn_off()
self._light.turn_off()
self._screen.up()
self._screen.turn_off()
self._projector.turn_off()
self._amp.turn_off()
self._player.turn_off()

print('Movie theater is closed')

@property
def handled_devices(self) -> tuple:
"""Get list of handled devices."""
return (
self._amp,
self._player,
self._light,
self._popper,
self._projector,
self._screen,
)
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",
"*/chapter_07_facade/*"
]
parallel = true

Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ exclude =
.git,
__pycache__,
.venv,
07_adapter/*,
08_facade/*,
09_template_method/*
29 changes: 29 additions & 0 deletions tests/test_chapter_07/test_facade_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Module for testing pattern "Facade"."""


import pytest

from patterns.chapter_07_facade.facades import HomeTheaterFacade

pytestmark = pytest.mark.parametrize(
('method_name', 'are_devices_on'),
[
('watch_movie', True),
('end_movie', False),
],
)


def test_facade_pattern(method_name: str, are_devices_on: bool) -> None:
"""Test facade pattern.
Calling one method of facade class should call all incapsulated methods.
"""
home_theater: HomeTheaterFacade = HomeTheaterFacade()
called_method = getattr(HomeTheaterFacade, method_name)

called_method(home_theater)

assert are_devices_on == all(
device.is_on for device in home_theater.handled_devices
)

0 comments on commit 7ee0d1b

Please sign in to comment.