|
| 1 | +import XCTest |
| 2 | +import VisuStructCore |
| 3 | + |
| 4 | +final class EditHistoryTests: XCTestCase { |
| 5 | + private func document(_ texts: String...) -> VisuStructDocument { |
| 6 | + var doc = VisuStructDocumentEditing.newEmpty() |
| 7 | + doc.elements = texts.map { VisuStructElement(type: .anweisung, textLines: [$0]) } |
| 8 | + return doc |
| 9 | + } |
| 10 | + |
| 11 | + func testUndoAndRedoRestoreSnapshots() { |
| 12 | + let a = document("a"), b = document("b"), c = document("c") |
| 13 | + var history = VisuStructEditHistory() |
| 14 | + XCTAssertFalse(history.canUndo) |
| 15 | + history.record(a) |
| 16 | + history.record(b) |
| 17 | + XCTAssertEqual(history.undo(from: c), b) |
| 18 | + XCTAssertEqual(history.undo(from: b), a) |
| 19 | + XCTAssertNil(history.undo(from: a)) |
| 20 | + XCTAssertEqual(history.redo(from: a), b) |
| 21 | + XCTAssertEqual(history.redo(from: b), c) |
| 22 | + XCTAssertNil(history.redo(from: c)) |
| 23 | + } |
| 24 | + |
| 25 | + func testRecordClearsRedo() { |
| 26 | + var history = VisuStructEditHistory() |
| 27 | + history.record(document("a")) |
| 28 | + _ = history.undo(from: document("b")) |
| 29 | + XCTAssertTrue(history.canRedo) |
| 30 | + history.record(document("a")) |
| 31 | + XCTAssertFalse(history.canRedo) |
| 32 | + } |
| 33 | + |
| 34 | + func testUndoStackIsLimited() { |
| 35 | + var history = VisuStructEditHistory(maxSteps: 3) |
| 36 | + for i in 0..<5 { history.record(document("\(i)")) } |
| 37 | + XCTAssertEqual(history.undoStack, [document("2"), document("3"), document("4")]) |
| 38 | + XCTAssertEqual(VisuStructEditHistory().maxSteps, 80) |
| 39 | + } |
| 40 | + |
| 41 | + func testRedoAlsoRespectsLimit() { |
| 42 | + var history = VisuStructEditHistory(maxSteps: 2) |
| 43 | + history.record(document("0")) |
| 44 | + history.record(document("1")) |
| 45 | + _ = history.undo(from: document("2")) |
| 46 | + history.record(document("x")) |
| 47 | + history.record(document("y")) |
| 48 | + XCTAssertEqual(history.undoStack.count, 2) |
| 49 | + } |
| 50 | + |
| 51 | + func testReset() { |
| 52 | + var history = VisuStructEditHistory() |
| 53 | + history.record(document("a")) |
| 54 | + _ = history.undo(from: document("b")) |
| 55 | + history.record(document("c")) |
| 56 | + history.reset() |
| 57 | + XCTAssertFalse(history.canUndo) |
| 58 | + XCTAssertFalse(history.canRedo) |
| 59 | + } |
| 60 | + |
| 61 | + func testSanitizedSelection() { |
| 62 | + let doc = document("a", "b") |
| 63 | + XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([1], in: doc), [1]) |
| 64 | + XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([5], in: doc), [0]) |
| 65 | + XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection(nil, in: doc), [0]) |
| 66 | + var empty = doc |
| 67 | + empty.elements = [] |
| 68 | + XCTAssertNil(VisuStructDocumentEditing.sanitizedSelection([0], in: empty)) |
| 69 | + } |
| 70 | +} |
0 commit comments