diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index ffcb4d7e6a6..6259332dcf4 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 @@ -48,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, @@ -61,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( @@ -76,8 +81,10 @@ public func assertMacroExpansion( testFileName: testFileName, indentationWidth: indentationWidth, buildConfiguration: buildConfiguration, + fileID: fileID, file: file, - line: line + line: line, + column: column ) } @@ -110,8 +117,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 +134,23 @@ 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, 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