Skip to content

Commit b2a0aee

Browse files
committed
Add symbol name unit tests
1 parent 9b13986 commit b2a0aee

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed
+326
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
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+
func testOperatorFunctionName() {
73+
assertSymbolName(ofFunction: "func == (one: Int, two: Int)", expecting: "==(one:two:)")
74+
}
75+
76+
// MARK: - Initializer
77+
78+
func testInitializerWithoutParameters() {
79+
assertSymbolName(ofInitializer: "init() {}", expecting: "init()")
80+
}
81+
82+
func testInitializerWithSingleNamedParameter() {
83+
assertSymbolName(ofInitializer: "init(one: Int) {}", expecting: "init(one:)")
84+
}
85+
86+
func testInitializerWithSingleUnnamedParameter() {
87+
assertSymbolName(ofInitializer: "init(_: Int) {}", expecting: "init(_:)")
88+
}
89+
90+
func testInitializerWithExternalParameterName() {
91+
assertSymbolName(ofInitializer: "init(one two: Int) {}", expecting: "init(one:)")
92+
}
93+
94+
func testInitializerWithExplicitExternalParameterName() {
95+
assertSymbolName(ofInitializer: "init(one _: Int) {}", expecting: "init(one:)")
96+
}
97+
98+
func testInitializerWithImplicitExternalParameterName() {
99+
assertSymbolName(ofInitializer: "init(_ two: Int) {}", expecting: "init(_:)")
100+
}
101+
102+
func testInitializerWithOnlyAnonymousParameters() {
103+
assertSymbolName(ofInitializer: "init(_ _: Int) {}", expecting: "init(_:)")
104+
}
105+
106+
func testInitializerWithMultipleNamedParameters() {
107+
assertSymbolName(ofInitializer: "init(one two: Int, three: Int) {}", expecting: "init(one:three:)")
108+
}
109+
110+
func testInitializerWithAllParametersHavingExternalNames() {
111+
assertSymbolName(ofInitializer: "init(one two: Int, three four: Int) {}", expecting: "init(one:three:)")
112+
}
113+
114+
func testInitializerWithMixedNamedAndAnonymousParameters() {
115+
assertSymbolName(ofInitializer: "init(one two: Int, _ four: Int) {}", expecting: "init(one:_:)")
116+
}
117+
118+
func testInitializerWithAllAnonymousParameters() {
119+
assertSymbolName(ofInitializer: "init(_ two: Int, _ four: Int) {}", expecting: "init(_:_:)")
120+
}
121+
122+
func testInitializerWithNameInBackticks() {
123+
assertSymbolName(ofInitializer: "init(`one`: Int) {}", expecting: "init(`one`:)")
124+
}
125+
126+
// MARK: - Subscript
127+
128+
func testSubscriptNameWithoutParameters() {
129+
assertSymbolName(ofSubscript: "subscript() -> Int { 0 }", expecting: "subscript()")
130+
}
131+
132+
func testSubscriptNameWithSingleNamedParameter() {
133+
assertSymbolName(ofSubscript: "subscript(index: Int) -> Int { 0 }", expecting: "subscript(_:)")
134+
}
135+
136+
func testSubscriptNameWithSingleUnnamedParameter() {
137+
assertSymbolName(ofSubscript: "subscript(_: Int) -> Int { 0 }", expecting: "subscript(_:)")
138+
}
139+
140+
func testSubscriptNameWithExternalParameterName() {
141+
assertSymbolName(ofSubscript: "subscript(index i: Int) -> Int { 0 }", expecting: "subscript(index:)")
142+
}
143+
144+
func testSubscriptNameWithExplicitExternalParameterName() {
145+
assertSymbolName(ofSubscript: "subscript(index _: Int) -> Int { 0 }", expecting: "subscript(index:)")
146+
}
147+
148+
func testSubscriptNameWithImplicitExternalParameterName() {
149+
assertSymbolName(ofSubscript: "subscript(_ i: Int) -> Int { 0 }", expecting: "subscript(_:)")
150+
}
151+
152+
func testSubscriptNameWithOnlyAnonymousParameters() {
153+
assertSymbolName(ofSubscript: "subscript(_ _: Int) -> Int { 0 }", expecting: "subscript(_:)")
154+
}
155+
156+
func testSubscriptNameWithMultipleNamedParameters() {
157+
assertSymbolName(ofSubscript: "subscript(x: Int, y: Int) -> Int { 0 }", expecting: "subscript(_:_:)")
158+
}
159+
160+
func testSubscriptNameWithMultipleParametersAndExternalNames() {
161+
assertSymbolName(
162+
ofSubscript: "subscript(indexX x: Int, indexY y: Int) -> Int { 0 }",
163+
expecting: "subscript(indexX:indexY:)"
164+
)
165+
}
166+
167+
func testSubscriptNameWithParameterNameInBackticks() {
168+
assertSymbolName(ofSubscript: "subscript(`index` i: Int) -> Int { 0 }", expecting: "subscript(`index`:)")
169+
}
170+
171+
// MARK: - Enum Case Element
172+
173+
func testEnumCaseElementWithoutAssociatedValues() {
174+
assertSymbolName(ofEnumCase: "case a", expecting: "a")
175+
}
176+
177+
func testEnumCaseElementWithSingleNamedAssociatedValue() {
178+
assertSymbolName(ofEnumCase: "case b(c: Int)", expecting: "b(c:)")
179+
}
180+
181+
func testEnumCaseElementWithSingleUnnamedAssociatedValue() {
182+
assertSymbolName(ofEnumCase: "case d(Int)", expecting: "d(_:)")
183+
}
184+
185+
func testEnumCaseElementWithSingleUnnamedAssociatedValueWithUnderscore() {
186+
assertSymbolName(ofEnumCase: "case e(_: Int)", expecting: "e(_:)")
187+
}
188+
189+
func testEnumCaseElementWithMultipleNamedAssociatedValues() {
190+
assertSymbolName(ofEnumCase: "case f(g: Int, h: Int)", expecting: "f(g:h:)")
191+
}
192+
193+
func testEnumCaseElementNameWithBackticks() {
194+
assertSymbolName(ofEnumCase: "case `i`", expecting: "`i`")
195+
}
196+
197+
func testEnumCaseElementWithAssociatedValueNameInBackticks() {
198+
assertSymbolName(ofEnumCase: "case j(`k`: Int)", expecting: "j(`k`:)")
199+
}
200+
201+
// MARK: - Macro
202+
203+
func testMacroDeclWithoutParameters() {
204+
assertSymbolName(ofMacroDecl: "macro myMacro()", expecting: "myMacro()")
205+
}
206+
207+
func testMacroDeclWithSingleNamedParameter() {
208+
assertSymbolName(ofMacroDecl: "macro myMacro(x: Int)", expecting: "myMacro(x:)")
209+
}
210+
211+
func testMacroDeclWithSingleUnnamedParameter() {
212+
assertSymbolName(ofMacroDecl: "macro myMacro(_: Int)", expecting: "myMacro(_:)")
213+
}
214+
215+
func testMacroDeclWithExternalParameterName() {
216+
assertSymbolName(ofMacroDecl: "macro myMacro(external internal: Int)", expecting: "myMacro(external:)")
217+
}
218+
219+
func testMacroDeclWithExplicitExternalParameterName() {
220+
assertSymbolName(ofMacroDecl: "macro myMacro(external _: Int)", expecting: "myMacro(external:)")
221+
}
222+
223+
func testMacroDeclWithImplicitExternalParameterName() {
224+
assertSymbolName(ofMacroDecl: "macro myMacro(_ internal: Int)", expecting: "myMacro(_:)")
225+
}
226+
227+
func testMacroDeclWithMultipleNamedParameters() {
228+
assertSymbolName(ofMacroDecl: "macro myMacro(x: Int, y: String)", expecting: "myMacro(x:y:)")
229+
}
230+
231+
func testMacroDeclWithAllParametersHavingExternalNames() {
232+
assertSymbolName(
233+
ofMacroDecl: "macro myMacro(external1 internal1: Int, external2 internal2: String)",
234+
expecting: "myMacro(external1:external2:)"
235+
)
236+
}
237+
238+
func testMacroDeclWithMixedNamedAndUnnamedParameters() {
239+
assertSymbolName(ofMacroDecl: "macro myMacro(x: Int, _: String)", expecting: "myMacro(x:_:)")
240+
}
241+
242+
func testMacroDeclWithNameInBackticks() {
243+
assertSymbolName(ofMacroDecl: "macro `myMacro`()", expecting: "`myMacro`()")
244+
}
245+
246+
func testMacroDeclWithParameterNameInBackticks() {
247+
assertSymbolName(ofMacroDecl: "macro myMacro(`x`: Int)", expecting: "myMacro(`x`:)")
248+
}
249+
250+
func testMacroDeclWithExternalParameterNameInBackticks() {
251+
assertSymbolName(ofMacroDecl: "macro myMacro(`external` internal: Int)", expecting: "myMacro(`external`:)")
252+
}
253+
}
254+
255+
// MARK: - Assertions
256+
257+
private func assertSymbolName(
258+
ofFunction function: DeclSyntax,
259+
expecting expectedSymbolName: String,
260+
file: StaticString = #filePath,
261+
line: UInt = #line
262+
) {
263+
guard let functionDecl = function.as(FunctionDeclSyntax.self) else {
264+
XCTFail("Expected function declaration not found.", file: file, line: line)
265+
return
266+
}
267+
268+
XCTAssertEqual(functionDecl.symbolName, expectedSymbolName, file: file, line: line)
269+
}
270+
271+
private func assertSymbolName(
272+
ofInitializer initializer: DeclSyntax,
273+
expecting expectedSymbolName: String,
274+
file: StaticString = #filePath,
275+
line: UInt = #line
276+
) {
277+
guard let initializerDecl = initializer.as(InitializerDeclSyntax.self) else {
278+
XCTFail("Expected initializer declaration not found.", file: file, line: line)
279+
return
280+
}
281+
282+
XCTAssertEqual(initializerDecl.symbolName, expectedSymbolName, file: file, line: line)
283+
}
284+
285+
private func assertSymbolName(
286+
ofSubscript subscriptDeclaration: DeclSyntax,
287+
expecting expectedSymbolName: String,
288+
file: StaticString = #filePath,
289+
line: UInt = #line
290+
) {
291+
guard let subscriptDecl = subscriptDeclaration.as(SubscriptDeclSyntax.self) else {
292+
XCTFail("Expected subscript declaration not found.", file: file, line: line)
293+
return
294+
}
295+
296+
XCTAssertEqual(subscriptDecl.symbolName, expectedSymbolName, file: file, line: line)
297+
}
298+
299+
private func assertSymbolName(
300+
ofEnumCase enumCaseDeclaration: DeclSyntax,
301+
expecting expectedSymbolName: String,
302+
file: StaticString = #filePath,
303+
line: UInt = #line
304+
) {
305+
guard let enumCaseDecl = enumCaseDeclaration.as(EnumCaseDeclSyntax.self),
306+
let enumCaseElement = enumCaseDecl.elements.first
307+
else {
308+
XCTFail("Expected enum case element declaration not found.", file: file, line: line)
309+
return
310+
}
311+
XCTAssertEqual(enumCaseElement.symbolName, expectedSymbolName, file: file, line: line)
312+
}
313+
314+
private func assertSymbolName(
315+
ofMacroDecl macroDecl: DeclSyntax,
316+
expecting expectedSymbolName: String,
317+
file: StaticString = #filePath,
318+
line: UInt = #line
319+
) {
320+
guard let macroDeclSyntax = macroDecl.as(MacroDeclSyntax.self) else {
321+
XCTFail("Expected macro declaration not found.", file: file, line: line)
322+
return
323+
}
324+
325+
XCTAssertEqual(macroDeclSyntax.symbolName, expectedSymbolName, file: file, line: line)
326+
}

0 commit comments

Comments
 (0)