Skip to content

Simplify go-to-definition tests #10686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 91 additions & 62 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,67 @@ namespace FourSlash {
}
}

public verifyGoToDefinitionIs(endMarker: string | string[]) {
this.verifyGoToDefinitionWorker(endMarker instanceof Array ? endMarker : [endMarker]);
}

public verifyGoToDefinition(arg0: any, endMarkerNames?: string | string[]) {
if (endMarkerNames) {
this.verifyGoToDefinitionPlain(arg0, endMarkerNames);
}
else if (arg0 instanceof Array) {
const pairs: [string | string[], string | string[]][] = arg0;
for (const [start, end] of pairs) {
this.verifyGoToDefinitionPlain(start, end);
}
}
else {
const obj: { [startMarkerName: string]: string | string[] } = arg0;
for (const startMarkerName in obj) {
if (ts.hasProperty(obj, startMarkerName)) {
this.verifyGoToDefinitionPlain(startMarkerName, obj[startMarkerName]);
}
}
}
}

private verifyGoToDefinitionPlain(startMarkerNames: string | string[], endMarkerNames: string | string[]) {
if (startMarkerNames instanceof Array) {
for (const start of startMarkerNames) {
this.verifyGoToDefinitionSingle(start, endMarkerNames);
}
}
else {
this.verifyGoToDefinitionSingle(startMarkerNames, endMarkerNames);
}
}

public verifyGoToDefinitionForMarkers(markerNames: string[]) {
for (const markerName of markerNames) {
this.verifyGoToDefinitionSingle(`${markerName}Reference`, `${markerName}Definition`);
}
}

private verifyGoToDefinitionSingle(startMarkerName: string, endMarkerNames: string | string[]) {
this.goToMarker(startMarkerName);
this.verifyGoToDefinitionWorker(endMarkerNames instanceof Array ? endMarkerNames : [endMarkerNames]);
}

private verifyGoToDefinitionWorker(endMarkers: string[]) {
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition) || [];

if (endMarkers.length !== definitions.length) {
this.raiseError(`goToDefinitions failed - expected to find ${endMarkers.length} definitions but got ${definitions.length}`);
}

for (let i = 0; i < endMarkers.length; i++) {
const marker = this.getMarkerByName(endMarkers[i]), definition = definitions[i];
if (marker.fileName !== definition.fileName || marker.position !== definition.textSpan.start) {
this.raiseError(`goToDefinition failed for definition ${i}: expected ${marker.fileName} at ${marker.position}, got ${definition.fileName} at ${definition.textSpan.start}`);
}
}
}

