Skip to content

Commit 8b8eb6b

Browse files
authored
Merge pull request swiftlang#43 from benlangmuir/test-lsp-layer
[lsp] Fill in some test coverage gaps
2 parents bb84e7b + 7a64c89 commit 8b8eb6b

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

Sources/LanguageServerProtocol/Diagnostic.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
/// The serverity level of a Diagnostic, between hint and error.
1314
public enum DiagnosticSeverity: Int, Codable, Hashable {
1415
case error = 1
1516
case warning = 2
1617
case information = 3
1718
case hint = 4
1819
}
1920

21+
/// A unique diagnostic code, which may be used identifier the diagnostic in e.g. documentation.
2022
public enum DiagnosticCode: Hashable {
2123
case number(Int)
2224
case string(String)
2325
}
2426

27+
/// A diagnostic message such a compiler error or warning.
2528
public struct Diagnostic: Codable, Hashable {
2629

2730
/// The primary position/range of the diagnostic.
@@ -30,7 +33,8 @@ public struct Diagnostic: Codable, Hashable {
3033
/// Whether this is a warning, error, etc.
3134
public var severity: DiagnosticSeverity?
3235

33-
/// The "code" of the diagnostice, which might be a number or string, typically providing a unique way to reference the diagnostic in e.g. documentation.
36+
/// The "code" of the diagnostice, which might be a number or string, typically providing a unique
37+
/// way to reference the diagnostic in e.g. documentation.
3438
public var code: DiagnosticCode?
3539

3640
/// A human-readable description of the source of this diagnostic, e.g. "sourcekitd"
@@ -42,7 +46,14 @@ public struct Diagnostic: Codable, Hashable {
4246
/// Related diagnostic notes.
4347
public var relatedInformation: [DiagnosticRelatedInformation]?
4448

45-
public init(range: Range<Position>, severity: DiagnosticSeverity?, code: DiagnosticCode? = nil, source: String?, message: String, relatedInformation: [DiagnosticRelatedInformation]? = nil) {
49+
public init(
50+
range: Range<Position>,
51+
severity: DiagnosticSeverity?,
52+
code: DiagnosticCode? = nil,
53+
source: String?,
54+
message: String,
55+
relatedInformation: [DiagnosticRelatedInformation]? = nil)
56+
{
4657
self.range = PositionRange(range)
4758
self.severity = severity
4859
self.code = code

Sources/LanguageServerProtocol/Error.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ public struct MessageDecodingError: Error, Hashable {
9595
self.id = id
9696
self.messageKind = messageKind
9797
}
98-
99-
public init(code: ErrorCode, message: String, requestID: RequestID? = nil, responseID: RequestID? = nil) {
100-
self.code = code
101-
self.message = message
102-
self.id = requestID ?? responseID
103-
104-
switch (requestID, responseID) {
105-
case (nil, nil): self.messageKind = .unknown
106-
case (nil, _): self.messageKind = .response
107-
case (_, nil): self.messageKind = .request
108-
case (_, _): preconditionFailure("cannot be both a request and response")
109-
}
110-
}
11198
}
11299

113100
extension MessageDecodingError {

Sources/LanguageServerProtocol/Language.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
/// A source code language identifier, such as "swift", or "objective-c".
1314
public enum Language: String, Codable, Hashable {
1415
case c
1516
case cpp // C++, not C preprocessor
@@ -29,6 +30,7 @@ public enum Language: String, Codable, Hashable {
2930
}
3031
}
3132

33+
/// Clang-compatible language name suitable for use with `-x <language>`.
3234
public var xflag: String? {
3335
switch self {
3436
case .swift: return "swift"
@@ -40,6 +42,7 @@ public enum Language: String, Codable, Hashable {
4042
}
4143
}
4244

45+
/// Clang-compatible language name for a header file. See `xflag`.
4346
public var xflagHeader: String? {
4447
return xflag.map { "\($0)-header" }
4548
}

Tests/LanguageServerProtocolTests/CodingTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ final class CodingTests: XCTestCase {
196196

197197
checkCoding(RequestID.number(100), json: "100")
198198
checkCoding(RequestID.string("100"), json: "\"100\"")
199+
200+
checkCoding(Language.c, json: "\"c\"")
201+
checkCoding(Language.cpp, json: "\"cpp\"")
202+
checkCoding(Language.objective_c, json: "\"objective-c\"")
203+
checkCoding(Language.objective_cpp, json: "\"objective-cpp\"")
204+
checkCoding(Language.swift, json: "\"swift\"")
205+
checkCoding(Language.unknown, json: "\"unknown\"")
206+
checkDecoding(json: "\"asdfAF\"", expected: Language.unknown)
207+
208+
checkCoding(DiagnosticCode.number(123), json: "123")
209+
checkCoding(DiagnosticCode.string("hi"), json: "\"hi\"")
199210
}
200211
}
201212

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
@testable import LanguageServerProtocol
14+
15+
import XCTest
16+
17+
final class LanguageServerProtocolTests: XCTestCase {
18+
19+
func testLanguageXFlag() {
20+
XCTAssertEqual(Language.c.xflag, "c")
21+
XCTAssertEqual(Language.c.xflagHeader, "c-header")
22+
XCTAssertEqual(Language.cpp.xflag, "c++")
23+
XCTAssertEqual(Language.cpp.xflagHeader, "c++-header")
24+
XCTAssertEqual(Language.objective_c.xflag, "objective-c")
25+
XCTAssertEqual(Language.objective_c.xflagHeader, "objective-c-header")
26+
XCTAssertEqual(Language.objective_cpp.xflag, "objective-c++")
27+
XCTAssertEqual(Language.objective_cpp.xflagHeader, "objective-c++-header")
28+
}
29+
}

Tests/LanguageServerProtocolTests/XCTestManifests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@ extension ConnectionTests {
2121
]
2222
}
2323

24+
extension LanguageServerProtocolTests {
25+
// DO NOT MODIFY: This is autogenerated, use:
26+
// `swift test --generate-linuxmain`
27+
// to regenerate.
28+
static let __allTests__LanguageServerProtocolTests = [
29+
("testLanguageXFlag", testLanguageXFlag),
30+
]
31+
}
32+
2433
public func __allTests() -> [XCTestCaseEntry] {
2534
return [
2635
testCase(CodingTests.__allTests__CodingTests),
2736
testCase(ConnectionTests.__allTests__ConnectionTests),
37+
testCase(LanguageServerProtocolTests.__allTests__LanguageServerProtocolTests),
2838
]
2939
}
3040
#endif

0 commit comments

Comments
 (0)