forked from CodeEditApp/CodeEditTextView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextViewTests.swift
More file actions
81 lines (62 loc) · 2.48 KB
/
TextViewTests.swift
File metadata and controls
81 lines (62 loc) · 2.48 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
75
76
77
78
79
80
81
import Testing
import AppKit
@testable import CodeEditTextView
@Suite
@MainActor
struct TextViewTests {
class MockDelegate: TextViewDelegate {
var shouldReplaceContents: ((_ textView: TextView, _ range: NSRange, _ string: String) -> Bool)?
func textView(_ textView: TextView, shouldReplaceContentsIn range: NSRange, with string: String) -> Bool {
shouldReplaceContents?(textView, range, string) ?? true
}
}
let textView: TextView
let delegate: MockDelegate
init() {
textView = TextView(string: "Lorem Ipsum")
delegate = MockDelegate()
textView.delegate = delegate
}
@Test
func delegateChangesText() {
var hasReplaced = false
delegate.shouldReplaceContents = { textView, _, _ -> Bool in
if !hasReplaced {
hasReplaced.toggle()
textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: " World ")
}
return true
}
textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "Hello")
#expect(textView.string == "Hello World Lorem Ipsum")
// available in test module
textView.layoutManager.lineStorage.validateInternalState()
}
@Test
func sharedTextStorage() {
let storage = NSTextStorage(string: "Hello world")
let textView1 = TextView(string: "")
textView1.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
textView1.layoutSubtreeIfNeeded()
textView1.setTextStorage(storage)
let textView2 = TextView(string: "")
textView2.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
textView2.layoutSubtreeIfNeeded()
textView2.setTextStorage(storage)
// Expect both text views to receive edited events from the storage
#expect(textView1.layoutManager.lineCount == 1)
#expect(textView2.layoutManager.lineCount == 1)
storage.replaceCharacters(in: NSRange(location: 11, length: 0), with: "\nMore Lines\n")
#expect(textView1.layoutManager.lineCount == 3)
#expect(textView2.layoutManager.lineCount == 3)
}
@Test("Custom UndoManager class receives events")
func customUndoManagerReceivesEvents() {
let textView = TextView(string: "")
textView.replaceCharacters(in: .zero, with: "Hello World")
textView.undo(nil)
#expect(textView.string == "")
textView.redo(nil)
#expect(textView.string == "Hello World")
}
}