public verifyGetEmitOutputForCurrentFile(expected: string): void {
const emit = this.languageService.getEmitOutput(this.activeFile.fileName);
if (emit.outputFiles.length !== 1) {
Expand Down Expand Up @@ -1561,21 +1622,6 @@ namespace FourSlash {
this.goToPosition(len);
}

public goToDefinition(definitionIndex: number) {
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
if (!definitions || !definitions.length) {
this.raiseError("goToDefinition failed - expected to find at least one definition location but got 0");
}

if (definitionIndex >= definitions.length) {
this.raiseError(`goToDefinition failed - definitionIndex value (${definitionIndex}) exceeds definition list size (${definitions.length})`);
}

const definition = definitions[definitionIndex];
this.openFile(definition.fileName);
this.currentCaretPosition = definition.textSpan.start;
}

public goToTypeDefinition(definitionIndex: number) {
const definitions = this.languageService.getTypeDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
if (!definitions || !definitions.length) {
Expand All @@ -1591,28 +1637,6 @@ namespace FourSlash {
this.currentCaretPosition = definition.textSpan.start;
}

public verifyDefinitionLocationExists(negative: boolean) {
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);

const foundDefinitions = definitions && definitions.length;

if (foundDefinitions && negative) {
this.raiseError(`goToDefinition - expected to 0 definition locations but got ${definitions.length}`);
}
else if (!foundDefinitions && !negative) {
this.raiseError("goToDefinition - expected to find at least one definition location but got 0");
}
}

public verifyDefinitionsCount(negative: boolean, expectedCount: number) {
const assertFn = negative ? assert.notEqual : assert.equal;

const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
const actualCount = definitions && definitions.length || 0;

assertFn(actualCount, expectedCount, this.messageAtLastKnownMarker("Definitions Count"));
}

public verifyTypeDefinitionsCount(negative: boolean, expectedCount: number) {
const assertFn = negative ? assert.notEqual : assert.equal;

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

public verifyDefinitionsName(negative: boolean, expectedName: string, expectedContainerName: string) {
public verifyGoToDefinitionName(expectedName: string, expectedContainerName: string) {
const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition);
const actualDefinitionName = definitions && definitions.length ? definitions[0].name : "";
const actualDefinitionContainerName = definitions && definitions.length ? definitions[0].containerName : "";
if (negative) {
assert.notEqual(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
assert.notEqual(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
}
else {
assert.equal(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
assert.equal(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
}
assert.equal(actualDefinitionName, expectedName, this.messageAtLastKnownMarker("Definition Info Name"));
assert.equal(actualDefinitionContainerName, expectedContainerName, this.messageAtLastKnownMarker("Definition Info Container Name"));
}

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

public getMarkerNames(): string[] {
return Object.keys(this.testData.markerPositions);
}

public getRanges(): Range[] {
return this.testData.ranges;
}
Expand Down Expand Up @@ -2742,6 +2764,10 @@ namespace FourSlashInterface {
return this.state.getMarkers();
}

public markerNames(): string[] {
return this.state.getMarkerNames();
}

public marker(name?: string): FourSlash.Marker {
return this.state.getMarkerByName(name);
}
Expand Down Expand Up @@ -2777,10 +2803,6 @@ namespace FourSlashInterface {
this.state.goToEOF();
}

public definition(definitionIndex = 0) {
this.state.goToDefinition(definitionIndex);
}

public type(definitionIndex = 0) {
this.state.goToTypeDefinition(definitionIndex);
}
Expand Down Expand Up @@ -2885,22 +2907,10 @@ namespace FourSlashInterface {
this.state.verifyQuickInfoExists(this.negative);
}

public definitionCountIs(expectedCount: number) {
this.state.verifyDefinitionsCount(this.negative, expectedCount);
}

public typeDefinitionCountIs(expectedCount: number) {
this.state.verifyTypeDefinitionsCount(this.negative, expectedCount);
}

public definitionLocationExists() {
this.state.verifyDefinitionLocationExists(this.negative);
}

public verifyDefinitionsName(name: string, containerName: string) {
this.state.verifyDefinitionsName(this.negative, name, containerName);
}

public isValidBraceCompletionAtPosition(openingBrace: string) {
this.state.verifyBraceCompletionAtPosition(this.negative, openingBrace);
}
Expand Down Expand Up @@ -2944,6 +2954,25 @@ namespace FourSlashInterface {
this.state.verifyCurrentFileContent(text);
}

public goToDefinitionIs(endMarkers: string | string[]) {
this.state.verifyGoToDefinitionIs(endMarkers);
}

public goToDefinition(startMarkerName: string | string[], endMarkerName: string | string[]): void;
public goToDefinition(startsAndEnds: [string | string[], string | string[]][]): void;
public goToDefinition(startsAndEnds: { [startMarkerName: string]: string | string[] }): void;
public goToDefinition(arg0: any, endMarkerName?: string | string[]) {
this.state.verifyGoToDefinition(arg0, endMarkerName);
}

public goToDefinitionForMarkers(...markerNames: string[]) {
this.state.verifyGoToDefinitionForMarkers(markerNames);
}

public goToDefinitionName(name: string, containerName: string) {
this.state.verifyGoToDefinitionName(name, containerName);
}

public verifyGetEmitOutputForCurrentFile(expected: string): void {
this.state.verifyGetEmitOutputForCurrentFile(expected);
}
Expand Down
29 changes: 13 additions & 16 deletions tests/cases/fourslash/ambientShorthandGotoDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@

goTo.marker("useFoo");
verify.quickInfoIs("import foo");
goTo.definition();
verify.caretAtMarker("importFoo");
goTo.definition();
verify.caretAtMarker("module");
verify.goToDefinition({
useFoo: "importFoo",
importFoo: "module"
});

goTo.marker("useBar");
verify.quickInfoIs("import bar");
goTo.definition();
verify.caretAtMarker("module");
verify.goToDefinition("useBar", "module");

goTo.marker("useBaz");
verify.quickInfoIs("import baz");
goTo.definition();
verify.caretAtMarker("importBaz");
goTo.marker("idBaz");
goTo.definition();
verify.caretAtMarker("module");
verify.goToDefinition({
useBaz: "importBaz",
idBaz: "module"
});

goTo.marker("useBang");
verify.quickInfoIs("import bang = require(\"jquery\")");
goTo.definition();
verify.caretAtMarker("importBang");
goTo.marker("idBang");
goTo.definition();
verify.caretAtMarker("module");
verify.goToDefinition({
useBang: "importBang",
idBang: "module"
});
4 changes: 1 addition & 3 deletions tests/cases/fourslash/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
// @Filename: a.ts
//// /*2*/export class Foo {}

goTo.marker('1');
goTo.definition();
verify.caretAtMarker('2');
verify.goToDefinition("1", "2");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/definitionNameOnEnumMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
////var enumMember = e./*1*/thirdMember;

goTo.marker("1");
verify.verifyDefinitionsName("thirdMember", "e");
verify.goToDefinitionName("thirdMember", "e");
20 changes: 16 additions & 4 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ declare namespace FourSlashInterface {
}
class test_ {
markers(): Marker[];
markerNames(): string[];
marker(name?: string): Marker;
ranges(): Range[];
rangesByText(): { [text: string]: Range[] };
Expand All @@ -108,7 +109,6 @@ declare namespace FourSlashInterface {
marker(name?: string): void;
bof(): void;
eof(): void;
definition(definitionIndex?: number): void;
type(definitionIndex?: number): void;
position(position: number, fileIndex?: number): any;
position(position: number, fileName?: string): any;
Expand All @@ -132,10 +132,7 @@ declare namespace FourSlashInterface {
errorExistsBeforeMarker(markerName?: string): void;
quickInfoIs(expectedText?: string, expectedDocumentation?: string): void;
quickInfoExists(): void;
definitionCountIs(expectedCount: number): void;
typeDefinitionCountIs(expectedCount: number): void;
definitionLocationExists(): void;
verifyDefinitionsName(name: string, containerName: string): void;
isValidBraceCompletionAtPosition(openingBrace?: string): void;
}
class verify extends verifyNegatable {
Expand All @@ -152,6 +149,21 @@ declare namespace FourSlashInterface {
eval(expr: string, value: any): void;
currentLineContentIs(text: string): void;
currentFileContentIs(text: string): void;
/** Verifies that goToDefinition at the current position would take you to `endMarker`. */
goToDefinitionIs(endMarkers: string | string[]): void;
goToDefinitionName(name: string, containerName: string): void;
/**
* `verify.goToDefinition("a", "b");` verifies that go-to-definition at marker "a" takes you to marker "b".
* `verify.goToDefinition(["a", "aa"], "b");` verifies that markers "a" and "aa" have the same definition "b".
* `verify.goToDefinition("a", ["b", "bb"]);` verifies that "a" has multiple definitions available.
*/
goToDefinition(startMarkerNames: string | string[], endMarkerNames: string | string[]): void;
/** Performs `goToDefinition` for each pair. */
goToDefinition(startsAndEnds: [string | string[], string | string[]][]): void;
/** Performs `goToDefinition` on each key and value. */
goToDefinition(startsAndEnds: { [startMarkerName: string]: string | string[] }): void;
/** Verifies goToDefinition for each `${markerName}Reference` -> `${markerName}Definition` */
goToDefinitionForMarkers(...markerNames: string[]): void;
verifyGetEmitOutputForCurrentFile(expected: string): void;
verifyGetEmitOutputContentsForCurrentFile(expected: ts.OutputFile[]): void;
/**
Expand Down
11 changes: 4 additions & 7 deletions tests/cases/fourslash/goToDeclarationDecoratorOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
//// @/*useDecSymbol*/dec [s]() {}
////}

goTo.marker("useDecString");
goTo.definition();
verify.caretAtMarker("defDecString");

goTo.marker("useDecSymbol");
goTo.definition();
verify.caretAtMarker("defDecSymbol");
verify.goToDefinition({
useDecString: "defDecString",
useDecSymbol: "defDecSymbol"
});
11 changes: 4 additions & 7 deletions tests/cases/fourslash/goToDefinitionAcrossMultipleProjects.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/// <reference path="fourslash.ts" />

//@Filename: a.ts
////var x: number;
////var /*def1*/x: number;

//@Filename: b.ts
////var x: number;
////var /*def2*/x: number;

//@Filename: c.ts
/////// <reference path="a.ts" />
/////// <reference path="b.ts" />
/////**/x++;
/////*use*/x++;

goTo.file("c.ts");
goTo.marker();

verify.definitionCountIs(2);
verify.goToDefinition("use", ["def1", "def2"]);
21 changes: 4 additions & 17 deletions tests/cases/fourslash/goToDefinitionAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@
//// x;
////}


goTo.marker('alias1Type');
goTo.definition();
verify.caretAtMarker('alias1Definition');

goTo.marker('alias2Type');
goTo.definition();
verify.caretAtMarker('alias2Definition');


goTo.marker('alias1Value');
goTo.definition();
verify.caretAtMarker('alias1Definition');

goTo.marker('alias2Value');
goTo.definition();
verify.caretAtMarker('alias2Definition');
verify.goToDefinition([
[["alias1Type", "alias1Value"], "alias1Definition"],
[["alias2Type", "alias2Value"], "alias2Definition"]
]);
Loading