Skip to content

Commit 37f8eac

Browse files
author
Andy Hanson
committed
Simplify go-to-definition tests
1 parent 3bcfb6b commit 37f8eac

File tree

82 files changed

+259
-647
lines changed

Some content is hidden

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

82 files changed

+259
-647
lines changed

src/harness/fourslash.ts

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,56 @@ namespace FourSlash {
525525
}
526526
}
527527

528+
public verifyGoToDefinitionIs(endMarker: string | string[]) {
529+
this.verifyGoToDefinitionWorker(endMarker instanceof Array ? endMarker : [endMarker]);
530+
}
531+
532+
public verifyGoToDefinition(startsAndEnds: (string | string[])[]) {
533+
if (startsAndEnds.length % 2) {
534+
throw new Error("verify.goToDefinition needs an even number of arguments.");
535+
}
536+
537+
for (let i = 0; i < startsAndEnds.length; i += 2) {
538+
const start = startsAndEnds[i];
539+
const end = startsAndEnds[i + 1];
540+
541+
if (start instanceof Array) {
542+
for (const s of start) {
543+
this.verifyGoToDefinitionSingle(s, end);
544+
}
545+
}
546+
else {
547+
this.verifyGoToDefinitionSingle(start, end);
548+
}
549+
}
550+
}
551+
552+
public verifyGoToDefinitionForMarkers(markerNames: string[]) {
553+
for (const markerName of markerNames) {
554+
this.verifyGoToDefinitionSingle(`${markerName}Reference`, `${markerName}Definition`);
555+
}
556+
}
557+
558+
private verifyGoToDefinitionSingle(start: string, end: string | string[]) {
559+
this.goToMarker(start);
560+
this.verifyGoToDefinitionWorker(end instanceof Array ? end : [end]);
561+
}
562+
563+
private verifyGoToDefinitionWorker(endMarkers: string[]) {
564+
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition) || [];
565+
566+
if (endMarkers.length !== definitions.length) {
567+
this.raiseError(`goToDefinitions failed - expected to find ${endMarkers.length} definitions but got ${definitions.length}`);
568+
}
569+
570+
for (let i = 0; i < endMarkers.length; i++) {
571+
const marker = this.getMarkerByName(endMarkers[i]), definition = definitions[i];
572+
if (marker.fileName !== definition.fileName || marker.position !== definition.textSpan.start) {
573+
this.raiseError(`goToDefinition failed for definition ${i}: expected ${marker.fileName} at ${marker.position}, got ${definition.fileName} at ${definition.textSpan.start}`);
574+
}
575+
}
576+
}
577+
528578
public verifyGetEmitOutputForCurrentFile(expected: string): void {
529579
const emit = this.languageService.getEmitOutput(this.activeFile.fileName);
530580
if (emit.outputFiles.length !== 1) {
@@ -1561,21 +1611,6 @@ namespace FourSlash {
15611611
this.goToPosition(len);
15621612
}
15631613

1564-
public goToDefinition(definitionIndex: number) {
1565-
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
1566-
if (!definitions || !definitions.length) {
1567-
this.raiseError("goToDefinition failed - expected to find at least one definition location but got 0");
1568-
}
1569-
1570-
if (definitionIndex >= definitions.length) {
1571-
this.raiseError(`goToDefinition failed - definitionIndex value (${definitionIndex}) exceeds definition list size (${definitions.length})`);
1572-
}
1573-
1574-
const definition = definitions[definitionIndex];
1575-
this.openFile(definition.fileName);
1576-
this.currentCaretPosition = definition.textSpan.start;
1577-
}
1578-
15791614
public goToTypeDefinition(definitionIndex: number) {
15801615
const definitions = this.languageService.getTypeDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
15811616
if (!definitions || !definitions.length) {
@@ -1591,28 +1626,6 @@ namespace FourSlash {
15911626
this.currentCaretPosition = definition.textSpan.start;
15921627
}
15931628

1594-
public verifyDefinitionLocationExists(negative: boolean) {
1595-
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
1596-
1597-
const foundDefinitions = definitions && definitions.length;
1598-
1599-
if (foundDefinitions && negative) {
1600-
this.raiseError(`goToDefinition - expected to 0 definition locations but got ${definitions.length}`);
1601-
}
1602-
else if (!foundDefinitions && !negative) {
1603-
this.raiseError("goToDefinition - expected to find at least one definition location but got 0");
1604-
}
1605-
}
1606-
1607-
public verifyDefinitionsCount(negative: boolean, expectedCount: number) {
1608-
const assertFn = negative ? assert.notEqual : assert.equal;
1609-
1610-
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
1611-
const actualCount = definitions && definitions.length || 0;
1612-
1613-
assertFn(actualCount, expectedCount, this.messageAtLastKnownMarker("Definitions Count"));
1614-
}
1615-
16161629
public verifyTypeDefinitionsCount(negative: boolean, expectedCount: number) {
16171630
const assertFn = negative ? assert.notEqual : assert.equal;
16181631

@@ -1622,25 +1635,23 @@ namespace FourSlash {
16221635
assertFn(actualCount, expectedCount, this.messageAtLastKnownMarker("Type definitions Count"));
16231636
}
16241637

1625-
public verifyDefinitionsName(negative: boolean, expectedName: string, expectedContainerName: string) {
1638+
public verifyGoToDefinitionName(expectedName: string, expectedContainerName: string) {
16261639
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
16271640
const actualDefinitionName = definitions && definitions.length ? definitions[0].name : "";
16281641
const actualDefinitionContainerName = definitions && definitions.length ? definitions[0].containerName : "";
1629-
if (negative) {
1630-
assert.notEqual(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
1631-
assert.notEqual(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
1632-
}
1633-
else {
1634-
assert.equal(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
1635-
assert.equal(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
1636-
}
1642+
assert.equal(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
1643+
assert.equal(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
16371644
}
16381645

16391646
public getMarkers(): Marker[] {
16401647
// Return a copy of the list
16411648
return this.testData.markers.slice(0);
16421649
}
16431650

1651+
public getMarkerNames(): string[] {
1652+
return Object.keys(this.testData.markerPositions);
1653+
}
1654+
16441655
public getRanges(): Range[] {
16451656
return this.testData.ranges;
16461657
}
@@ -2742,6 +2753,10 @@ namespace FourSlashInterface {
27422753
return this.state.getMarkers();
27432754
}
27442755

2756+
public markerNames(): string[] {
2757+
return this.state.getMarkerNames();
2758+
}
2759+
27452760
public marker(name?: string): FourSlash.Marker {
27462761
return this.state.getMarkerByName(name);
27472762
}
@@ -2777,10 +2792,6 @@ namespace FourSlashInterface {
27772792
this.state.goToEOF();
27782793
}
27792794

2780-
public definition(definitionIndex = 0) {
2781-
this.state.goToDefinition(definitionIndex);
2782-
}
2783-
27842795
public type(definitionIndex = 0) {
27852796
this.state.goToTypeDefinition(definitionIndex);
27862797
}
@@ -2885,22 +2896,10 @@ namespace FourSlashInterface {
28852896
this.state.verifyQuickInfoExists(this.negative);
28862897
}
28872898

2888-
public definitionCountIs(expectedCount: number) {
2889-
this.state.verifyDefinitionsCount(this.negative, expectedCount);
2890-
}
2891-
28922899
public typeDefinitionCountIs(expectedCount: number) {
28932900
this.state.verifyTypeDefinitionsCount(this.negative, expectedCount);
28942901
}
28952902

2896-
public definitionLocationExists() {
2897-
this.state.verifyDefinitionLocationExists(this.negative);
2898-
}
2899-
2900-
public verifyDefinitionsName(name: string, containerName: string) {
2901-
this.state.verifyDefinitionsName(this.negative, name, containerName);
2902-
}
2903-
29042903
public isValidBraceCompletionAtPosition(openingBrace: string) {
29052904
this.state.verifyBraceCompletionAtPosition(this.negative, openingBrace);
29062905
}
@@ -2944,6 +2943,22 @@ namespace FourSlashInterface {
29442943
this.state.verifyCurrentFileContent(text);
29452944
}
29462945

2946+
public goToDefinitionIs(endMarkers: string | string[]) {
2947+
this.state.verifyGoToDefinitionIs(endMarkers);
2948+
}
2949+
2950+
public goToDefinition(...startsAndEnds: (string | string[])[]) {
2951+
this.state.verifyGoToDefinition(startsAndEnds);
2952+
}
2953+
2954+
public goToDefinitionForMarkers(...markerNames: string[]) {
2955+
this.state.verifyGoToDefinitionForMarkers(markerNames);
2956+
}
2957+
2958+
public goToDefinitionName(name: string, containerName: string) {
2959+
this.state.verifyGoToDefinitionName(name, containerName);
2960+
}
2961+
29472962
public verifyGetEmitOutputForCurrentFile(expected: string): void {
29482963
this.state.verifyGetEmitOutputForCurrentFile(expected);
29492964
}

tests/cases/fourslash/ambientShorthandGotoDefinition.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,22 @@
1212

1313
goTo.marker("useFoo");
1414
verify.quickInfoIs("import foo");
15-
goTo.definition();
16-
verify.caretAtMarker("importFoo");
17-
goTo.definition();
18-
verify.caretAtMarker("module");
15+
verify.goToDefinition(
16+
"useFoo", "importFoo",
17+
"importFoo", "module");
1918

2019
goTo.marker("useBar");
2120
verify.quickInfoIs("import bar");
22-
goTo.definition();
23-
verify.caretAtMarker("module");
21+
verify.goToDefinition("useBar", "module");
2422

2523
goTo.marker("useBaz");
2624
verify.quickInfoIs("import baz");
27-
goTo.definition();
28-
verify.caretAtMarker("importBaz");
29-
goTo.marker("idBaz");
30-
goTo.definition();
31-
verify.caretAtMarker("module");
25+
verify.goToDefinition(
26+
"useBaz", "importBaz",
27+
"idBaz", "module");
3228

3329
goTo.marker("useBang");
3430
verify.quickInfoIs("import bang = require(\"jquery\")");
35-
goTo.definition();
36-
verify.caretAtMarker("importBang");
37-
goTo.marker("idBang");
38-
goTo.definition();
39-
verify.caretAtMarker("module");
31+
verify.goToDefinition(
32+
"useBang", "importBang",
33+
"idBang", "module");

tests/cases/fourslash/definition.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77
// @Filename: a.ts
88
//// /*2*/export class Foo {}
99

10-
goTo.marker('1');
11-
goTo.definition();
12-
verify.caretAtMarker('2');
10+
verify.goToDefinition("1", "2");

tests/cases/fourslash/definitionNameOnEnumMember.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
////var enumMember = e./*1*/thirdMember;
99

1010
goTo.marker("1");
11-
verify.verifyDefinitionsName("thirdMember", "e");
11+
verify.goToDefinitionName("thirdMember", "e");

tests/cases/fourslash/fourslash.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ declare namespace FourSlashInterface {
9999
}
100100
class test_ {
101101
markers(): Marker[];
102+
markerNames(): string[];
102103
marker(name?: string): Marker;
103104
ranges(): Range[];
104105
rangesByText(): { [text: string]: Range[] };
@@ -108,7 +109,6 @@ declare namespace FourSlashInterface {
108109
marker(name?: string): void;
109110
bof(): void;
110111
eof(): void;
111-
definition(definitionIndex?: number): void;
112112
type(definitionIndex?: number): void;
113113
position(position: number, fileIndex?: number): any;
114114
position(position: number, fileName?: string): any;
@@ -132,10 +132,7 @@ declare namespace FourSlashInterface {
132132
errorExistsBeforeMarker(markerName?: string): void;
133133
quickInfoIs(expectedText?: string, expectedDocumentation?: string): void;
134134
quickInfoExists(): void;
135-
definitionCountIs(expectedCount: number): void;
136135
typeDefinitionCountIs(expectedCount: number): void;
137-
definitionLocationExists(): void;
138-
verifyDefinitionsName(name: string, containerName: string): void;
139136
isValidBraceCompletionAtPosition(openingBrace?: string): void;
140137
}
141138
class verify extends verifyNegatable {
@@ -152,6 +149,18 @@ declare namespace FourSlashInterface {
152149
eval(expr: string, value: any): void;
153150
currentLineContentIs(text: string): void;
154151
currentFileContentIs(text: string): void;
152+
/** Verifies that goToDefinition at the current position would take you to `endMarker`. */
153+
goToDefinitionIs(endMarkers: string | string[]): void;
154+
goToDefinitionName(name: string, containerName: string): void;
155+
/**
156+
* `verify.goToDefinition("a", "b");` verifies that go-to-definition at marker "a" takes you to marker "b".
157+
* `verify.goToDefinition(["a", "aa"], "b");` verifies that markers "a" and "aa" have the same definition "b".
158+
* `verify.goToDefinition("a", ["b", "bb"]);` verifies that "a" has multiple definitions available.
159+
* Finally, `verify.goToDefinition("a", "b", "c", "d");` is just `verify.goToDefinition("a", "b"); verify.goToDefinition("c", "d");`.
160+
*/
161+
goToDefinition(...startsAndEnds: (string | string[])[]): void;
162+
/** Verifies goToDefinition for each `${markerName}Reference` -> `${markerName}Definition` */
163+
goToDefinitionForMarkers(...markerNames: string[]): void;
155164
verifyGetEmitOutputForCurrentFile(expected: string): void;
156165
verifyGetEmitOutputContentsForCurrentFile(expected: ts.OutputFile[]): void;
157166
/**

tests/cases/fourslash/goToDeclarationDecoratorOverloads.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
//// @/*useDecSymbol*/dec [s]() {}
1414
////}
1515

16-
goTo.marker("useDecString");
17-
goTo.definition();
18-
verify.caretAtMarker("defDecString");
19-
20-
goTo.marker("useDecSymbol");
21-
goTo.definition();
22-
verify.caretAtMarker("defDecSymbol");
16+
verify.goToDefinition(
17+
"useDecString", "defDecString",
18+
"useDecSymbol", "defDecSymbol");
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
/// <reference path="fourslash.ts" />
22

33
//@Filename: a.ts
4-
////var x: number;
4+
////var /*def1*/x: number;
55

66
//@Filename: b.ts
7-
////var x: number;
7+
////var /*def2*/x: number;
88

99
//@Filename: c.ts
1010
/////// <reference path="a.ts" />
1111
/////// <reference path="b.ts" />
12-
/////**/x++;
12+
/////*use*/x++;
1313

14-
goTo.file("c.ts");
15-
goTo.marker();
16-
17-
verify.definitionCountIs(2);
14+
verify.goToDefinition("use", ["def1", "def2"]);

tests/cases/fourslash/goToDefinitionAlias.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@
2323
//// x;
2424
////}
2525

26-
27-
goTo.marker('alias1Type');
28-
goTo.definition();
29-
verify.caretAtMarker('alias1Definition');
30-
31-
goTo.marker('alias2Type');
32-
goTo.definition();
33-
verify.caretAtMarker('alias2Definition');
34-
35-
36-
goTo.marker('alias1Value');
37-
goTo.definition();
38-
verify.caretAtMarker('alias1Definition');
39-
40-
goTo.marker('alias2Value');
41-
goTo.definition();
42-
verify.caretAtMarker('alias2Definition');
26+
verify.goToDefinition(
27+
["alias1Type", "alias1Value"], "alias1Definition",
28+
["alias2Type", "alias2Value"], "alias2Definition");

tests/cases/fourslash/goToDefinitionAmbiants.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,4 @@
1414
////ambientClass./*staticMethodReference*/method();
1515
////ambientClassVariable./*instanceMethodReference*/method();
1616

17-
var markerList = [
18-
"ambientVariable",
19-
"ambientFunction",
20-
"constructor",
21-
"staticMethod",
22-
"instanceMethod",
23-
];
24-
25-
markerList.forEach((marker) => {
26-
goTo.marker(marker + 'Reference');
27-
goTo.definition();
28-
verify.caretAtMarker(marker + 'Definition');
29-
});
17+
verify.goToDefinitionForMarkers("ambientVariable", "ambientFunction", "constructor", "staticMethod", "instanceMethod");

0 commit comments

Comments
 (0)