|
18 | 18 | // macros are invoked. //
|
19 | 19 | //==========================================================================//
|
20 | 20 |
|
| 21 | +import SwiftDiagnostics |
21 | 22 | import SwiftSyntax
|
| 23 | +import SwiftSyntaxMacroExpansion |
22 | 24 | import SwiftSyntaxMacros
|
23 | 25 | import SwiftSyntaxMacrosTestSupport
|
24 | 26 | import XCTest
|
@@ -261,4 +263,61 @@ final class AccessorMacroTests: XCTestCase {
|
261 | 263 | indentationWidth: indentationWidth
|
262 | 264 | )
|
263 | 265 | }
|
| 266 | + |
| 267 | + func testEmpty() { |
| 268 | + struct TestMacro: AccessorMacro { |
| 269 | + static func expansion( |
| 270 | + of node: AttributeSyntax, |
| 271 | + providingAccessorsOf declaration: some DeclSyntaxProtocol, |
| 272 | + in context: some MacroExpansionContext |
| 273 | + ) throws -> [AccessorDeclSyntax] { |
| 274 | + return [] |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + // The compiler will reject this with |
| 279 | + // 'Expansion of macro 'Test()' did not produce a non-observing accessor' |
| 280 | + // We consider this a semantic error because swift-syntax doesn't have |
| 281 | + // knowledge about which accessors are observing and which ones aren't. |
| 282 | + assertMacroExpansion( |
| 283 | + "@Test var x: Int", |
| 284 | + expandedSource: "var x: Int", |
| 285 | + macros: ["Test": TestMacro.self] |
| 286 | + ) |
| 287 | + |
| 288 | + assertMacroExpansion( |
| 289 | + "@Test var x: Int { 1 }", |
| 290 | + expandedSource: "var x: Int { 1 }", |
| 291 | + macros: ["Test": TestMacro.self] |
| 292 | + ) |
| 293 | + } |
| 294 | + |
| 295 | + func testEmitErrorFromMacro() { |
| 296 | + struct TestMacro: AccessorMacro { |
| 297 | + static func expansion( |
| 298 | + of node: AttributeSyntax, |
| 299 | + providingAccessorsOf declaration: some DeclSyntaxProtocol, |
| 300 | + in context: some MacroExpansionContext |
| 301 | + ) throws -> [AccessorDeclSyntax] { |
| 302 | + context.diagnose(Diagnostic(node: node, message: MacroExpansionErrorMessage("test"))) |
| 303 | + return [] |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + assertMacroExpansion( |
| 308 | + "@Test var x: Int", |
| 309 | + expandedSource: "var x: Int", |
| 310 | + diagnostics: [ |
| 311 | + DiagnosticSpec(message: "test", line: 1, column: 1) |
| 312 | + ], |
| 313 | + macros: ["Test": TestMacro.self] |
| 314 | + ) |
| 315 | + |
| 316 | + assertMacroExpansion( |
| 317 | + "@Test var x: Int { 1 }", |
| 318 | + expandedSource: "var x: Int { 1 }", |
| 319 | + diagnostics: [DiagnosticSpec(message: "test", line: 1, column: 1)], |
| 320 | + macros: ["Test": TestMacro.self] |
| 321 | + ) |
| 322 | + } |
264 | 323 | }
|
0 commit comments