Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)`).
Expand Down Expand Up @@ -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(
Expand All @@ -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)`).
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/SwiftTestingTests.swift
Original file line number Diff line number Diff line change
@@ -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
Loading