|
| 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 | + //==========================================================================// |
| 14 | + // IMPORTANT: The macros defined in this file are intended to test the // |
| 15 | + // behavior of MacroSystem. Many of them do not serve as good examples of // |
| 16 | + // how macros should be written. In particular, they often lack error // |
| 17 | + // handling because it is not needed in the few test cases in which these // |
| 18 | + // macros are invoked. // |
| 19 | + //==========================================================================// |
| 20 | + |
| 21 | +import SwiftSyntax |
| 22 | +import SwiftDiagnostics |
| 23 | +@_spi(Diagnostics) import SwiftSyntaxBuilder |
| 24 | +import SwiftSyntaxMacros |
| 25 | +import SwiftSyntaxMacroExpansion |
| 26 | +import SwiftSyntaxMacrosTestSupport |
| 27 | +import XCTest |
| 28 | + |
| 29 | +//private class TestMacroExpansionContext: MacroExpansionContext { |
| 30 | +// public var diagnostics = [Diagnostic]() |
| 31 | +// |
| 32 | +// func diagnose(_ diagnostic: Diagnostic) { |
| 33 | +// diagnostics.append(diagnostic) |
| 34 | +// } |
| 35 | +// |
| 36 | +// func makeUniqueName(_ name: String) -> TokenSyntax { |
| 37 | +// return TokenSyntax.identifier(name) |
| 38 | +// } |
| 39 | +// |
| 40 | +// func location( |
| 41 | +// of node: some SyntaxProtocol, |
| 42 | +// at position: PositionInSyntaxNode, |
| 43 | +// filePathMode: SourceLocationFilePathMode |
| 44 | +// ) -> AbstractSourceLocation? { |
| 45 | +// return nil |
| 46 | +// } |
| 47 | +//} |
| 48 | + |
| 49 | +private struct DummyError: Error { |
| 50 | + static let diagnosticTestError = DummyError() |
| 51 | +} |
| 52 | + |
| 53 | +final class MacroExpansionContextTests: XCTestCase { |
| 54 | + |
| 55 | + func testMacroExpansionContextAddDiagnosticsAddsSwiftSyntaxInterpolationErrorsWithWrappingMessage(){ |
| 56 | + let context = BasicMacroExpansionContext() |
| 57 | + let error = SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: DeclSyntax.self, actualType: ExprSyntax.self) |
| 58 | + // Since we only care about the error switch inside of addDagnostics, we don't care about the particular node we're passing in. |
| 59 | + context.addDiagnostics(from: error, node: ExprSyntax("1")) |
| 60 | + |
| 61 | + XCTAssertEqual(context.diagnostics.count, 1) |
| 62 | + XCTAssertTrue(context.diagnostics.first!.message.starts(with: "Internal macro error:")) |
| 63 | + } |
| 64 | + |
| 65 | + func testMacroExpansionContextAddDiagnosticsUsesErrorDescriptionForDiagMessage() { |
| 66 | + let context = BasicMacroExpansionContext() |
| 67 | + let error = DummyError.diagnosticTestError |
| 68 | + |
| 69 | + context.addDiagnostics(from: error, node: ExprSyntax("1")) |
| 70 | + XCTAssertEqual(context.diagnostics.count, 1) |
| 71 | + XCTAssertEqual(context.diagnostics.first!.message, String(describing: error)) |
| 72 | + } |
| 73 | +} |
0 commit comments