-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathTextViewControllerTests.swift
414 lines (338 loc) · 18.5 KB
/
TextViewControllerTests.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import XCTest
@testable import CodeEditSourceEditor
import SwiftTreeSitter
import AppKit
import SwiftUI
import TextStory
// swiftlint:disable all
final class TextViewControllerTests: XCTestCase {
var controller: TextViewController!
var theme: EditorTheme!
override func setUpWithError() throws {
theme = Mock.theme()
controller = TextViewController(
string: "",
language: .default,
font: .monospacedSystemFont(ofSize: 11, weight: .medium),
theme: theme,
tabWidth: 4,
indentOption: .spaces(count: 4),
lineHeight: 1.0,
wrapLines: true,
cursorPositions: [],
editorOverscroll: 0.5,
useThemeBackground: true,
highlightProviders: [],
contentInsets: NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0),
isEditable: true,
isSelectable: true,
letterSpacing: 1.0,
useSystemCursor: false,
bracketPairHighlight: .flash
)
controller.loadView()
}
// MARK: Capture Names
func test_captureNames() throws {
// test for "keyword"
let captureName1 = "keyword"
let color1 = controller.attributesFor(CaptureName.fromString(captureName1))[.foregroundColor] as? NSColor
XCTAssertEqual(color1, NSColor.systemPink)
// test for "comment"
let captureName2 = "comment"
let color2 = controller.attributesFor(CaptureName.fromString(captureName2))[.foregroundColor] as? NSColor
XCTAssertEqual(color2, NSColor.systemGreen)
/* ... additional tests here ... */
// test for empty case
let captureName3 = ""
let color3 = controller.attributesFor(CaptureName.fromString(captureName3))[.foregroundColor] as? NSColor
XCTAssertEqual(color3, NSColor.textColor)
// test for random case
let captureName4 = "abc123"
let color4 = controller.attributesFor(CaptureName.fromString(captureName4))[.foregroundColor] as? NSColor
XCTAssertEqual(color4, NSColor.textColor)
}
// MARK: Overscroll
func test_editorOverScroll() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .init(x: .zero,
y: .zero,
width: 100,
height: 100)
controller.editorOverscroll = 0
controller.contentInsets = nil
controller.reloadUI()
// editorOverscroll: 0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
controller.editorOverscroll = 0.5
controller.reloadUI()
// editorOverscroll: 0.5
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 50.0)
controller.editorOverscroll = 1.0
controller.reloadUI()
// editorOverscroll: 1.0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 87.0)
}
// MARK: Insets
func test_editorInsets() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .init(
x: .zero,
y: .zero,
width: 100,
height: 100
)
func assertInsetsEqual(_ lhs: NSEdgeInsets, _ rhs: NSEdgeInsets) throws {
XCTAssertEqual(lhs.top, rhs.top)
XCTAssertEqual(lhs.right, rhs.right)
XCTAssertEqual(lhs.bottom, rhs.bottom)
XCTAssertEqual(lhs.left, rhs.left)
}
controller.editorOverscroll = 0
controller.contentInsets = nil
controller.reloadUI()
// contentInsets: 0
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
XCTAssertEqual(controller.gutterView.frame.origin.y, 0)
// contentInsets: 16
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
XCTAssertEqual(controller.gutterView.frame.origin.y, -16)
// contentInsets: different
controller.contentInsets = NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1)
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1))
XCTAssertEqual(controller.gutterView.frame.origin.y, -32.5)
// contentInsets: 16
// editorOverscroll: 0.5
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
controller.editorOverscroll = 0.5
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16 + 50, right: 16))
XCTAssertEqual(controller.gutterView.frame.origin.y, -16)
}
func test_editorOverScroll_ZeroCondition() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .zero
// editorOverscroll: 0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
}
// MARK: Indent
func test_indentOptionString() {
XCTAssertEqual(" ", IndentOption.spaces(count: 1).stringValue)
XCTAssertEqual(" ", IndentOption.spaces(count: 2).stringValue)
XCTAssertEqual(" ", IndentOption.spaces(count: 3).stringValue)
XCTAssertEqual(" ", IndentOption.spaces(count: 4).stringValue)
XCTAssertEqual(" ", IndentOption.spaces(count: 5).stringValue)
XCTAssertEqual("\t", IndentOption.tab.stringValue)
}
func test_indentBehavior() {
controller.highlighter = nil
// Insert 1 space
controller.indentOption = .spaces(count: 1)
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.length), with: "")
controller.textView.selectionManager.setSelectedRange(NSRange(location: 0, length: 0))
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, " ")
// Insert 2 spaces
controller.indentOption = .spaces(count: 2)
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.length), with: "")
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, " ")
// Insert 3 spaces
controller.indentOption = .spaces(count: 3)
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.length), with: "")
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, " ")
// Insert 4 spaces
controller.indentOption = .spaces(count: 4)
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.length), with: "")
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, " ")
// Insert tab
controller.indentOption = .tab
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.length), with: "")
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, "\t")
// Insert lots of spaces
controller.indentOption = .spaces(count: 1000)
controller.textView.replaceCharacters(in: NSRange(location: 0, length: controller.textView.textStorage.length), with: "")
controller.textView.insertText("\t", replacementRange: .zero)
XCTAssertEqual(controller.textView.string, String(repeating: " ", count: 1000))
}
func test_letterSpacing() {
let font: NSFont = .monospacedSystemFont(ofSize: 11, weight: .medium)
controller.letterSpacing = 1.0
XCTAssertEqual(
controller.attributesFor(nil)[.kern]! as! CGFloat,
(" " as NSString).size(withAttributes: [.font: font]).width * 0.0
)
controller.letterSpacing = 2.0
XCTAssertEqual(
controller.attributesFor(nil)[.kern]! as! CGFloat,
(" " as NSString).size(withAttributes: [.font: font]).width * 1.0
)
controller.letterSpacing = 1.0
}
// MARK: Bracket Highlights
func test_bracketHighlights() {
controller.scrollView.setFrameSize(NSSize(width: 500, height: 500))
controller.viewDidLoad()
let _ = controller.textView.becomeFirstResponder()
controller.bracketPairHighlight = nil
controller.setText("{ Lorem Ipsum {} }")
controller.setCursorPositions([CursorPosition(line: 1, column: 2)]) // After first opening {
XCTAssert(controller.highlightLayers.isEmpty, "Controller added highlight layer when setting is set to `nil`")
controller.setCursorPositions([CursorPosition(line: 1, column: 3)])
controller.bracketPairHighlight = .bordered(color: .black)
controller.textView.setNeedsDisplay()
controller.setCursorPositions([CursorPosition(line: 1, column: 2)]) // After first opening {
XCTAssert(controller.highlightLayers.count == 2, "Controller created an incorrect number of layers for bordered. Expected 2, found \(controller.highlightLayers.count)")
controller.setCursorPositions([CursorPosition(line: 1, column: 3)])
XCTAssert(controller.highlightLayers.isEmpty, "Controller failed to remove bracket pair layers.")
controller.bracketPairHighlight = .underline(color: .black)
controller.setCursorPositions([CursorPosition(line: 1, column: 2)]) // After first opening {
XCTAssert(controller.highlightLayers.count == 2, "Controller created an incorrect number of layers for underline. Expected 2, found \(controller.highlightLayers.count)")
controller.setCursorPositions([CursorPosition(line: 1, column: 3)])
XCTAssert(controller.highlightLayers.isEmpty, "Controller failed to remove bracket pair layers.")
controller.bracketPairHighlight = .flash
controller.setCursorPositions([CursorPosition(line: 1, column: 2)]) // After first opening {
XCTAssert(controller.highlightLayers.count == 1, "Controller created more than one layer for flash animation. Expected 1, found \(controller.highlightLayers.count)")
controller.setCursorPositions([CursorPosition(line: 1, column: 3)])
XCTAssert(controller.highlightLayers.isEmpty, "Controller failed to remove bracket pair layers.")
controller.setCursorPositions([CursorPosition(line: 1, column: 2)]) // After first opening {
XCTAssert(controller.highlightLayers.count == 1, "Controller created more than one layer for flash animation. Expected 1, found \(controller.highlightLayers.count)")
let exp = expectation(description: "Test after 0.8 seconds")
let result = XCTWaiter.wait(for: [exp], timeout: 0.8)
if result == XCTWaiter.Result.timedOut {
XCTAssert(controller.highlightLayers.isEmpty, "Controller failed to remove layer after flash animation. Expected 0, found \(controller.highlightLayers.count)")
} else {
XCTFail("Delay interrupted")
}
}
func test_findClosingPair() {
let _ = controller.textView.becomeFirstResponder()
controller.textView.string = "{ Lorem Ipsum {} }"
var idx: Int?
// Test walking forwards
idx = controller.findClosingPair("{", "}", from: 1, limit: 18, reverse: false)
XCTAssert(idx == 17, "Walking forwards failed. Expected `17`, found: `\(String(describing: idx))`")
// Test walking backwards
idx = controller.findClosingPair("}", "{", from: 17, limit: 0, reverse: true)
XCTAssert(idx == 0, "Walking backwards failed. Expected `0`, found: `\(String(describing: idx))`")
// Test extra pair
controller.textView.string = "{ Loren Ipsum {}} }"
idx = controller.findClosingPair("{", "}", from: 1, limit: 19, reverse: false)
XCTAssert(idx == 16, "Walking forwards with extra bracket pair failed. Expected `16`, found: `\(String(describing: idx))`")
// Text extra pair backwards
controller.textView.string = "{ Loren Ipsum {{} }"
idx = controller.findClosingPair("}", "{", from: 18, limit: 0, reverse: true)
XCTAssert(idx == 14, "Walking backwards with extra bracket pair failed. Expected `14`, found: `\(String(describing: idx))`")
// Test missing pair
controller.textView.string = "{ Loren Ipsum { }"
idx = controller.findClosingPair("{", "}", from: 1, limit: 17, reverse: false)
XCTAssert(idx == nil, "Walking forwards with missing pair failed. Expected `nil`, found: `\(String(describing: idx))`")
// Test missing pair backwards
controller.textView.string = " Loren Ipsum {} }"
idx = controller.findClosingPair("}", "{", from: 17, limit: 0, reverse: true)
XCTAssert(idx == nil, "Walking backwards with missing pair failed. Expected `nil`, found: `\(String(describing: idx))`")
}
// MARK: Set Text
func test_setText() {
let _ = controller.textView.becomeFirstResponder()
controller.textView.string = "Hello World"
controller.textView.selectionManager.setSelectedRange(NSRange(location: 1, length: 2))
controller.setText("\nHello World with newline!")
XCTAssert(controller.string == "\nHello World with newline!")
XCTAssertEqual(controller.cursorPositions.count, 1)
XCTAssertEqual(controller.cursorPositions[0].line, 2)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
XCTAssertEqual(controller.cursorPositions[0].range.location, 1)
XCTAssertEqual(controller.cursorPositions[0].range.length, 2)
XCTAssertEqual(controller.textView.selectionManager.textSelections.count, 1)
XCTAssertEqual(controller.textView.selectionManager.textSelections[0].range.location, 1)
XCTAssertEqual(controller.textView.selectionManager.textSelections[0].range.length, 2)
}
// MARK: Cursor Positions
func test_cursorPositionRangeInit() {
let _ = controller.textView.becomeFirstResponder()
controller.setText("Hello World")
// Test adding a position returns a valid one
controller.setCursorPositions([CursorPosition(range: NSRange(location: 0, length: 5))])
XCTAssertEqual(controller.cursorPositions.count, 1)
XCTAssertEqual(controller.cursorPositions[0].range.location, 0)
XCTAssertEqual(controller.cursorPositions[0].range.length, 5)
XCTAssertEqual(controller.cursorPositions[0].line, 1)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
// Test an invalid position is ignored
controller.setCursorPositions([CursorPosition(range: NSRange(location: -1, length: 25))])
XCTAssertTrue(controller.cursorPositions.count == 0)
// Test that column and line are correct
controller.setText("1\n2\n3\n4\n")
controller.setCursorPositions([CursorPosition(range: NSRange(location: 2, length: 0))])
XCTAssertEqual(controller.cursorPositions.count, 1)
XCTAssertEqual(controller.cursorPositions[0].range.location, 2)
XCTAssertEqual(controller.cursorPositions[0].range.length, 0)
XCTAssertEqual(controller.cursorPositions[0].line, 2)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
// Test order and validity of multiple positions.
controller.setCursorPositions([
CursorPosition(range: NSRange(location: 2, length: 0)),
CursorPosition(range: NSRange(location: 5, length: 1))
])
XCTAssertEqual(controller.cursorPositions.count, 2)
XCTAssertEqual(controller.cursorPositions[0].range.location, 2)
XCTAssertEqual(controller.cursorPositions[0].range.length, 0)
XCTAssertEqual(controller.cursorPositions[0].line, 2)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
XCTAssertEqual(controller.cursorPositions[1].range.location, 5)
XCTAssertEqual(controller.cursorPositions[1].range.length, 1)
XCTAssertEqual(controller.cursorPositions[1].line, 3)
XCTAssertEqual(controller.cursorPositions[1].column, 2)
}
func test_cursorPositionRowColInit() {
let _ = controller.textView.becomeFirstResponder()
controller.setText("Hello World")
// Test adding a position returns a valid one
controller.setCursorPositions([CursorPosition(line: 1, column: 1)])
XCTAssertEqual(controller.cursorPositions.count, 1)
XCTAssertEqual(controller.cursorPositions[0].range.location, 0)
XCTAssertEqual(controller.cursorPositions[0].range.length, 0)
XCTAssertEqual(controller.cursorPositions[0].line, 1)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
// Test an invalid position is ignored
controller.setCursorPositions([CursorPosition(line: -1, column: 10)])
XCTAssertTrue(controller.cursorPositions.count == 0)
// Test that column and line are correct
controller.setText("1\n2\n3\n4\n")
controller.setCursorPositions([CursorPosition(line: 2, column: 1)])
XCTAssertEqual(controller.cursorPositions.count, 1)
XCTAssertEqual(controller.cursorPositions[0].range.location, 2)
XCTAssertEqual(controller.cursorPositions[0].range.length, 0)
XCTAssertEqual(controller.cursorPositions[0].line, 2)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
// Test order and validity of multiple positions.
controller.setCursorPositions([
CursorPosition(line: 1, column: 1),
CursorPosition(line: 3, column: 1)
])
XCTAssertEqual(controller.cursorPositions.count, 2)
XCTAssertEqual(controller.cursorPositions[0].range.location, 0)
XCTAssertEqual(controller.cursorPositions[0].range.length, 0)
XCTAssertEqual(controller.cursorPositions[0].line, 1)
XCTAssertEqual(controller.cursorPositions[0].column, 1)
XCTAssertEqual(controller.cursorPositions[1].range.location, 4)
XCTAssertEqual(controller.cursorPositions[1].range.length, 0)
XCTAssertEqual(controller.cursorPositions[1].line, 3)
XCTAssertEqual(controller.cursorPositions[1].column, 1)
}
// MARK: - TreeSitterClient
func test_treeSitterSetUp() {
// Set up with a user-initiated `TreeSitterClient` should still use that client for things like tag
// completion.
let controller = Mock.textViewController(theme: Mock.theme())
XCTAssertNotNil(controller.treeSitterClient)
}
}
// swiftlint:enable all