From 2ddc19b47756fe10d8cef43c5d29f61113614e8f Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 16:35:05 +0000 Subject: [PATCH 1/6] Record issues if we're in a Swift testing environment --- .../SwiftSyntaxMacrosTestSupport/Assertions.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index ffcb4d7e6a6..8d7d0b46983 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -17,6 +17,9 @@ public import SwiftSyntaxMacroExpansion public import SwiftSyntaxMacros @_spi(XCTestFailureLocation) public import SwiftSyntaxMacrosGenericTestSupport private import XCTest +#if canImport(Testing) +import Testing +#endif #else import SwiftIfConfig import SwiftSyntax @@ -110,8 +113,10 @@ public func assertMacroExpansion( testFileName: String = "test.swift", indentationWidth: Trivia = .spaces(4), buildConfiguration: (any BuildConfiguration)? = nil, + fileID: StaticString = #fileID, file: StaticString = #filePath, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { SwiftSyntaxMacrosGenericTestSupport.assertMacroExpansion( originalSource, @@ -125,7 +130,15 @@ public func assertMacroExpansion( indentationWidth: indentationWidth, buildConfiguration: buildConfiguration, failureHandler: { + #if canImport(Testing) + if Test.current != nil { + Issue.record(Comment(rawValue: $0.message), sourceLocation: .init(fileID: fileID.description, filePath: file.description, line: Int(line), column: Int(column))) + } else { + XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) + } + #else XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) + #endif }, fileID: "", // Not used in the failure handler filePath: file, From ba15ee0df079187488001e80308ce5da5538ff6c Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 16:45:29 +0000 Subject: [PATCH 2/6] Add support in other method --- Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index 8d7d0b46983..97e422bca74 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -64,8 +64,10 @@ public func assertMacroExpansion( testFileName: String = "test.swift", indentationWidth: Trivia = .spaces(4), buildConfiguration: (any BuildConfiguration)? = nil, + fileID: StaticString = #fileID, file: StaticString = #filePath, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { let specs = macros.mapValues { MacroSpec(type: $0) } assertMacroExpansion( @@ -79,8 +81,10 @@ public func assertMacroExpansion( testFileName: testFileName, indentationWidth: indentationWidth, buildConfiguration: buildConfiguration, + fileID: fileID, file: file, - line: line + line: line, + column: column ) } From dff217933295f68e5c41a01f5fd47462cb2cc769 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 17:01:24 +0000 Subject: [PATCH 3/6] Add tests --- .../AccessorMacroTests.swift | 2 +- .../SwiftTestingTests.swift | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift diff --git a/Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift index 3560cca6174..ebae5041784 100644 --- a/Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift +++ b/Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift @@ -25,7 +25,7 @@ import SwiftSyntaxMacros import SwiftSyntaxMacrosTestSupport import XCTest -private struct ConstantOneGetter: AccessorMacro { +struct ConstantOneGetter: AccessorMacro { static func expansion( of node: AttributeSyntax, providingAccessorsOf declaration: some DeclSyntaxProtocol, diff --git a/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift new file mode 100644 index 00000000000..223be4f2943 --- /dev/null +++ b/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift @@ -0,0 +1,49 @@ +#if canImport(Testing) +import Testing +import SwiftSyntaxMacrosTestSupport + +@Suite("Swift Testing Macro Expansion Tests") +struct SwiftTestingMacroExpansionTests { + @Test("Test Happy Path") + func testHappyPathWorks() { + assertMacroExpansion( + """ + @constantOne + var x: Int /*1*/ // hello + """, + expandedSource: """ + var x: Int { /*1*/ // hello + get { + return 1 + } + } + """, + macros: ["constantOne": ConstantOneGetter.self], + indentationWidth: .spaces(2) + ) + } + + @Test("Test Failure") + func failureReportedCorrectly() { + withKnownIssue { + assertMacroExpansion( + """ + @constantOne + var x: Int /*1*/ // hello + """, + expandedSource: """ + var x: Int { /*1*/ // hello + get { + return 1 + } + } + """, + macros: ["constantOne": ConstantOneGetter.self], + indentationWidth: .spaces(4) + ) + } matching: { issue in + issue.description.contains("Macro expansion did not produce the expected expanded source") + } + } +} +#endif From d7fd8ff6b898f41217d0ad4cbf7104384436e8d8 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 17:39:30 +0000 Subject: [PATCH 4/6] Private imports --- Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index 97e422bca74..e0c3184abfc 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -18,7 +18,7 @@ public import SwiftSyntaxMacros @_spi(XCTestFailureLocation) public import SwiftSyntaxMacrosGenericTestSupport private import XCTest #if canImport(Testing) -import Testing +private import Testing #endif #else import SwiftIfConfig From 8f5568645b1d5ec876268fed1854bd0378033d22 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 17:41:46 +0000 Subject: [PATCH 5/6] Fix some of the CI issues --- .../SwiftSyntaxMacrosTestSupport/Assertions.swift | 2 +- .../SwiftTestingTests.swift | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index e0c3184abfc..0e486e6fd1a 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -51,7 +51,7 @@ public typealias DiagnosticSpec = SwiftSyntaxMacrosGenericTestSupport.Diagnostic /// - indentationWidth: The indentation width used in the expansion. /// - buildConfiguration: a build configuration that will be made available /// to the macro implementation -/// - SeeAlso: ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:file:line:)`` +/// - SeeAlso: ``assertMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` /// to also specify the list of conformances passed to the macro expansion. public func assertMacroExpansion( _ originalSource: String, diff --git a/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift index 223be4f2943..6c048722bb8 100644 --- a/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift +++ b/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift @@ -1,3 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 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 +// +//===----------------------------------------------------------------------===// + #if canImport(Testing) import Testing import SwiftSyntaxMacrosTestSupport From be537f1b188e77b747d3305020a22d5517b2ad10 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 21 Nov 2025 17:46:44 +0000 Subject: [PATCH 6/6] Fix formatting --- Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index 0e486e6fd1a..6259332dcf4 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -136,7 +136,15 @@ public func assertMacroExpansion( failureHandler: { #if canImport(Testing) if Test.current != nil { - Issue.record(Comment(rawValue: $0.message), sourceLocation: .init(fileID: fileID.description, filePath: file.description, line: Int(line), column: Int(column))) + Issue.record( + Comment(rawValue: $0.message), + sourceLocation: .init( + fileID: fileID.description, + filePath: file.description, + line: Int(line), + column: Int(column) + ) + ) } else { XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) }