-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathTreeSitterClientTests.swift
197 lines (165 loc) · 7.38 KB
/
TreeSitterClientTests.swift
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import XCTest
import CodeEditTextView
@testable import CodeEditSourceEditor
// swiftlint:disable all
final class TreeSitterClientTests: XCTestCase {
private var maxSyncContentLength: Int = 0
override func setUp() {
maxSyncContentLength = TreeSitterClient.Constants.maxSyncContentLength
TreeSitterClient.Constants.maxSyncContentLength = 0
}
override func tearDown() {
TreeSitterClient.Constants.maxSyncContentLength = maxSyncContentLength
}
@MainActor
func performEdit(
textView: TextView,
client: TreeSitterClient,
string: String,
range: NSRange,
completion: @escaping (Result<IndexSet, Error>) -> Void
) {
let delta = string.isEmpty ? -range.length : range.length
textView.replaceString(in: range, with: string)
client.applyEdit(textView: textView, range: range, delta: delta, completion: completion)
}
@MainActor
func test_clientSetup() async {
let client = Mock.treeSitterClient()
let textView = Mock.textView()
client.setUp(textView: textView, codeLanguage: .swift)
let expectation = XCTestExpectation(description: "Setup occurs")
Task.detached {
while client.state == nil {
try await Task.sleep(for: .seconds(0.5))
}
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: 5.0)
let primaryLanguage = client.state?.primaryLayer.id
let layerCount = client.state?.layers.count
XCTAssertEqual(primaryLanguage, .swift, "Client set up incorrect language")
XCTAssertEqual(layerCount, 1, "Client set up too many layers")
}
func resultIsCancel<T>(_ result: Result<T, Error>) -> Bool {
if case let .failure(error) = result {
if case HighlightProvidingError.operationCancelled = error {
return true
}
}
return false
}
@MainActor
func test_editsDuringSetup() {
let client = Mock.treeSitterClient()
let textView = Mock.textView()
client.setUp(textView: textView, codeLanguage: .swift)
// Perform a highlight query
let cancelledQuery = XCTestExpectation(description: "Highlight query should be cancelled by edits.")
client.queryHighlightsFor(textView: textView, range: NSRange(location: 0, length: 10)) { result in
if self.resultIsCancel(result) {
cancelledQuery.fulfill()
} else {
XCTFail("Highlight query was not cancelled.")
}
}
// Perform an edit
let cancelledEdit = XCTestExpectation(description: "First edit should be cancelled by second one.")
performEdit(textView: textView, client: client, string: "func ", range: .zero) { result in
if self.resultIsCancel(result) {
cancelledEdit.fulfill()
} else {
XCTFail("Edit was not cancelled.")
}
}
// Perform a second edit
let successEdit = XCTestExpectation(description: "Second edit should succeed.")
performEdit(textView: textView, client: client, string: "", range: NSRange(location: 0, length: 5)) { result in
if case let .success(ranges) = result {
XCTAssertEqual(ranges.count, 0, "Edits, when combined, should produce the original syntax tree.")
successEdit.fulfill()
} else {
XCTFail("Second edit was not successful.")
}
}
wait(for: [cancelledQuery, cancelledEdit, successEdit], timeout: 5.0)
}
@MainActor
func test_multipleSetupsCancelAllOperations() async {
let client = Mock.treeSitterClient()
let textView = Mock.textView()
// First setup, wrong language
client.setUp(textView: textView, codeLanguage: .c)
// Perform a highlight query
let cancelledQuery = XCTestExpectation(description: "Highlight query should be cancelled by second setup.")
client.queryHighlightsFor(textView: textView, range: NSRange(location: 0, length: 10)) { result in
if self.resultIsCancel(result) {
cancelledQuery.fulfill()
} else {
XCTFail("Highlight query was not cancelled by the second setup.")
}
}
// Perform an edit
let cancelledEdit = XCTestExpectation(description: "First edit should be cancelled by second setup.")
performEdit(textView: textView, client: client, string: "func ", range: .zero) { result in
if self.resultIsCancel(result) {
cancelledEdit.fulfill()
} else {
XCTFail("Edit was not cancelled by the second setup.")
}
}
// Second setup, which should cancel all previous operations
client.setUp(textView: textView, codeLanguage: .swift)
let finalSetupExpectation = XCTestExpectation(description: "Final setup should complete successfully.")
Task.detached {
while client.state?.primaryLayer.id != .swift {
try await Task.sleep(for: .seconds(0.5))
}
finalSetupExpectation.fulfill()
}
await fulfillment(of: [cancelledQuery, cancelledEdit, finalSetupExpectation], timeout: 5.0)
// Ensure only the final setup's language is active
let primaryLanguage = client.state?.primaryLayer.id
let layerCount = client.state?.layers.count
XCTAssertEqual(primaryLanguage, .swift, "Client set up incorrect language after re-setup.")
XCTAssertEqual(layerCount, 1, "Client set up too many layers after re-setup.")
}
@MainActor
func test_cancelAllEditsUntilFinalOne() {
let client = Mock.treeSitterClient()
let textView = Mock.textView()
textView.setText("asadajkfijio;amfjamc;aoijaoajkvarpfjo;sdjlkj")
client.setUp(textView: textView, codeLanguage: .swift)
// Set up random edits
let editExpectations = (0..<10).map { index -> XCTestExpectation in
let expectation = XCTestExpectation(description: "Edit \(index) should be cancelled.")
let isDeletion = Int.random(in: 0..<10) < 4
let editText = isDeletion ? "" : "\(index)"
let editLocation = Int.random(in: 0..<textView.string.count)
let editRange = if isDeletion {
NSRange(location: editLocation, length: 1)
} else {
NSRange(location: editLocation, length: 0)
}
performEdit(textView: textView, client: client, string: editText, range: editRange) { result in
if self.resultIsCancel(result) {
expectation.fulfill()
} else {
XCTFail("Edit \(index) was not cancelled.")
}
}
return expectation
}
// Final edit that should succeed
let finalEditExpectation = XCTestExpectation(description: "Final edit should succeed.")
performEdit(textView: textView, client: client, string: "", range: textView.documentRange) { result in
if case .success(_) = result {
finalEditExpectation.fulfill()
} else {
XCTFail("Final edit did not succeed.")
}
}
wait(for: editExpectations + [finalEditExpectation], timeout: 5.0)
}
}
// swiftlint:enable all