Skip to content

Commit 32b6746

Browse files
author
Andy
authored
Merge pull request #10686 from Microsoft/go_to_definition_tests
Simplify go-to-definition tests
2 parents 85a13b8 + c75f6d0 commit 32b6746

File tree

82 files changed

+298
-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

+298
-647
lines changed

src/harness/fourslash.ts

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

528+
public verifyGoToDefinitionIs(endMarker: string | string[]) {
529+
this.verifyGoToDefinitionWorker(endMarker instanceof Array ? endMarker : [endMarker]);
530+
}
531+
532+
public verifyGoToDefinition(arg0: any, endMarkerNames?: string | string[]) {
533+
if (endMarkerNames) {
534+
this.verifyGoToDefinitionPlain(arg0, endMarkerNames);
535+
}
536+
else if (arg0 instanceof Array) {
537+
const pairs: [string | string[], string | string[]][] = arg0;
538+
for (const [start, end] of pairs) {
539+
this.verifyGoToDefinitionPlain(start, end);
540+
}
541+
}
542+
else {
543+
const obj: { [startMarkerName: string]: string | string[] } = arg0;
544+
for (const startMarkerName in obj) {
545+
if (ts.hasProperty(obj, startMarkerName)) {
546+
this.verifyGoToDefinitionPlain(startMarkerName, obj[startMarkerName]);
547+
}
548+
}
549+
}
550+
}
551+
552+
private verifyGoToDefinitionPlain(startMarkerNames: string | string[], endMarkerNames: string | string[]) {
553+
if (startMarkerNames instanceof Array) {
554+
for (const start of startMarkerNames) {
555+
this.verifyGoToDefinitionSingle(start, endMarkerNames);
556+
}
557+
}
558+
else {
559+
this.verifyGoToDefinitionSingle(startMarkerNames, endMarkerNames);
560+
}
561+
}
562+
563+
public verifyGoToDefinitionForMarkers(markerNames: string[]) {
564+
for (const markerName of markerNames) {
565+
this.verifyGoToDefinitionSingle(`${markerName}Reference`, `${markerName}Definition`);
566+
}
567+
}
568+
569+
private verifyGoToDefinitionSingle(startMarkerName: string, endMarkerNames: string | string[]) {
570+
this.goToMarker(startMarkerName);
571+
this.verifyGoToDefinitionWorker(endMarkerNames instanceof Array ? endMarkerNames : [endMarkerNames]);
572+
}
573+
574+
private verifyGoToDefinitionWorker(endMarkers: string[]) {
575+
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition) || [];
576+
577+
if (endMarkers.length !== definitions.length) {
578+
this.raiseError(`goToDefinitions failed - expected to find ${endMarkers.length} definitions but got ${definitions.length}`);
579+
}
580+
581+
for (let i = 0; i < endMarkers.length; i++) {
582+
const marker = this.getMarkerByName(endMarkers[i]), definition = definitions[i];
583+
if (marker.fileName !== definition.fileName || marker.position !== definition.textSpan.start) {
584+
this.raiseError(`goToDefinition failed for definition ${i}: expected ${marker.fileName} at ${marker.position}, got ${definition.fileName} at ${definition.textSpan.start}`);
585+
}
586+
}
587+
}
588+
528589
public verifyGetEmitOutputForCurrentFile(expected: string): void {
529590
const emit = this.languageService.getEmitOutput(this.activeFile.fileName);
530591
if (emit.outputFiles.length !== 1) {
@@ -1561,21 +1622,6 @@ namespace FourSlash {
15611622
this.goToPosition(len);
15621623
}
15631624

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-
15791625
public goToTypeDefinition(definitionIndex: number) {
15801626
const definitions = this.languageService.getTypeDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
15811627
if (!definitions || !definitions.length) {
@@ -1591,28 +1637,6 @@ namespace FourSlash {
15911637
this.currentCaretPosition = definition.textSpan.start;
15921638
}
15931639

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-
16161640
public verifyTypeDefinitionsCount(negative: boolean, expectedCount: number) {
16171641
const assertFn = negative ? assert.notEqual : assert.equal;
16181642

@@ -1622,25 +1646,23 @@ namespace FourSlash {
16221646
assertFn(actualCount, expectedCount, this.messageAtLastKnownMarker("Type definitions Count"));
16231647
}
16241648

1625-
public verifyDefinitionsName(negative: boolean, expectedName: string, expectedContainerName: string) {
1649+
public verifyGoToDefinitionName(expectedName: string, expectedContainerName: string) {
16261650
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
16271651
const actualDefinitionName = definitions && definitions.length ? definitions[0].name : "";
16281652
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-
}
1653+
assert.equal(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
1654+
assert.equal(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
16371655
}
16381656

16391657
public getMarkers(): Marker[] {
16401658
// Return a copy of the list
16411659
return this.testData.markers.slice(0);
16421660
}
16431661

1662+
public getMarkerNames(): string[] {
1663+
return Object.keys(this.testData.markerPositions);
1664+
}
1665+
16441666
public getRanges(): Range[] {
16451667
return this.testData.ranges;
16461668
}
@@ -2742,6 +2764,10 @@ namespace FourSlashInterface {
27422764
return this.state.getMarkers();
27432765
}
27442766

2767+
public markerNames(): string[] {
2768+
return this.state.getMarkerNames();
2769+
}
2770+
27452771
public marker(name?: string): FourSlash.Marker {
27462772
return this.state.getMarkerByName(name);
27472773
}
@@ -2777,10 +2803,6 @@ namespace FourSlashInterface {
27772803
this.state.goToEOF();
27782804
}
27792805

2780-
public definition(definitionIndex = 0) {
2781-
this.state.goToDefinition(definitionIndex);
2782-
}
2783-
27842806
public type(definitionIndex = 0) {
27852807
this.state.goToTypeDefinition(definitionIndex);
27862808
}
@@ -2885,22 +2907,10 @@ namespace FourSlashInterface {
28852907
this.state.verifyQuickInfoExists(this.negative);
28862908
}
28872909

2888-
public definitionCountIs(expectedCount: number) {
2889-
this.state.verifyDefinitionsCount(this.negative, expectedCount);
2890-
}
2891-
28922910
public typeDefinitionCountIs(expectedCount: number) {
28932911
this.state.verifyTypeDefinitionsCount(this.negative, expectedCount);
28942912
}
28952913

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-
29042914
public isValidBraceCompletionAtPosition(openingBrace: string) {
29052915
this.state.verifyBraceCompletionAtPosition(this.negative, openingBrace);
29062916
}
@@ -2944,6 +2954,25 @@ namespace FourSlashInterface {
29442954
this.state.verifyCurrentFileContent(text);
29452955
}
29462956

2957+
public goToDefinitionIs(endMarkers: string | string[]) {
2958+
this.state.verifyGoToDefinitionIs(endMarkers);
2959+
}
2960+
2961+
public goToDefinition(startMarkerName: string | string[], endMarkerName: string | string[]): void;
2962+
public goToDefinition(startsAndEnds: [string | string[], string | string[]][]): void;
2963+
public goToDefinition(startsAndEnds: { [startMarkerName: string]: string | string[] }): void;
2964+
public goToDefinition(arg0: any, endMarkerName?: string | string[]) {
2965+
this.state.verifyGoToDefinition(arg0, endMarkerName);
2966+
}
2967+
2968+
public goToDefinitionForMarkers(...markerNames: string[]) {
2969+
this.state.verifyGoToDefinitionForMarkers(markerNames);
2970+
}
2971+
2972+
public goToDefinitionName(name: string, containerName: string) {
2973+
this.state.verifyGoToDefinitionName(name, containerName);
2974+
}
2975+
29472976
public verifyGetEmitOutputForCurrentFile(expected: string): void {
29482977
this.state.verifyGetEmitOutputForCurrentFile(expected);
29492978
}

tests/cases/fourslash/ambientShorthandGotoDefinition.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@
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"
18+
});
1919

2020
goTo.marker("useBar");
2121
verify.quickInfoIs("import bar");
22-
goTo.definition();
23-
verify.caretAtMarker("module");
22+
verify.goToDefinition("useBar", "module");
2423

2524
goTo.marker("useBaz");
2625
verify.quickInfoIs("import baz");
27-
goTo.definition();
28-
verify.caretAtMarker("importBaz");
29-
goTo.marker("idBaz");
30-
goTo.definition();
31-
verify.caretAtMarker("module");
26+
verify.goToDefinition({
27+
useBaz: "importBaz",
28+
idBaz: "module"
29+
});
3230

3331
goTo.marker("useBang");
3432
verify.quickInfoIs("import bang = require(\"jquery\")");
35-
goTo.definition();
36-
verify.caretAtMarker("importBang");
37-
goTo.marker("idBang");
38-
goTo.definition();
39-
verify.caretAtMarker("module");
33+
verify.goToDefinition({
34+
useBang: "importBang",
35+
idBang: "module"
36+
});

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: 16 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,21 @@ 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+
*/
160+
goToDefinition(startMarkerNames: string | string[], endMarkerNames: string | string[]): void;
161+
/** Performs `goToDefinition` for each pair. */
162+
goToDefinition(startsAndEnds: [string | string[], string | string[]][]): void;
163+
/** Performs `goToDefinition` on each key and value. */
164+
goToDefinition(startsAndEnds: { [startMarkerName: string]: string | string[] }): void;
165+
/** Verifies goToDefinition for each `${markerName}Reference` -> `${markerName}Definition` */
166+
goToDefinitionForMarkers(...markerNames: string[]): void;
155167
verifyGetEmitOutputForCurrentFile(expected: string): void;
156168
verifyGetEmitOutputContentsForCurrentFile(expected: ts.OutputFile[]): void;
157169
/**

tests/cases/fourslash/goToDeclarationDecoratorOverloads.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
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"
19+
});
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: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,7 @@
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"]
29+
]);

0 commit comments

Comments
 (0)