From 1f045e1227cf1a15acff83c827f9a0d232ef32ec Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 3 Oct 2014 15:33:29 -0700 Subject: [PATCH] Adding rename tests. --- src/harness/fourslash.ts | 58 +++++++++++++++++++++++------- tests/cases/fourslash/fourslash.ts | 5 ++- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index e746bd9e7c9..ebc6ec61d8f 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -758,6 +758,10 @@ module FourSlash { return this.languageService.getImplementorsAtPosition(this.activeFile.fileName, this.currentCaretPosition); } + private assertionMessage(name: string, actualValue: any, expectedValue: any) { + return "\nActual " + name + ":\n\t" + actualValue + "\nExpected value:\n\t" + expectedValue; + } + public verifyQuickInfo(negative: boolean, expectedTypeName?: string, docComment?: string, symbolName?: string, kind?: string) { [expectedTypeName, docComment, symbolName, kind].forEach(str => { if (str) { @@ -772,40 +776,68 @@ module FourSlash { var actualQuickInfoSymbolName = actualQuickInfo ? actualQuickInfo.fullSymbolName : ""; var actualQuickInfoKind = actualQuickInfo ? actualQuickInfo.kind : ""; - function assertionMessage(name: string, actualValue: string, expectedValue: string) { - return "\nActual " + name + ":\n\t" + actualValue + "\nExpected value:\n\t" + expectedValue; - } - if (negative) { if (expectedTypeName !== undefined) { - assert.notEqual(actualQuickInfoMemberName, expectedTypeName, assertionMessage("quick info member name", actualQuickInfoMemberName, expectedTypeName)); + assert.notEqual(actualQuickInfoMemberName, expectedTypeName, this.assertionMessage("quick info member name", actualQuickInfoMemberName, expectedTypeName)); } if (docComment != undefined) { - assert.notEqual(actualQuickInfoDocComment, docComment, assertionMessage("quick info doc comment", actualQuickInfoDocComment, docComment)); + assert.notEqual(actualQuickInfoDocComment, docComment, this.assertionMessage("quick info doc comment", actualQuickInfoDocComment, docComment)); } if (symbolName !== undefined) { - assert.notEqual(actualQuickInfoSymbolName, symbolName, assertionMessage("quick info symbol name", actualQuickInfoSymbolName, symbolName)); + assert.notEqual(actualQuickInfoSymbolName, symbolName, this.assertionMessage("quick info symbol name", actualQuickInfoSymbolName, symbolName)); } if (kind !== undefined) { - assert.notEqual(actualQuickInfoKind, kind, assertionMessage("quick info kind", actualQuickInfoKind, kind)); + assert.notEqual(actualQuickInfoKind, kind, this.assertionMessage("quick info kind", actualQuickInfoKind, kind)); } } else { if (expectedTypeName !== undefined) { - assert.equal(actualQuickInfoMemberName, expectedTypeName, assertionMessage("quick info member", actualQuickInfoMemberName, expectedTypeName)); + assert.equal(actualQuickInfoMemberName, expectedTypeName, this.assertionMessage("quick info member", actualQuickInfoMemberName, expectedTypeName)); } if (docComment != undefined) { - assert.equal(actualQuickInfoDocComment, docComment, assertionMessage("quick info doc", actualQuickInfoDocComment, docComment)); + assert.equal(actualQuickInfoDocComment, docComment, this.assertionMessage("quick info doc", actualQuickInfoDocComment, docComment)); } if (symbolName !== undefined) { - assert.equal(actualQuickInfoSymbolName, symbolName, assertionMessage("quick info symbol name", actualQuickInfoSymbolName, symbolName)); + assert.equal(actualQuickInfoSymbolName, symbolName, this.assertionMessage("quick info symbol name", actualQuickInfoSymbolName, symbolName)); } if (kind !== undefined) { - assert.equal(actualQuickInfoKind, kind, assertionMessage("quick info kind", actualQuickInfoKind, kind)); + assert.equal(actualQuickInfoKind, kind, this.assertionMessage("quick info kind", actualQuickInfoKind, kind)); } } } - public verifyQuickInfoExists(negative: number) { + public verifyRenameLocations(findInStrings: boolean, findInComments: boolean) { + var renameInfo = this.languageService.getRenameInfo(this.activeFile.fileName, this.currentCaretPosition); + if (renameInfo.canRename) { + var references = this.languageService.findRenameLocations( + this.activeFile.fileName, this.currentCaretPosition, findInStrings, findInComments); + + var ranges = this.getRanges(); + if (ranges.length !== references.length) { + this.raiseError(this.assertionMessage("Rename locations", references.length, ranges.length)); + } + + ranges = ranges.sort((r1, r2) => r1.start - r2.start); + references = references.sort((r1, r2) => r1.textSpan.start() - r2.textSpan.start()); + + for (var i = 0, n = ranges.length; i < n; i++) { + var reference = references[i]; + var range = ranges[i]; + + if (reference.textSpan.start() !== range.start || + reference.textSpan.end() !== range.end) { + + this.raiseError(this.assertionMessage("Rename location", + "[" + reference.textSpan.start() + "," + reference.textSpan.end() + ")", + "[" + range.start + "," + range.end + ")")); + } + } + } + else { + this.raiseError("Expected rename to succeed, but it actually failed."); + } + } + + public verifyQuickInfoExists(negative: boolean) { this.taoInvalidReason = 'verifyQuickInfoExists NYI'; var actualQuickInfo = this.languageService.getTypeAtPosition(this.activeFile.fileName, this.currentCaretPosition); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index c204a98c4a4..537ff8527d6 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -240,7 +240,6 @@ module FourSlashInterface { } export class verify extends verifyNegatable { - public caretAtMarker(markerName?: string) { FourSlash.currentTestState.verifyCaretAtMarker(markerName); } @@ -417,6 +416,10 @@ module FourSlashInterface { public renameInfoFailed(message?: string) { FourSlash.currentTestState.verifyRenameInfoFailed(message) } + + public renameLocations(findInStrings: boolean, findInComments: boolean) { + FourSlash.currentTestState.verifyRenameLocations(findInStrings, findInComments); + } } export class edit {