1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftDiagnostics
@_spi(FixItApplier) import SwiftIDEUtils
@_spi(Testing) @_spi(RawSyntax) @_spi(AlternateTokenIntrospection) @_spi(ExperimentalLanguageFeatures) import SwiftParser
@_spi(RawSyntax) import SwiftParserDiagnostics
@_spi(RawSyntax) import SwiftSyntax
import XCTest
import _SwiftSyntaxTestSupport
// MARK: Lexing Assertions
struct LexemeSpec {
let rawTokenKind: RawTokenKind
let leadingTrivia: SyntaxText
let tokenText: SyntaxText
let trailingTrivia: SyntaxText
let errorLocationMarker: String
let diagnostic: String?
let flags: Lexer.Lexeme.Flags
/// The file and line at which this `LexemeSpec` was created, so that assertion failures can be reported at its location.
let file: StaticString
let line: UInt
init(
_ rawTokenKind: RawTokenKind,
leading: SyntaxText = "",
text: SyntaxText,
trailing: SyntaxText = "",
errorLocationMarker: String = "1️⃣",
diagnostic: String? = nil,
flags: Lexer.Lexeme.Flags = [],
file: StaticString = #filePath,
line: UInt = #line
) {
self.rawTokenKind = rawTokenKind
self.leadingTrivia = leading
self.tokenText = text
self.trailingTrivia = trailing
self.errorLocationMarker = errorLocationMarker
self.diagnostic = diagnostic
self.flags = flags
self.file = file
self.line = line
}
}
/// Asserts that two lexical streams are structurally equal, including their trivia and any
/// text.
///
/// - Parameters:
/// - lhs: The actual lexeme stream.
/// - rhs: The expected lexeme stream.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
private func assertTokens(
_ actual: [Lexer.Lexeme],
_ expected: [LexemeSpec],
markerLocations: [String: Int],
file: StaticString = #filePath,
line: UInt = #line
) {
guard actual.count == expected.count else {
return XCTFail(
"""
Expected \(expected.count) tokens but got \(actual.count):
\(actual.map({ "\($0.rawTokenKind) '\($0.tokenText)'" }).joined(separator: "\n"))
""",
file: file,
line: line
)
}
// The byte offset at which the leading trivia of `actualLexeme` starts.
var lexemeStartOffset = 0
for (actualLexeme, expectedLexeme) in zip(actual, expected) {
defer {
lexemeStartOffset += actualLexeme.byteLength
}
if actualLexeme.rawTokenKind != expectedLexeme.rawTokenKind {
XCTFail(
"Expected token kind \(expectedLexeme.rawTokenKind) but got \(actualLexeme.rawTokenKind)",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
if actualLexeme.leadingTriviaText != expectedLexeme.leadingTrivia {
assertStringsEqualWithDiff(
String(syntaxText: actualLexeme.leadingTriviaText),
String(syntaxText: expectedLexeme.leadingTrivia),
"Leading trivia does not match",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
if actualLexeme.tokenText.debugDescription != expectedLexeme.tokenText.debugDescription {
assertStringsEqualWithDiff(
actualLexeme.tokenText.debugDescription,
expectedLexeme.tokenText.debugDescription,
"Token text does not match",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
if actualLexeme.trailingTriviaText != expectedLexeme.trailingTrivia {
assertStringsEqualWithDiff(
String(syntaxText: actualLexeme.trailingTriviaText),
String(syntaxText: expectedLexeme.trailingTrivia),
"Trailing trivia does not match",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
switch (actualLexeme.diagnostic, expectedLexeme.diagnostic) {
case (nil, nil): break
case (nil, .some):
XCTFail(
"Expected an error but did not receive one",
file: expectedLexeme.file,
line: expectedLexeme.line
)
case (let actualError?, nil):
XCTFail(
"Did not expect an error but got \(actualError.kind) at \(actualError.byteOffset)",
file: expectedLexeme.file,
line: expectedLexeme.line
)
case (let actualError?, let expectedError?):
// Create a token from the lexeme so we can pass it to `TokenDiagnostic.diagnosticMessage(in:)`
let arena = ParsingSyntaxArena(parseTriviaFunction: TriviaParser.parseTrivia)
let rawToken = RawTokenSyntax(
kind: actualLexeme.rawTokenKind,
wholeText: arena.intern(actualLexeme.wholeText),
textRange: actualLexeme.textRange,
presence: .present,
tokenDiagnostic: actualLexeme.diagnostic,
arena: arena
)
let token = Syntax(raw: RawSyntax(rawToken), rawNodeArena: arena).cast(TokenSyntax.self)
assertStringsEqualWithDiff(
actualError.diagnosticMessage(in: token).message,
expectedError,
file: expectedLexeme.file,
line: expectedLexeme.line
)
if let location = markerLocations[expectedLexeme.errorLocationMarker] {
XCTAssertEqual(
Int(actualError.byteOffset),
location - lexemeStartOffset,
"Expected location did not match",
file: expectedLexeme.file,
line: expectedLexeme.line
)
} else {
XCTFail(
"Did not find marker \(expectedLexeme.errorLocationMarker) in the source code",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
}
if actualLexeme.flags != expectedLexeme.flags {
XCTFail(
"Expected flags \(expectedLexeme.flags.debugDescription) but got \(actualLexeme.flags.debugDescription)",
file: expectedLexeme.file,
line: expectedLexeme.line
)
}
}
}
func assertLexemes(
_ markedSource: String,
lexemes expectedLexemes: [LexemeSpec],
file: StaticString = #filePath,
line: UInt = #line
) {
var (markerLocations, source) = extractMarkers(markedSource)
markerLocations["START"] = 0
var expectedLexemes = expectedLexemes
if expectedLexemes.last?.rawTokenKind != .endOfFile {
expectedLexemes.append(LexemeSpec(.endOfFile, text: ""))
}
let lookaheadTracker = UnsafeMutablePointer<LookaheadTracker>.allocate(capacity: 1)
defer {
lookaheadTracker.deallocate()
}
lookaheadTracker.initialize(to: LookaheadTracker())
source.withUTF8 { buf in
var lexemes = [Lexer.Lexeme]()
for token in Lexer.tokenize(buf, from: 0, lookaheadTracker: lookaheadTracker) {
lexemes.append(token)
if token.rawTokenKind == .endOfFile {
break
}
}
assertTokens(lexemes, expectedLexemes, markerLocations: markerLocations, file: file, line: line)
}
}
// MARK: Parsing Assertions
struct NoteSpec {
/// The location to which the note should point.
let locationMarker: String
/// Asserts that the note has this message.
let message: String
/// The file and line at which this ``NoteSpec`` was created, so that assertion failures can be reported at its location.
let file: StaticString
let line: UInt
init(
locationMarker: String = "ℹ️",
message: String,
file: StaticString = #filePath,
line: UInt = #line
) {
self.locationMarker = locationMarker
self.message = message
self.file = file
self.line = line
}
}
/// An abstract data structure to describe how a diagnostic produced by the parser should look like.
struct DiagnosticSpec {
/// The name of a marker (of the form `1️⃣`) in the source code that marks the location where the diagnostic should be produced.
let locationMarker: String
/// If not `nil`, assert that the diagnostic has the given ID.
let id: MessageID?
/// If not `nil`, assert that the diagnostic has the given message.
let message: String?
/// Assert that the diagnostic has the given severity.
let severity: DiagnosticSeverity
/// If not `nil`, assert that the highlighted range has this content.
let highlight: String?
/// If not `nil`, assert that the diagnostic contains notes with these messages.
let notes: [NoteSpec]
/// If not `nil`, assert that the diagnostic contains fix-its with these messages.
/// Use the `fixedSource` parameter on `AssertParse` to check that applying the Fix-It yields the expected result.
let fixIts: [String]
/// The file and line at which this ``DiagnosticSpec`` was created, so that assertion failures can be reported at its location.
let file: StaticString
let line: UInt
init(
locationMarker: String = "1️⃣",
id: MessageID? = nil,
message: String?,
severity: DiagnosticSeverity = .error,
highlight: String? = nil,
notes: [NoteSpec] = [],
fixIts: [String] = [],
file: StaticString = #filePath,
line: UInt = #line
) {
self.locationMarker = locationMarker
self.id = id
self.message = message
self.severity = severity
self.highlight = highlight
self.notes = notes
self.fixIts = fixIts
self.file = file
self.line = line
}
}
/// Assert that `location` is the same as that of `locationMarker` in `tree`.
func assertLocation<T: SyntaxProtocol>(
_ location: SourceLocation,
in tree: T,
markerLocations: [String: Int],
expectedLocationMarker locationMarker: String,
file: StaticString = #filePath,
line: UInt = #line
) {
if let markerLoc = markerLocations[locationMarker] {
let locationConverter = SourceLocationConverter(fileName: "", tree: tree)
let actualLocation = location
let expectedLocation = locationConverter.location(for: AbsolutePosition(utf8Offset: markerLoc))
if actualLocation.line != expectedLocation.line || actualLocation.column != expectedLocation.column {
XCTFail(
"Expected location \(expectedLocation.line):\(expectedLocation.column) but got \(actualLocation.line):\(actualLocation.column)",
file: file,
line: line
)
}
} else {
XCTFail("Did not find marker \(locationMarker) in the source code", file: file, line: line)
}
}
/// Assert that the diagnostic `note` produced in `tree` matches `spec`,
/// using `markerLocations` to translate markers to actual source locations.
func assertNote<T: SyntaxProtocol>(
_ note: Note,
in tree: T,
markerLocations: [String: Int],
expected spec: NoteSpec
) {
XCTAssertEqual(note.message, spec.message, file: spec.file, line: spec.line)
let locationConverter = SourceLocationConverter(fileName: "", tree: tree)
assertLocation(
note.location(converter: locationConverter),
in: tree,
markerLocations: markerLocations,
expectedLocationMarker: spec.locationMarker,
file: spec.file,
line: spec.line
)
}
/// Assert that the diagnostic `diag` produced in `tree` matches `spec`,
/// using `markerLocations` to translate markers to actual source locations.
func assertDiagnostic<T: SyntaxProtocol>(
_ diag: Diagnostic,
in tree: T,
markerLocations: [String: Int],
expected spec: DiagnosticSpec
) {
let locationConverter = SourceLocationConverter(fileName: "", tree: tree)
assertLocation(
diag.location(converter: locationConverter),
in: tree,
markerLocations: markerLocations,
expectedLocationMarker: spec.locationMarker,
file: spec.file,
line: spec.line
)
if let id = spec.id {
XCTAssertEqual(diag.diagnosticID, id, file: spec.file, line: spec.line)
}
if let message = spec.message {
assertStringsEqualWithDiff(diag.message, message, file: spec.file, line: spec.line)
}
XCTAssertEqual(spec.severity, diag.diagMessage.severity, file: spec.file, line: spec.line)
if diag.message.contains("\n") {
XCTFail(
"""
Diagnostic message should only span a single line. Message was:
\(diag.message)
""",
file: spec.file,
line: spec.line
)
}
let highlight = spec.highlight ?? diag.node.description
assertStringsEqualWithDiff(
diag.highlights.map(\.description).joined().trimmingTrailingWhitespace(),
highlight.trimmingTrailingWhitespace(),
"Highlighted source code did not match",
file: spec.file,
line: spec.line
)
if diag.notes.count != spec.notes.count {
XCTFail(
"""
Expected \(spec.notes.count) notes but received \(diag.notes.count):
\(diag.notes.map(\.debugDescription).joined(separator: "\n"))
""",
file: spec.file,
line: spec.line
)
} else {
for (note, expectedNote) in zip(diag.notes, spec.notes) {
assertNote(note, in: tree, markerLocations: markerLocations, expected: expectedNote)
}
}
if diag.fixIts.count != spec.fixIts.count {
XCTFail(
"""
Expected \(spec.fixIts.count) fix its but received \(diag.fixIts.count):
\(diag.fixIts.map { $0.message.message }.joined(separator: "\n"))
""",
file: spec.file,
line: spec.line
)
} else if spec.fixIts != diag.fixIts.map(\.message.message) {
assertStringsEqualWithDiff(
diag.fixIts.map(\.message.message).joined(separator: "\n"),
spec.fixIts.joined(separator: "\n"),
file: spec.file,
line: spec.line
)
}
}
class MutatedTreePrinter: SyntaxVisitor {
private var mutations: [Int: TokenSpec] = [:]
private var printedSource: [UInt8] = []
/// Prints `tree` by replacing the tokens whose offset is in `mutations` by
/// a token that matches the corresponding ``TokenSpec``.
static func print(tree: Syntax, mutations: [Int: TokenSpec]) -> [UInt8] {
let printer = MutatedTreePrinter(mutations: mutations)
printer.walk(tree)
return printer.printedSource
}
private init(mutations: [Int: TokenSpec]) {
self.mutations = mutations
super.init(viewMode: .sourceAccurate)
}
override func visit(_ node: TokenSyntax) -> SyntaxVisitorContinueKind {
if let mutation = mutations[node.positionAfterSkippingLeadingTrivia.utf8Offset] {
let token = TokenSyntax(
mutation.synthesizedTokenKind,
leadingTrivia: node.leadingTrivia,
trailingTrivia: node.trailingTrivia,
presence: .present
)
printedSource.append(contentsOf: token.syntaxTextBytes)
return .skipChildren
}
printedSource.append(contentsOf: node.syntaxTextBytes)
return .skipChildren
}
}
/// Flip the presence of the `flipTokenAtIndex`-th token in a tree, i.e. making
/// it present if it is missing or missing if it is present. All other nodes are
/// not modified.
class TokenPresenceFlipper: SyntaxRewriter {
var visitedTokens = 0
let flipTokenAtIndex: Int
init(flipTokenAtIndex: Int) {
self.flipTokenAtIndex = flipTokenAtIndex
super.init(viewMode: .all)
}
override func visit(_ token: TokenSyntax) -> TokenSyntax {
defer { visitedTokens += 1 }
if visitedTokens == flipTokenAtIndex {
switch token.presence {
case .present:
return token.with(\.presence, .missing)
case .missing:
return token.with(\.presence, .present)
}
}
return token
}
}
public struct AssertParseOptions: OptionSet, Sendable {
public var rawValue: UInt8
public init(rawValue: UInt8) {
self.rawValue = rawValue
}
/// Trivia mismatches in the substructure should cause failures
public static let substructureCheckTrivia = AssertParseOptions(rawValue: 1 << 0)
/// Replace all `\r` and `\r\n` in the fixed source by `\n`.
/// Useful to match source code that contains other line endings to expected
/// fixed source that has `\n` line endings.
public static let normalizeNewlinesInFixedSource = AssertParseOptions(rawValue: 1 << 1)
}
extension ParserTestCase {
/// After a test case has been mutated, assert that the mutated source
/// round-trips and doesn’t hit any assertion failures in the parser.
fileprivate static func assertMutationRoundTrip<S: SyntaxProtocol>(
source: [UInt8],
_ parse: (inout Parser) -> S,
swiftVersion: Parser.SwiftVersion?,
experimentalFeatures: Parser.ExperimentalFeatures,
file: StaticString,
line: UInt
) {
source.withUnsafeBufferPointer { buf in
let mutatedSource = String(decoding: buf, as: UTF8.self)
// Check that we don't hit any assertions in the parser while parsing
// the mutated source and that it round-trips
var mutatedParser = Parser(buf, swiftVersion: swiftVersion, experimentalFeatures: experimentalFeatures)
let mutatedTree = parse(&mutatedParser)
// Run the diagnostic generator to make sure it doesn’t crash
_ = ParseDiagnosticsGenerator.diagnostics(for: mutatedTree)
assertStringsEqualWithDiff(
"\(mutatedTree)",
mutatedSource,
"A mutation of the original test case failed to round-trip.",
additionalInfo: """
To debug the failure, add a new `assertParse` test with the following source as input:
\(mutatedSource)
Parsed syntax tree of mutation:
\(mutatedTree.debugDescription)
""",
file: file,
line: line
)
}
}
/// Verifies that parsing of `markedSource` produces expected results using a
/// combination of various testing techniques:
///
/// 1. Asserts that parsing of `markedSource` round-trips, ie. that the printed
/// parsed tree is the same as the input.
/// 2. Checks that parsing produces the expected list of diagnostics. If no
/// diagnostics are passed, asserts that the input parses without any errors.
/// 3. Checks that applying all Fix-Its of the source code results in the
/// expected fixed source, effectively testing the Fix-Its.
/// 4. If a substructure is passed, asserts that the parsed tree contains a
/// subtree of that structure.
/// 5. Mutates the test input by flipping each token's presence (ie. for every
/// token, either remove it from the input if it is present in the parsed
/// tree or synthesize it if it was missing) and verifies that this
/// mutated input round-trips. This test is disabled if the
/// `SKIP_LONG_TESTS` environment variable is set.
/// 6. If swift-syntax is compiled with the
/// `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION` environment variable
/// set, mutates the input based on tokens the parse is trying to parse.
/// See the *Test Case Mutation* section in CONTRIBUTING.md.
///
///
/// - Parameters:
/// - markedSource: source that can include markers of the form `1️⃣`,
/// to be used as locations in the following parameters.
/// - parse: The function with which the source code should be parsed.
/// Defaults to parsing as a source file.
/// - expectedSubstructure: Asserts the parsed syntax tree contains this structure.
/// - substructureAfterMarker: Changes the position to start the structure
/// assertion from, ie. allows matching a particular substructure rather
/// than the whole source.
/// - expectedDiagnostics: Asserts the given diagnostics were output, by default it
/// asserts the parse was successful (ie. it has no diagnostics). Note
/// that `DiagnosticsSpec` uses the location marked by `1️⃣` by default.
/// - applyFixIts: Applies only the fix-its with these messages.
/// - expectedFixedSource: Asserts that the source after applying fix-its matches
/// this string.
/// - swiftVersion: The version of Swift using which the file should be parsed.
/// Defaults to the latest version.
/// - experimentalFeatures: A list of experimental features to enable, or
/// `nil` to enable the default set of features provided by the test case.
func assertParse<S: SyntaxProtocol>(
_ markedSource: String,
_ parse: @Sendable (inout Parser) -> S = { SourceFileSyntax.parse(from: &$0) },
substructure expectedSubstructure: (some SyntaxProtocol)? = Optional<Syntax>.none,
substructureAfterMarker: String = "START",
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
applyFixIts: [String]? = nil,
fixedSource expectedFixedSource: String? = nil,
options: AssertParseOptions = [],
swiftVersion: Parser.SwiftVersion? = nil,
experimentalFeatures: Parser.ExperimentalFeatures? = nil,
file: StaticString = #filePath,
line: UInt = #line
) {
let experimentalFeatures = experimentalFeatures ?? self.experimentalFeatures
// Verify the parser can round-trip the source
var (markerLocations, source) = extractMarkers(markedSource)
markerLocations["START"] = 0
var parser = Parser(source, swiftVersion: swiftVersion, experimentalFeatures: experimentalFeatures)
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
if !longTestsDisabled {
parser.enableAlternativeTokenChoices()
}
#endif
let tree: S = parse(&parser)
// Round-trip
assertStringsEqualWithDiff(
"\(tree)",
source,
"Test case failed to round-trip",
additionalInfo: """
Parsed syntax tree:
\(tree.debugDescription)
""",
file: file,
line: line
)
// Substructure
if let expectedSubstructure {
let subtreeMatcher = SubtreeMatcher(tree, markers: markerLocations)
do {
try subtreeMatcher.assertSameStructure(
afterMarker: substructureAfterMarker,
expectedSubstructure,
includeTrivia: options.contains(.substructureCheckTrivia),
file: file,
line: line
)
} catch {
XCTFail("Matching for a subtree failed with error: \(error)", file: file, line: line)
}
}
// Diagnostics
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
if diags.count != expectedDiagnostics.count {
XCTFail(
"""
Expected \(expectedDiagnostics.count) diagnostics but received \(diags.count):
\(diags.map(\.debugDescription).joined(separator: "\n"))
""",
file: file,
line: line
)
} else {
for (diag, expectedDiag) in zip(diags, expectedDiagnostics) {
assertDiagnostic(diag, in: tree, markerLocations: markerLocations, expected: expectedDiag)
}
}
// Applying Fix-Its
if expectedDiagnostics.contains(where: { !$0.fixIts.isEmpty }) && expectedFixedSource == nil {
XCTFail("Expected a fixed source if the test case produces diagnostics with Fix-Its", file: file, line: line)
} else if let expectedFixedSource = expectedFixedSource {
let fixedTree = FixItApplier.applyFixes(from: diags, filterByMessages: applyFixIts, to: tree)
var fixedTreeDescription = fixedTree.description
if options.contains(.normalizeNewlinesInFixedSource) {
fixedTreeDescription =
fixedTreeDescription
.replacingOccurrences(of: "\r\n", with: "\n")
.replacingOccurrences(of: "\r", with: "\n")
}
assertStringsEqualWithDiff(
fixedTreeDescription.trimmingTrailingWhitespace(),
expectedFixedSource.trimmingTrailingWhitespace(),
"Applying all Fix-Its didn’t produce the expected fixed source",
file: file,
line: line
)
}
if expectedDiagnostics.allSatisfy({ $0.fixIts.isEmpty }) && expectedFixedSource != nil {
XCTFail(
"Fixed source was provided but the test case produces no diagnostics with Fix-Its",
file: file,
line: line
)
}
if expectedDiagnostics.isEmpty && diags.isEmpty {
assertBasicFormat(
source: source,
parse: parse,
swiftVersion: swiftVersion,
experimentalFeatures: experimentalFeatures,
file: file,
line: line
)
}
if !longTestsDisabled {
DispatchQueue.concurrentPerform(iterations: Array(tree.tokens(viewMode: .all)).count) { tokenIndex in
let flippedTokenTree = TokenPresenceFlipper(flipTokenAtIndex: tokenIndex).rewrite(Syntax(tree))
_ = ParseDiagnosticsGenerator.diagnostics(for: flippedTokenTree)
Self.assertMutationRoundTrip(
source: flippedTokenTree.syntaxTextBytes,
parse,
swiftVersion: swiftVersion,
experimentalFeatures: experimentalFeatures,
file: file,
line: line
)
}
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
let mutations: [(offset: Int, replacement: TokenSpec)] = parser.alternativeTokenChoices.flatMap {
offset,
replacements in
return replacements.map { (offset, $0) }
}
DispatchQueue.concurrentPerform(iterations: mutations.count) { index in
let mutation = mutations[index]
let alternateSource = MutatedTreePrinter.print(
tree: Syntax(tree),
mutations: [mutation.offset: mutation.replacement]
)
Self.assertMutationRoundTrip(
source: alternateSource,
parse,
swiftVersion: swiftVersion,
experimentalFeatures: experimentalFeatures,
file: file,
line: line
)
}
#endif
}
}
}
/// Removes trivia from all tokens that don’t occur inside multiline string
/// literals.
///
/// We keep trivia inside multiline string literals because the indentation of
/// the closing quote of a multi-line string literals has impact on how much
/// leading trivia is stripped from the literal’s content.
class TriviaRemover: SyntaxRewriter {
override func visit(_ node: StringLiteralExprSyntax) -> ExprSyntax {
if node.openingQuote.tokenKind == .multilineStringQuote {
return ExprSyntax(node)
} else {
return super.visit(node)
}
}
override func visit(_ node: SimpleStringLiteralExprSyntax) -> ExprSyntax {
if node.openingQuote.tokenKind == .multilineStringQuote {
return ExprSyntax(node)
} else {
return super.visit(node)
}
}
override func visit(_ token: TokenSyntax) -> TokenSyntax {
return token.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
}
}
func assertBasicFormat<S: SyntaxProtocol>(
source: String,
parse: (inout Parser) -> S,
swiftVersion: Parser.SwiftVersion?,
experimentalFeatures: Parser.ExperimentalFeatures,
file: StaticString = #filePath,
line: UInt = #line
) {
var parser = Parser(source, swiftVersion: swiftVersion, experimentalFeatures: experimentalFeatures)
let sourceTree = parse(&parser)
let withoutTrivia = TriviaRemover(viewMode: .sourceAccurate).rewrite(sourceTree)
let formatted = withoutTrivia.formatted()
var formattedParser = Parser(
formatted.description,
swiftVersion: swiftVersion,
experimentalFeatures: experimentalFeatures
)
let formattedReparsed = Syntax(parse(&formattedParser))
do {
let subtreeMatcher = SubtreeMatcher(formattedReparsed, markers: [:])
try subtreeMatcher.assertSameStructure(
sourceTree,
includeTrivia: false,
additionalInfo: """
Removing trivia, formatting using BasicFormat and re-parsing did not produce the same syntax tree.
Formatted source:
\(formatted)
""",
file: file,
line: line
)
} catch {
XCTFail("Matching for a subtree failed with error: \(error)", file: file, line: line)
}
}
|