@@ -2276,34 +2276,46 @@ namespace FourSlash {
2276
2276
}
2277
2277
2278
2278
/**
2279
- * Compares expected text to the text that would be in the sole range
2280
- * (ie: [|...|]) in the file after applying the codefix sole codefix
2281
- * in the source file.
2282
- *
2283
- * Because codefixes are only applied on the working file, it is unsafe
2284
- * to apply this more than once (consider a refactoring across files).
2279
+ * Finds and applies a code action corresponding to the supplied parameters.
2280
+ * If index is undefined, applies the unique code action available.
2281
+ * @param errorCode The error code that generated the code action.
2282
+ * @param index The nth (0-index-based) codeaction available generated by errorCode.
2285
2283
*/
2286
- public verifyRangeAfterCodeFix ( expectedText : string , includeWhiteSpace ?: boolean , errorCode ?: number , index ?: number ) {
2284
+ public getAndApplyCodeActions ( errorCode ?: number , index ?: number ) {
2285
+ const fileName = this . activeFile . fileName ;
2286
+ this . applyCodeActions ( fileName , this . getCodeFixActions ( fileName , errorCode ) , index ) ;
2287
+ }
2288
+
2289
+ public verifyRangeIs ( expectedText : string , includeWhiteSpace ?: boolean ) {
2287
2290
const ranges = this . getRanges ( ) ;
2288
2291
if ( ranges . length !== 1 ) {
2289
2292
this . raiseError ( "Exactly one range should be specified in the testfile." ) ;
2290
2293
}
2291
2294
2292
- const fileName = this . activeFile . fileName ;
2293
-
2294
- this . applyCodeAction ( fileName , this . getCodeFixActions ( fileName , errorCode ) , index ) ;
2295
-
2296
- const actualText = this . rangeText ( ranges [ 0 ] ) ;
2295
+ const actualText = this . normalizeNewlines ( this . rangeText ( ranges [ 0 ] ) ) ;
2297
2296
2298
2297
const result = includeWhiteSpace
2299
- ? normalizeNewLines ( actualText ) === normalizeNewLines ( expectedText )
2298
+ ? actualText === this . normalizeNewlines ( expectedText )
2300
2299
: this . removeWhitespace ( actualText ) === this . removeWhitespace ( expectedText ) ;
2301
2300
2302
2301
if ( ! result ) {
2303
2302
this . raiseError ( `Actual text doesn't match expected text. Actual:\n'${ actualText } '\nExpected:\n'${ expectedText } '` ) ;
2304
2303
}
2305
2304
}
2306
2305
2306
+ /**
2307
+ * Compares expected text to the text that would be in the sole range
2308
+ * (ie: [|...|]) in the file after applying the codefix sole codefix
2309
+ * in the source file.
2310
+ *
2311
+ * Because codefixes are only applied on the working file, it is unsafe
2312
+ * to apply this more than once (consider a refactoring across files).
2313
+ */
2314
+ public verifyRangeAfterCodeFix ( expectedText : string , includeWhiteSpace ?: boolean , errorCode ?: number , index ?: number ) {
2315
+ this . getAndApplyCodeActions ( errorCode , index ) ;
2316
+ this . verifyRangeIs ( expectedText , includeWhiteSpace ) ;
2317
+ }
2318
+
2307
2319
/**
2308
2320
* Applies fixes for the errors in fileName and compares the results to
2309
2321
* expectedContents after all fixes have been applied.
@@ -2316,7 +2328,7 @@ namespace FourSlash {
2316
2328
public verifyFileAfterCodeFix ( expectedContents : string , fileName ?: string ) {
2317
2329
fileName = fileName ? fileName : this . activeFile . fileName ;
2318
2330
2319
- this . applyCodeAction ( fileName , this . getCodeFixActions ( fileName ) ) ;
2331
+ this . applyCodeActions ( fileName , this . getCodeFixActions ( fileName ) ) ;
2320
2332
2321
2333
const actualContents : string = this . getFileContent ( fileName ) ;
2322
2334
if ( this . removeWhitespace ( actualContents ) !== this . removeWhitespace ( expectedContents ) ) {
@@ -2354,11 +2366,10 @@ namespace FourSlash {
2354
2366
return actions ;
2355
2367
}
2356
2368
2357
- private applyCodeAction ( fileName : string , actions : ts . CodeAction [ ] , index ?: number ) : void {
2369
+ private applyCodeActions ( fileName : string , actions : ts . CodeAction [ ] , index ?: number ) : void {
2358
2370
if ( index === undefined ) {
2359
2371
if ( ! ( actions && actions . length === 1 ) ) {
2360
- const actionText = ( actions && actions . length ) ? JSON . stringify ( actions ) : "none" ;
2361
- this . raiseError ( `Should find exactly one codefix, but found ${ actionText } ` ) ;
2372
+ this . raiseError ( `Should find exactly one codefix, but ${ actions ? actions . length : "none" } found.` ) ;
2362
2373
}
2363
2374
index = 0 ;
2364
2375
}
@@ -2758,7 +2769,7 @@ namespace FourSlash {
2758
2769
2759
2770
const codeActions = this . languageService . getRefactorCodeActions ( this . activeFile . fileName , formattingOptions , markerPos , refactorNameToApply ) ;
2760
2771
2761
- this . applyCodeAction ( this . activeFile . fileName , codeActions ) ;
2772
+ this . applyCodeActions ( this . activeFile . fileName , codeActions ) ;
2762
2773
const actualContent = this . getFileContent ( this . activeFile . fileName ) ;
2763
2774
2764
2775
if ( this . normalizeNewlines ( actualContent ) !== this . normalizeNewlines ( expectedContent ) ) {
@@ -3805,6 +3816,14 @@ namespace FourSlashInterface {
3805
3816
this . state . verifyFileAfterApplyingRefactorAtMarker ( markerName , expectedContent , refactorNameToApply , formattingOptions ) ;
3806
3817
}
3807
3818
3819
+ public rangeIs ( expectedText : string , includeWhiteSpace ?: boolean ) : void {
3820
+ this . state . verifyRangeIs ( expectedText , includeWhiteSpace ) ;
3821
+ }
3822
+
3823
+ public applyCodeFix ( errorCode ?: number , index ?: number ) : void {
3824
+ this . state . getAndApplyCodeActions ( errorCode , index ) ;
3825
+ }
3826
+
3808
3827
public importFixAtPosition ( expectedTextArray : string [ ] , errorCode ?: number ) : void {
3809
3828
this . state . verifyImportFixAtPosition ( expectedTextArray , errorCode ) ;
3810
3829
}
0 commit comments