diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index ffcb4d7e6a6..5d9d614273b 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) +private import Testing +#endif #else import SwiftIfConfig import SwiftSyntax @@ -34,6 +37,10 @@ public typealias DiagnosticSpec = SwiftSyntaxMacrosGenericTestSupport.Diagnostic /// Assert that expanding the given macros in the original source produces /// the given expanded source code. /// +/// - Warning: If you call this function inside a Swift Testing test case, +/// the test will report no assertion failures even if the test fails. +/// Use ``expectMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` instead. +/// /// - Parameters: /// - originalSource: The original source code, which is expected to contain /// macros in various places (e.g., `#stringify(x + y)`). @@ -61,8 +68,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( @@ -76,14 +85,20 @@ public func assertMacroExpansion( testFileName: testFileName, indentationWidth: indentationWidth, buildConfiguration: buildConfiguration, + fileID: fileID, file: file, - line: line + line: line, + column: column ) } /// Assert that expanding the given macros in the original source produces /// the given expanded source code. /// +/// - Warning: If you call this function inside a Swift Testing test case, +/// the test will report no assertion failures even if the test fails. +/// Use ``expectMacroExpansion(_:expandedSource:diagnostics:macroSpecs:applyFixIts:fixedSource:testModuleName:testFileName:indentationWidth:buildConfiguration:fileID:file:line:column:)`` instead. +/// /// - Parameters: /// - originalSource: The original source code, which is expected to contain /// macros in various places (e.g., `#stringify(x + y)`). @@ -110,8 +125,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, @@ -126,6 +143,17 @@ public func assertMacroExpansion( buildConfiguration: buildConfiguration, failureHandler: { XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine) + #if(canImport(Testing)) + Issue.record( + Comment(rawValue: $0.message), + sourceLocation: .init( + fileID: fileID.description, + filePath: file.description, + line: Int(line), + column: Int(column) + ) + ) + #endif }, fileID: "", // Not used in the failure handler filePath: file, 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..6c048722bb8 --- /dev/null +++ b/Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +@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