Skip to content

Commit 7970039

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

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Package.swift

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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",
56+
typeName: "RegisteredMacro"
57+
)
58+
XCTAssertNotNil(registeredMacro)
59+
XCTAssertTrue(registeredMacro == RegisteredMacro.self)
60+
61+
/// Test the plugin doesn't provide macros other than ``
62+
let dummyMacro = plugin.resolveMacro(
63+
moduleName: "SwiftCompilerPluginTest",
64+
typeName: "DummyMacro"
65+
)
66+
XCTAssertNil(dummyMacro)
67+
XCTAssertFalse(dummyMacro == DummyMacro.self)
68+
69+
}
70+
}

0 commit comments

Comments
 (0)