Skip to content

Enable some skipped tests in IncrementalParsingTests.swift #1905

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 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
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
27 changes: 22 additions & 5 deletions Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,27 @@ import XCTest
/// 1. Round-trip on the incrementally parsed syntax tree.
/// 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.
/// 3. verify the reused nodes fall into expectations.
///
/// - parameter reusedNodes: The element order should respect to the order of `ReusedNodeSpec.source` in `source`.
/// e.g. for `source`
/// ```
/// func foo() {}
/// foo()
/// ```
/// The `reusedNodes` should be
/// ```
/// [
/// ReusedNodeSpec("func foo() {}", kind: .codeBlockItem),
/// ReusedNodeSpec("foo()", kind: .codeBlockItem)
/// ]
/// ```
public func assertIncrementalParse(
_ source: String,
reusedNodes expectedReusedNodes: [ReusedNodeSpec] = [],
file: StaticString = #file,
line: UInt = #line
) {
let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)

let originalString = String(originalSource)
let editedString = String(editedSource)
Expand Down Expand Up @@ -78,8 +92,9 @@ public func assertIncrementalParse(
return
}

var lastRangeUpperBound = originalString.startIndex
for expectedReusedNode in expectedReusedNodes {
guard let range = getByteSourceRange(for: expectedReusedNode.source, in: originalString) else {
guard let range = byteSourceRange(for: expectedReusedNode.source, in: originalString, after: lastRangeUpperBound) else {
XCTFail("Fail to find string in original source,", file: expectedReusedNode.file, line: expectedReusedNode.line)
continue
}
Expand All @@ -105,11 +120,13 @@ public func assertIncrementalParse(
file: expectedReusedNode.file,
line: expectedReusedNode.line
)

lastRangeUpperBound = originalString.index(originalString.startIndex, offsetBy: range.endOffset)
}
}

fileprivate func getByteSourceRange(for substring: String, in sourceString: String) -> ByteSourceRange? {
if let range = sourceString.range(of: substring) {
fileprivate func byteSourceRange(for substring: String, in sourceString: String, after: String.Index) -> ByteSourceRange? {
if let range = sourceString[after...].range(of: substring) {
return ByteSourceRange(
offset: sourceString.utf8.distance(from: sourceString.startIndex, to: range.lowerBound),
length: sourceString.utf8.distance(from: range.lowerBound, to: range.upperBound)
Expand Down Expand Up @@ -148,7 +165,7 @@ public struct ReusedNodeSpec {
/// Contents between `⏩️` and `⏸️` are source text that before modification, contents
/// betwwen `⏸️` and `⏪️` are source text that after modification
/// i.e. `⏩️foo⏸️bar⏪️`, the original source is `foo` and the edited source is `bar`
public func getEditsAndSources(_ source: String) -> (edits: ConcurrentEdits, orignialSource: Substring, editedSource: Substring) {
public func extractEditsAndSources(from source: String) -> (edits: ConcurrentEdits, orignialSource: Substring, editedSource: Substring) {
var editedSource = Substring()
var originalSource = Substring()
var concurrentEdits: [IncrementalEdit] = []
Expand Down
46 changes: 23 additions & 23 deletions Tests/SwiftParserTest/IncrementalParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public class IncrementalParsingTests: XCTestCase {
)
}

public func testMultiEditMapping() throws {
public func testMultiEditMapping() {
assertIncrementalParse(
"""
let one: Int;let two: Int; let three: Int; ⏩️⏸️ ⏪️⏩️⏸️ ⏪️let found: Int;let five: Int;
Expand Down Expand Up @@ -197,8 +197,7 @@ public class IncrementalParsingTests: XCTestCase {
)
}

public func testWarpInClass() throws {
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
public func testWarpInClass() {
assertIncrementalParse(
"""
⏩️⏸️class Foo {⏪️
Expand All @@ -213,26 +212,21 @@ public class IncrementalParsingTests: XCTestCase {
reusedNodes: [
ReusedNodeSpec(
"""
func foo1() {
print("Hello Foo!")
}
print("Hello Foo!")
""",
kind: .functionDecl
kind: .codeBlockItem
),
ReusedNodeSpec(
"""
func foo2() {
print("Hello again!")
}
print("Hello again")
""",
kind: .functionDecl
kind: .codeBlockItem
),
]
)
}

public func testUnwarpClass() throws {
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
public func testUnwarpClass() {
assertIncrementalParse(
"""
⏩️class Bar {⏸️⏪️
Expand All @@ -250,21 +244,27 @@ public class IncrementalParsingTests: XCTestCase {
reusedNodes: [
ReusedNodeSpec(
"""
func bar1() {
let pi = 3.1415
print("Pi is (approximately) \\(pi)")
}
let pi = 3.1415
""",
kind: .functionDecl
kind: .codeBlockItem
),
ReusedNodeSpec(
"""
func bar2() {
print("I can compute Pi as well:")
bar1()
}
print("Pi is (approximately) \\(pi)")
""",
kind: .functionDecl
kind: .codeBlockItem
),
ReusedNodeSpec(
"""
print("I can compute Pi as well:")
""",
kind: .codeBlockItem
),
ReusedNodeSpec(
"""
bar1()
""",
kind: .codeBlockItem
),
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class IncrementalParseUtilTest: XCTestCase {
}
"""

let (concurrentEdits, originalSource, _) = getEditsAndSources(source)
let (concurrentEdits, originalSource, _) = extractEditsAndSources(from: source)

XCTAssertEqual(
concurrentEdits.edits,
Expand Down Expand Up @@ -57,7 +57,7 @@ public class IncrementalParseUtilTest: XCTestCase {
public func testReplaceMultiByteCharWithShorter() {
let source = "⏩️👨‍👩‍👧‍👦⏸️🎉⏪️"

let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)

XCTAssertEqual(String(originalSource), "👨‍👩‍👧‍👦")
XCTAssertEqual(String(editedSource), "🎉")
Expand All @@ -72,7 +72,7 @@ public class IncrementalParseUtilTest: XCTestCase {
public func testReplaceWithMultiByteChar() {
let source = "⏩️a⏸️👨‍👩‍👧‍👦⏪️"

let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)

XCTAssertEqual(String(originalSource), "a")
XCTAssertEqual(String(editedSource), "👨‍👩‍👧‍👦")
Expand Down