forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeStoreTests.swift
More file actions
295 lines (248 loc) · 10.5 KB
/
RangeStoreTests.swift
File metadata and controls
295 lines (248 loc) · 10.5 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import Testing
@testable import CodeEditSourceEditor
extension RangeStore {
var length: Int { _guts.summary.length }
var count: Int { _guts.count }
}
@Suite
struct RangeStoreTests {
typealias Store = RangeStore<StyledRangeContainer.StyleElement>
@Test
func initWithLength() {
for _ in 0..<100 {
let length = Int.random(in: 0..<1000)
let store = Store(documentLength: length)
#expect(store.length == length)
}
}
// MARK: - Storage
@Test
func storageRemoveCharacters() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 10..<12, withCount: 0)
#expect(store.length == 98, "Failed to remove correct range")
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageRemoveFromEnd() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 95..<100, withCount: 0)
#expect(store.length == 95, "Failed to remove correct range")
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageRemoveSingleCharacterFromEnd() {
var store = Store(documentLength: 10)
store.set( // Test that we can delete a character associated with a single syntax run too
runs: [
.empty(length: 8),
.init(length: 1, value: .init(modifiers: [.abstract])),
.init(length: 1, value: .init(modifiers: [.declaration]))
],
for: 0..<10
)
store.storageUpdated(replacedCharactersIn: 9..<10, withCount: 0)
#expect(store.length == 9, "Failed to remove correct range")
#expect(store.count == 2)
}
@Test
func storageRemoveFromBeginning() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 0..<15, withCount: 0)
#expect(store.length == 85, "Failed to remove correct range")
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageRemoveAll() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 0..<100, withCount: 0)
#expect(store.length == 0, "Failed to remove correct range")
#expect(store.count == 0, "Failed to remove all runs")
}
@Test
func storageInsert() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 45..<45, withCount: 10)
#expect(store.length == 110)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageInsertAtEnd() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 100..<100, withCount: 10)
#expect(store.length == 110)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageInsertAtBeginning() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 0..<0, withCount: 10)
#expect(store.length == 110)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageInsertFromEmpty() {
var store = Store(documentLength: 0)
store.storageUpdated(replacedCharactersIn: 0..<0, withCount: 10)
#expect(store.length == 10)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageEdit() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 45..<50, withCount: 10)
#expect(store.length == 105)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageEditAtEnd() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 95..<100, withCount: 10)
#expect(store.length == 105)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageEditAtBeginning() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 0..<5, withCount: 10)
#expect(store.length == 105)
#expect(store.count == 1, "Failed to coalesce")
}
@Test
func storageEditAll() {
var store = Store(documentLength: 100)
store.storageUpdated(replacedCharactersIn: 0..<100, withCount: 10)
#expect(store.length == 10)
#expect(store.count == 1, "Failed to coalesce")
}
// MARK: - Styles
@Test
func setOneRun() {
var store = Store(documentLength: 100)
store.set(value: .init(capture: .comment, modifiers: [.static]), for: 45..<50)
#expect(store.length == 100)
#expect(store.count == 3)
let runs = store.runs(in: 0..<100)
#expect(runs.count == 3)
#expect(runs[0].length == 45)
#expect(runs[1].length == 5)
#expect(runs[2].length == 50)
#expect(runs[0].value?.capture == nil)
#expect(runs[1].value?.capture == .comment)
#expect(runs[2].value?.capture == nil)
#expect(runs[0].value?.modifiers == nil)
#expect(runs[1].value?.modifiers == [.static])
#expect(runs[2].value?.modifiers == nil)
}
@Test
func queryOverlappingRun() {
var store = Store(documentLength: 100)
store.set(value: .init(capture: .comment, modifiers: [.static]), for: 45..<50)
#expect(store.length == 100)
#expect(store.count == 3)
let runs = store.runs(in: 47..<100)
#expect(runs.count == 2)
#expect(runs[0].length == 3)
#expect(runs[1].length == 50)
#expect(runs[0].value?.capture == .comment)
#expect(runs[1].value?.capture == nil)
#expect(runs[0].value?.modifiers == [.static])
#expect(runs[1].value?.modifiers == nil)
}
@Test
func setMultipleRuns() {
var store = Store(documentLength: 100)
store.set(value: .init(capture: .comment, modifiers: [.static]), for: 5..<15)
store.set(value: .init(capture: .keyword, modifiers: []), for: 20..<30)
store.set(value: .init(capture: .string, modifiers: [.static]), for: 35..<40)
store.set(value: .init(capture: .function, modifiers: []), for: 45..<50)
store.set(value: .init(capture: .variable, modifiers: []), for: 60..<70)
#expect(store.length == 100)
let runs = store.runs(in: 0..<100)
#expect(runs.count == 11)
#expect(runs.reduce(0, { $0 + $1.length }) == 100)
let lengths = [5, 10, 5, 10, 5, 5, 5, 5, 10, 10, 30]
let captures: [CaptureName?] = [nil, .comment, nil, .keyword, nil, .string, nil, .function, nil, .variable, nil]
let modifiers: [CaptureModifierSet] = [[], [.static], [], [], [], [.static], [], [], [], [], []]
runs.enumerated().forEach {
#expect($0.element.length == lengths[$0.offset])
#expect($0.element.value?.capture == captures[$0.offset])
#expect($0.element.value?.modifiers ?? [] == modifiers[$0.offset])
}
}
@Test
func setMultipleRunsAndStorageUpdate() {
var store = Store(documentLength: 100)
var lengths = [5, 10, 5, 10, 5, 5, 5, 5, 10, 10, 30]
var captures: [CaptureName?] = [nil, .comment, nil, .keyword, nil, .string, nil, .function, nil, .variable, nil]
var modifiers: [CaptureModifierSet] = [[], [.static], [], [], [], [.static], [], [], [], [], []]
store.set(
runs: zip(zip(lengths, captures), modifiers).map {
Store.Run(length: $0.0, value: .init(capture: $0.1, modifiers: $1))
},
for: 0..<100
)
#expect(store.length == 100)
var runs = store.runs(in: 0..<100)
#expect(runs.count == 11)
#expect(runs.reduce(0, { $0 + $1.length }) == 100)
runs.enumerated().forEach {
#expect(
$0.element.length == lengths[$0.offset],
"Run \($0.offset) has incorrect length: \($0.element.length). Expected \(lengths[$0.offset])"
)
#expect(
$0.element.value?.capture == captures[$0.offset], // swiftlint:disable:next line_length
"Run \($0.offset) has incorrect capture: \(String(describing: $0.element.value?.capture)). Expected \(String(describing: captures[$0.offset]))"
)
#expect(
$0.element.value?.modifiers == modifiers[$0.offset], // swiftlint:disable:next line_length
"Run \($0.offset) has incorrect modifiers: \(String(describing: $0.element.value?.modifiers)). Expected \(modifiers[$0.offset])"
)
}
store.storageUpdated(replacedCharactersIn: 30..<45, withCount: 10)
runs = store.runs(in: 0..<95)
#expect(runs.count == 9)
#expect(runs.reduce(0, { $0 + $1.length }) == 95)
lengths = [5, 10, 5, 10, 10, 5, 10, 10, 30]
captures = [nil, .comment, nil, .keyword, nil, .function, nil, .variable, nil]
modifiers = [[], [.static], [], [], [], [], [], [], []]
runs.enumerated().forEach {
#expect($0.element.length == lengths[$0.offset])
#expect($0.element.value?.capture == captures[$0.offset])
#expect($0.element.value?.modifiers ?? [] == modifiers[$0.offset])
}
}
// MARK: - Query
// A few known bad cases
@Test(arguments: [3..<8, 65..<100, 0..<5, 5..<12])
func runsInAlwaysBoundedByRange(_ range: Range<Int>) {
var store = Store(documentLength: 100)
let lengths = [5, 10, 5, 10, 5, 5, 5, 5, 10, 10, 30]
let captures: [CaptureName?] = [nil, .comment, nil, .keyword, nil, .string, nil, .function, nil, .variable, nil]
let modifiers: [CaptureModifierSet] = [[], [.static], [], [], [], [.static], [], [], [], [], []]
store.set(
runs: zip(zip(lengths, captures), modifiers).map {
Store.Run(length: $0.0, value: .init(capture: $0.1, modifiers: $1))
},
for: 0..<100
)
#expect(
store.runs(in: range).reduce(0, { $0 + $1.length }) == (range.upperBound - range.lowerBound),
"Runs returned by storage did not equal requested range"
)
#expect(store.runs(in: range).allSatisfy({ $0.length > 0 }))
}
// Randomized version of the previous test
@Test
func runsAlwaysBoundedByRangeRandom() {
func range() -> Range<Int> {
let start = Int.random(in: 0..<100)
let end = Int.random(in: start..<100)
return start..<end
}
for _ in 0..<1000 {
runsInAlwaysBoundedByRange(range())
}
}
}