Skip to content

Commit e275899

Browse files
committed
[Plugin] Add unit tests for plugin support library
1 parent eec66fd commit e275899

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ let package = Package(
201201
"SwiftRefactor", "SwiftSyntaxBuilder", "_SwiftSyntaxTestSupport",
202202
]
203203
),
204+
.testTarget(
205+
name: "SwiftCompilerPluginTest",
206+
dependencies: [
207+
"SwiftCompilerPlugin",
208+
]
209+
),
204210
]
205211
)
206212

Sources/SwiftCompilerPlugin/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import SwiftSyntaxMacros
1616

1717
/// Implementation for `CompilerPlugin` macro related request processing.
1818
extension CompilerPlugin {
19-
// @testable
20-
public func resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
19+
public // @testable
20+
func resolveMacro(moduleName: String, typeName: String) -> Macro.Type? {
2121
let qualifedName = "\(moduleName).\(typeName)"
2222

2323
for type in self.providingMacros {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import XCTest
14+
import SwiftCompilerPlugin
15+
import SwiftSyntax
16+
import SwiftSyntaxMacros
17+
18+
/// Dummy macro
19+
struct DummyMacro: ExpressionMacro {
20+
static func expansion<
21+
Node: FreestandingMacroExpansionSyntax,
22+
Context: MacroExpansionContext
23+
>(
24+
of node: Node,
25+
in context: Context
26+
) throws -> ExprSyntax {
27+
fatalError()
28+
}
29+
}
30+
31+
struct RegisteredMacro: ExpressionMacro {
32+
static func expansion<
33+
Node: FreestandingMacroExpansionSyntax,
34+
Context: MacroExpansionContext
35+
>(
36+
of node: Node,
37+
in context: Context
38+
) throws -> ExprSyntax {
39+
fatalError()
40+
}
41+
}
42+
43+
struct MyPlugin: CompilerPlugin {
44+
let providingMacros: [Macro.Type] = [
45+
RegisteredMacro.self
46+
]
47+
}
48+
49+
public class CompilerPluginTests: XCTestCase {
50+
51+
func testResolveMacro() {
52+
let plugin = MyPlugin()
53+
54+
let registeredMacro = plugin.resolveMacro(
55+
moduleName: "SwiftCompilerPluginTest", typeName: "RegisteredMacro")
56+
XCTAssertNotNil(registeredMacro)
57+
XCTAssertTrue(registeredMacro == RegisteredMacro.self)
58+
59+
/// Test the plugin doesn't provide macros other than ``
60+
let dummyMacro = plugin.resolveMacro(
61+
moduleName: "SwiftCompilerPluginTest", typeName: "DummyMacro")
62+
XCTAssertNil(dummyMacro)
63+
XCTAssertFalse(dummyMacro == DummyMacro.self)
64+
65+
}
66+
}

0 commit comments

Comments
 (0)