Skip to content

Commit ea57c2b

Browse files
committed
Convert inlay type hint tests to use position markers
1 parent abf98b9 commit ea57c2b

File tree

1 file changed

+84
-70
lines changed

1 file changed

+84
-70
lines changed

Tests/SourceKitLSPTests/InlayHintTests.swift

Lines changed: 84 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@ import XCTest
1818
final class InlayHintTests: XCTestCase {
1919
// MARK: - Helpers
2020

21-
func performInlayHintRequest(text: String, range: Range<Position>? = nil) async throws -> [InlayHint] {
21+
func performInlayHintRequest(
22+
markedText: String,
23+
range: (fromMarker: String, toMarker: String)? = nil
24+
) async throws -> (DocumentPositions, [InlayHint]) {
2225
let testClient = try await TestSourceKitLSPClient()
2326
let uri = DocumentURI(for: .swift)
2427

28+
let (positions, text) = DocumentPositions.extract(from: markedText)
2529
testClient.openDocument(text, uri: uri)
2630

31+
let range: Range<Position>? =
32+
if let range {
33+
positions[range.fromMarker]..<positions[range.toMarker]
34+
} else {
35+
nil
36+
}
2737
let request = InlayHintRequest(textDocument: TextDocumentIdentifier(uri), range: range)
28-
return try await testClient.send(request)
38+
return (positions, try await testClient.send(request))
2939
}
3040

3141
private func makeInlayHint(
@@ -51,27 +61,27 @@ final class InlayHintTests: XCTestCase {
5161
// MARK: - Tests
5262

5363
func testEmpty() async throws {
54-
let text = ""
55-
let hints = try await performInlayHintRequest(text: text)
64+
let (_, hints) = try await performInlayHintRequest(markedText: "")
5665
XCTAssertEqual(hints, [])
5766
}
5867

5968
func testBindings() async throws {
60-
let text = """
61-
let x = 4
62-
var y = "test" + "123"
63-
"""
64-
let hints = try await performInlayHintRequest(text: text)
69+
let (positions, hints) = try await performInlayHintRequest(
70+
markedText: """
71+
let x1️⃣ = 4
72+
var y2️⃣ = "test" + "123"
73+
"""
74+
)
6575
XCTAssertEqual(
6676
hints,
6777
[
6878
makeInlayHint(
69-
position: Position(line: 0, utf16index: 5),
79+
position: positions["1️⃣"],
7080
kind: .type,
7181
label: ": Int"
7282
),
7383
makeInlayHint(
74-
position: Position(line: 1, utf16index: 5),
84+
position: positions["2️⃣"],
7585
kind: .type,
7686
label: ": String"
7787
),
@@ -80,30 +90,31 @@ final class InlayHintTests: XCTestCase {
8090
}
8191

8292
func testRanged() async throws {
83-
let text = """
84-
func square(_ x: Double) -> Double {
85-
let result = x * x
86-
return result
87-
}
88-
89-
func collatz(_ n: Int) -> Int {
90-
let even = n % 2 == 0
91-
let result = even ? (n / 2) : (3 * n + 1)
92-
return result
93-
}
94-
"""
95-
let range = Position(line: 6, utf16index: 0)..<Position(line: 9, utf16index: 0)
96-
let hints = try await performInlayHintRequest(text: text, range: range)
93+
let (positions, hints) = try await performInlayHintRequest(
94+
markedText: """
95+
func square(_ x: Double) -> Double {
96+
let result = x * x
97+
return result
98+
}
99+
100+
func collatz(_ n: Int) -> Int {
101+
1️⃣ let even2️⃣ = n % 2 == 0
102+
let result3️⃣ = even ? (n / 2) : (3 * n + 1)
103+
return result
104+
} 4️⃣
105+
""",
106+
range: ("1️⃣", "4️⃣")
107+
)
97108
XCTAssertEqual(
98109
hints,
99110
[
100111
makeInlayHint(
101-
position: Position(line: 6, utf16index: 10),
112+
position: positions["2️⃣"],
102113
kind: .type,
103114
label: ": Bool"
104115
),
105116
makeInlayHint(
106-
position: Position(line: 7, utf16index: 12),
117+
position: positions["3️⃣"],
107118
kind: .type,
108119
label: ": Int"
109120
),
@@ -112,47 +123,48 @@ final class InlayHintTests: XCTestCase {
112123
}
113124

114125
func testFields() async throws {
115-
let text = """
116-
class X {
117-
let instanceMember = 3
118-
static let staticMember = "abc"
119-
}
120-
121-
struct Y {
122-
var instanceMember = "def" + "ghi"
123-
static let staticMember = 1 + 2
124-
}
125-
126-
enum Z {
127-
static let staticMember = 3.0
128-
}
129-
"""
130-
let hints = try await performInlayHintRequest(text: text)
126+
let (positions, hints) = try await performInlayHintRequest(
127+
markedText: """
128+
class X {
129+
let instanceMember1️⃣ = 3
130+
static let staticMember2️⃣ = "abc"
131+
}
132+
133+
struct Y {
134+
var instanceMember3️⃣ = "def" + "ghi"
135+
static let staticMember4️⃣ = 1 + 2
136+
}
137+
138+
enum Z {
139+
static let staticMember5️⃣ = 3.0
140+
}
141+
"""
142+
)
131143
XCTAssertEqual(
132144
hints,
133145
[
134146
makeInlayHint(
135-
position: Position(line: 1, utf16index: 20),
147+
position: positions["1️⃣"],
136148
kind: .type,
137149
label: ": Int"
138150
),
139151
makeInlayHint(
140-
position: Position(line: 2, utf16index: 25),
152+
position: positions["2️⃣"],
141153
kind: .type,
142154
label: ": String"
143155
),
144156
makeInlayHint(
145-
position: Position(line: 6, utf16index: 20),
157+
position: positions["3️⃣"],
146158
kind: .type,
147159
label: ": String"
148160
),
149161
makeInlayHint(
150-
position: Position(line: 7, utf16index: 25),
162+
position: positions["4️⃣"],
151163
kind: .type,
152164
label: ": Int"
153165
),
154166
makeInlayHint(
155-
position: Position(line: 11, utf16index: 25),
167+
position: positions["5️⃣"],
156168
kind: .type,
157169
label: ": Double"
158170
),
@@ -161,49 +173,51 @@ final class InlayHintTests: XCTestCase {
161173
}
162174

163175
func testExplicitTypeAnnotation() async throws {
164-
let text = """
165-
let x: String = "abc"
166-
167-
struct X {
168-
var y: Int = 34
169-
}
170-
"""
171-
let hints = try await performInlayHintRequest(text: text)
176+
let (_, hints) = try await performInlayHintRequest(
177+
markedText: """
178+
let x: String = "abc"
179+
180+
struct X {
181+
var y: Int = 34
182+
}
183+
"""
184+
)
172185
XCTAssertEqual(hints, [])
173186
}
174187

175188
func testClosureParams() async throws {
176-
let text = """
177-
func f(x: Int) {}
178-
179-
let g = { (x: Int) in }
180-
let h: (String) -> String = { x in x }
181-
let i: (Double, Double) -> Double = { (x, y) in
182-
x + y
183-
}
184-
"""
185-
let hints = try await performInlayHintRequest(text: text)
189+
let (positions, hints) = try await performInlayHintRequest(
190+
markedText: """
191+
func f(x: Int) {}
192+
193+
let g1️⃣ = { (x: Int) in }
194+
let h: (String) -> String = { x2️⃣ in x }
195+
let i: (Double, Double) -> Double = { (x3️⃣, y4️⃣) in
196+
x + y
197+
}
198+
"""
199+
)
186200
XCTAssertEqual(
187201
hints,
188202
[
189203
makeInlayHint(
190-
position: Position(line: 2, utf16index: 5),
204+
position: positions["1️⃣"],
191205
kind: .type,
192206
label: ": (Int) -> ()"
193207
),
194208
makeInlayHint(
195-
position: Position(line: 3, utf16index: 31),
209+
position: positions["2️⃣"],
196210
kind: .type,
197211
label: ": String",
198212
hasEdit: false
199213
),
200214
makeInlayHint(
201-
position: Position(line: 4, utf16index: 40),
215+
position: positions["3️⃣"],
202216
kind: .type,
203217
label: ": Double"
204218
),
205219
makeInlayHint(
206-
position: Position(line: 4, utf16index: 43),
220+
position: positions["4️⃣"],
207221
kind: .type,
208222
label: ": Double"
209223
),

0 commit comments

Comments
 (0)