Repository navigation
Expand file tree
/
Copy pathPullDiagnosticsTests.swift
More file actions
109 lines (97 loc) · 3.59 KB
/
Copy pathPullDiagnosticsTests.swift
File metadata and controls
109 lines (97 loc) · 3.59 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LSPTestSupport
import LanguageServerProtocol
import SKTestSupport
import XCTest
final class PullDiagnosticsTests: XCTestCase {
func testUnknownIdentifierDiagnostic() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI.for(.swift)
testClient.openDocument(
"""
func foo() {
invalid
}
""",
uri: uri
)
let report = try await testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri)))
guard case .full(let fullReport) = report else {
XCTFail("Expected full diagnostics report")
return
}
XCTAssertEqual(fullReport.items.count, 1)
let diagnostic = try XCTUnwrap(fullReport.items.first)
XCTAssertEqual(diagnostic.range, Position(line: 1, utf16index: 2)..<Position(line: 1, utf16index: 9))
}
/// Test that we can get code actions for pulled diagnostics (https://github.com/apple/sourcekit-lsp/issues/776)
func testCodeActions() async throws {
let testClient = try await TestSourceKitLSPClient(
capabilities: ClientCapabilities(
workspace: nil,
textDocument: .init(
codeAction: .init(codeActionLiteralSupport: .init(codeActionKind: .init(valueSet: [.quickFix])))
)
)
)
let uri = DocumentURI.for(.swift)
testClient.openDocument(
"""
protocol MyProtocol {
func bar()
}
struct Test: MyProtocol {}
""",
uri: uri
)
let report = try await testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri)))
guard case .full(let fullReport) = report else {
XCTFail("Expected full diagnostics report")
return
}
let diagnostics = fullReport.items
XCTAssertEqual(diagnostics.count, 1)
let diagnostic = try XCTUnwrap(diagnostics.first)
XCTAssertEqual(diagnostic.range, Position(line: 4, utf16index: 7)..<Position(line: 4, utf16index: 7))
let note = try XCTUnwrap(diagnostic.relatedInformation?.first)
XCTAssertEqual(note.location.range, Position(line: 4, utf16index: 7)..<Position(line: 4, utf16index: 7))
XCTAssertEqual(note.codeActions?.count ?? 0, 1)
let response = try await testClient.send(
CodeActionRequest(
range: note.location.range,
context: CodeActionContext(
diagnostics: diagnostics,
only: [.quickFix],
triggerKind: .invoked
),
textDocument: TextDocumentIdentifier(note.location.uri)
)
)
guard case .codeActions(let actions) = response else {
XCTFail("Expected codeActions response")
return
}
XCTAssertEqual(actions.count, 1)
let action = try XCTUnwrap(actions.first)
// Allow the action message to be the one before or after
// https://github.com/apple/swift/pull/67909, ensuring this test passes with
// a sourcekitd that contains the change from that PR as well as older
// toolchains that don't contain the change yet.
XCTAssert(
[
"Add stubs for conformance",
"Do you want to add protocol stubs?",
].contains(action.title)
)
}
}