Skip to content

Commit 78a4fab

Browse files
authored
Merge pull request #896 from ahoppen/ahoppen/simplify-tests
Simplify tests
2 parents f0ee6b1 + 6679652 commit 78a4fab

33 files changed

+1750
-2046
lines changed

Sources/LSPTestSupport/Assertions.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import XCTest
1414

15-
/// Same as `assertNoThrow` but executes the trailing closure.
15+
/// Same as `XCTAssertNoThrow` but executes the trailing closure.
1616
public func assertNoThrow<T>(
1717
_ expression: () throws -> T,
1818
_ message: @autoclosure () -> String = "",
@@ -22,6 +22,20 @@ public func assertNoThrow<T>(
2222
XCTAssertNoThrow(try expression(), message(), file: file, line: line)
2323
}
2424

25+
/// Same as `assertNoThrow` but allows the closure to be `async`.
26+
public func assertNoThrow<T>(
27+
_ expression: () async throws -> T,
28+
_ message: @autoclosure () -> String = "",
29+
file: StaticString = #filePath,
30+
line: UInt = #line
31+
) async {
32+
do {
33+
_ = try await expression()
34+
} catch {
35+
XCTFail("Expression was not expected to throw but threw \(error)", file: file, line: line)
36+
}
37+
}
38+
2539
/// Same as `XCTAssertThrows` but executes the trailing closure.
2640
public func assertThrowsError<T>(
2741
_ expression: @autoclosure () async throws -> T,
@@ -77,6 +91,17 @@ public func assertNotNil<T: Equatable>(
7791
XCTAssertNotNil(expression, message(), file: file, line: line)
7892
}
7993

94+
/// Same as `XCTUnwrap` but doesn't take autoclosures and thus `expression`
95+
/// can contain `await`.
96+
public func unwrap<T>(
97+
_ expression: T?,
98+
_ message: @autoclosure () -> String = "",
99+
file: StaticString = #filePath,
100+
line: UInt = #line
101+
) throws -> T {
102+
return try XCTUnwrap(expression, file: file, line: line)
103+
}
104+
80105
extension XCTestCase {
81106
private struct ExpectationNotFulfilledError: Error, CustomStringConvertible {
82107
var expecatations: [XCTestExpectation]

Sources/LSPTestSupport/TestJSONRPCConnection.swift

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,6 @@ public final class TestClient: MessageHandler {
112112
})
113113
}
114114

115-
public func handleNextNotification<N>(_ handler: @escaping (Notification<N>) -> Void) {
116-
guard oneShotNotificationHandlers.isEmpty else {
117-
XCTFail("unexpected one shot notification handler registered")
118-
return
119-
}
120-
appendOneShotNotificationHandler(handler)
121-
}
122-
123-
public func handleNextRequest<R>(_ handler: @escaping (Request<R>) -> Void) {
124-
precondition(oneShotRequestHandlers.isEmpty)
125-
appendOneShotRequestHandler(handler)
126-
}
127-
128115
public func handle<N>(_ params: N, from clientID: ObjectIdentifier) where N: NotificationType {
129116
let notification = Notification(params, clientID: clientID)
130117

@@ -168,58 +155,6 @@ extension TestClient: Connection {
168155
) -> RequestID {
169156
return server.send(request, reply: reply)
170157
}
171-
172-
/// Send a notification and expect a notification in reply synchronously.
173-
/// For testing notifications that behave like requests - e.g. didChange & publishDiagnostics.
174-
public func sendNoteSync<NReply>(
175-
_ notification: some NotificationType,
176-
_ handler: @escaping (Notification<NReply>) -> Void
177-
) {
178-
179-
let expectation = XCTestExpectation(description: "sendNoteSync - note received")
180-
181-
handleNextNotification { (note: Notification<NReply>) in
182-
handler(note)
183-
expectation.fulfill()
184-
}
185-
186-
send(notification)
187-
188-
let result = XCTWaiter.wait(for: [expectation], timeout: defaultTimeout)
189-
guard result == .completed else {
190-
XCTFail("error \(result) waiting for notification in response to \(notification)")
191-
return
192-
}
193-
}
194-
195-
/// Send a notification and expect two notifications in reply synchronously.
196-
/// For testing notifications that behave like requests - e.g. didChange & publishDiagnostics.
197-
public func sendNoteSync<NSend, NReply1, NReply2>(
198-
_ notification: NSend,
199-
_ handler1: @escaping (Notification<NReply1>) -> Void,
200-
_ handler2: @escaping (Notification<NReply2>) -> Void
201-
) where NSend: NotificationType {
202-
203-
let expectation = XCTestExpectation(description: "sendNoteSync - note received")
204-
expectation.expectedFulfillmentCount = 2
205-
206-
handleNextNotification { (note: Notification<NReply1>) in
207-
handler1(note)
208-
expectation.fulfill()
209-
}
210-
appendOneShotNotificationHandler { (note: Notification<NReply2>) in
211-
handler2(note)
212-
expectation.fulfill()
213-
}
214-
215-
send(notification)
216-
217-
let result = XCTWaiter.wait(for: [expectation], timeout: defaultTimeout)
218-
guard result == .completed else {
219-
XCTFail("wait for notification in response to \(notification) failed with \(result)")
220-
return
221-
}
222-
}
223158
}
224159

225160
public final class TestServer: MessageHandler {

Sources/SKTestSupport/SKSwiftPMTestWorkspace.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ public final class SKSwiftPMTestWorkspace {
5353
public let toolchain: Toolchain
5454

5555
/// Connection to the language server.
56-
public let testServer: TestSourceKitServer
57-
58-
public var sk: TestClient { testServer.client }
56+
public let testClient: TestSourceKitLSPClient
5957

6058
/// When `testServer` is not `nil`, the workspace will be opened in that server, otherwise a new server will be created for the workspace
61-
public init(projectDir: URL, tmpDir: URL, toolchain: Toolchain, testServer: TestSourceKitServer? = nil) async throws {
62-
self.testServer = testServer ?? TestSourceKitServer(connectionKind: .local)
59+
public init(
60+
projectDir: URL,
61+
tmpDir: URL,
62+
toolchain: Toolchain,
63+
testClient: TestSourceKitLSPClient? = nil
64+
) async throws {
65+
self.testClient = testClient ?? TestSourceKitLSPClient()
6366

6467
self.projectDir = URL(
6568
fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: projectDir.path)).pathString
@@ -105,7 +108,7 @@ public final class SKSwiftPMTestWorkspace {
105108
listenToUnitEvents: false
106109
)
107110

108-
let server = self.testServer.server!
111+
let server = self.testClient.server
109112
let workspace = await Workspace(
110113
documentManager: DocumentManager(),
111114
rootUri: DocumentURI(sources.rootDirectory),
@@ -153,7 +156,7 @@ extension SKSwiftPMTestWorkspace {
153156

154157
extension SKSwiftPMTestWorkspace {
155158
public func openDocument(_ url: URL, language: Language) throws {
156-
sk.send(
159+
testClient.send(
157160
DidOpenTextDocumentNotification(
158161
textDocument: TextDocumentItem(
159162
uri: DocumentURI(url),
@@ -166,15 +169,15 @@ extension SKSwiftPMTestWorkspace {
166169
}
167170

168171
public func closeDocument(_ url: URL) {
169-
sk.send(DidCloseTextDocumentNotification(textDocument: TextDocumentIdentifier(DocumentURI(url))))
172+
testClient.send(DidCloseTextDocumentNotification(textDocument: TextDocumentIdentifier(DocumentURI(url))))
170173
}
171174
}
172175

173176
extension XCTestCase {
174177

175178
public func staticSourceKitSwiftPMWorkspace(
176179
name: String,
177-
server: TestSourceKitServer? = nil
180+
testClient: TestSourceKitLSPClient? = nil
178181
) async throws -> SKSwiftPMTestWorkspace? {
179182
let testDirName = testDirectoryName
180183
let toolchain = ToolchainRegistry.shared.default!
@@ -183,7 +186,7 @@ extension XCTestCase {
183186
tmpDir: URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
184187
.appendingPathComponent("sk-test-data/\(testDirName)/\(name)", isDirectory: true),
185188
toolchain: toolchain,
186-
testServer: server
189+
testClient: testClient
187190
)
188191

189192
let hasClangFile: Bool = workspace.sources.locations.contains { _, loc in

Sources/SKTestSupport/SKTibsTestWorkspace.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ fileprivate extension SourceKitServer {
3636
public final class SKTibsTestWorkspace {
3737

3838
public let tibsWorkspace: TibsTestWorkspace
39-
public let testServer: TestSourceKitServer
39+
public let testClient: TestSourceKitLSPClient
4040

4141
public var index: IndexStoreDB { tibsWorkspace.index }
4242
public var builder: TibsBuilder { tibsWorkspace.builder }
4343
public var sources: TestSources { tibsWorkspace.sources }
44-
public var sk: TestClient { testServer.client }
4544

4645
public init(
4746
immutableProjectDir: URL,
@@ -50,9 +49,9 @@ public final class SKTibsTestWorkspace {
5049
removeTmpDir: Bool,
5150
toolchain: Toolchain,
5251
clientCapabilities: ClientCapabilities,
53-
testServer: TestSourceKitServer? = nil
52+
testClient: TestSourceKitLSPClient? = nil
5453
) async throws {
55-
self.testServer = testServer ?? TestSourceKitServer(connectionKind: .local)
54+
self.testClient = testClient ?? TestSourceKitLSPClient()
5655
self.tibsWorkspace = try TibsTestWorkspace(
5756
immutableProjectDir: immutableProjectDir,
5857
persistentBuildDir: persistentBuildDir,
@@ -69,9 +68,9 @@ public final class SKTibsTestWorkspace {
6968
tmpDir: URL,
7069
toolchain: Toolchain,
7170
clientCapabilities: ClientCapabilities,
72-
testServer: TestSourceKitServer? = nil
71+
testClient: TestSourceKitLSPClient? = nil
7372
) async throws {
74-
self.testServer = testServer ?? TestSourceKitServer(connectionKind: .local)
73+
self.testClient = testClient ?? TestSourceKitLSPClient()
7574

7675
self.tibsWorkspace = try TibsTestWorkspace(
7776
projectDir: projectDir,
@@ -99,8 +98,8 @@ public final class SKTibsTestWorkspace {
9998
indexDelegate: indexDelegate
10099
)
101100

102-
await workspace.buildSystemManager.setDelegate(testServer.server!)
103-
await testServer.server!.setWorkspaces([workspace])
101+
await workspace.buildSystemManager.setDelegate(testClient.server)
102+
await testClient.server.setWorkspaces([workspace])
104103
}
105104
}
106105

@@ -123,7 +122,7 @@ extension SKTibsTestWorkspace {
123122

124123
extension SKTibsTestWorkspace {
125124
public func openDocument(_ url: URL, language: Language) throws {
126-
sk.send(
125+
testClient.send(
127126
DidOpenTextDocumentNotification(
128127
textDocument: TextDocumentItem(
129128
uri: DocumentURI(url),
@@ -143,7 +142,7 @@ extension XCTestCase {
143142
clientCapabilities: ClientCapabilities = .init(),
144143
tmpDir: URL? = nil,
145144
removeTmpDir: Bool = true,
146-
server: TestSourceKitServer? = nil
145+
testClient: TestSourceKitLSPClient? = nil
147146
) async throws -> SKTibsTestWorkspace? {
148147
let testDirName = testDirectoryName
149148
let workspace = try await SKTibsTestWorkspace(
@@ -157,7 +156,7 @@ extension XCTestCase {
157156
removeTmpDir: removeTmpDir,
158157
toolchain: ToolchainRegistry.shared.default!,
159158
clientCapabilities: clientCapabilities,
160-
testServer: server
159+
testClient: testClient
161160
)
162161

163162
if workspace.builder.targets.contains(where: { target in !target.clangTUs.isEmpty })

0 commit comments

Comments
 (0)