-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 SwiftSyntax | ||
import XCTest | ||
|
||
final class SymbolNameTests: XCTestCase { | ||
|
||
// MARK: - Functions | ||
|
||
func testFunctionNameWithoutParameters() { | ||
assertSymbolName(ofFunction: "func name() {}", expecting: "name()") | ||
} | ||
|
||
func testFunctionNameWithSingleNamedParameter() { | ||
assertSymbolName(ofFunction: "func name(one: Int) {}", expecting: "name(one:)") | ||
} | ||
|
||
func testFunctionNameWithSingleUnnamedParameter() { | ||
assertSymbolName(ofFunction: "func name(_: Int) {}", expecting: "name(_:)") | ||
} | ||
|
||
func testFunctionNameWithExternalParameterName() { | ||
assertSymbolName(ofFunction: "func name(one two: Int) {}", expecting: "name(one:)") | ||
} | ||
|
||
func testFunctionNameWithExplicitExternalParameterName() { | ||
assertSymbolName(ofFunction: "func name(one _: Int) {}", expecting: "name(one:)") | ||
} | ||
|
||
func testFunctionNameWithImplicitExternalParameterName() { | ||
assertSymbolName(ofFunction: "func name(_ two: Int) {}", expecting: "name(_:)") | ||
} | ||
|
||
func testFunctionNameWithOnlyAnonymousParameters() { | ||
assertSymbolName(ofFunction: "func name(_ _: Int) {}", expecting: "name(_:)") | ||
} | ||
|
||
func testFunctionNameWithMultipleNamedParameters() { | ||
assertSymbolName(ofFunction: "func name(one two: Int, three: Int) {}", expecting: "name(one:three:)") | ||
} | ||
|
||
func testFunctionNameWithAllParametersHavingExternalNames() { | ||
assertSymbolName(ofFunction: "func name(one two: Int, three four: Int) {}", expecting: "name(one:three:)") | ||
} | ||
|
||
func testFunctionNameWithMixedNamedAndAnonymousParameters() { | ||
assertSymbolName(ofFunction: "func name(one two: Int, _ four: Int) {}", expecting: "name(one:_:)") | ||
} | ||
|
||
func testFunctionNameWithAllAnonymousParameters() { | ||
assertSymbolName(ofFunction: "func name(_ two: Int, _ four: Int) {}", expecting: "name(_:_:)") | ||
} | ||
|
||
func testFunctionNameWithBackticks() { | ||
assertSymbolName(ofFunction: "func `name`() {}", expecting: "name()") | ||
} | ||
|
||
func testFunctionNameWithParameterNameInBackticks() { | ||
assertSymbolName(ofFunction: "func name(`one`: Int) {}", expecting: "name(one:)") | ||
} | ||
|
||
// FIXME: Does this make sense? | ||
func testOperatorFunctionName() { | ||
assertSymbolName(ofFunction: "func == (one: Int, two: Int)", expecting: "==(one:two:)") | ||
} | ||
|
||
private func assertSymbolName( | ||
ofFunction function: DeclSyntax, | ||
expecting expectedSymbolName: String, | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
guard let functionDecl = function.as(FunctionDeclSyntax.self) else { | ||
XCTFail("Expected function declaration not found.", file: file, line: line) | ||
return | ||
} | ||
|
||
XCTAssertEqual(functionDecl.symbolName, expectedSymbolName, file: file, line: line) | ||
} | ||
|
||
// MARK: - Initializer | ||
|
||
func testInitializerWithoutParameters() { | ||
assertSymbolName(ofInitializer: "init() {}", expecting: "init()") | ||
} | ||
|
||
func testInitializerWithSingleNamedParameter() { | ||
assertSymbolName(ofInitializer: "init(one: Int) {}", expecting: "init(one:)") | ||
} | ||
|
||
func testInitializerWithSingleUnnamedParameter() { | ||
assertSymbolName(ofInitializer: "init(_: Int) {}", expecting: "init(_:)") | ||
} | ||
|
||
func testInitializerWithExternalParameterName() { | ||
assertSymbolName(ofInitializer: "init(one two: Int) {}", expecting: "init(one:)") | ||
} | ||
|
||
func testInitializerWithExplicitExternalParameterName() { | ||
assertSymbolName(ofInitializer: "init(one _: Int) {}", expecting: "init(one:)") | ||
} | ||
|
||
func testInitializerWithImplicitExternalParameterName() { | ||
assertSymbolName(ofInitializer: "init(_ two: Int) {}", expecting: "init(_:)") | ||
} | ||
|
||
func testInitializerWithOnlyAnonymousParameters() { | ||
assertSymbolName(ofInitializer: "init(_ _: Int) {}", expecting: "init(_:)") | ||
} | ||
|
||
func testInitializerWithMultipleNamedParameters() { | ||
assertSymbolName(ofInitializer: "init(one two: Int, three: Int) {}", expecting: "init(one:three:)") | ||
} | ||
|
||
func testInitializerWithAllParametersHavingExternalNames() { | ||
assertSymbolName(ofInitializer: "init(one two: Int, three four: Int) {}", expecting: "init(one:three:)") | ||
} | ||
|
||
func testInitializerWithMixedNamedAndAnonymousParameters() { | ||
assertSymbolName(ofInitializer: "init(one two: Int, _ four: Int) {}", expecting: "init(one:_:)") | ||
} | ||
|
||
func testInitializerWithAllAnonymousParameters() { | ||
assertSymbolName(ofInitializer: "init(_ two: Int, _ four: Int) {}", expecting: "init(_:_:)") | ||
} | ||
|
||
func testInitializerWithNameInBackticks() { | ||
assertSymbolName(ofInitializer: "init(`one`: Int) {}", expecting: "init(one:)") | ||
} | ||
|
||
private func assertSymbolName( | ||
ofInitializer initializer: DeclSyntax, | ||
expecting expectedSymbolName: String, | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
guard let initializerDecl = initializer.as(InitializerDeclSyntax.self) else { | ||
XCTFail("Expected initializer declaration not found.", file: file, line: line) | ||
return | ||
} | ||
|
||
XCTAssertEqual(initializerDecl.symbolName, expectedSymbolName, file: file, line: line) | ||
} | ||
|
||
// MARK: - Subscript | ||
|
||
func testSubscriptNameWithoutParameters() { | ||
assertSymbolName(ofSubscript: "subscript() -> Int { 0 }", expecting: "subscript()") | ||
} | ||
|
||
func testSubscriptNameWithSingleNamedParameter() { | ||
assertSymbolName(ofSubscript: "subscript(index: Int) -> Int { 0 }", expecting: "subscript(_:)") | ||
} | ||
|
||
func testSubscriptNameWithSingleUnnamedParameter() { | ||
assertSymbolName(ofSubscript: "subscript(_: Int) -> Int { 0 }", expecting: "subscript(_:)") | ||
} | ||
|
||
func testSubscriptNameWithExternalParameterName() { | ||
assertSymbolName(ofSubscript: "subscript(index i: Int) -> Int { 0 }", expecting: "subscript(index:)") | ||
} | ||
|
||
func testSubscriptNameWithExplicitExternalParameterName() { | ||
assertSymbolName(ofSubscript: "subscript(index _: Int) -> Int { 0 }", expecting: "subscript(index:)") | ||
} | ||
|
||
func testSubscriptNameWithImplicitExternalParameterName() { | ||
assertSymbolName(ofSubscript: "subscript(_ i: Int) -> Int { 0 }", expecting: "subscript(_:)") | ||
} | ||
|
||
func testSubscriptNameWithOnlyAnonymousParameters() { | ||
assertSymbolName(ofSubscript: "subscript(_ _: Int) -> Int { 0 }", expecting: "subscript(_:)") | ||
} | ||
|
||
func testSubscriptNameWithMultipleNamedParameters() { | ||
assertSymbolName(ofSubscript: "subscript(x: Int, y: Int) -> Int { 0 }", expecting: "subscript(_:_:)") | ||
} | ||
|
||
func testSubscriptNameWithMultipleParametersAndExternalNames() { | ||
assertSymbolName(ofSubscript: "subscript(indexX x: Int, indexY y: Int) -> Int { 0 }", expecting: "subscript(indexX:indexY:)") | ||
} | ||
|
||
func testSubscriptNameWithParameterNameInBackticks() { | ||
assertSymbolName(ofSubscript: "subscript(`index` i: Int) -> Int { 0 }", expecting: "subscript(index:)") | ||
} | ||
|
||
private func assertSymbolName( | ||
ofSubscript subscriptDeclaration: DeclSyntax, | ||
expecting expectedSymbolName: String, | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
guard let subscriptDecl = subscriptDeclaration.as(SubscriptDeclSyntax.self) else { | ||
XCTFail("Expected subscript declaration not found.", file: file, line: line) | ||
return | ||
} | ||
|
||
XCTAssertEqual(subscriptDecl.symbolName, expectedSymbolName, file: file, line: line) | ||
} | ||
} |