swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

Modify `assertMacroExpansion()` so that it only conditionally calls `XCTFail()`.

Open grynspan opened this issue 1 year ago • 1 comments

This PR changes the signature of assertMacroExpansion() such that an alternative failure handler can be passed for use when e.g. swift-testing is being used.

Right now, the function calls XCTAssertEqual(), XCTFail(), etc. on the assumption that the caller is using XCTest. This assumption does not always hold and the dependency on XCTest may prevent a developer from writing effective tests leveraging assertMacroExpansion() while also using swift-testing or other testing libraries.

It is not possible for swift-syntax to include swift-testing as a dependency at this time, so the function cannot be amended to directly call the equivalent of XCTFail() from that library. Nor can the function check if the current process is running an XCTest-based test because XCTest does not expose API to determine if a test is running.

Until such time as swift-syntax can directly call into swift-testing, developers can do so when calling assertMacroExpansion() like so:

assertMacroExpansion(
  "@foo bar",
  "expanded_foo bar",
  macros: ...,
  ...
) { message, fileID, filePath, line, column in
  Issue.record(Comment(rawValue: message), fileID: fileID, filePath: String(describing: filePath), Int(line), Int(column))
}

An improved interface is a future direction.

Resolves #2400.

grynspan avatar Dec 13 '23 19:12 grynspan

@swift-ci please test

grynspan avatar Dec 13 '23 19:12 grynspan

I don't think we're pursuing the right solution here.

grynspan avatar Apr 30 '24 14:04 grynspan