-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSwiftFocusedCodeFinderTests.swift
More file actions
507 lines (471 loc) · 15.4 KB
/
SwiftFocusedCodeFinderTests.swift
File metadata and controls
507 lines (471 loc) · 15.4 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import Foundation
import SuggestionBasic
import XCTest
@testable import FocusedCodeFinder
func document(code: String) -> FocusedCodeFinder.Document {
.init(
documentURL: URL(fileURLWithPath: "/"),
content: code,
lines: code.components(separatedBy: "\n").map { "\($0)\n" }
)
}
final class SwiftFocusedCodeFinder_Selection_Tests: XCTestCase {
func test_selecting_a_line_inside_the_function_the_scope_should_be_the_function() {
let code = """
public struct A: B, C {
@ViewBuilder private func f(_ a: String) -> String {
let a = 1
let b = 2
let c = 3
let d = 4
let e = 5
}
}
"""
let range = CursorRange(
start: CursorPosition(line: 4, character: 0),
end: CursorPosition(line: 4, character: 13)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "public struct A: B, C",
name: "A",
range: .init(startPair: (0, 0), endPair: (8, 1))
),
.init(
signature: "@ViewBuilder private func f(_ a: String) -> String",
name: "f",
range: .init(startPair: (1, 4), endPair: (7, 5))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (8, 1)),
smallestContextRange: .init(startPair: (4, 0), endPair: (4, 13)),
focusedRange: .init(startPair: (4, 0), endPair: (4, 13)),
focusedCode: """
let c = 3
""",
imports: [],
includes: []
))
}
func test_selecting_a_function_inside_a_struct_the_scope_should_be_the_struct() {
let code = """
@MainActor
public struct A: B, C {
func f() {
let a = 1
let b = 2
let c = 3
let d = 4
let e = 5
}
}
"""
let range = CursorRange(
start: CursorPosition(line: 2, character: 0),
end: CursorPosition(line: 7, character: 5)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "@MainActor public struct A: B, C",
name: "A",
range: .init(startPair: (0, 0), endPair: (9, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (9, 1)),
smallestContextRange: .init(startPair: (2, 0), endPair: (7, 5)),
focusedRange: .init(startPair: (2, 0), endPair: (7, 5)),
focusedCode: """
func f() {
let a = 1
let b = 2
let c = 3
let d = 4
let e = 5
""",
imports: [],
includes: []
))
}
func test_selecting_a_variable_inside_a_class_the_scope_should_be_the_class() {
let code = """
@MainActor final public class A: P<B, C, D>, K {
var a = 1
var b = 2
var c = 3
var d = 4
var e = 5
}
"""
let range = CursorRange(
start: CursorPosition(line: 1, character: 0),
end: CursorPosition(line: 1, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "@MainActor final public class A: P<B, C, D>, K",
name: "A",
range: .init(startPair: (0, 0), endPair: (6, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (6, 1)),
smallestContextRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedCode: """
var a = 1
""",
imports: [],
includes: []
))
}
func test_selecting_a_function_inside_a_protocol_the_scope_should_be_the_protocol() {
let code = """
public protocol A: Hashable {
func f()
func g()
func h()
func i()
func j()
}
"""
let range = CursorRange(
start: CursorPosition(line: 1, character: 0),
end: CursorPosition(line: 1, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "public protocol A: Hashable",
name: "A",
range: .init(startPair: (0, 0), endPair: (6, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (6, 1)),
smallestContextRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedCode: """
func f()
""",
imports: [],
includes: []
))
}
func test_selecting_a_variable_inside_an_extension_the_scope_should_be_the_extension() {
let code = """
private extension A: Equatable {
var a = 1
var b = 2
var c = 3
var d = 4
var e = 5
}
"""
let range = CursorRange(
start: CursorPosition(line: 1, character: 0),
end: CursorPosition(line: 1, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "private extension A: Equatable",
name: "A",
range: .init(startPair: (0, 0), endPair: (6, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (6, 1)),
smallestContextRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedRange: .init(startPair: (1, 0), endPair: (1, 9)),
focusedCode: """
var a = 1
""",
imports: [],
includes: []
))
}
func test_selecting_a_static_function_from_an_actor_the_scope_should_be_the_actor() {
let code = """
@gloablActor
public actor A {
static func f() {}
static func g() {}
static func h() {}
static func i() {}
static func j() {}
}
"""
let range = CursorRange(
start: CursorPosition(line: 2, character: 0),
end: CursorPosition(line: 2, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "@gloablActor public actor A",
name: "A",
range: .init(startPair: (0, 0), endPair: (7, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (7, 1)),
smallestContextRange: .init(startPair: (2, 0), endPair: (2, 9)),
focusedRange: .init(startPair: (2, 0), endPair: (2, 9)),
focusedCode: """
static func f() {}
""",
imports: [],
includes: []
))
}
func test_selecting_a_case_inside_an_enum_the_scope_should_be_the_enum() {
let code = """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
"""
let range = CursorRange(
start: CursorPosition(line: 3, character: 0),
end: CursorPosition(line: 3, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "@MainActor public indirect enum A",
name: "A",
range: .init(startPair: (0, 0), endPair: (8, 1))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (8, 1)),
smallestContextRange: .init(startPair: (3, 0), endPair: (3, 9)),
focusedRange: .init(startPair: (3, 0), endPair: (3, 9)),
focusedCode: """
case a
""",
imports: [],
includes: []
))
}
func test_selecting_a_line_inside_computed_variable_the_scope_should_be_the_variable() {
let code = """
struct A {
@SomeWrapper public private(set) var a: Int {
let a = 1
let b = 2
let c = 3
let d = 4
let e = 5
}
}
"""
let range = CursorRange(
start: CursorPosition(line: 2, character: 0),
end: CursorPosition(line: 2, character: 9)
)
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: .max).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .scope(signature: [
.init(
signature: "struct A",
name: "A",
range: .init(startPair: (0, 0), endPair: (8, 1))
),
.init(
signature: "@SomeWrapper public private(set) var a: Int",
name: "a",
range: .init(startPair: (1, 4), endPair: (7, 5))
),
]),
contextRange: .init(startPair: (0, 0), endPair: (8, 1)),
smallestContextRange: .init(startPair: (2, 0), endPair: (2, 9)),
focusedRange: .init(startPair: (2, 0), endPair: (2, 9)),
focusedCode: """
let a = 1
""",
imports: [],
includes: []
))
}
func test_selecting_a_line_in_freestanding_macro_the_scope_should_be_the_macro() {
// TODO:
}
}
final class SwiftFocusedCodeFinder_FocusedCode_Tests: XCTestCase {
func test_get_focused_code_on_top_level_should_fallback_to_unknown_language() {
let code = """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
func hello() {
print("hello")
print("hello")
}
"""
let range = CursorRange(startPair: (0, 0), endPair: (0, 0))
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 1000).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .top,
contextRange: .init(startPair: (0, 0), endPair: (13, 2)),
smallestContextRange: .init(startPair: (0, 0), endPair: (13, 2)),
focusedRange: .init(startPair: (0, 0), endPair: (13, 2)),
focusedCode: """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
func hello() {
print("hello")
print("hello")
}
""",
imports: [],
includes: []
))
}
func test_get_focused_code_inside_enum_the_whole_enum_will_be_the_focused_code() {
let code = """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
"""
let range = CursorRange(startPair: (3, 0), endPair: (3, 0))
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 1000).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .file,
contextRange: .init(startPair: (0, 0), endPair: (0, 0)),
smallestContextRange: .init(startPair: (0, 0), endPair: (8, 1)),
focusedRange: .init(startPair: (0, 0), endPair: (8, 1)),
focusedCode: """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
""",
imports: [],
includes: []
))
}
func test_get_focused_code_inside_enum_with_limited_max_line_count() {
let code = """
@MainActor
public
indirect enum A {
case a
case b
case c
case d
case e
}
"""
let range = CursorRange(startPair: (3, 0), endPair: (3, 0))
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 3).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context, .init(
scope: .file,
contextRange: .init(startPair: (0, 0), endPair: (0, 0)),
smallestContextRange: .init(startPair: (0, 0), endPair: (8, 1)),
focusedRange: .init(startPair: (2, 0), endPair: (4, 11)),
focusedCode: """
indirect enum A {
case a
case b
""",
imports: [],
includes: []
))
}
}
final class SwiftFocusedCodeFinder_Import_Tests: XCTestCase {
func test_parsing_imports() {
let code = """
import OnTop
import Second
import Third
struct Foo {
}
import BelowStructFoo
class Bar {
}
import BelowClassBar
"""
let range = CursorRange.zero
let context = SwiftFocusedCodeFinder(maxFocusedCodeLineCount: 3).findFocusedCode(
in: document(code: code),
containingRange: range
)
XCTAssertEqual(context.imports, [
"OnTop",
"Second",
"Third",
"BelowStructFoo",
"BelowClassBar",
])
}
}