Skip to content

Commit ac58c12

Browse files
committed
Split SourceKitTests.swift
SourceKitTests.swift was a collection of unrelated tests. Split the apart into multiple, dedicated test files
1 parent 8af0bb5 commit ac58c12

File tree

6 files changed

+580
-534
lines changed

6 files changed

+580
-534
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
import SKTestSupport
15+
import XCTest
16+
17+
final class ClangdTests: XCTestCase {
18+
func testClangdGoToInclude() async throws {
19+
try XCTSkipIf(!hasClangd)
20+
21+
let ws = try await MultiFileTestWorkspace(files: [
22+
"Object.h": "",
23+
"main.c": """
24+
#include 1️⃣"Object.h"
25+
""",
26+
])
27+
let (mainUri, positions) = try ws.openDocument("main.c")
28+
let headerUri = try ws.uri(for: "Object.h")
29+
30+
let goToInclude = DefinitionRequest(
31+
textDocument: TextDocumentIdentifier(mainUri),
32+
position: positions["1️⃣"]
33+
)
34+
let resp = try await ws.testClient.send(goToInclude)
35+
36+
let locationsOrLinks = try XCTUnwrap(resp, "No response for go-to-#include")
37+
switch locationsOrLinks {
38+
case .locations(let locations):
39+
XCTAssert(!locations.isEmpty, "Found no locations for go-to-#include")
40+
if let loc = locations.first {
41+
XCTAssertEqual(loc.uri, headerUri)
42+
}
43+
case .locationLinks(let locationLinks):
44+
XCTAssert(!locationLinks.isEmpty, "Found no location links for go-to-#include")
45+
if let link = locationLinks.first {
46+
XCTAssertEqual(link.targetUri, headerUri)
47+
}
48+
}
49+
}
50+
51+
func testClangdGoToDefinitionWithoutIndex() async throws {
52+
try XCTSkipIf(!hasClangd)
53+
54+
let ws = try await MultiFileTestWorkspace(files: [
55+
"Object.h": """
56+
struct Object {
57+
int field;
58+
};
59+
""",
60+
"main.c": """
61+
#include "Object.h"
62+
63+
int main(int argc, const char *argv[]) {
64+
struct 1️⃣Object *obj;
65+
}
66+
""",
67+
])
68+
69+
let (mainUri, positions) = try ws.openDocument("main.c")
70+
let headerUri = try ws.uri(for: "Object.h")
71+
72+
let goToDefinition = DefinitionRequest(
73+
textDocument: TextDocumentIdentifier(mainUri),
74+
position: positions["1️⃣"]
75+
)
76+
let resp = try await ws.testClient.send(goToDefinition)
77+
78+
let locationsOrLinks = try XCTUnwrap(resp, "No response for go-to-definition")
79+
switch locationsOrLinks {
80+
case .locations(let locations):
81+
XCTAssert(!locations.isEmpty, "Found no locations for go-to-definition")
82+
if let loc = locations.first {
83+
XCTAssertEqual(loc.uri, headerUri)
84+
}
85+
case .locationLinks(let locationLinks):
86+
XCTAssert(!locationLinks.isEmpty, "Found no location links for go-to-definition")
87+
if let link = locationLinks.first {
88+
XCTAssertEqual(link.targetUri, headerUri)
89+
}
90+
}
91+
}
92+
93+
func testClangdGoToDeclaration() async throws {
94+
try XCTSkipIf(!hasClangd)
95+
96+
let ws = try await MultiFileTestWorkspace(files: [
97+
"Object.h": """
98+
struct Object {
99+
int field;
100+
};
101+
102+
struct Object * newObject();
103+
""",
104+
"main.c": """
105+
#include "Object.h"
106+
107+
int main(int argc, const char *argv[]) {
108+
struct Object *obj = 1️⃣newObject();
109+
}
110+
""",
111+
])
112+
113+
let (mainUri, positions) = try ws.openDocument("main.c")
114+
let headerUri = try ws.uri(for: "Object.h")
115+
116+
let goToInclude = DeclarationRequest(
117+
textDocument: TextDocumentIdentifier(mainUri),
118+
position: positions["1️⃣"]
119+
)
120+
let resp = try await ws.testClient.send(goToInclude)
121+
122+
let locationsOrLinks = try XCTUnwrap(resp, "No response for go-to-declaration")
123+
switch locationsOrLinks {
124+
case .locations(let locations):
125+
XCTAssert(!locations.isEmpty, "Found no locations for go-to-declaration")
126+
if let loc = locations.first {
127+
XCTAssertEqual(loc.uri, headerUri)
128+
}
129+
case .locationLinks(let locationLinks):
130+
XCTAssert(!locationLinks.isEmpty, "Found no location links for go-to-declaration")
131+
if let link = locationLinks.first {
132+
XCTAssertEqual(link.targetUri, headerUri)
133+
}
134+
}
135+
}
136+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
import SKTestSupport
15+
import XCTest
16+
17+
final class DependencyTrackingTests: XCTestCase {
18+
func testDependenciesUpdatedSwift() async throws {
19+
let ws = try await SwiftPMTestWorkspace(
20+
files: [
21+
"LibA/LibA.swift": """
22+
public func aaa() {}
23+
""",
24+
"LibB/LibB.swift": """
25+
import LibA
26+
public func bbb() {
27+
aaa()
28+
}
29+
""",
30+
],
31+
manifest: """
32+
// swift-tools-version: 5.7
33+
34+
import PackageDescription
35+
36+
let package = Package(
37+
name: "MyLibrary",
38+
targets: [
39+
.target(name: "LibA"),
40+
.target(name: "LibB", dependencies: ["LibA"]),
41+
]
42+
)
43+
"""
44+
)
45+
46+
let (libBUri, _) = try ws.openDocument("LibB.swift")
47+
48+
let initialDiags = try await ws.testClient.nextDiagnosticsNotification()
49+
// Semantic analysis: expect module import error.
50+
XCTAssertEqual(initialDiags.diagnostics.count, 1)
51+
if let diagnostic = initialDiags.diagnostics.first {
52+
// FIXME: The error message for the missing module is misleading on Darwin
53+
// https://github.com/apple/swift-package-manager/issues/5925
54+
XCTAssert(
55+
diagnostic.message.contains("Could not build Objective-C module")
56+
|| diagnostic.message.contains("No such module"),
57+
"expected module import error but found \"\(diagnostic.message)\""
58+
)
59+
}
60+
61+
try await SwiftPMTestWorkspace.build(at: ws.scratchDirectory)
62+
63+
await ws.testClient.server.filesDependenciesUpdated([libBUri])
64+
65+
let updatedDiags = try await ws.testClient.nextDiagnosticsNotification()
66+
// Semantic analysis: no more errors expected, import should resolve since we built.
67+
XCTAssertEqual(updatedDiags.diagnostics.count, 0)
68+
}
69+
70+
func testDependenciesUpdatedCXX() async throws {
71+
let ws = try await MultiFileTestWorkspace(files: [
72+
"lib.c": """
73+
int libX(int value) {
74+
return value ? 22 : 0;
75+
}
76+
""",
77+
"main.c": """
78+
#include "lib-generated.h"
79+
80+
int main(int argc, const char *argv[]) {
81+
return libX(argc);
82+
}
83+
""",
84+
"compile_flags.txt": "",
85+
])
86+
87+
let generatedHeaderURL = try ws.uri(for: "main.c").fileURL!.deletingLastPathComponent()
88+
.appendingPathComponent("lib-generated.h", isDirectory: false)
89+
90+
// Write an empty header file first since clangd doesn't handle missing header
91+
// files without a recently upstreamed extension.
92+
try "".write(to: generatedHeaderURL, atomically: true, encoding: .utf8)
93+
let (mainUri, _) = try ws.openDocument("main.c")
94+
95+
let openDiags = try await ws.testClient.nextDiagnosticsNotification()
96+
// Expect one error:
97+
// - Implicit declaration of function invalid
98+
XCTAssertEqual(openDiags.diagnostics.count, 1)
99+
100+
// Update the header file to have the proper contents for our code to build.
101+
let contents = "int libX(int value);"
102+
try contents.write(to: generatedHeaderURL, atomically: true, encoding: .utf8)
103+
104+
await ws.testClient.server.filesDependenciesUpdated([mainUri])
105+
106+
let updatedDiags = try await ws.testClient.nextDiagnosticsNotification()
107+
// No more errors expected, import should resolve since we the generated header file
108+
// now has the proper contents.
109+
XCTAssertEqual(updatedDiags.diagnostics.count, 0)
110+
}
111+
}

0 commit comments

Comments
 (0)