-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update macro template to support Swift Testing tests #8890 #8897
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,11 +116,6 @@ public final class InitPackage { | |
installedSwiftPMConfiguration: InstalledSwiftPMConfiguration, | ||
fileSystem: FileSystem | ||
) throws { | ||
if options.packageType == .macro && options.supportedTestingLibraries.contains(.swiftTesting) { | ||
// FIXME: https://github.com/swiftlang/swift-syntax/issues/2400 | ||
throw InitError.unsupportedTestingLibraryForPackageType(.swiftTesting, .macro) | ||
} | ||
|
||
self.options = options | ||
self.pkgname = name | ||
self.moduleName = name.spm_mangledToC99ExtendedIdentifier() | ||
|
@@ -346,7 +341,6 @@ public final class InitPackage { | |
dependencies: [ | ||
"\(pkgname)Macros", | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), | ||
.product(name: "Testing", package: "swift-testing"), | ||
] | ||
), | ||
""" | ||
|
@@ -726,15 +720,22 @@ public final class InitPackage { | |
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import SwiftSyntaxMacroExpansion | ||
|
||
"""## | ||
|
||
if options.supportedTestingLibraries.contains(.swiftTesting) { | ||
content += "import Testing\n" | ||
} | ||
if options.supportedTestingLibraries.contains(.xctest) { | ||
content += "import XCTest\n" | ||
content += ##""" | ||
import Testing | ||
import SwiftSyntaxMacrosGenericTestSupport | ||
|
||
"""## | ||
} else if options.supportedTestingLibraries.contains(.xctest) { | ||
content += ##""" | ||
import XCTest | ||
import SwiftSyntaxMacrosTestSupport | ||
|
||
"""## | ||
} | ||
|
||
content += ##""" | ||
|
@@ -743,8 +744,8 @@ public final class InitPackage { | |
#if canImport(\##(moduleName)Macros) | ||
import \##(moduleName)Macros | ||
|
||
let testMacros: [String: Macro.Type] = [ | ||
"stringify": StringifyMacro.self, | ||
let testMacros: [String: MacroSpec] = [ | ||
"stringify": MacroSpec(type: StringifyMacro.self), | ||
] | ||
#endif | ||
|
||
|
@@ -755,10 +756,68 @@ public final class InitPackage { | |
// for it *and* Testing if it is enabled. | ||
|
||
if options.supportedTestingLibraries.contains(.swiftTesting) { | ||
// FIXME: https://github.com/swiftlang/swift-syntax/issues/2400 | ||
} | ||
|
||
if options.supportedTestingLibraries.contains(.xctest) { | ||
content += ##""" | ||
struct \##(moduleName)Tests { | ||
|
||
@Test | ||
func macro() { | ||
#if canImport(\##(moduleName)Macros) | ||
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 | ||
) | ||
) | ||
Comment on lines
+774
to
+783
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: Could this be refactored to an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extension Issue {
@discardableResult static func record(_ failure: TestFailureSpec) -> Self {
Self.record(
Comment(rawValue: failure.message),
sourceLocation: SourceLocation(
fileID: failure.location.fileID,
filePath: failure.location.filePath,
line: failure.location.line,
column: failure.location.column
)
)
}
} cc @grynspan Was there anything blocking us from shipping that extension directly in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also choose to factor this to a free function: func failureHander(_ failure: TestFailureSpec) {
Issue.record(
Comment(rawValue: failure.message),
sourceLocation:
SourceLocation(
fileID: failure.location.fileID,
filePath: failure.location.filePath,
line: failure.location.line,
column: failure.location.column
)
)
} And then pass that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swift Testing cannot directly link to swift-syntax since it doesn't exist in the toolchain at runtime. That means we cannot export any symbols that reference types from swift-syntax (except in our macro target which is only loaded/run at compile time.) |
||
} | ||
#else | ||
Issue.record("macros are only supported when running tests for the host platform") | ||
#endif | ||
} | ||
|
||
@Test | ||
func macroWithStringLiteral() { | ||
#if canImport(\##(moduleName)Macros) | ||
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 | ||
} | ||
} | ||
|
||
"""## | ||
} else if options.supportedTestingLibraries.contains(.xctest) { | ||
content += ##""" | ||
final class \##(moduleName)Tests: XCTestCase { | ||
func testMacro() throws { | ||
|
@@ -770,7 +829,7 @@ public final class InitPackage { | |
expandedSource: """ | ||
(a + b, "a + b") | ||
""", | ||
macros: testMacros | ||
macroSpecs: testMacros | ||
) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
|
@@ -786,7 +845,7 @@ public final class InitPackage { | |
expandedSource: #""" | ||
("Hello, \(name)", #""Hello, \(name)""#) | ||
"""#, | ||
macros: testMacros | ||
macroSpecs: testMacros | ||
) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
|
@@ -887,16 +946,13 @@ public final class InitPackage { | |
|
||
private enum InitError: Swift.Error { | ||
case manifestAlreadyExists | ||
case unsupportedTestingLibraryForPackageType(_ testingLibrary: TestingLibrary, _ packageType: InitPackage.PackageType) | ||
} | ||
|
||
extension InitError: CustomStringConvertible { | ||
var description: String { | ||
switch self { | ||
case .manifestAlreadyExists: | ||
return "a manifest file already exists in this directory" | ||
case let .unsupportedTestingLibraryForPackageType(library, packageType): | ||
return "\(library) cannot be used when initializing a \(packageType) package" | ||
} | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should also importSwiftSyntaxMacrosGenericTestSupport
?Ahh… nevermind… I see it below!