Skip to content

Commit 8b77cb6

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

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ 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`
25+
/// ```
26+
/// func foo() {}
27+
/// foo()
28+
/// ```
29+
/// The `reusedNodes` should be
30+
/// ```
31+
/// [
32+
/// ReusedNodeSpec("func foo() {}", kind: .codeBlockItem),
33+
/// ReusedNodeSpec("foo()", kind: .codeBlockItem)
34+
/// ]
35+
/// ```
2236
public func assertIncrementalParse(
2337
_ source: String,
2438
reusedNodes expectedReusedNodes: [ReusedNodeSpec] = [],
2539
file: StaticString = #file,
2640
line: UInt = #line
2741
) {
28-
let (concurrentEdits, originalSource, editedSource) = getEditsAndSources(source)
42+
let (concurrentEdits, originalSource, editedSource) = extractEditsAndSources(from: source)
2943

3044
let originalString = String(originalSource)
3145
let editedString = String(editedSource)
@@ -78,8 +92,9 @@ public func assertIncrementalParse(
7892
return
7993
}
8094

95+
var lastRangeUpperBound = originalString.startIndex
8196
for expectedReusedNode in expectedReusedNodes {
82-
guard let range = getByteSourceRange(for: expectedReusedNode.source, in: originalString) else {
97+
guard let range = byteSourceRange(for: expectedReusedNode.source, in: originalString, after: lastRangeUpperBound) else {
8398
XCTFail("Fail to find string in original source,", file: expectedReusedNode.file, line: expectedReusedNode.line)
8499
continue
85100
}
@@ -105,11 +120,13 @@ public func assertIncrementalParse(
105120
file: expectedReusedNode.file,
106121
line: expectedReusedNode.line
107122
)
123+
124+
lastRangeUpperBound = originalString.index(originalString.startIndex, offsetBy: range.endOffset)
108125
}
109126
}
110127

111-
fileprivate func getByteSourceRange(for substring: String, in sourceString: String) -> ByteSourceRange? {
112-
if let range = sourceString.range(of: substring) {
128+
fileprivate func byteSourceRange(for substring: String, in sourceString: String, after: String.Index) -> ByteSourceRange? {
129+
if let range = sourceString[after...].range(of: substring) {
113130
return ByteSourceRange(
114131
offset: sourceString.utf8.distance(from: sourceString.startIndex, to: range.lowerBound),
115132
length: sourceString.utf8.distance(from: range.lowerBound, to: range.upperBound)
@@ -148,7 +165,7 @@ public struct ReusedNodeSpec {
148165
/// Contents between `⏩️` and `⏸️` are source text that before modification, contents
149166
/// betwwen `⏸️` and `⏪️` are source text that after modification
150167
/// 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) {
168+
public func extractEditsAndSources(from source: String) -> (edits: ConcurrentEdits, orignialSource: Substring, editedSource: Substring) {
152169
var editedSource = Substring()
153170
var originalSource = Substring()
154171
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)