Skip to content

Commit f133a67

Browse files
author
Arthur Ozga
committed
wip testing
1 parent 2187e67 commit f133a67

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

src/harness/fourslash.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,15 +2122,15 @@ namespace FourSlash {
21222122
* Because codefixes are only applied on the working file, it is unsafe
21232123
* to apply this more than once (consider a refactoring across files).
21242124
*/
2125-
public verifyRangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean) {
2125+
public verifyRangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number) {
21262126
const ranges = this.getRanges();
21272127
if (ranges.length !== 1) {
21282128
this.raiseError("Exactly one range should be specified in the testfile.");
21292129
}
21302130

21312131
const fileName = this.activeFile.fileName;
21322132

2133-
this.applyCodeFixActions(fileName, this.getCodeFixActions(fileName, errorCode));
2133+
this.applyCodeAction(fileName, this.getCodeFixActions(fileName, errorCode), index);
21342134

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

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

2158-
this.applyCodeFixActions(fileName, this.getCodeFixActions(fileName));
2158+
this.applyCodeAction(fileName, this.getCodeFixActions(fileName));
21592159

21602160
const actualContents: string = this.getFileContent(fileName);
21612161
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
@@ -2193,12 +2193,20 @@ namespace FourSlash {
21932193
return actions;
21942194
}
21952195

2196-
private applyCodeFixActions(fileName: string, actions: ts.CodeAction[]): void {
2197-
if (!(actions && actions.length === 1)) {
2198-
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found.`);
2196+
private applyCodeAction(fileName: string, actions: ts.CodeAction[], index?: number): void {
2197+
if (index === undefined) {
2198+
if (!(actions && actions.length === 1)) {
2199+
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found.`);
2200+
}
2201+
index = 0;
21992202
}
2200-
2201-
const fileChanges = ts.find(actions[0].changes, change => change.fileName === fileName);
2203+
else {
2204+
if (!(actions && actions.length >= index + 1)) {
2205+
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${actions ? actions.length : "none"} found.`);
2206+
}
2207+
}
2208+
2209+
const fileChanges = ts.find(actions[index].changes, change => change.fileName === fileName);
22022210
if (!fileChanges) {
22032211
this.raiseError("The CodeFix found doesn't provide any changes in this file.");
22042212
}
@@ -3535,8 +3543,8 @@ namespace FourSlashInterface {
35353543
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
35363544
}
35373545

3538-
public rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void {
3539-
this.state.verifyRangeAfterCodeFix(expectedText, errorCode, includeWhiteSpace);
3546+
public rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void {
3547+
this.state.verifyRangeAfterCodeFix(expectedText, includeWhiteSpace, errorCode, index);
35403548
}
35413549

35423550
public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// [|class A {
4+
//// constructor() {
5+
//// this.x = { a: 10, b: "hello" };
6+
//// }
7+
//// }|]
8+
9+
verify.rangeAfterCodeFix(`
10+
class A {
11+
x: { a: number, b: string };
12+
13+
constructor() {
14+
this.x = 10;
15+
}
16+
}
17+
`);

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ declare namespace FourSlashInterface {
225225
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
226226
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
227227
noDocCommentTemplate(): void;
228-
rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void;
228+
rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number): void;
229229
importFixAtPosition(expectedTextArray: string[], errorCode?: number): void;
230230

231231
navigationBar(json: any): void;

tests/cases/fourslash/unusedImports2FS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
////
1717
//// }
1818

19-
verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`, /*errorCode*/ undefined, /*includeWhiteSpace*/ true);
19+
verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`, /*includeWhiteSpace*/ true, /*errorCode*/ undefined);

tests/cases/fourslash/unusedLocalsInFunction3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// z+1;
88
////}
99

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

0 commit comments

Comments
 (0)