Skip to content

Commit d7ed7d1

Browse files
committed
Add fix-it handling tests for macro examples
It introduces an enhancement to our macro expansion functionality with the addition of fix-it tests. In `AddBlockerTests`, a new test case has been added to validate the scenario where an addition operation should be a subtraction, including a diagnostic message and the application of the suggested fix-it. Similarly, `AddCompletionHandlerMacroTests` is now equipped with a test case that applies a fix-it for adding the 'async' keyword to functions that are missing it but have the `@AddCompletionHandler` attribute.
1 parent 6808da5 commit d7ed7d1

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Examples/Sources/MacroExamples/Implementation/Peer/AddCompletionHandlerMacro.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public struct AddCompletionHandlerMacro: PeerMacro {
3838
} else {
3939
newEffects = FunctionEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async))
4040
}
41+
newEffects.asyncSpecifier?.trailingTrivia = [.spaces(1)]
4142

4243
var newSignature = funcDecl.signature
4344
newSignature.effectSpecifiers = newEffects

Examples/Tests/MacroExamples/Implementation/Expression/AddBlockerTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ final class AddBlockerTests: XCTestCase {
3939
)
4040
}
4141

42+
func testExpansionWithSubtractionAppliesFixIt() {
43+
assertMacroExpansion(
44+
"""
45+
#addBlocker(x * y + z)
46+
""",
47+
expandedSource: """
48+
x * y - z
49+
""",
50+
diagnostics: [
51+
DiagnosticSpec(
52+
message: "blocked an add; did you mean to subtract?",
53+
line: 1,
54+
column: 19,
55+
severity: .warning,
56+
fixIts: [FixItSpec(message: "use '-'")]
57+
)
58+
],
59+
macros: macros,
60+
applyFixIts: ["use '-'"],
61+
fixedSource:
62+
"""
63+
#addBlocker(x * y - z)
64+
""",
65+
indentationWidth: .spaces(2)
66+
)
67+
}
68+
4269
func testExpansionPreservesSubtraction() {
4370
assertMacroExpansion(
4471
"""

Examples/Tests/MacroExamples/Implementation/Peer/AddCompletionHandlerMacroTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,44 @@ final class AddCompletionHandlerMacroTests: XCTestCase {
9898
indentationWidth: .spaces(2)
9999
)
100100
}
101+
102+
func testExpansionOnNonAsyncFunctionAppliesFixIt() {
103+
assertMacroExpansion(
104+
"""
105+
struct Test {
106+
@AddCompletionHandler
107+
func fetchData() -> String {
108+
return "Hello, World!"
109+
}
110+
}
111+
""",
112+
expandedSource: """
113+
struct Test {
114+
func fetchData() -> String {
115+
return "Hello, World!"
116+
}
117+
}
118+
""",
119+
diagnostics: [
120+
DiagnosticSpec(
121+
message: "can only add a completion-handler variant to an 'async' function",
122+
line: 3,
123+
column: 3,
124+
severity: .error,
125+
fixIts: [FixItSpec(message: "add 'async'")]
126+
)
127+
],
128+
macros: macros,
129+
applyFixIts: ["add 'async'"],
130+
fixedSource: """
131+
struct Test {
132+
@AddCompletionHandler
133+
func fetchData() async -> String {
134+
return "Hello, World!"
135+
}
136+
}
137+
""",
138+
indentationWidth: .spaces(2)
139+
)
140+
}
101141
}

0 commit comments

Comments
 (0)