|
| 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 | +} |
0 commit comments