This repository was archived by the owner on Nov 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathEditHistoryTests.swift
More file actions
70 lines (63 loc) · 2.53 KB
/
Copy pathEditHistoryTests.swift
File metadata and controls
70 lines (63 loc) · 2.53 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
import XCTest
import VisuStructCore
final class EditHistoryTests: XCTestCase {
private func document(_ texts: String...) -> VisuStructDocument {
var doc = VisuStructDocumentEditing.newEmpty()
doc.elements = texts.map { VisuStructElement(type: .anweisung, textLines: [$0]) }
return doc
}
func testUndoAndRedoRestoreSnapshots() {
let a = document("a"), b = document("b"), c = document("c")
var history = VisuStructEditHistory()
XCTAssertFalse(history.canUndo)
history.record(a)
history.record(b)
XCTAssertEqual(history.undo(from: c), b)
XCTAssertEqual(history.undo(from: b), a)
XCTAssertNil(history.undo(from: a))
XCTAssertEqual(history.redo(from: a), b)
XCTAssertEqual(history.redo(from: b), c)
XCTAssertNil(history.redo(from: c))
}
func testRecordClearsRedo() {
var history = VisuStructEditHistory()
history.record(document("a"))
_ = history.undo(from: document("b"))
XCTAssertTrue(history.canRedo)
history.record(document("a"))
XCTAssertFalse(history.canRedo)
}
func testUndoStackIsLimited() {
var history = VisuStructEditHistory(maxSteps: 3)
for i in 0..<5 { history.record(document("\(i)")) }
XCTAssertEqual(history.undoStack, [document("2"), document("3"), document("4")])
XCTAssertEqual(VisuStructEditHistory().maxSteps, 80)
}
func testRedoAlsoRespectsLimit() {
var history = VisuStructEditHistory(maxSteps: 2)
history.record(document("0"))
history.record(document("1"))
_ = history.undo(from: document("2"))
history.record(document("x"))
history.record(document("y"))
XCTAssertEqual(history.undoStack.count, 2)
}
func testReset() {
var history = VisuStructEditHistory()
history.record(document("a"))
_ = history.undo(from: document("b"))
history.record(document("c"))
history.reset()
XCTAssertFalse(history.canUndo)
XCTAssertFalse(history.canRedo)
}
func testSanitizedSelection() {
let doc = document("a", "b")
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([1], in: doc), [1])
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([5], in: doc), [0])
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection(nil, in: doc), [0])
var empty = doc
empty.elements = []
XCTAssertNil(VisuStructDocumentEditing.sanitizedSelection([0], in: empty))
}
}