Skip to content

Commit ea2d7e4

Browse files
committed
assertMacroExpansion should assert if there are errors
Invalid macro expansions should generate an assertion in `assertMacroExpansion` rather than being ignored.
1 parent 9b513ab commit ea2d7e4

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift

Lines changed: 19 additions & 2 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
@@ -272,10 +273,26 @@ public func assertMacroExpansion(
272273
let context = BasicMacroExpansionContext(
273274
sourceFiles: [origSourceFile: .init(moduleName: testModuleName, fullFilePath: testFileName)]
274275
)
275-
let expandedSourceFile = origSourceFile.expand(macros: macros, in: context).formatted(using: BasicFormat(indentationWidth: indentationWidth))
276276

277+
let expandedSourceFile = origSourceFile.expand(macros: macros, in: context)
278+
let diags = ParseDiagnosticsGenerator.diagnostics(for: expandedSourceFile)
279+
if !diags.isEmpty {
280+
XCTFail(
281+
"""
282+
Expanded source should not contain any syntax errors, but contains:
283+
\(DiagnosticsFormatter.annotatedSource(tree: expandedSourceFile, diags: diags))
284+
285+
Expanded syntax tree was:
286+
\(expandedSourceFile.debugDescription)
287+
""",
288+
file: file,
289+
line: line
290+
)
291+
}
292+
293+
let formattedSourceFile = expandedSourceFile.formatted(using: BasicFormat(indentationWidth: indentationWidth))
277294
assertStringsEqualWithDiff(
278-
expandedSourceFile.description.trimmingCharacters(in: .newlines),
295+
formattedSourceFile.description.trimmingCharacters(in: .newlines),
279296
expandedSource.trimmingCharacters(in: .newlines),
280297
additionalInfo: """
281298
Actual expanded source:

0 commit comments

Comments
 (0)