-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_editor_undo.py
More file actions
67 lines (58 loc) · 2.22 KB
/
Copy pathtest_editor_undo.py
File metadata and controls
67 lines (58 loc) · 2.22 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
"""Behavioral tests for the editor-undo mini-project."""
from __future__ import annotations
from patterns.behavioral.command.examples.editor_undo.editing import (
delete_span,
insert_text,
replace_span,
)
from patterns.behavioral.command.examples.editor_undo.models import Document
from patterns.behavioral.command.pattern import UndoStack
class TestEditingCommands:
def test_insert_then_undo_restores_exact_text(self) -> None:
doc = Document("hello world")
stack = UndoStack()
stack.push(insert_text(doc, 5, ","))
assert doc.text == "hello, world"
stack.undo()
assert doc.text == "hello world"
def test_delete_remembers_what_it_removed(self) -> None:
doc = Document("hello world")
stack = UndoStack()
stack.push(delete_span(doc, 0, 6))
assert doc.text == "world"
stack.undo()
assert doc.text == "hello world" # the removed span came back verbatim
def test_replace_round_trips(self) -> None:
doc = Document("hello world")
stack = UndoStack()
stack.push(replace_span(doc, 0, 5, "goodbye"))
assert doc.text == "goodbye world"
stack.undo()
assert doc.text == "hello world"
def test_delete_undo_redo_cycle_reuses_captured_state(self) -> None:
doc = Document("abcdef")
stack = UndoStack()
stack.push(delete_span(doc, 1, 3))
stack.undo()
stack.redo()
assert doc.text == "aef"
stack.undo()
assert doc.text == "abcdef"
def test_session_log_reads_as_an_audit_trail(self) -> None:
doc = Document()
stack = UndoStack()
stack.push(insert_text(doc, 0, "hi"))
stack.push(delete_span(doc, 0, 1))
assert stack.log() == ("insert 'hi'@0", "delete 1@0")
def test_editing_session_end_to_end(self) -> None:
doc = Document()
stack = UndoStack()
stack.push(insert_text(doc, 0, "hello world"))
stack.push(replace_span(doc, 0, 5, "goodbye"))
stack.push(delete_span(doc, 7, 6))
assert doc.text == "goodbye"
stack.undo()
stack.undo()
assert doc.text == "hello world"
stack.redo()
assert doc.text == "goodbye world"