Skip to content

Commit 49d100b

Browse files
authored
@test applied to function with newline before its name causes error in expanded code (#520)
This fixes an issue where certain trivia, such as a newline (`\n`) placed in between the `func` keyword and a function's name on a function with the `@Test` attribute causes an error in the macro expansion code. For example: ```swift @test func nameOnSecondLine() { ... } ``` ### Modifications: Trim trivia from function name in macro expansion code. ### Result: These examples now build without errors. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated. Resolves rdar://130797660
1 parent 0e2d9cb commit 49d100b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Sources/TestingMacros/TestDeclarationMacro.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
299299
thunkBody = ""
300300
} else if let typeName {
301301
if functionDecl.isStaticOrClass {
302-
thunkBody = "_ = \(forwardCall("\(typeName).\(functionDecl.name)\(forwardedParamsExpr)"))"
302+
thunkBody = "_ = \(forwardCall("\(typeName).\(functionDecl.name.trimmed)\(forwardedParamsExpr)"))"
303303
} else {
304304
let instanceName = context.makeUniqueName(thunking: functionDecl)
305305
let varOrLet = functionDecl.isMutating ? "var" : "let"
306306
thunkBody = """
307307
\(raw: varOrLet) \(raw: instanceName) = \(forwardInit("\(typeName)()"))
308-
_ = \(forwardCall("\(raw: instanceName).\(functionDecl.name)\(forwardedParamsExpr)"))
308+
_ = \(forwardCall("\(raw: instanceName).\(functionDecl.name.trimmed)\(forwardedParamsExpr)"))
309309
"""
310310

311311
// If there could be an Objective-C selector associated with this test,
@@ -327,7 +327,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
327327
}
328328
}
329329
} else {
330-
thunkBody = "_ = \(forwardCall("\(functionDecl.name)\(forwardedParamsExpr)"))"
330+
thunkBody = "_ = \(forwardCall("\(functionDecl.name.trimmed)\(forwardedParamsExpr)"))"
331331
}
332332

333333
// If this function is synchronous and is not explicitly isolated to the

Tests/TestingTests/MiscellaneousTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ struct TestsWithAsyncArguments {
206206
)
207207
func parameterizedTestWithTrailingComment(value: Int) {}
208208

209+
@Test(.hidden) func // Meaningful trivia: intentional newline before name
210+
globalMultiLineTestDecl() async {}
211+
212+
@Suite(.hidden)
213+
struct MultiLineSuite {
214+
@Test(.hidden) func // Meaningful trivia: intentional newline before name
215+
multiLineTestDecl() async {}
216+
217+
@Test(.hidden) static func // Meaningful trivia: intentional newline before name
218+
staticMultiLineTestDecl() async {}
219+
}
220+
209221
@Suite("Miscellaneous tests")
210222
struct MiscellaneousTests {
211223
@Test("Free function's name")

0 commit comments

Comments
 (0)