Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adopt Swift-Testing in test utils such as SwiftSyntaxMacrosTestSupport #2720

Open
MahdiBM opened this issue Jul 8, 2024 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@MahdiBM
Copy link
Contributor

MahdiBM commented Jul 8, 2024

Description

Currently you need to use XCTest to run macro tests with SwiftSyntaxMacrosTestSupport functions because they rely on some XCTest functions such as XCTFail.

swift-syntax tests utils should also support Swift-Testing.

@MahdiBM MahdiBM added the enhancement New feature or request label Jul 8, 2024
@MahdiBM
Copy link
Contributor Author

MahdiBM commented Jul 8, 2024

Not that hard to do, even manually.
This works like the currently-provided XCTest-related functions.

import SwiftSyntax
import SwiftSyntaxMacrosGenericTestSupport
import SwiftSyntaxMacros
import SwiftSyntaxMacroExpansion
import Testing

func assertMacroExpansionWithSwiftTesting(
    _ originalSource: String,
    expandedSource expectedExpandedSource: String,
    diagnostics: [DiagnosticSpec] = [],
    macros: [String: any Macro.Type],
    applyFixIts: [String]? = nil,
    fixedSource expectedFixedSource: String? = nil,
    testModuleName: String = "TestModule",
    testFileName: String = "test.swift",
    indentationWidth: Trivia = .spaces(4),
    sourceLocation: Testing.SourceLocation = Testing.SourceLocation()
) {
    let macroSpecs = macros.mapValues { MacroSpec(type: $0) }
    SwiftSyntaxMacrosGenericTestSupport.assertMacroExpansion(
        originalSource,
        expandedSource: expectedExpandedSource,
        diagnostics: diagnostics,
        macroSpecs: macroSpecs,
        applyFixIts: applyFixIts,
        fixedSource: expectedFixedSource,
        testModuleName: testModuleName,
        testFileName: testFileName,
        indentationWidth: indentationWidth,
        failureHandler: {
            #expect(Bool(false), .init(stringLiteral: $0.message), sourceLocation: sourceLocation)
        },
        fileID: "",  // Not used in the failure handler
        filePath: "", /// MahdiBM comment: requires StaticString so just set it to "" for now.
        line: UInt(sourceLocation.line),
        column: 0  // Not used in the failure handler
    )
}

[Update]: added some more imports to the code.

@ahoppen
Copy link
Member

ahoppen commented Jul 8, 2024

Synced to Apple’s issue tracker as rdar://131312113

@MahdiBM MahdiBM changed the title Adapt Swift-Testing in test utils such as SwiftSyntaxMacrosTestSupport Adopt Swift-Testing in test utils such as SwiftSyntaxMacrosTestSupport Jul 8, 2024
@grynspan
Copy link
Contributor

grynspan commented Oct 4, 2024

FWIW rather than #expect(Bool(false), ...), write Issue.record(...) :)

kevinrpb added a commit to kevinrpb/Defaults that referenced this issue Nov 19, 2024
swift-testing is not supported for macro expansion tests yet.
See swiftlang/swift-syntax#2720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants