Skip to content

Update macro template to support Swift Testing tests #8890 #8897

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MaelRB
Copy link
Contributor

@MaelRB MaelRB commented Jul 5, 2025

Add support for macro project using Swift Testing in InitPackage.

Motivation:

Since the introduction of SwiftSyntaxMacrosGenericTestSupport, Swift Testing should be supported to test macro

Modifications:

  • Update writeMacroTestsFile to support Swift Testing
  • Use MacroSpec for both XCTest and Swift Testing
  • Remove swift-testing dependency from the Package manifest when building macro template for Swift Testing
  • Remove unsupportedTestingLibraryForPackageType error as it's unused
  • Add unit tests for macro template generation

Result:

Generated test file when using Swift Testing:

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacroExpansion
import Testing
import SwiftSyntaxMacrosGenericTestSupport

// Macro implementations build for the host, so the corresponding module is not available when cross-compiling. Cross-compiled tests may still make use of the macro itself in end-to-end tests.
#if canImport(fooMacros)
import fooMacros

let testMacros: [String: MacroSpec] = [
    "stringify": MacroSpec(type: StringifyMacro.self),
]
#endif

struct fooTests {
    
    @Test
    func macro() {
#if canImport(fooMacros)
        assertMacroExpansion(
            """
            #stringify(a + b)
            """,
            expandedSource: """
            (a + b, "a + b")
            """,
            macroSpecs: testMacros
        ) {
            Issue.record(
                "\($0.message)",
                sourceLocation:
                    SourceLocation(
                        fileID: $0.location.fileID,
                        filePath: $0.location.filePath,
                        line: $0.location.line,
                        column: $0.location.column
                    )
            )
        }
#else
        Issue.record("macros are only supported when running tests for the host platform")
#endif
    }
    
    @Test
    func macroWithStringLiteral() {
#if canImport(fooMacros)
        assertMacroExpansion(
            #"""
            #stringify("Hello, \(name)")
            """#,
            expandedSource: #"""
            ("Hello, \(name)", #""Hello, \(name)""#)
            """#,
            macroSpecs: testMacros
        ) {
            Issue.record(
                "\($0.message)",
                sourceLocation:
                    SourceLocation(
                        fileID: $0.location.fileID,
                        filePath: $0.location.filePath,
                        line: $0.location.line,
                        column: $0.location.column
                    )
            )
        }
#else
        Issue.record("macros are only supported when running tests for the host platform")
#endif
    }
}

Fixes: #8890

Add support for macro project using Swift Testing.
@bkhouri
Copy link
Contributor

bkhouri commented Jul 5, 2025

This resolves #8890

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update macro template to support Swift Testing tests
2 participants