Skip to content

Commit

Permalink
Add symbol name unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matejkob committed Apr 2, 2024
1 parent 3035807 commit 97ea3f3
Showing 1 changed file with 207 additions and 0 deletions.
207 changes: 207 additions & 0 deletions Tests/SwiftSyntaxTest/SymbolNameTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//===----------------------------------------------------------------------===//
//
// 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 testSubscriptWithoutParameters() {
assertSymbolName(ofSubscript: "subscript() -> Int { 0 }", expecting: "subscript()")
}

func testSubscriptWithSingleParameter() {
assertSymbolName(ofSubscript: "subscript(index: Int) -> Int { 0 }", expecting: "subscript(_:)")
}

func testSubscriptWithExternalParameterNames() {
assertSymbolName(ofSubscript: "subscript(index i: Int) -> Int { 0 }", expecting: "subscript(index:)")
}

func testSubscriptWithUnnamedParameter() {
assertSymbolName(ofSubscript: "subscript(_ index: Int) -> Int { 0 }", expecting: "subscript(_:)")
}

func testSubscriptWithUnnamedParameter_2() {
assertSymbolName(ofSubscript: "subscript(_ i: Int) -> Int { 0 }", expecting: "subscript(_:)")
}


func testSubscriptWithOnlyAnonymousParameters() {
assertSymbolName(ofSubscript: "subscript(_ _: Int) -> Int { 0 }", expecting: "subscript(_:)")
}

func testSubscriptWithMultipleParameters() {
assertSymbolName(ofSubscript: "subscript(x: Int, y: Int) -> Int { 0 }", expecting: "subscript(_:_:)")
}

func testSubscriptWithMultipleParameters2() {
assertSymbolName(ofSubscript: "subscript(indexX x: Int, indexY y: Int) -> Int { 0 }", expecting: "subscript(indexX:indexY:)")
}

func testSubscriptWithParameterNameInBackticks() {
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)
}
}

0 comments on commit 97ea3f3

Please sign in to comment.