|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | +import SwiftParser |
| 15 | +import XCTest |
| 16 | + |
| 17 | +/// This function is used to verify the correctness of incremental parsing |
| 18 | +/// containing three parts: |
| 19 | +/// 1. Round-trip on the incrementally parsed syntax tree. |
| 20 | +/// 2. verify that incrementally parsing the edited source base on the original source produces the same syntax tree as reparsing the post-edit file from scratch. |
| 21 | +/// 3. verify the reused nodes fall into expectations. |
| 22 | +public func verifyIncrementalParse( |
| 23 | + originalSource: String, |
| 24 | + editedSource: String, |
| 25 | + concurrentEdits: ConcurrentEdits, |
| 26 | + expectedReusedRanges: [(ByteSourceRange, SyntaxKind)] |
| 27 | +) { |
| 28 | + |
| 29 | + let originalTree = Parser.parse(source: originalSource) |
| 30 | + |
| 31 | + let reusedNodesCollector = IncrementalParseReusedNodeCollector() |
| 32 | + let transition = IncrementalParseTransition(previousTree: originalTree, edits: concurrentEdits, reusedNodeDelegate: reusedNodesCollector) |
| 33 | + |
| 34 | + let newTree = Parser.parse(source: editedSource) |
| 35 | + let incrementallyParsedNewTree = Parser.parse(source: editedSource, parseTransition: transition) |
| 36 | + |
| 37 | + // Round-trip |
| 38 | + assertStringsEqualWithDiff(editedSource, "\(incrementallyParsedNewTree)") |
| 39 | + |
| 40 | + // Substructure |
| 41 | + let subtreeMatcher = SubtreeMatcher(Syntax(incrementallyParsedNewTree), markers: [:]) |
| 42 | + do { |
| 43 | + try subtreeMatcher.assertSameStructure(Syntax(newTree)) |
| 44 | + } catch { |
| 45 | + XCTFail("Matching for a subtree failed with error: \(error)") |
| 46 | + } |
| 47 | + |
| 48 | + // Re-used nodes |
| 49 | + // The range of node should ignore Trivia |
| 50 | + if expectedReusedRanges.isEmpty { |
| 51 | + XCTAssert(reusedNodesCollector.rangeAndNodes.isEmpty) |
| 52 | + } else { |
| 53 | + XCTAssertEqual(reusedNodesCollector.rangeAndNodes.count, expectedReusedRanges.count) |
| 54 | + |
| 55 | + for (_, node) in reusedNodesCollector.rangeAndNodes { |
| 56 | + do { |
| 57 | + let range = ByteSourceRange(offset: node.positionAfterSkippingLeadingTrivia.utf8Offset, length: node.byteSizeAfterTrimmingTrivia) |
| 58 | + let targetKind = try XCTUnwrap(expectedReusedRanges.first(where: { $0.0 == range })).1 |
| 59 | + XCTAssertEqual(targetKind, node.kind) |
| 60 | + } catch { |
| 61 | + XCTFail("Fail to find expected reused range") |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Extract the markers in source string for incremental parse and return the information |
| 68 | +/// needed for test |
| 69 | +/// |
| 70 | +/// `➡️` marks the start of reused range and `⬅️` marks the end of reused range |
| 71 | +/// e.g. `class foo {} ➡️struct bar {}⬅️`, the reused range is `struct bar {}` |
| 72 | +/// |
| 73 | +/// - Returns: |
| 74 | +/// - reusedRange: Expected reused ranges in source, sorted by their offset |
| 75 | +/// - originalSource, editedSource, edits: Please refer to `getEditsAndSources` |
| 76 | +public func extractMarkersForIncrementParseTest(_ markedSource: String) -> (originalSource: String, editedSource: String, edits: ConcurrentEdits, reusedRange: [ByteSourceRange]) { |
| 77 | + var concurrentEdits: [SourceEdit] = [] |
| 78 | + var originalSubStrings: [Substring] = [] |
| 79 | + var editedSubStrings: [Substring] = [] |
| 80 | + var reusedRange: [ByteSourceRange] = [] |
| 81 | + |
| 82 | + func extractEditsAndSources(_ subString: Substring) { |
| 83 | + let (edits, originalSource, editedSource) = getEditsAndSources(String(subString)) |
| 84 | + |
| 85 | + concurrentEdits.append( |
| 86 | + contentsOf: |
| 87 | + edits.edits.map { |
| 88 | + return SourceEdit( |
| 89 | + offset: originalSubStrings.map({ $0.utf8.count }).reduce(0, +) + $0.offset, |
| 90 | + length: $0.length, |
| 91 | + replacementLength: $0.replacementLength |
| 92 | + ) |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + originalSubStrings.append(originalSource) |
| 97 | + editedSubStrings.append(editedSource) |
| 98 | + } |
| 99 | + |
| 100 | + var lastStartIndex = markedSource.startIndex |
| 101 | + while let startIndex = markedSource[lastStartIndex...].firstIndex(of: "➡️"), |
| 102 | + let endIndex = markedSource[startIndex...].firstIndex(of: "⬅️") |
| 103 | + { |
| 104 | + |
| 105 | + extractEditsAndSources(markedSource[lastStartIndex..<startIndex]) |
| 106 | + |
| 107 | + reusedRange.append( |
| 108 | + ByteSourceRange( |
| 109 | + offset: originalSubStrings.map({ $0.utf8.count }).reduce(0, +), |
| 110 | + length: markedSource.utf8.distance(from: markedSource.index(after: startIndex), to: endIndex) |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + extractEditsAndSources(markedSource[markedSource.index(after: startIndex)..<endIndex]) |
| 115 | + |
| 116 | + lastStartIndex = markedSource.index(after: endIndex) |
| 117 | + } |
| 118 | + |
| 119 | + extractEditsAndSources(markedSource[lastStartIndex...]) |
| 120 | + |
| 121 | + do { |
| 122 | + let edits = try ConcurrentEdits(concurrent: concurrentEdits) |
| 123 | + return (originalSubStrings.joined(), editedSubStrings.joined(), edits, reusedRange.sorted(by: { $0.offset < $1.offset })) |
| 124 | + } catch { |
| 125 | + fatalError("ConcurrentEdits created by the test case do not satisfy ConcurrentEdits requirements, please check the test setup") |
| 126 | + } |
| 127 | +} |
0 commit comments