-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_commands.py
More file actions
74 lines (62 loc) · 2.42 KB
/
Copy pathtest_commands.py
File metadata and controls
74 lines (62 loc) · 2.42 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
64
65
66
67
68
69
70
71
72
73
74
"""Behavioral tests for the Command pattern's library code."""
from __future__ import annotations
from patterns.behavioral.command.pattern import Undoable, UndoStack
def _append_command(log: list[str], item: str) -> Undoable:
return Undoable(
do=lambda: log.append(item),
undo=lambda: log.remove(item),
label=f"append {item}",
)
class TestUndoStack:
def test_push_executes_and_records(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
stack.push(_append_command(log, "b"))
assert log == ["a", "b"]
assert stack.log() == ("append a", "append b")
def test_undo_reverses_newest_first(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
stack.push(_append_command(log, "b"))
undone = stack.undo()
assert undone is not None and undone.label == "append b"
assert log == ["a"]
def test_redo_replays_the_undone_command(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
stack.undo()
assert log == []
redone = stack.redo()
assert redone is not None and redone.label == "append a"
assert log == ["a"]
def test_new_push_clears_the_redo_branch(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
stack.undo()
stack.push(_append_command(log, "b")) # diverge: the undone future dies
assert stack.redo() is None
assert log == ["b"]
def test_undo_redo_on_empty_history_are_safe(self) -> None:
stack = UndoStack()
assert stack.undo() is None
assert stack.redo() is None
assert not stack.can_undo
assert not stack.can_redo
def test_can_undo_and_can_redo_report_true_when_true(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
assert stack.can_undo and not stack.can_redo
stack.undo()
assert stack.can_redo and not stack.can_undo
def test_log_reflects_only_applied_commands(self) -> None:
log: list[str] = []
stack = UndoStack()
stack.push(_append_command(log, "a"))
stack.push(_append_command(log, "b"))
stack.undo()
assert stack.log() == ("append a",)