From 7a0c3bf7f120c364b749dfa66bc793ce91abd0d4 Mon Sep 17 00:00:00 2001 From: balancy Date: Wed, 11 Oct 2023 14:48:47 +0200 Subject: [PATCH] Add test for composite pattern --- patterns/chapter_09_composite/humains.py | 6 +++- .../test_chapter_09/test_composite_pattern.py | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/test_chapter_09/test_composite_pattern.py diff --git a/patterns/chapter_09_composite/humains.py b/patterns/chapter_09_composite/humains.py index c5cfec3..acfb4c7 100644 --- a/patterns/chapter_09_composite/humains.py +++ b/patterns/chapter_09_composite/humains.py @@ -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.""" @@ -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 diff --git a/tests/test_chapter_09/test_composite_pattern.py b/tests/test_chapter_09/test_composite_pattern.py new file mode 100644 index 0000000..31e8f37 --- /dev/null +++ b/tests/test_chapter_09/test_composite_pattern.py @@ -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