Skip to content

Commit

Permalink
Add test showing that a fix-it wrongly replaces parent instead of tar…
Browse files Browse the repository at this point in the history
…geted child

See #15.
  • Loading branch information
gohanlon committed Dec 26, 2023
1 parent c27a3e6 commit 3108b10
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Tests/MacroTestingTests/FixItTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import MacroTesting
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import XCTest

private enum ReplaceFirstMemberMacro: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard
let nodeToReplace = declaration.memberBlock.members.first,
let newNode = try? MemberBlockItemSyntax(
decl: VariableDeclSyntax(SyntaxNodeString(stringLiteral: "\n let oye: Oye"))
)
else { return [] }

context.diagnose(
Diagnostic(
node: node.attributeName,
message: SimpleDiagnosticMessage(
message: "First member needs to be replaced",
diagnosticID: MessageID(domain: "domain", id: "diagnostic2"),
severity: .warning
),
fixIts: [
FixIt(
message: SimpleDiagnosticMessage(
message: "Replace the first member",
diagnosticID: MessageID(domain: "domain", id: "fixit1"),
severity: .error
),
changes: [
.replace(oldNode: Syntax(nodeToReplace), newNode: Syntax(newNode))
]
)
]
)
)

return []
}
}

final class FixItTests: BaseTestCase {
override func invokeTest() {
withMacroTesting(macros: [ReplaceFirstMemberMacro.self]) {
super.invokeTest()
}
}

func testReplaceFirstMember_IncorrectlyReplacesParent() {
assertMacro {
"""
@ReplaceFirstMember
struct FooBar {
let foo: Foo
let bar: Bar
let baz: Baz
}
"""
} diagnostics: {
"""
@ReplaceFirstMember
┬─────────────────
╰─ ⚠️ First member needs to be replaced
✏️ Replace the first member
struct FooBar {
let foo: Foo
let bar: Bar
let baz: Baz
}
"""
} fixes: {
"""
@ReplaceFirstMember
struct FooBar {
let oye: Oye
}
"""
} expansion: {
"""
struct FooBar {
let oye: Oye
}
"""
}
}
}

0 comments on commit 3108b10

Please sign in to comment.