Skip to content

Commit 2c48e26

Browse files
author
Andy
authored
Merge pull request #13568 from Microsoft/fourslash
Simplify fourslash tests by adding some helpers
2 parents d36cd9b + 9665f25 commit 2c48e26

File tree

195 files changed

+482
-1301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+482
-1301
lines changed

src/harness/fourslash.ts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ namespace FourSlash {
372372
}
373373

374374
// Entry points from fourslash.ts
375-
public goToMarker(name = "") {
376-
const marker = this.getMarkerByName(name);
375+
public goToMarker(name: string | Marker = "") {
376+
const marker = typeof name === "string" ? this.getMarkerByName(name) : name;
377377
if (this.activeFile.fileName !== marker.fileName) {
378378
this.openFile(marker.fileName);
379379
}
@@ -382,10 +382,37 @@ namespace FourSlash {
382382
if (marker.position === -1 || marker.position > content.length) {
383383
throw new Error(`Marker "${name}" has been invalidated by unrecoverable edits to the file.`);
384384
}
385-
this.lastKnownMarker = name;
385+
const mName = typeof name === "string" ? name : this.markerName(marker);
386+
this.lastKnownMarker = mName;
386387
this.goToPosition(marker.position);
387388
}
388389

390+
public goToEachMarker(action: () => void) {
391+
const markers = this.getMarkers();
392+
assert(markers.length);
393+
for (const marker of markers) {
394+
this.goToMarker(marker);
395+
action();
396+
}
397+
}
398+
399+
public goToEachRange(action: () => void) {
400+
const ranges = this.getRanges();
401+
assert(ranges.length);
402+
for (const range of ranges) {
403+
this.goToRangeStart(range);
404+
action();
405+
}
406+
}
407+
408+
private markerName(m: Marker): string {
409+
return ts.forEachEntry(this.testData.markerPositions, (marker, name) => {
410+
if (marker === m) {
411+
return name;
412+
}
413+
})!;
414+
}
415+
389416
public goToPosition(pos: number) {
390417
this.currentCaretPosition = pos;
391418
}
@@ -1668,7 +1695,7 @@ namespace FourSlash {
16681695
this.goToPosition(len);
16691696
}
16701697

1671-
private goToRangeStart({fileName, start}: Range) {
1698+
public goToRangeStart({fileName, start}: Range) {
16721699
this.openFile(fileName);
16731700
this.goToPosition(start);
16741701
}
@@ -2365,6 +2392,21 @@ namespace FourSlash {
23652392
return this.languageService.getDocumentHighlights(this.activeFile.fileName, this.currentCaretPosition, filesToSearch);
23662393
}
23672394

2395+
public verifyRangesAreOccurrences(isWriteAccess?: boolean) {
2396+
const ranges = this.getRanges();
2397+
for (const r of ranges) {
2398+
this.goToRangeStart(r);
2399+
this.verifyOccurrencesAtPositionListCount(ranges.length);
2400+
for (const range of ranges) {
2401+
this.verifyOccurrencesAtPositionListContains(range.fileName, range.start, range.end, isWriteAccess);
2402+
}
2403+
}
2404+
}
2405+
2406+
public verifyRangesAreRenameLocations(findInStrings: boolean, findInComments: boolean) {
2407+
this.goToEachRange(() => this.verifyRenameLocations(findInStrings, findInComments));
2408+
}
2409+
23682410
public verifyRangesWithSameTextAreDocumentHighlights() {
23692411
this.rangesByText().forEach(ranges => this.verifyRangesAreDocumentHighlights(ranges));
23702412
}
@@ -3078,10 +3120,22 @@ namespace FourSlashInterface {
30783120
// Moves the caret to the specified marker,
30793121
// or the anonymous marker ('/**/') if no name
30803122
// is given
3081-
public marker(name?: string) {
3123+
public marker(name?: string | FourSlash.Marker) {
30823124
this.state.goToMarker(name);
30833125
}
30843126

3127+
public eachMarker(action: () => void) {
3128+
this.state.goToEachMarker(action);
3129+
}
3130+
3131+
public rangeStart(range: FourSlash.Range) {
3132+
this.state.goToRangeStart(range);
3133+
}
3134+
3135+
public eachRange(action: () => void) {
3136+
this.state.goToEachRange(action);
3137+
}
3138+
30853139
public bof() {
30863140
this.state.goToBOF();
30873141
}
@@ -3432,6 +3486,14 @@ namespace FourSlashInterface {
34323486
this.state.verifyOccurrencesAtPositionListCount(expectedCount);
34333487
}
34343488

3489+
public rangesAreOccurrences(isWriteAccess?: boolean) {
3490+
this.state.verifyRangesAreOccurrences(isWriteAccess);
3491+
}
3492+
3493+
public rangesAreRenameLocations(findInStrings = false, findInComments = false) {
3494+
this.state.verifyRangesAreRenameLocations(findInStrings, findInComments);
3495+
}
3496+
34353497
public rangesAreDocumentHighlights(ranges?: FourSlash.Range[]) {
34363498
this.state.verifyRangesAreDocumentHighlights(ranges);
34373499
}

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_Generics.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@
1111

1212
////function A</*genericName5*/
1313

14-
15-
test.markers().forEach((m) => {
16-
goTo.position(m.position, m.fileName);
17-
verify.completionListIsEmpty();
18-
});
14+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_catch.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,4 @@
66

77
//// try {} catch(a/*catchVariable2*/
88

9-
10-
test.markers().forEach((m) => {
11-
goTo.position(m.position, m.fileName);
12-
verify.completionListIsEmpty();
13-
});
9+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_classes.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@
66

77
////class a/*className2*/
88

9-
test.markers().forEach((m) => {
10-
goTo.position(m.position, m.fileName);
11-
verify.completionListIsEmpty();
12-
});
9+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_destructuring.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,4 @@
1616

1717
//// function func2({ a, b/*parameter2*/
1818

19-
test.markers().forEach(m => {
20-
goTo.position(m.position, m.fileName);
21-
verify.completionListIsEmpty();
22-
});
19+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_enumMembers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44

55
////enum a { /*enumValueName1*/
66

7-
8-
test.markers().forEach((m) => {
9-
goTo.position(m.position, m.fileName);
10-
verify.completionListIsEmpty();
11-
});
7+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_enumMembers2.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@
33
////var aa = 1;
44
////enum a { foo, /*enumValueName3*/
55

6-
test.markers().forEach((m) => {
7-
goTo.position(m.position, m.fileName);
8-
verify.completionListIsEmpty();
9-
});
6+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_enums.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@
88

99
////var x = 0; enum /*enumName4*/
1010

11-
test.markers().forEach((m) => {
12-
goTo.position(m.position, m.fileName);
13-
verify.completionListIsEmpty();
14-
});
11+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_functions.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,4 @@
66

77
////function a/*functionName2*/
88

9-
10-
test.markers().forEach((m) => {
11-
goTo.position(m.position, m.fileName);
12-
verify.completionListIsEmpty();
13-
});
9+
goTo.eachMarker(() => verify.completionListIsEmpty());

tests/cases/fourslash/completionListAtIdentifierDefinitionLocations_interfaceMembers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@
44

55
////interface a { /*interfaceValue1*/
66

7-
test.markers().forEach((m) => {
8-
goTo.position(m.position, m.fileName);
9-
verify.completionListIsEmpty();
10-
});
7+
goTo.eachMarker(() => verify.completionListIsEmpty());

0 commit comments

Comments
 (0)