|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 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 SwiftSyntax |
| 14 | +import XCTest |
| 15 | + |
| 16 | +final class SymbolNameTests: XCTestCase { |
| 17 | + |
| 18 | + // MARK: - Functions |
| 19 | + |
| 20 | + func testFunctionNameWithoutParameters() { |
| 21 | + assertSymbolName(ofFunction: "func name() {}", expecting: "name()") |
| 22 | + } |
| 23 | + |
| 24 | + func testFunctionNameWithSingleNamedParameter() { |
| 25 | + assertSymbolName(ofFunction: "func name(one: Int) {}", expecting: "name(one:)") |
| 26 | + } |
| 27 | + |
| 28 | + func testFunctionNameWithSingleUnnamedParameter() { |
| 29 | + assertSymbolName(ofFunction: "func name(_: Int) {}", expecting: "name(_:)") |
| 30 | + } |
| 31 | + |
| 32 | + func testFunctionNameWithExternalParameterName() { |
| 33 | + assertSymbolName(ofFunction: "func name(one two: Int) {}", expecting: "name(one:)") |
| 34 | + } |
| 35 | + |
| 36 | + func testFunctionNameWithExplicitExternalParameterName() { |
| 37 | + assertSymbolName(ofFunction: "func name(one _: Int) {}", expecting: "name(one:)") |
| 38 | + } |
| 39 | + |
| 40 | + func testFunctionNameWithImplicitExternalParameterName() { |
| 41 | + assertSymbolName(ofFunction: "func name(_ two: Int) {}", expecting: "name(_:)") |
| 42 | + } |
| 43 | + |
| 44 | + func testFunctionNameWithOnlyAnonymousParameters() { |
| 45 | + assertSymbolName(ofFunction: "func name(_ _: Int) {}", expecting: "name(_:)") |
| 46 | + } |
| 47 | + |
| 48 | + func testFunctionNameWithMultipleNamedParameters() { |
| 49 | + assertSymbolName(ofFunction: "func name(one two: Int, three: Int) {}", expecting: "name(one:three:)") |
| 50 | + } |
| 51 | + |
| 52 | + func testFunctionNameWithAllParametersHavingExternalNames() { |
| 53 | + assertSymbolName(ofFunction: "func name(one two: Int, three four: Int) {}", expecting: "name(one:three:)") |
| 54 | + } |
| 55 | + |
| 56 | + func testFunctionNameWithMixedNamedAndAnonymousParameters() { |
| 57 | + assertSymbolName(ofFunction: "func name(one two: Int, _ four: Int) {}", expecting: "name(one:_:)") |
| 58 | + } |
| 59 | + |
| 60 | + func testFunctionNameWithAllAnonymousParameters() { |
| 61 | + assertSymbolName(ofFunction: "func name(_ two: Int, _ four: Int) {}", expecting: "name(_:_:)") |
| 62 | + } |
| 63 | + |
| 64 | + func testFunctionNameWithBackticks() { |
| 65 | + assertSymbolName(ofFunction: "func `name`() {}", expecting: "name()") |
| 66 | + } |
| 67 | + |
| 68 | + func testFunctionNameWithParameterNameInBackticks() { |
| 69 | + assertSymbolName(ofFunction: "func name(`one`: Int) {}", expecting: "name(one:)") |
| 70 | + } |
| 71 | + |
| 72 | + // FIXME: Does this make sense? |
| 73 | + func testOperatorFunctionName() { |
| 74 | + assertSymbolName(ofFunction: "func == (one: Int, two: Int)", expecting: "==(one:two:)") |
| 75 | + } |
| 76 | + |
| 77 | + private func assertSymbolName( |
| 78 | + ofFunction function: DeclSyntax, |
| 79 | + expecting expectedSymbolName: String, |
| 80 | + file: StaticString = #filePath, |
| 81 | + line: UInt = #line |
| 82 | + ) { |
| 83 | + guard let functionDecl = function.as(FunctionDeclSyntax.self) else { |
| 84 | + XCTFail("Expected function declaration not found.", file: file, line: line) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + XCTAssertEqual(functionDecl.symbolName, expectedSymbolName, file: file, line: line) |
| 89 | + } |
| 90 | + |
| 91 | + // MARK: - Initializer |
| 92 | + |
| 93 | + func testInitializerWithoutParameters() { |
| 94 | + assertSymbolName(ofInitializer: "init() {}", expecting: "init()") |
| 95 | + } |
| 96 | + |
| 97 | + func testInitializerWithSingleNamedParameter() { |
| 98 | + assertSymbolName(ofInitializer: "init(one: Int) {}", expecting: "init(one:)") |
| 99 | + } |
| 100 | + |
| 101 | + func testInitializerWithSingleUnnamedParameter() { |
| 102 | + assertSymbolName(ofInitializer: "init(_: Int) {}", expecting: "init(_:)") |
| 103 | + } |
| 104 | + |
| 105 | + func testInitializerWithExternalParameterName() { |
| 106 | + assertSymbolName(ofInitializer: "init(one two: Int) {}", expecting: "init(one:)") |
| 107 | + } |
| 108 | + |
| 109 | + func testInitializerWithExplicitExternalParameterName() { |
| 110 | + assertSymbolName(ofInitializer: "init(one _: Int) {}", expecting: "init(one:)") |
| 111 | + } |
| 112 | + |
| 113 | + func testInitializerWithImplicitExternalParameterName() { |
| 114 | + assertSymbolName(ofInitializer: "init(_ two: Int) {}", expecting: "init(_:)") |
| 115 | + } |
| 116 | + |
| 117 | + func testInitializerWithOnlyAnonymousParameters() { |
| 118 | + assertSymbolName(ofInitializer: "init(_ _: Int) {}", expecting: "init(_:)") |
| 119 | + } |
| 120 | + |
| 121 | + func testInitializerWithMultipleNamedParameters() { |
| 122 | + assertSymbolName(ofInitializer: "init(one two: Int, three: Int) {}", expecting: "init(one:three:)") |
| 123 | + } |
| 124 | + |
| 125 | + func testInitializerWithAllParametersHavingExternalNames() { |
| 126 | + assertSymbolName(ofInitializer: "init(one two: Int, three four: Int) {}", expecting: "init(one:three:)") |
| 127 | + } |
| 128 | + |
| 129 | + func testInitializerWithMixedNamedAndAnonymousParameters() { |
| 130 | + assertSymbolName(ofInitializer: "init(one two: Int, _ four: Int) {}", expecting: "init(one:_:)") |
| 131 | + } |
| 132 | + |
| 133 | + func testInitializerWithAllAnonymousParameters() { |
| 134 | + assertSymbolName(ofInitializer: "init(_ two: Int, _ four: Int) {}", expecting: "init(_:_:)") |
| 135 | + } |
| 136 | + |
| 137 | + func testInitializerWithNameInBackticks() { |
| 138 | + assertSymbolName(ofInitializer: "init(`one`: Int) {}", expecting: "init(one:)") |
| 139 | + } |
| 140 | + |
| 141 | + private func assertSymbolName( |
| 142 | + ofInitializer initializer: DeclSyntax, |
| 143 | + expecting expectedSymbolName: String, |
| 144 | + file: StaticString = #filePath, |
| 145 | + line: UInt = #line |
| 146 | + ) { |
| 147 | + guard let initializerDecl = initializer.as(InitializerDeclSyntax.self) else { |
| 148 | + XCTFail("Expected initializer declaration not found.", file: file, line: line) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + XCTAssertEqual(initializerDecl.symbolName, expectedSymbolName, file: file, line: line) |
| 153 | + } |
| 154 | + |
| 155 | + // MARK: - Subscript |
| 156 | + |
| 157 | + func testSubscriptNameWithoutParameters() { |
| 158 | + assertSymbolName(ofSubscript: "subscript() -> Int { 0 }", expecting: "subscript()") |
| 159 | + } |
| 160 | + |
| 161 | + func testSubscriptNameWithSingleNamedParameter() { |
| 162 | + assertSymbolName(ofSubscript: "subscript(index: Int) -> Int { 0 }", expecting: "subscript(_:)") |
| 163 | + } |
| 164 | + |
| 165 | + func testSubscriptNameWithSingleUnnamedParameter() { |
| 166 | + assertSymbolName(ofSubscript: "subscript(_: Int) -> Int { 0 }", expecting: "subscript(_:)") |
| 167 | + } |
| 168 | + |
| 169 | + func testSubscriptNameWithExternalParameterName() { |
| 170 | + assertSymbolName(ofSubscript: "subscript(index i: Int) -> Int { 0 }", expecting: "subscript(index:)") |
| 171 | + } |
| 172 | + |
| 173 | + func testSubscriptNameWithExplicitExternalParameterName() { |
| 174 | + assertSymbolName(ofSubscript: "subscript(index _: Int) -> Int { 0 }", expecting: "subscript(index:)") |
| 175 | + } |
| 176 | + |
| 177 | + func testSubscriptNameWithImplicitExternalParameterName() { |
| 178 | + assertSymbolName(ofSubscript: "subscript(_ i: Int) -> Int { 0 }", expecting: "subscript(_:)") |
| 179 | + } |
| 180 | + |
| 181 | + func testSubscriptNameWithOnlyAnonymousParameters() { |
| 182 | + assertSymbolName(ofSubscript: "subscript(_ _: Int) -> Int { 0 }", expecting: "subscript(_:)") |
| 183 | + } |
| 184 | + |
| 185 | + func testSubscriptNameWithMultipleNamedParameters() { |
| 186 | + assertSymbolName(ofSubscript: "subscript(x: Int, y: Int) -> Int { 0 }", expecting: "subscript(_:_:)") |
| 187 | + } |
| 188 | + |
| 189 | + func testSubscriptNameWithMultipleParametersAndExternalNames() { |
| 190 | + assertSymbolName(ofSubscript: "subscript(indexX x: Int, indexY y: Int) -> Int { 0 }", expecting: "subscript(indexX:indexY:)") |
| 191 | + } |
| 192 | + |
| 193 | + func testSubscriptNameWithParameterNameInBackticks() { |
| 194 | + assertSymbolName(ofSubscript: "subscript(`index` i: Int) -> Int { 0 }", expecting: "subscript(index:)") |
| 195 | + } |
| 196 | + |
| 197 | + private func assertSymbolName( |
| 198 | + ofSubscript subscriptDeclaration: DeclSyntax, |
| 199 | + expecting expectedSymbolName: String, |
| 200 | + file: StaticString = #filePath, |
| 201 | + line: UInt = #line |
| 202 | + ) { |
| 203 | + guard let subscriptDecl = subscriptDeclaration.as(SubscriptDeclSyntax.self) else { |
| 204 | + XCTFail("Expected subscript declaration not found.", file: file, line: line) |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + XCTAssertEqual(subscriptDecl.symbolName, expectedSymbolName, file: file, line: line) |
| 209 | + } |
| 210 | + |
| 211 | + // MARK: - Enum Case Element |
| 212 | + |
| 213 | + func testEnumCaseElementWithoutAssociatedValues() { |
| 214 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case a", expecting: "a") |
| 215 | + } |
| 216 | + |
| 217 | + func testEnumCaseElementWithSingleNamedAssociatedValue() { |
| 218 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case b(c: Int)", expecting: "b(c:)") |
| 219 | + } |
| 220 | + |
| 221 | + func testEnumCaseElementWithSingleUnnamedAssociatedValue() { |
| 222 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case d(Int)", expecting: "d(_:)") |
| 223 | + } |
| 224 | + |
| 225 | + func testEnumCaseElementWithSingleUnnamedAssociatedValueWithUnderscore() { |
| 226 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case e(_: Int)", expecting: "e(_:)") |
| 227 | + } |
| 228 | + |
| 229 | + func testEnumCaseElementWithMultipleNamedAssociatedValues() { |
| 230 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case f(g: Int, h: Int)", expecting: "f(g:h:)") |
| 231 | + } |
| 232 | + |
| 233 | + func testEnumCaseElementNameWithBackticks() { |
| 234 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case `i`", expecting: "i") |
| 235 | + } |
| 236 | + |
| 237 | + func testEnumCaseElementWithAssociatedValueNameInBackticks() { |
| 238 | + assertSymbolName(ofEnumCaseElementFromEnumCase: "case j(`k`: Int)", expecting: "j(k:)") |
| 239 | + } |
| 240 | + |
| 241 | + private func assertSymbolName( |
| 242 | + ofEnumCaseElementFromEnumCase enumCaseDeclaration: DeclSyntax, |
| 243 | + expecting expectedSymbolName: String, |
| 244 | + file: StaticString = #filePath, |
| 245 | + line: UInt = #line |
| 246 | + ) { |
| 247 | + guard let enumCaseDecl = enumCaseDeclaration.as(EnumCaseDeclSyntax.self), |
| 248 | + let enumCaseElement = enumCaseDecl.elements.first else { |
| 249 | + XCTFail("Expected enum case element declaration not found.", file: file, line: line) |
| 250 | + return |
| 251 | + } |
| 252 | + |
| 253 | + XCTAssertEqual(enumCaseElement.symbolName, expectedSymbolName, file: file, line: line) |
| 254 | + } |
| 255 | +} |
0 commit comments