Skip to content

Use assertMacroExpansion in test cases of macro template #6485

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

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public final class InitPackage {
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "\(pkgname)",
targets: ["\(pkgname)"]),
targets: ["\(pkgname)"]
),
.executable(
name: "\(pkgname)Client",
targets: ["\(pkgname)Client"]
Expand All @@ -252,7 +253,7 @@ public final class InitPackage {
pkgParams.append("""
dependencies: [
// Depend on the latest Swift 5.9 prerelease of SwiftSyntax
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-10-a"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
]
""")
}
Expand All @@ -279,7 +280,8 @@ public final class InitPackage {
name: "\(pkgname)",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]),
]
),
]
"""
} else if packageType == .buildToolPlugin {
Expand All @@ -303,23 +305,29 @@ public final class InitPackage {
"""
} else if packageType == .macro {
param += """
// Macro implementation, only built for the host and never part of a client program.
.macro(name: "\(pkgname)Macros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
// Macro implementation that performs the source transformation of a macro.
.macro(
name: "\(pkgname)Macros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

// Library that exposes a macro as part of its API, which is used in client programs.
.target(name: "\(pkgname)", dependencies: ["\(pkgname)Macros"]),

// A client of the library, which is able to use the macro in its
// own code.
// A client of the library, which is able to use the macro in its own code.
.executableTarget(name: "\(pkgname)Client", dependencies: ["\(pkgname)"]),

// A test target used to develop the macro implementation.
.testTarget(name: "\(pkgname)Tests", dependencies: ["\(pkgname)Macros"]),
.testTarget(
name: "\(pkgname)Tests",
dependencies: [
"\(pkgname)Macros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
]
"""
} else {
Expand Down Expand Up @@ -618,43 +626,37 @@ public final class InitPackage {
private func writeMacroTestsFile(_ path: AbsolutePath) throws {
try writePackageFile(path) { stream in
stream <<< ##"""
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
import \##(moduleName)Macros

var testMacros: [String: Macro.Type] = [
"stringify" : StringifyMacro.self,
let testMacros: [String: Macro.Type] = [
"stringify": StringifyMacro.self,
]

final class \##(moduleName)Tests: XCTestCase {
func testMacro() {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest

// Test input is a source file containing uses of the macro.
let sf: SourceFileSyntax =
#"""
let a = #stringify(x + y)
let b = #stringify("Hello, \(name)")
"""#

let context = BasicMacroExpansionContext(
sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")]
assertMacroExpansion(
"""
#stringify(a + b)
""",
expandedSource: """
(a + b, "a + b")
""",
macros: testMacros
)
}

// Expand the macro to produce a new source file with the
// result of the expansion, and ensure that it has the
// expected source code.
let transformedSF = sf.expand(macros: testMacros, in: context)

XCTAssertEqual(
transformedSF.description,
func testMacroWithStringLiteral() {
assertMacroExpansion(
#"""
let a = (x + y, "x + y")
let b = ("Hello, \(name)", #""Hello, \(name)""#)
"""#
#stringify("Hello, \(name)")
"""#,
expandedSource: #"""
("Hello, \(name)", #""Hello, \(name)""#)
"""#,
macros: testMacros
)
}
}
Expand Down