Robert J. Clegg
Robert J. Clegg

Reputation: 7360

Testing Swift's Macros

I'm trying out Swift Macros and have watched both WWDC 23 sessions on the topic:

During my testing of my own macro I found the the diagnostics has two identical elements during testing - however the Xcode compiler only shows one error - example:

I have a this as a test implementation - which obviously will always throw - but just once.

public struct URLMacro: ExpressionMacro {
  public static func expansion(of node: some FreestandingMacroExpansionSyntax, 
                                 in context: some MacroExpansionContext) throws -> ExprSyntax {
            throw URLMacroError.invaldURL
    }
}

Testing out the macro in the client:

let url = #URL("")

Produces a compiler error (As expected) - #URL The input URL is invalid

However, the following test fails:

    func testInvalidURL() {
        assertMacroExpansion(
            #"""
            #URL("")
            """#,
            expandedSource:
                #"""
                #URL("")
                """#,
            diagnostics: [
                DiagnosticSpec(message: "The input URL is invalid", line: 1, column: 1)
            ],
            macros: testMacros)
    }

With the following:

failed - Expected 1 diagnostics but received 2:
1:1: #URL The input URL is invalid
1:1: #URL The input URL is invalid

Using Xcode 15 - Beta 6

To get the test to pass, I have to have two identical DiagnosticSpec elements in diagnostics Is this a bug or am I doing something wrong here?

Upvotes: 1

Views: 640

Answers (0)

Related Questions