Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 97ea3f3

Browse files
committedApr 2, 2024
Add symbol name unit tests
1 parent 3035807 commit 97ea3f3

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
 
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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 testSubscriptWithoutParameters() {
158+
assertSymbolName(ofSubscript: "subscript() -> Int { 0 }", expecting: "subscript()")
159+
}
160+
161+
func testSubscriptWithSingleParameter() {
162+
assertSymbolName(ofSubscript: "subscript(index: Int) -> Int { 0 }", expecting: "subscript(_:)")
163+
}
164+
165+
func testSubscriptWithExternalParameterNames() {
166+
assertSymbolName(ofSubscript: "subscript(index i: Int) -> Int { 0 }", expecting: "subscript(index:)")
167+
}
168+
169+
func testSubscriptWithUnnamedParameter() {
170+
assertSymbolName(ofSubscript: "subscript(_ index: Int) -> Int { 0 }", expecting: "subscript(_:)")
171+
}
172+
173+
func testSubscriptWithUnnamedParameter_2() {
174+
assertSymbolName(ofSubscript: "subscript(_ i: Int) -> Int { 0 }", expecting: "subscript(_:)")
175+
}
176+
177+
178+
func testSubscriptWithOnlyAnonymousParameters() {
179+
assertSymbolName(ofSubscript: "subscript(_ _: Int) -> Int { 0 }", expecting: "subscript(_:)")
180+
}
181+
182+
func testSubscriptWithMultipleParameters() {
183+
assertSymbolName(ofSubscript: "subscript(x: Int, y: Int) -> Int { 0 }", expecting: "subscript(_:_:)")
184+
}
185+
186+
func testSubscriptWithMultipleParameters2() {
187+
assertSymbolName(ofSubscript: "subscript(indexX x: Int, indexY y: Int) -> Int { 0 }", expecting: "subscript(indexX:indexY:)")
188+
}
189+
190+
func testSubscriptWithParameterNameInBackticks() {
191+
assertSymbolName(ofSubscript: "subscript(`index` i: Int) -> Int { 0 }", expecting: "subscript(index:)")
192+
}
193+
194+
private func assertSymbolName(
195+
ofSubscript subscriptDeclaration: DeclSyntax,
196+
expecting expectedSymbolName: String,
197+
file: StaticString = #filePath,
198+
line: UInt = #line
199+
) {
200+
guard let subscriptDecl = subscriptDeclaration.as(SubscriptDeclSyntax.self) else {
201+
XCTFail("Expected subscript declaration not found.", file: file, line: line)
202+
return
203+
}
204+
205+
XCTAssertEqual(subscriptDecl.symbolName, expectedSymbolName, file: file, line: line)
206+
}
207+
}

0 commit comments

Comments
 (0)
Please sign in to comment.