|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | +import SwiftDiagnostics |
| 15 | +@_spi(Diagnostics) import SwiftSyntaxBuilder |
| 16 | +import SwiftSyntaxMacros |
| 17 | +import SwiftSyntaxMacroExpansion |
| 18 | +import SwiftSyntaxMacrosTestSupport |
| 19 | +import XCTest |
| 20 | + |
| 21 | + |
| 22 | +// An error that is not `SyntaxStringInterpolationError`, only used to verify |
| 23 | +// that other error types won't get prefixed with `Internal macro error:` when |
| 24 | +// passed to `MacroExpansionContext.addDiagnostics`. |
| 25 | +private struct DummyError: Error { |
| 26 | + static let diagnosticTestError = DummyError() |
| 27 | +} |
| 28 | + |
| 29 | +// An extension macro that will fail with |
| 30 | +// `SyntaxStringInterpolationError.producedInvalidNodeType` |
| 31 | +private struct DummyMacro: ExtensionMacro { |
| 32 | + static func expansion( |
| 33 | + of node: AttributeSyntax, |
| 34 | + attachedTo declaration: some DeclGroupSyntax, |
| 35 | + providingExtensionsOf type: some TypeSyntaxProtocol, |
| 36 | + conformingTo protocols: [TypeSyntax], |
| 37 | + in context: some MacroExpansionContext |
| 38 | + ) throws -> [ExtensionDeclSyntax] { |
| 39 | + let ext = try ExtensionDeclSyntax("var x: Int") |
| 40 | + return [ext] |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +final class MacroExpansionContextTests: XCTestCase { |
| 45 | + |
| 46 | + func testMacroExpansionContextAddDiagnosticsAddsSwiftSyntaxInterpolationErrorsWithWrappingMessage(){ |
| 47 | + let context = BasicMacroExpansionContext() |
| 48 | + let error = SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: DeclSyntax.self, actualType: ExprSyntax.self) |
| 49 | + // Since we only care about the error switch inside of addDagnostics, we don't care about the particular node we're passing in. |
| 50 | + context.addDiagnostics(from: error, node: ExprSyntax("1")) |
| 51 | + |
| 52 | + XCTAssertEqual(context.diagnostics.count, 1) |
| 53 | + XCTAssertTrue(context.diagnostics.first!.message.starts(with: "Internal macro error:")) |
| 54 | + } |
| 55 | + |
| 56 | + // Verify that any other error messages do not get "Internal macro error:" prefix. |
| 57 | + func testMacroExpansionContextAddDiagnosticsUsesErrorDescriptionForDiagMessage() { |
| 58 | + let context = BasicMacroExpansionContext() |
| 59 | + let error = DummyError.diagnosticTestError |
| 60 | + |
| 61 | + context.addDiagnostics(from: error, node: ExprSyntax("1")) |
| 62 | + XCTAssertEqual(context.diagnostics.count, 1) |
| 63 | + XCTAssertEqual(context.diagnostics.first!.message, String(describing: error)) |
| 64 | + } |
| 65 | + |
| 66 | + func testMacroExpansionSyntaxInterpolationErrorGetsPrefixed() { |
| 67 | + let expectedDiagnostic = DiagnosticSpec( |
| 68 | + message: "Internal macro error: Parsing the code snippet was expected to produce a ExtensionDeclSyntax but produced a DeclSyntax", |
| 69 | + line: 1, |
| 70 | + column: 1 |
| 71 | + ) |
| 72 | + |
| 73 | + assertMacroExpansion( |
| 74 | + "@dummy struct Foo {}", |
| 75 | + expandedSource: "struct Foo {}", |
| 76 | + diagnostics: [expectedDiagnostic], |
| 77 | + macros: ["dummy": DummyMacro.self] |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
0 commit comments