@@ -231,7 +231,8 @@ public final class InitPackage {
231
231
// Products define the executables and libraries a package produces, making them visible to other packages.
232
232
.library(
233
233
name: " \( pkgname) " ,
234
- targets: [ " \( pkgname) " ]),
234
+ targets: [ " \( pkgname) " ]
235
+ ),
235
236
.executable(
236
237
name: " \( pkgname) Client " ,
237
238
targets: [ " \( pkgname) Client " ]
@@ -252,7 +253,7 @@ public final class InitPackage {
252
253
pkgParams. append ( """
253
254
dependencies: [
254
255
// 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 " ),
256
257
]
257
258
""" )
258
259
}
@@ -279,7 +280,8 @@ public final class InitPackage {
279
280
name: " \( pkgname) " ,
280
281
dependencies: [
281
282
.product(name: " ArgumentParser " , package: " swift-argument-parser " ),
282
- ]),
283
+ ]
284
+ ),
283
285
]
284
286
"""
285
287
} else if packageType == . buildToolPlugin {
@@ -303,23 +305,29 @@ public final class InitPackage {
303
305
"""
304
306
} else if packageType == . macro {
305
307
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
+ ]
312
315
),
313
316
314
317
// Library that exposes a macro as part of its API, which is used in client programs.
315
318
.target(name: " \( pkgname) " , dependencies: [ " \( pkgname) Macros " ]),
316
319
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.
319
321
.executableTarget(name: " \( pkgname) Client " , dependencies: [ " \( pkgname) " ]),
320
322
321
323
// 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
+ ),
323
331
]
324
332
"""
325
333
} else {
@@ -617,43 +625,37 @@ public final class InitPackage {
617
625
private func writeMacroTestsFile( _ path: AbsolutePath ) throws {
618
626
try writePackageFile ( path) { stream in
619
627
stream <<< ##"""
620
- import SwiftSyntax
621
- import SwiftSyntaxBuilder
622
628
import SwiftSyntaxMacros
629
+ import SwiftSyntaxMacrosTestSupport
623
630
import XCTest
624
631
import \##( moduleName) Macros
625
632
626
- var testMacros: [String: Macro.Type] = [
627
- "stringify" : StringifyMacro.self,
633
+ let testMacros: [String: Macro.Type] = [
634
+ "stringify": StringifyMacro.self,
628
635
]
629
636
630
637
final class \##( moduleName) Tests: XCTestCase {
631
638
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
644
647
)
648
+ }
645
649
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(
653
652
#"""
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
657
659
)
658
660
}
659
661
}
0 commit comments