|
| 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