From ea2d7e4ef794c7d29f38067be7ddeaffac58c3e0 Mon Sep 17 00:00:00 2001 From: Ben Barham Date: Fri, 9 Jun 2023 13:01:40 -0700 Subject: [PATCH] assertMacroExpansion should assert if there are errors Invalid macro expansions should generate an assertion in `assertMacroExpansion` rather than being ignored. --- .../Assertions.swift | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift index 092ff601f9e..2be2fbb1bed 100644 --- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift +++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift @@ -14,6 +14,7 @@ import _SwiftSyntaxTestSupport import SwiftBasicFormat import SwiftDiagnostics import SwiftParser +import SwiftParserDiagnostics import SwiftSyntax import SwiftSyntaxMacros import XCTest @@ -272,10 +273,26 @@ public func assertMacroExpansion( let context = BasicMacroExpansionContext( sourceFiles: [origSourceFile: .init(moduleName: testModuleName, fullFilePath: testFileName)] ) - let expandedSourceFile = origSourceFile.expand(macros: macros, in: context).formatted(using: BasicFormat(indentationWidth: indentationWidth)) + let expandedSourceFile = origSourceFile.expand(macros: macros, in: context) + let diags = ParseDiagnosticsGenerator.diagnostics(for: expandedSourceFile) + if !diags.isEmpty { + XCTFail( + """ + Expanded source should not contain any syntax errors, but contains: + \(DiagnosticsFormatter.annotatedSource(tree: expandedSourceFile, diags: diags)) + + Expanded syntax tree was: + \(expandedSourceFile.debugDescription) + """, + file: file, + line: line + ) + } + + let formattedSourceFile = expandedSourceFile.formatted(using: BasicFormat(indentationWidth: indentationWidth)) assertStringsEqualWithDiff( - expandedSourceFile.description.trimmingCharacters(in: .newlines), + formattedSourceFile.description.trimmingCharacters(in: .newlines), expandedSource.trimmingCharacters(in: .newlines), additionalInfo: """ Actual expanded source: