-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_composite.py
More file actions
63 lines (45 loc) · 1.95 KB
/
Copy pathtest_composite.py
File metadata and controls
63 lines (45 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Behavioral tests for the Composite building block."""
from __future__ import annotations
from dataclasses import dataclass
import pytest
from patterns.structural.composite import Composite
@dataclass(frozen=True)
class Task:
hours: int
def total(self) -> int:
return self.hours
class TestRollup:
def test_a_container_totals_its_leaves(self) -> None:
team = Composite[int](sum, [Task(3), Task(5)])
assert team.total() == 8
def test_nesting_rolls_up_through_every_level(self) -> None:
team = Composite[int](sum, [Task(3), Task(5)])
project = Composite[int](sum, [team, Task(8)])
portfolio = Composite[int](sum, [project])
assert portfolio.total() == 16
def test_an_empty_container_totals_the_combine_identity(self) -> None:
assert Composite[int](sum).total() == 0
def test_leaf_and_subtree_are_interchangeable_to_callers(self) -> None:
def describe(node: Task | Composite[int]) -> str:
return f"{node.total()}h" # never asks which kind it holds
assert describe(Task(4)) == "4h"
assert describe(Composite[int](sum, [Task(4)])) == "4h"
class TestHonestInterfaces:
def test_child_management_lives_only_on_the_container(self) -> None:
assert not hasattr(Task(1), "add")
assert not hasattr(Task(1), "remove")
def test_add_and_remove_change_the_rollup(self) -> None:
team = Composite[int](sum, [Task(3)])
extra = Task(5)
team.add(extra)
assert team.total() == 8
team.remove(extra)
assert team.total() == 3
def test_removing_a_stranger_raises(self) -> None:
with pytest.raises(ValueError):
Composite[int](sum).remove(Task(1))
def test_iteration_walks_direct_children_in_order(self) -> None:
first, second = Task(1), Task(2)
team = Composite[int](sum, [first, second])
assert list(team) == [first, second]
assert len(team) == 2