Skip to content

Commit c816a99

Browse files
committed
Enable some skipping tests in IncrementalParsingTests.swift
Also make some naming improvements
1 parent 7ed925d commit c816a99

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ import XCTest
1919
/// 1. Round-trip on the incrementally parsed syntax tree.
2020
/// 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.
2121
/// 3. verify the reused nodes fall into expectations.
22+
///
23+
/// - parameter reusedNodes: The element order should respect to the order of `ReusedNodeSpec.source` in `source`.
24+
/// e.g. for `source` func foo() {}\n foo()
25+
/// The `reusedNodes` should be [
26+
/// ReusedNodeSpec("func foo() {}", kind: .codeBlockItem),
27+
/// ReusedNodeSpec("foo()", kind: .codeBlockItem)
28+
/// ]
2229
public func assertIncrementalParse(
2330
_ source: String,
2431
reusedNodes expectedReusedNodes: [ReusedNodeSpec] = [],
2532
file: StaticString = #file,
2633
line: UInt = #line
2734
) {
28-
let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
35+
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)
2936

3037
let originalString = String(originalSource)
3138
let editedString = String(editedSource)
@@ -78,13 +85,22 @@ public func assertIncrementalParse(
7885
return
7986
}
8087

88+
var lastRangeUpperBound = originalString.startIndex
8189
for expectedReusedNode in expectedReusedNodes {
82-
guard let range = getByteSourceRange(for: expectedReusedNode.source, in: originalString) else {
90+
guard let range = byteSourceRange(for: expectedReusedNode.source, in: originalString[lastRangeUpperBound...]) else {
8391
XCTFail("Fail to find string in original source,", file: expectedReusedNode.file, line: expectedReusedNode.line)
8492
continue
8593
}
8694

87-
guard let reusedNode = reusedNodes.first(where: { $0.byteRangeAfterTrimmingTrivia == range }) else {
95+
guard
96+
let reusedNode = reusedNodes.first(where: {
97+
$0.byteRangeAfterTrimmingTrivia
98+
== ByteSourceRange(
99+
offset: range.offset + originalString.distance(from: originalString.startIndex, to: lastRangeUpperBound),
100+
length: range.length
101+
)
102+
})
103+
else {
88104
XCTFail(
89105
"""
90106
Fail to match the range of \(expectedReusedNode.source) in:
@@ -105,10 +121,12 @@ public func assertIncrementalParse(
105121
file: expectedReusedNode.file,
106122
line: expectedReusedNode.line
107123
)
124+
125+
lastRangeUpperBound = originalString.index(originalString.startIndex, offsetBy: range.endOffset)
108126
}
109127
}
110128

111-
fileprivate func getByteSourceRange(for substring: String, in sourceString: String) -> ByteSourceRange? {
129+
fileprivate func byteSourceRange(for substring: String, in sourceString: Substring) -> ByteSourceRange? {
112130
if let range = sourceString.range(of: substring) {
113131
return ByteSourceRange(
114132
offset: sourceString.utf8.distance(from: sourceString.startIndex, to: range.lowerBound),
@@ -148,7 +166,7 @@ public struct ReusedNodeSpec {
148166
/// Contents between `⏩️` and `⏸️` are source text that before modification, contents
149167
/// betwwen `⏸️` and `⏪️` are source text that after modification
150168
/// i.e. `⏩️foo⏸️bar⏪️`, the original source is `foo` and the edited source is `bar`
151-
public func getEditsAndSources(_ source: String) -> (edits: ConcurrentEdits, orignialSource: Substring, editedSource: Substring) {
169+
public func extractEditsAndSources(from source: String) -> (edits: ConcurrentEdits, orignialSource: Substring, editedSource: Substring) {
152170
var editedSource = Substring()
153171
var originalSource = Substring()
154172
var concurrentEdits: [IncrementalEdit] = []

Tests/SwiftParserTest/IncrementalParsingTests.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public class IncrementalParsingTests: XCTestCase {
161161
)
162162
}
163163

164-
public func testMultiEditMapping() throws {
164+
public func testMultiEditMapping() {
165165
assertIncrementalParse(
166166
"""
167167
let one: Int;let two: Int; let three: Int; ⏩️⏸️ ⏪️⏩️⏸️ ⏪️let found: Int;let five: Int;
@@ -197,8 +197,7 @@ public class IncrementalParsingTests: XCTestCase {
197197
)
198198
}
199199

200-
public func testWarpInClass() throws {
201-
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
200+
public func testWarpInClass() {
202201
assertIncrementalParse(
203202
"""
204203
⏩️⏸️class Foo {⏪️
@@ -213,26 +212,21 @@ public class IncrementalParsingTests: XCTestCase {
213212
reusedNodes: [
214213
ReusedNodeSpec(
215214
"""
216-
func foo1() {
217-
print("Hello Foo!")
218-
}
215+
print("Hello Foo!")
219216
""",
220-
kind: .functionDecl
217+
kind: .codeBlockItem
221218
),
222219
ReusedNodeSpec(
223220
"""
224-
func foo2() {
225-
print("Hello again!")
226-
}
221+
print("Hello again")
227222
""",
228-
kind: .functionDecl
223+
kind: .codeBlockItem
229224
),
230225
]
231226
)
232227
}
233228

234-
public func testUnwarpClass() throws {
235-
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
229+
public func testUnwarpClass() {
236230
assertIncrementalParse(
237231
"""
238232
⏩️class Bar {⏸️⏪️
@@ -250,21 +244,27 @@ public class IncrementalParsingTests: XCTestCase {
250244
reusedNodes: [
251245
ReusedNodeSpec(
252246
"""
253-
func bar1() {
254-
let pi = 3.1415
255-
print("Pi is (approximately) \\(pi)")
256-
}
247+
let pi = 3.1415
257248
""",
258-
kind: .functionDecl
249+
kind: .codeBlockItem
259250
),
260251
ReusedNodeSpec(
261252
"""
262-
func bar2() {
263-
print("I can compute Pi as well:")
264-
bar1()
265-
}
253+
print("Pi is (approximately) \\(pi)")
266254
""",
267-
kind: .functionDecl
255+
kind: .codeBlockItem
256+
),
257+
ReusedNodeSpec(
258+
"""
259+
print("I can compute Pi as well:")
260+
""",
261+
kind: .codeBlockItem
262+
),
263+
ReusedNodeSpec(
264+
"""
265+
bar1()
266+
""",
267+
kind: .codeBlockItem
268268
),
269269
]
270270
)

Tests/SwiftSyntaxTestSupportTest/IncrementalParseTestUtilsTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class IncrementalParseUtilTest: XCTestCase {
2727
}
2828
"""
2929

30-
let (concurrentEdits, originalSource, _) = getEditsAndSources(source)
30+
let (concurrentEdits, originalSource, _) = extractEditsAndSources(from: source)
3131

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

60-
let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
60+
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)
6161

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

75-
let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
75+
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)
7676

7777
XCTAssertEqual(String(originalSource), "a")
7878
XCTAssertEqual(String(editedSource), "👨‍👩‍👧‍👦")

0 commit comments

Comments
 (0)