Skip to content

Commit 8d31abc

Browse files
committed
Modernize hover tests
1 parent 8066447 commit 8d31abc

File tree

1 file changed

+98
-150
lines changed

1 file changed

+98
-150
lines changed

Tests/SourceKitLSPTests/HoverTests.swift

Lines changed: 98 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -16,191 +16,139 @@ import SKTestSupport
1616
import XCTest
1717

1818
final class HoverTests: XCTestCase {
19-
func testHover() async throws {
19+
func testBasic() async throws {
20+
try await assertHover(
21+
"""
22+
/// This is a doc comment for S.
23+
///
24+
/// Details.
25+
struct 1️⃣S {}
26+
""",
27+
expectedContent: """
28+
S
29+
```swift
30+
struct S
31+
```
32+
33+
---
34+
This is a doc comment for S.
35+
36+
### Discussion
37+
38+
Details.
39+
"""
40+
)
41+
}
42+
43+
func testHoverTriggeredFromComment() async throws {
2044
let testClient = try await TestSourceKitLSPClient()
2145
let url = URL(fileURLWithPath: "/\(UUID())/a.swift")
2246
let uri = DocumentURI(url)
2347

24-
testClient.openDocument(
48+
let positions = testClient.openDocument(
2549
"""
26-
/// This is a doc comment for S.
50+
/// Thi1️⃣s is a doc comment for S.
2751
///
2852
/// Details.
2953
struct S {}
3054
""",
3155
uri: uri
3256
)
3357

34-
do {
35-
let resp = try await testClient.send(
36-
HoverRequest(
37-
textDocument: TextDocumentIdentifier(url),
38-
position: Position(line: 3, utf16index: 7)
39-
)
40-
)
41-
42-
XCTAssertNotNil(resp)
43-
if let hover = resp {
44-
XCTAssertNil(hover.range)
45-
guard case .markupContent(let content) = hover.contents else {
46-
XCTFail("hover.contents is not .markupContents")
47-
return
48-
}
49-
XCTAssertEqual(content.kind, .markdown)
50-
XCTAssertEqual(
51-
content.value,
52-
"""
53-
S
54-
```swift
55-
struct S
56-
```
57-
58-
---
59-
This is a doc comment for S.
60-
61-
### Discussion
62-
63-
Details.
64-
"""
65-
)
66-
}
67-
}
68-
69-
do {
70-
let resp = try await testClient.send(
71-
HoverRequest(
72-
textDocument: TextDocumentIdentifier(url),
73-
position: Position(line: 0, utf16index: 7)
74-
)
75-
)
76-
77-
XCTAssertNil(resp)
78-
}
58+
let response = try await testClient.send(
59+
HoverRequest(textDocument: TextDocumentIdentifier(url), position: positions["1️⃣"])
60+
)
61+
62+
XCTAssertNil(response)
7963
}
8064

8165
func testMultiCursorInfoResultsHover() async throws {
82-
let testClient = try await TestSourceKitLSPClient()
83-
let uri = DocumentURI.for(.swift)
84-
85-
let positions = testClient.openDocument(
66+
try await assertHover(
8667
"""
8768
struct Foo {
8869
init() {}
8970
}
9071
_ = 1️⃣Foo()
9172
""",
92-
uri: uri
93-
)
94-
95-
let response = try await testClient.send(
96-
HoverRequest(
97-
textDocument: TextDocumentIdentifier(uri),
98-
position: positions["1️⃣"]
99-
)
100-
)
73+
expectedContent: """
74+
Foo
75+
```swift
76+
struct Foo
77+
```
10178
102-
guard case .markupContent(let content) = response?.contents else {
103-
XCTFail("hover.contents is not .markupContents")
104-
return
105-
}
106-
XCTAssertEqual(content.kind, .markdown)
107-
XCTAssertEqual(
108-
content.value,
109-
"""
110-
Foo
111-
```swift
112-
struct Foo
113-
```
79+
---
11480
115-
---
81+
# Alternative result
82+
init()
83+
```swift
84+
init()
85+
```
11686
117-
# Alternative result
118-
init()
119-
```swift
120-
init()
121-
```
87+
---
12288
123-
---
89+
"""
90+
)
91+
}
12492

93+
func testHoverNameEscapingOnFunction() async throws {
94+
try await assertHover(
12595
"""
96+
/// this is **bold** documentation
97+
func 1️⃣test(_ a: Int, _ b: Int) { }
98+
""",
99+
expectedContent: ##"""
100+
test(\_:\_:)
101+
```swift
102+
func test(_ a: Int, _ b: Int)
103+
```
104+
105+
---
106+
this is **bold** documentation
107+
"""##
126108
)
127109
}
128110

129-
func testHoverNameEscaping() async throws {
130-
let testClient = try await TestSourceKitLSPClient()
131-
let uri = DocumentURI.for(.swift)
132-
133-
testClient.openDocument(
111+
func testHoverNameEscapingOnOperator() async throws {
112+
try await assertHover(
134113
"""
135-
/// this is **bold** documentation
136-
func test(_ a: Int, _ b: Int) { }
137114
/// this is *italic* documentation
138-
func *%*(lhs: String, rhs: String) { }
115+
func 1️⃣*%*(lhs: String, rhs: String) { }
139116
""",
140-
uri: uri
117+
expectedContent: ##"""
118+
\*%\*(\_:\_:)
119+
```swift
120+
func *%* (lhs: String, rhs: String)
121+
```
122+
123+
---
124+
this is *italic* documentation
125+
"""##
141126
)
127+
}
128+
}
142129

143-
do {
144-
let resp = try await testClient.send(
145-
HoverRequest(
146-
textDocument: TextDocumentIdentifier(uri),
147-
position: Position(line: 1, utf16index: 7)
148-
)
149-
)
150-
151-
XCTAssertNotNil(resp)
152-
if let hover = resp {
153-
XCTAssertNil(hover.range)
154-
guard case .markupContent(let content) = hover.contents else {
155-
XCTFail("hover.contents is not .markupContents")
156-
return
157-
}
158-
XCTAssertEqual(content.kind, .markdown)
159-
XCTAssertEqual(
160-
content.value,
161-
##"""
162-
test(\_:\_:)
163-
```swift
164-
func test(_ a: Int, _ b: Int)
165-
```
166-
167-
---
168-
this is **bold** documentation
169-
"""##
170-
)
171-
}
172-
}
173-
174-
do {
175-
let resp = try await testClient.send(
176-
HoverRequest(
177-
textDocument: TextDocumentIdentifier(uri),
178-
position: Position(line: 3, utf16index: 7)
179-
)
180-
)
181-
182-
XCTAssertNotNil(resp)
183-
if let hover = resp {
184-
XCTAssertNil(hover.range)
185-
guard case .markupContent(let content) = hover.contents else {
186-
XCTFail("hover.contents is not .markupContents")
187-
return
188-
}
189-
XCTAssertEqual(content.kind, .markdown)
190-
XCTAssertEqual(
191-
content.value,
192-
##"""
193-
\*%\*(\_:\_:)
194-
```swift
195-
func *%* (lhs: String, rhs: String)
196-
```
197-
198-
---
199-
this is *italic* documentation
200-
"""##
201-
)
202-
}
203-
}
130+
private func assertHover(
131+
_ markedSource: String,
132+
expectedContent: String,
133+
file: StaticString = #file,
134+
line: UInt = #line
135+
) async throws {
136+
let testClient = try await TestSourceKitLSPClient()
137+
let uri = DocumentURI.for(.swift)
138+
139+
let positions = testClient.openDocument(markedSource, uri: uri)
140+
141+
let response = try await testClient.send(
142+
HoverRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
143+
)
144+
145+
let hover = try XCTUnwrap(response, file: file, line: line)
146+
XCTAssertNil(hover.range, file: file, line: line)
147+
guard case .markupContent(let content) = hover.contents else {
148+
XCTFail("hover.contents is not .markupContents", file: file, line: line)
149+
return
204150
}
151+
XCTAssertEqual(content.kind, .markdown, file: file, line: line)
152+
XCTAssertEqual(content.value, expectedContent, file: file, line: line)
205153

206154
}

0 commit comments

Comments
 (0)