Skip to content

Added unit test for testing the removal of an attached macro #2471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// macros are invoked. //
//==========================================================================//

import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
Expand Down Expand Up @@ -424,4 +425,83 @@ final class MemberMacroTests: XCTestCase {
indentationWidth: indentationWidth
)
}

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: SwiftSyntaxMacros.MacroExpansionErrorMessage("'@ActorOnly' is only applicable to actors."),
fixIt: FixIt(
message: SwiftSyntaxMacros.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: 4,
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
}
""",
indentationWidth: indentationWidth
)
}
}