Skip to content

Commit

Permalink
Add test for composite pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
balancy committed Oct 11, 2023
1 parent 2eff294 commit 7a0c3bf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion patterns/chapter_09_composite/humains.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
class AbstractHumain(ABC):
"""Abstract humain class."""

def __init__(self, name: str) -> None:
"""Initialize humain."""
self._name = name

@property
def parent(self) -> AbstractHumain | None:
"""Return parent of humain."""
Expand All @@ -36,7 +40,7 @@ def is_composite(self) -> bool:
@property
@abstractmethod
def family_tree(self) -> str:
"""Return family tree of humain."""
"""Return family tree of humain for print."""
pass


Expand Down
33 changes: 33 additions & 0 deletions tests/test_chapter_09/test_composite_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Module for testing pattern "Composite"."""


import pytest

from patterns.chapter_09_composite.humains import AbstractHumain, Child, Humain


@pytest.mark.parametrize(
('parent', 'children', 'expected_names_in_family_tree'),
[
(
Humain('father'),
[Child('son'), Child('daughter')],
['father', 'son', 'daughter'],
),
],
)
def test_all_members_in_family_tree(
capsys,
parent: AbstractHumain,
children: list[AbstractHumain],
expected_names_in_family_tree: list[str],
) -> None:
"""Test if all family mambers are in family tree."""
for child in children:
parent.add_child(child)

print(parent.family_tree)
captured = capsys.readouterr()

for expected_name in expected_names_in_family_tree:
assert expected_name in captured.out

0 comments on commit 7a0c3bf

Please sign in to comment.