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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
//===----------------------------------------------------------------------===//
//
// 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 SourceKitLSP
import XCTest
final class InlayHintTests: XCTestCase {
// MARK: - Helpers
func performInlayHintRequest(text: String, range: Range<Position>? = nil) async throws -> [InlayHint] {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(text, uri: uri)
let request = InlayHintRequest(textDocument: TextDocumentIdentifier(uri), range: range)
return try await testClient.send(request)
}
private func makeInlayHint(
position: Position,
kind: InlayHintKind,
label: String,
hasEdit: Bool = true
) -> InlayHint {
let textEdits: [TextEdit]?
if hasEdit {
textEdits = [TextEdit(range: position..<position, newText: label)]
} else {
textEdits = nil
}
return InlayHint(
position: position,
label: .string(label),
kind: kind,
textEdits: textEdits
)
}
// MARK: - Tests
func testEmpty() async throws {
let text = ""
let hints = try await performInlayHintRequest(text: text)
XCTAssertEqual(hints, [])
}
func testBindings() async throws {
let text = """
let x = 4
var y = "test" + "123"
"""
let hints = try await performInlayHintRequest(text: text)
XCTAssertEqual(
hints,
[
makeInlayHint(
position: Position(line: 0, utf16index: 5),
kind: .type,
label: ": Int"
),
makeInlayHint(
position: Position(line: 1, utf16index: 5),
kind: .type,
label: ": String"
),
]
)
}
func testRanged() async throws {
let text = """
func square(_ x: Double) -> Double {
let result = x * x
return result
}
func collatz(_ n: Int) -> Int {
let even = n % 2 == 0
let result = even ? (n / 2) : (3 * n + 1)
return result
}
"""
let range = Position(line: 6, utf16index: 0)..<Position(line: 9, utf16index: 0)
let hints = try await performInlayHintRequest(text: text, range: range)
XCTAssertEqual(
hints,
[
makeInlayHint(
position: Position(line: 6, utf16index: 10),
kind: .type,
label: ": Bool"
),
makeInlayHint(
position: Position(line: 7, utf16index: 12),
kind: .type,
label: ": Int"
),
]
)
}
func testFields() async throws {
let text = """
class X {
let instanceMember = 3
static let staticMember = "abc"
}
struct Y {
var instanceMember = "def" + "ghi"
static let staticMember = 1 + 2
}
enum Z {
static let staticMember = 3.0
}
"""
let hints = try await performInlayHintRequest(text: text)
XCTAssertEqual(
hints,
[
makeInlayHint(
position: Position(line: 1, utf16index: 20),
kind: .type,
label: ": Int"
),
makeInlayHint(
position: Position(line: 2, utf16index: 25),
kind: .type,
label: ": String"
),
makeInlayHint(
position: Position(line: 6, utf16index: 20),
kind: .type,
label: ": String"
),
makeInlayHint(
position: Position(line: 7, utf16index: 25),
kind: .type,
label: ": Int"
),
makeInlayHint(
position: Position(line: 11, utf16index: 25),
kind: .type,
label: ": Double"
),
]
)
}
func testExplicitTypeAnnotation() async throws {
let text = """
let x: String = "abc"
struct X {
var y: Int = 34
}
"""
let hints = try await performInlayHintRequest(text: text)
XCTAssertEqual(hints, [])
}
func testClosureParams() async throws {
let text = """
func f(x: Int) {}
let g = { (x: Int) in }
let h: (String) -> String = { x in x }
let i: (Double, Double) -> Double = { (x, y) in
x + y
}
"""
let hints = try await performInlayHintRequest(text: text)
XCTAssertEqual(
hints,
[
makeInlayHint(
position: Position(line: 2, utf16index: 5),
kind: .type,
label: ": (Int) -> ()"
),
makeInlayHint(
position: Position(line: 3, utf16index: 31),
kind: .type,
label: ": String",
hasEdit: false
),
makeInlayHint(
position: Position(line: 4, utf16index: 40),
kind: .type,
label: ": Double"
),
makeInlayHint(
position: Position(line: 4, utf16index: 43),
kind: .type,
label: ": Double"
),
]
)
}
}
|