Skip to content

Commit dce5590

Browse files
authored
Use assertMacroExpansion in test cases of macro template (#6485)
* Some minor improvements to the macro template * Use `assertMacroExpansion` in test cases of macro template
1 parent 0551596 commit dce5590

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public final class InitPackage {
231231
// Products define the executables and libraries a package produces, making them visible to other packages.
232232
.library(
233233
name: "\(pkgname)",
234-
targets: ["\(pkgname)"]),
234+
targets: ["\(pkgname)"]
235+
),
235236
.executable(
236237
name: "\(pkgname)Client",
237238
targets: ["\(pkgname)Client"]
@@ -252,7 +253,7 @@ public final class InitPackage {
252253
pkgParams.append("""
253254
dependencies: [
254255
// Depend on the latest Swift 5.9 prerelease of SwiftSyntax
255-
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-10-a"),
256+
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
256257
]
257258
""")
258259
}
@@ -279,7 +280,8 @@ public final class InitPackage {
279280
name: "\(pkgname)",
280281
dependencies: [
281282
.product(name: "ArgumentParser", package: "swift-argument-parser"),
282-
]),
283+
]
284+
),
283285
]
284286
"""
285287
} else if packageType == .buildToolPlugin {
@@ -303,23 +305,29 @@ public final class InitPackage {
303305
"""
304306
} else if packageType == .macro {
305307
param += """
306-
// Macro implementation, only built for the host and never part of a client program.
307-
.macro(name: "\(pkgname)Macros",
308-
dependencies: [
309-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
310-
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
311-
]
308+
// Macro implementation that performs the source transformation of a macro.
309+
.macro(
310+
name: "\(pkgname)Macros",
311+
dependencies: [
312+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
313+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
314+
]
312315
),
313316
314317
// Library that exposes a macro as part of its API, which is used in client programs.
315318
.target(name: "\(pkgname)", dependencies: ["\(pkgname)Macros"]),
316319
317-
// A client of the library, which is able to use the macro in its
318-
// own code.
320+
// A client of the library, which is able to use the macro in its own code.
319321
.executableTarget(name: "\(pkgname)Client", dependencies: ["\(pkgname)"]),
320322
321323
// A test target used to develop the macro implementation.
322-
.testTarget(name: "\(pkgname)Tests", dependencies: ["\(pkgname)Macros"]),
324+
.testTarget(
325+
name: "\(pkgname)Tests",
326+
dependencies: [
327+
"\(pkgname)Macros",
328+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
329+
]
330+
),
323331
]
324332
"""
325333
} else {
@@ -617,43 +625,37 @@ public final class InitPackage {
617625
private func writeMacroTestsFile(_ path: AbsolutePath) throws {
618626
try writePackageFile(path) { stream in
619627
stream <<< ##"""
620-
import SwiftSyntax
621-
import SwiftSyntaxBuilder
622628
import SwiftSyntaxMacros
629+
import SwiftSyntaxMacrosTestSupport
623630
import XCTest
624631
import \##(moduleName)Macros
625632
626-
var testMacros: [String: Macro.Type] = [
627-
"stringify" : StringifyMacro.self,
633+
let testMacros: [String: Macro.Type] = [
634+
"stringify": StringifyMacro.self,
628635
]
629636
630637
final class \##(moduleName)Tests: XCTestCase {
631638
func testMacro() {
632-
// XCTest Documentation
633-
// https://developer.apple.com/documentation/xctest
634-
635-
// Test input is a source file containing uses of the macro.
636-
let sf: SourceFileSyntax =
637-
#"""
638-
let a = #stringify(x + y)
639-
let b = #stringify("Hello, \(name)")
640-
"""#
641-
642-
let context = BasicMacroExpansionContext(
643-
sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")]
639+
assertMacroExpansion(
640+
"""
641+
#stringify(a + b)
642+
""",
643+
expandedSource: """
644+
(a + b, "a + b")
645+
""",
646+
macros: testMacros
644647
)
648+
}
645649
646-
// Expand the macro to produce a new source file with the
647-
// result of the expansion, and ensure that it has the
648-
// expected source code.
649-
let transformedSF = sf.expand(macros: testMacros, in: context)
650-
651-
XCTAssertEqual(
652-
transformedSF.description,
650+
func testMacroWithStringLiteral() {
651+
assertMacroExpansion(
653652
#"""
654-
let a = (x + y, "x + y")
655-
let b = ("Hello, \(name)", #""Hello, \(name)""#)
656-
"""#
653+
#stringify("Hello, \(name)")
654+
"""#,
655+
expandedSource: #"""
656+
("Hello, \(name)", #""Hello, \(name)""#)
657+
"""#,
658+
macros: testMacros
657659
)
658660
}
659661
}

0 commit comments

Comments
 (0)