From 462b61855e09c581650578571f61070f9ebf355a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20B=C4=85k?= Date: Tue, 2 Apr 2024 09:49:17 +0200 Subject: [PATCH] Add symbol name unit tests --- Tests/SwiftSyntaxTest/SymbolNameTests.swift | 210 ++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Tests/SwiftSyntaxTest/SymbolNameTests.swift diff --git a/Tests/SwiftSyntaxTest/SymbolNameTests.swift b/Tests/SwiftSyntaxTest/SymbolNameTests.swift new file mode 100644 index 00000000000..9abedceae60 --- /dev/null +++ b/Tests/SwiftSyntaxTest/SymbolNameTests.swift @@ -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) + } +}