Skip to content

Commit 08bbf70

Browse files
authored
Merge pull request swiftlang#100 from akyrtzi/incremental-parse-test-cases
[tests] Add the incremental re-parsing lit tests from the Swift repo
2 parents fc36c46 + 47728be commit 08bbf70

12 files changed

+197
-53
lines changed

β€ŽSources/SwiftSyntax/IncrementalParseTransition.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public final class IncrementalParseTransition {
7575
/// 1. not be overlapping.
7676
/// 2. should be in increasing source offset order.
7777
public static func isEditArrayValid(_ edits: [SourceEdit]) -> Bool {
78+
// Not quite sure if we should disallow creating an `IncrementalParseTransition`
79+
// object without edits but there doesn't seem to be much benefit if we do,
80+
// and there are 'lit' tests that want to test incremental re-parsing without edits.
81+
guard !edits.isEmpty else { return true }
82+
7883
for i in 1..<edits.count {
7984
let prevEdit = edits[i-1]
8085
let curEdit = edits[i]

β€ŽSources/lit-test-helper/main.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extension CommandLineArguments {
7474
let regex = try NSRegularExpression(
7575
pattern: "([0-9]+):([0-9]+)-([0-9]+):([0-9]+)=(.*)")
7676
var parsedEdits = [IncrementalEdit]()
77-
let editArgs = try self.getRequiredValues("-incremental-edit")
77+
let editArgs = try self.getValues("-incremental-edit")
7878
for edit in editArgs {
7979
guard let match =
8080
regex.firstMatch(in: edit,
@@ -300,7 +300,7 @@ func performParseIncremental(args: CommandLineArguments) throws {
300300
if !expectedReparseRegions.isEmpty {
301301
try verifyReusedRegions(expectedReparseRegions: expectedReparseRegions,
302302
reusedRegions: regions,
303-
sourceURL: preEditURL)
303+
source: postEditText)
304304
}
305305
}
306306

@@ -318,8 +318,7 @@ enum TestingError: Error, CustomStringConvertible {
318318

319319
func verifyReusedRegions(expectedReparseRegions: [SourceRegion],
320320
reusedRegions: [ByteSourceRange],
321-
sourceURL: URL) throws {
322-
let text = try String(contentsOf: sourceURL)
321+
source text: String) throws {
323322
let fileLength = text.utf8.count
324323

325324
// Compute the repared regions by inverting the reused regions
@@ -416,6 +415,6 @@ do {
416415
exit(0)
417416
} catch {
418417
printerr("\(error)")
419-
printerr("Run swift-swiftsyntax-test -help for more help.")
418+
printerr("Run lit-test-helper -help for more help.")
420419
exit(1)
421420
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case ADD_ELSE
3+
4+
func container() {
5+
#if false
6+
<<ADD_ELSE<|||#else>>>
7+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case INSERT_SPACE
3+
4+
class AnimationType {
5+
func foo(x: Blah) {
6+
switch x {
7+
case (.
8+
9+
extension AnimationType {
10+
public<<INSERT_SPACE<||| >>>

β€Žlit_tests/incrParse/funcs.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case NO_CHANGES
3+
// RUN: %validate-incrparse %s --test-case ADD_FUNC_PARENS
4+
// RUN: %validate-incrparse %s --test-case ADD_OPENING_BRACE
5+
// RUN: %validate-incrparse %s --test-case REMOVE_FUNC_KEYWORD
6+
// RUN: %validate-incrparse %s --test-case ADD_PARAM_NAME
7+
// RUN: %validate-incrparse %s --test-case ADD_PARAM_TYPE
8+
9+
func start() {}
10+
11+
class Bar
12+
13+
let y = 1
14+
15+
class InvalidFuncDecls {
16+
func parensAdded<<ADD_FUNC_PARENS<|||()>>> {
17+
}
18+
19+
func openingBraceAdded() <<ADD_OPENING_BRACE|||{>>>
20+
21+
func closingBraceAdded() {
22+
23+
<<ADD_ClOSING_BRACE|||}>>>
24+
25+
<<REMOVE_FUNC_KEYWORD<func|||>>> funcKeywordRemoved() {
26+
27+
}
28+
29+
func addingParamName(<<ADD_PARAM_NAME<|||arg>>>) {
30+
31+
}
32+
33+
func addingParamColon(arg<<ADD_PARAM_COLON<|||:>>>) {
34+
35+
}
36+
37+
func addingParamType(arg:<<ADD_PARAM_TYPE<||| String>>>) {
38+
39+
}
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case STRING
3+
4+
// SR-8995 rdar://problem/45259469
5+
6+
self = <<STRING<|||_ _>>>foo(1)[object1, object2] + o bar(1)

β€Žlit_tests/incrParse/invalid.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case NO_CHANGES
3+
// RUN: %validate-incrparse %s --test-case NESTED_INITIALIZERS
4+
5+
func start() {}
6+
7+
class Bar
8+
9+
let y = 1
10+
11+
class NestedInitializers {
12+
<<NESTED_INITIALIZERS<|||init() {>>>
13+
init() {
14+
15+
}
16+
<<NESTED_INITIALIZERS<|||}>>>
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case MULTI
3+
4+
let one: Int;<reparse MULTI>let two: Int; let three: Int; <<MULTI<||| >>><<MULTI<||| >>>let found: Int;</reparse MULTI>let five: Int;

β€Žlit_tests/incrParse/reuse.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case ADD_PROPERTY
3+
// RUN: %validate-incrparse %s --test-case WRAP_IN_CLASS
4+
// RUN: %validate-incrparse %s --test-case UNWRAP_CLASS
5+
// RUN: %validate-incrparse %s --test-case NEXT_TOKEN_CALCULATION
6+
7+
func start() {}
8+
9+
<reparse ADD_PROPERTY>struct Foo {</reparse ADD_PROPERTY>
10+
let a: Int
11+
let b: Int
12+
let c: Int
13+
<reparse ADD_PROPERTY>
14+
let d: String
15+
<<ADD_PROPERTY<|||let e_newProp: String>>>
16+
let f: Int
17+
</reparse ADD_PROPERTY>
18+
let g: Int
19+
<reparse ADD_PROPERTY>}</reparse ADD_PROPERTY>
20+
21+
// FIXME: The functions inside the class should not need to get reparsed
22+
<<WRAP_IN_CLASS<|||class Foo {>>>
23+
func foo1() {
24+
print("Hello Foo!")
25+
}
26+
27+
func foo2() {
28+
print("Hello again")
29+
}
30+
31+
<<UNWRAP_CLASS<class Bar {|||>>>
32+
func bar1() {
33+
let pi = 3.1415
34+
print("Pi is (approximately) \(pi)")
35+
}
36+
37+
func bar2() {
38+
print("I can compute Pi as well:")
39+
bar1()
40+
}
41+
}
42+
43+
// The indentation on these lines is important for the test case
44+
let a = "hello"
45+
<reparse NEXT_TOKEN_CALCULATION> let c = "<<NEXT_TOKEN_CALCULATION< |||>>>world"</reparse NEXT_TOKEN_CALCULATION>
46+

β€Žlit_tests/incrParse/simple.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case REPLACE
3+
// RUN: %validate-incrparse %s --test-case REPLACE_BY_LONGER
4+
// RUN: %validate-incrparse %s --test-case REPLACE_BY_SHORTER
5+
// RUN: %validate-incrparse %s --test-case INSERT
6+
// RUN: %validate-incrparse %s --test-case REMOVE
7+
// RUN: %validate-incrparse %s --test-case ATTACH_TO_PREV_NODE
8+
// RUN: %validate-incrparse %s --test-case CLASS_SURROUNDING
9+
// RUN: %validate-incrparse %s --test-case MULTI_EDIT
10+
// RUN: %validate-incrparse %s --test-case MULTI_EDIT_SAME_LINE
11+
// RUN: %validate-incrparse %s --test-case REPLACE_WITH_MULTI_BYTE_CHAR
12+
// RUN: %validate-incrparse %s --test-case REPLACE_MULTI_BYTE_CHAR_WITH_SHORTER
13+
// RUN: %validate-incrparse %s --test-case LAST_CHARACTER_OF_STRUCT
14+
// RUN: %validate-incrparse %s --test-case ADD_ARRAY_CLOSE_BRACKET
15+
// RUN: %validate-incrparse %s --test-case ADD_IF_OPEN_BRACE
16+
// RUN: %validate-incrparse %s --test-case EXTEND_IDENTIFIER
17+
18+
func start() {}
19+
20+
<reparse REPLACE>
21+
func foo() {
22+
}
23+
24+
_ = <<REPLACE<6|||7>>>
25+
_ = <<REPLACE_BY_LONGER<6|||"Hello World">>></reparse REPLACE>
26+
_ = <<REPLACE_BY_SHORTER<"Hello again"|||"a">>>
27+
<<INSERT<|||foo()>>>
28+
<<REMOVE<print("abc")|||>>>
29+
foo()
30+
<<ATTACH_TO_PREV_NODE<|||{}>>>
31+
_ = 1
32+
33+
<<CLASS_SURROUNDING<|||class C {>>>
34+
func method1() {}
35+
36+
<<MULTI_EDIT<|||class C {>>>
37+
func method1() {}
38+
<<MULTI_EDIT<|||}>>>
39+
40+
<<MULTI_EDIT_SAME_LINE<_|||let x>>> = <<MULTI_EDIT_SAME_LINE<1|||"hi">>>
41+
42+
let x = "<<REPLACE_WITH_MULTI_BYTE_CHAR<a|||πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦>>>"
43+
let x = "<<REPLACE_MULTI_BYTE_CHAR_WITH_SHORTER<πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦|||πŸŽ‰>>>"
44+
45+
private struc<<LAST_CHARACTER_OF_STRUCT<|||t>>> MyStruct {
46+
}
47+
48+
var computedVar: [Int] {
49+
return [1
50+
<<ADD_ARRAY_CLOSE_BRACKET<|||]>>>
51+
}
52+
53+
if true <<ADD_IF_OPEN_BRACE<|||{>>>
54+
_ = 5
55+
}
56+
57+
let y<<EXTEND_IDENTIFIER<|||ou>>> = 42

β€Žlit_tests/lit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ config.lit_test_helper = inferSwiftBinary('lit-test-helper')
5757

5858
config.substitutions.append(('%empty-directory\(([^)]+)\)', 'rm -rf \"\\1\" && mkdir -p \"\\1\"'))
5959
config.substitutions.append(('%FileCheck', config.filecheck))
60-
config.substitutions.append(('%incr-transfer-roundtrip', '%s --temp-dir %%t --swiftsyntax-lit-test-helper %s' %
60+
config.substitutions.append(('%validate-incrparse', '%s --temp-dir %%t --swiftsyntax-lit-test-helper %s' %
6161
(config.incr_transfer_round_trip, config.lit_test_helper)))
6262
config.substitutions.append(('%lit-test-helper', config.lit_test_helper))

β€Žlit_tests/simple.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
Β (0)