Skip to content

Commit a394d49

Browse files
authored
Merge pull request swiftlang#1905 from StevenWong12/enable_skipping_tests
Enable some skipped tests in `IncrementalParsingTests.swift`
2 parents efb05b0 + 8b77cb6 commit a394d49

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
@@ -164,7 +164,7 @@ public class IncrementalParsingTests: XCTestCase {
164164
)
165165
}
166166

167-
public func testMultiEditMapping() throws {
167+
public func testMultiEditMapping() {
168168
assertIncrementalParse(
169169
"""
170170
let one: Int;let two: Int; let three: Int; ⏩️⏸️ ⏪️⏩️⏸️ ⏪️let found: Int;let five: Int;
@@ -199,8 +199,7 @@ public class IncrementalParsingTests: XCTestCase {
199199
)
200200
}
201201

202-
public func testWarpInClass() throws {
203-
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
202+
public func testWarpInClass() {
204203
assertIncrementalParse(
205204
"""
206205
⏩️⏸️class Foo {⏪️
@@ -215,26 +214,21 @@ public class IncrementalParsingTests: XCTestCase {
215214
reusedNodes: [
216215
ReusedNodeSpec(
217216
"""
218-
func foo1() {
219-
print("Hello Foo!")
220-
}
217+
print("Hello Foo!")
221218
""",
222-
kind: .functionDecl
219+
kind: .codeBlockItem
223220
),
224221
ReusedNodeSpec(
225222
"""
226-
func foo2() {
227-
print("Hello again!")
228-
}
223+
print("Hello again")
229224
""",
230-
kind: .functionDecl
225+
kind: .codeBlockItem
231226
),
232227
]
233228
)
234229
}
235230

236-
public func testUnwarpClass() throws {
237-
try XCTSkipIf(true, "Swift parser does not handle node reuse yet")
231+
public func testUnwarpClass() {
238232
assertIncrementalParse(
239233
"""
240234
⏩️class Bar {⏸️⏪️
@@ -252,21 +246,27 @@ public class IncrementalParsingTests: XCTestCase {
252246
reusedNodes: [
253247
ReusedNodeSpec(
254248
"""
255-
func bar1() {
256-
let pi = 3.1415
257-
print("Pi is (approximately) \\(pi)")
258-
}
249+
let pi = 3.1415
259250
""",
260-
kind: .functionDecl
251+
kind: .codeBlockItem
261252
),
262253
ReusedNodeSpec(
263254
"""
264-
func bar2() {
265-
print("I can compute Pi as well:")
266-
bar1()
267-
}
255+
print("Pi is (approximately) \\(pi)")
256+
""",
257+
kind: .codeBlockItem
258+
),
259+
ReusedNodeSpec(
260+
"""
261+
print("I can compute Pi as well:")
262+
""",
263+
kind: .codeBlockItem
264+
),
265+
ReusedNodeSpec(
266+
"""
267+
bar1()
268268
""",
269-
kind: .functionDecl
269+
kind: .codeBlockItem
270270
),
271271
]
272272
)

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)