From c4e19a9c6a374ce5e920d2b325ff17f28ccbdbf1 Mon Sep 17 00:00:00 2001 From: Joe Newton Date: Wed, 15 Nov 2023 09:18:15 -0500 Subject: [PATCH] Added unit test for testing the removal of an attached macro --- .../MemberMacroTests.swift | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift index a281cc69744..3ff8bdda8dc 100644 --- a/Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift +++ b/Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift @@ -18,6 +18,7 @@ // macros are invoked. // //==========================================================================// +import SwiftDiagnostics import SwiftSyntax import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros @@ -299,4 +300,83 @@ final class MemberMacroTests: XCTestCase { ] ) } + + func testRemoveAttributeFixIt() { + struct ActorOnlyMacro: MemberMacro { + static func expansion( + of node: AttributeSyntax, + providingMembersOf declaration: some DeclGroupSyntax, + conformingTo protocols: [TypeSyntax], + in context: some MacroExpansionContext + ) throws -> [DeclSyntax] { + guard declaration.is(ActorDeclSyntax.self) else { + throw DiagnosticsError(diagnostics: [ + Diagnostic( + node: node, + message: MacroExpansionErrorMessage("'@ActorOnly' is only applicable to actors."), + fixIt: FixIt( + message: MacroExpansionFixItMessage("Remove '@ActorOnly' attribute."), + changes: [ + // This doesn't account for other attributes that *could* be present in the + // attribute list, but for this test it will be fine. + .replace( + oldNode: Syntax(declaration.attributes), + newNode: Syntax(AttributeListSyntax()) + ) + ] + ) + ) + ]) + } + return [] + } + } + + assertMacroExpansion( + """ + actor Foo { + var foo: Int + } + + @ActorOnly + struct Bar { + var bar: Int + } + """, + expandedSource: + """ + actor Foo { + var foo: Int + } + struct Bar { + var bar: Int + } + """, + diagnostics: [ + DiagnosticSpec( + message: "'@ActorOnly' is only applicable to actors.", + line: 5, + column: 1, + fixIts: [ + FixItSpec(message: "Remove '@ActorOnly' attribute.") + ] + ) + ], + macros: [ + "ActorOnly": ActorOnlyMacro.self + ], + applyFixIts: [ + "Remove '@ActorOnly' attribute." + ], + fixedSource: + """ + actor Foo { + var foo: Int + } + struct Bar { + var bar: Int + } + """ + ) + } }