Skip to content

Commit

Permalink
wip testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Ozga committed Feb 16, 2017
1 parent 2187e67 commit f133a67
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/harness/fourslash.ts
Expand Up @@ -2122,15 +2122,15 @@ namespace FourSlash {
* Because codefixes are only applied on the working file, it is unsafe
* to apply this more than once (consider a refactoring across files).
*/
public verifyRangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean) {
public verifyRangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number) {
const ranges = this.getRanges();
if (ranges.length !== 1) {
this.raiseError("Exactly one range should be specified in the testfile.");
}

const fileName = this.activeFile.fileName;

this.applyCodeFixActions(fileName, this.getCodeFixActions(fileName, errorCode));
this.applyCodeAction(fileName, this.getCodeFixActions(fileName, errorCode), index);

const actualText = this.rangeText(ranges[0]);

Expand All @@ -2155,7 +2155,7 @@ namespace FourSlash {
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string) {
fileName = fileName ? fileName : this.activeFile.fileName;

this.applyCodeFixActions(fileName, this.getCodeFixActions(fileName));
this.applyCodeAction(fileName, this.getCodeFixActions(fileName));

const actualContents: string = this.getFileContent(fileName);
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
Expand Down Expand Up @@ -2193,12 +2193,20 @@ namespace FourSlash {
return actions;
}

private applyCodeFixActions(fileName: string, actions: ts.CodeAction[]): void {
if (!(actions && actions.length === 1)) {
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found.`);
private applyCodeAction(fileName: string, actions: ts.CodeAction[], index?: number): void {
if (index === undefined) {
if (!(actions && actions.length === 1)) {
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found.`);
}
index = 0;
}

const fileChanges = ts.find(actions[0].changes, change => change.fileName === fileName);
else {
if (!(actions && actions.length >= index + 1)) {
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${actions ? actions.length : "none"} found.`);
}
}

const fileChanges = ts.find(actions[index].changes, change => change.fileName === fileName);
if (!fileChanges) {
this.raiseError("The CodeFix found doesn't provide any changes in this file.");
}
Expand Down Expand Up @@ -3535,8 +3543,8 @@ namespace FourSlashInterface {
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
}

public rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void {
this.state.verifyRangeAfterCodeFix(expectedText, errorCode, includeWhiteSpace);
public rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void {
this.state.verifyRangeAfterCodeFix(expectedText, includeWhiteSpace, errorCode, index);
}

public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void {
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixUndeclaredPropertyObjectLiteral.ts
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

//// [|class A {
//// constructor() {
//// this.x = { a: 10, b: "hello" };
//// }
//// }|]

verify.rangeAfterCodeFix(`
class A {
x: { a: number, b: string };
constructor() {
this.x = 10;
}
}
`);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/fourslash.ts
Expand Up @@ -225,7 +225,7 @@ declare namespace FourSlashInterface {
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
noDocCommentTemplate(): void;
rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void;
rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number): void;
importFixAtPosition(expectedTextArray: string[], errorCode?: number): void;

navigationBar(json: any): void;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedImports2FS.ts
Expand Up @@ -16,4 +16,4 @@
////
//// }

verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`, /*errorCode*/ undefined, /*includeWhiteSpace*/ true);
verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`, /*includeWhiteSpace*/ true, /*errorCode*/ undefined);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedLocalsInFunction3.ts
Expand Up @@ -7,4 +7,4 @@
//// z+1;
////}

verify.rangeAfterCodeFix("var x,z = 1;", 6133);
verify.rangeAfterCodeFix("var x,z = 1;", /*includeWhiteSpace*/ undefined, 6133);

0 comments on commit f133a67

Please sign in to comment.