Skip to content

Commit be4a332

Browse files
authored
Merge pull request #1785 from bnbarham/cherry-assert-on-macro-errors
[5.9] assertMacroExpansion should assert if there are errors
2 parents 561e9d3 + 751f966 commit be4a332

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import _SwiftSyntaxTestSupport
1414
import SwiftBasicFormat
1515
import SwiftDiagnostics
1616
import SwiftParser
17+
import SwiftParserDiagnostics
1718
import SwiftSyntax
1819
import SwiftSyntaxMacros
1920
import XCTest
@@ -279,11 +280,27 @@ public func assertMacroExpansion(
279280
let context = BasicMacroExpansionContext(
280281
sourceFiles: [origSourceFile: .init(moduleName: testModuleName, fullFilePath: testFileName)]
281282
)
282-
let expandedSourceFile = origSourceFile.expand(macros: macros, in: context).formatted(using: BasicFormat(indentationWidth: indentationWidth))
283283

284+
let expandedSourceFile = origSourceFile.expand(macros: macros, in: context)
285+
let diags = ParseDiagnosticsGenerator.diagnostics(for: expandedSourceFile)
286+
if !diags.isEmpty {
287+
XCTFail(
288+
"""
289+
Expanded source should not contain any syntax errors, but contains:
290+
\(DiagnosticsFormatter.annotatedSource(tree: expandedSourceFile, diags: diags))
291+
292+
Expanded syntax tree was:
293+
\(expandedSourceFile.debugDescription)
294+
""",
295+
file: file,
296+
line: line
297+
)
298+
}
299+
300+
let formattedSourceFile = expandedSourceFile.formatted(using: BasicFormat(indentationWidth: indentationWidth))
284301
assertStringsEqualWithDiff(
285-
expandedSourceFile.description.trimmingTrailingWhitespace().trimmingCharacters(in: .newlines),
286-
expandedSource.trimmingTrailingWhitespace().trimmingCharacters(in: .newlines),
302+
formattedSourceFile.description.trimmingCharacters(in: .newlines),
303+
expandedSource.trimmingCharacters(in: .newlines),
287304
additionalInfo: """
288305
Actual expanded source:
289306
\(expandedSource)

0 commit comments

Comments
 (0)