Skip to content

[5.9] assertMacroExpansion should assert if there are errors #1785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import _SwiftSyntaxTestSupport
import SwiftBasicFormat
import SwiftDiagnostics
import SwiftParser
import SwiftParserDiagnostics
import SwiftSyntax
import SwiftSyntaxMacros
import XCTest
Expand Down Expand Up @@ -279,11 +280,27 @@ 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.trimmingTrailingWhitespace().trimmingCharacters(in: .newlines),
expandedSource.trimmingTrailingWhitespace().trimmingCharacters(in: .newlines),
formattedSourceFile.description.trimmingCharacters(in: .newlines),
expandedSource.trimmingCharacters(in: .newlines),
additionalInfo: """
Actual expanded source:
\(expandedSource)
Expand Down