@@ -58,7 +58,7 @@ func testStringify() {
58
58
"""
59
59
#stringify(a + b)
60
60
"""
61
- } matches : {
61
+ } expansion : {
62
62
"""
63
63
(a + b, "a + b")
64
64
"""
@@ -86,14 +86,14 @@ assertMacro(["stringify": StringifyMacro.self], record: true) {
86
86
"""
87
87
#stringify(a + b)
88
88
"""
89
- } matches : {
89
+ } expansion : {
90
90
"""
91
91
(a + b, "a + b")
92
92
"""
93
93
}
94
94
```
95
95
96
- Now when you run the test again the freshest expanded macro will be written to the ` matches `
96
+ Now when you run the test again the freshest expanded macro will be written to the ` expansion `
97
97
trailing closure.
98
98
99
99
If you're writing many tests for a macro, you can avoid the repetitive work of specifying the macros
@@ -115,7 +115,7 @@ class StringifyMacroTests: XCTestCase {
115
115
"""
116
116
#stringify(a + b)
117
117
"""
118
- } matches : {
118
+ } expansion : {
119
119
"""
120
120
(a + b, "a + b")
121
121
"""
@@ -142,27 +142,47 @@ override func invokeTest() {
142
142
Macro Testing can also test diagnostics, such as warnings, errors, notes, and fix-its. When a macro
143
143
expansion emits a diagnostic, it will render inline in the test. For example, a macro that adds
144
144
completion handler functions to async functions may emit an error and fix-it when it is applied to a
145
- non-async function. The resulting macro test will fully capture this information:
145
+ non-async function. The resulting macro test will fully capture this information, including where
146
+ the diagnostics are emitted, how the fix-its are applied, and how the final macro expands:
146
147
147
148
``` swift
148
149
func testNonAsyncFunctionDiagnostic () {
149
150
assertMacro {
150
151
"""
151
152
@AddCompletionHandler
152
- func f(a: Int, for b: String, _ value: Double ) -> String {
153
+ func f(a: Int, for b: String) -> String {
153
154
return b
154
- }
155
+ }
155
156
"""
156
- } matches : {
157
+ } diagnostics : {
157
158
"""
158
159
@AddCompletionHandler
159
- func f(a: Int, for b: String, _ value: Double ) -> String {
160
+ func f(a: Int, for b: String) -> String {
160
161
┬───
161
162
╰─ 🛑 can only add a completion-handler variant to an 'async' function
162
163
✏️ add 'async'
163
164
return b
164
165
}
165
166
"""
167
+ } fixes : {
168
+ """
169
+ @AddCompletionHandler
170
+ func f(a: Int, for b: String) async -> String {
171
+ return b
172
+ }
173
+ """
174
+ } expansion : {
175
+ """
176
+ func f(a: Int, for b: String) async -> String {
177
+ return b
178
+ }
179
+
180
+ func f(a: Int, for b: String, completionHandler: @escaping (String) -> Void) {
181
+ Task {
182
+ completionHandler(await f(a: a, for: b, value))
183
+ }
184
+ }
185
+ """
166
186
}
167
187
}
168
188
```
0 commit comments