@@ -255,7 +255,21 @@ interface String { charAt: any; }
255
255
interface Array<T> {}`
256
256
} ;
257
257
258
- function testConvertToAsyncFunction ( caption : string , text : string , baselineFolder : string , includeLib ?: boolean , expectFailure = false , onlyProvideAction = false ) {
258
+ type WithSkipAndOnly < T extends any [ ] > = ( ( ...args : T ) => void ) & {
259
+ skip : ( ...args : T ) => void ;
260
+ only : ( ...args : T ) => void ;
261
+ } ;
262
+
263
+ function createTestWrapper < T extends any [ ] > ( fn : ( it : Mocha . PendingTestFunction , ...args : T ) => void ) : WithSkipAndOnly < T > {
264
+ wrapped . skip = ( ...args : T ) => fn ( it . skip , ...args ) ;
265
+ wrapped . only = ( ...args : T ) => fn ( it . only , ...args ) ;
266
+ return wrapped ;
267
+ function wrapped ( ...args : T ) {
268
+ return fn ( it , ...args ) ;
269
+ }
270
+ }
271
+
272
+ function testConvertToAsyncFunction ( it : Mocha . PendingTestFunction , caption : string , text : string , baselineFolder : string , includeLib ?: boolean , expectFailure = false , onlyProvideAction = false ) {
259
273
const t = extractTest ( text ) ;
260
274
const selectionRange = t . ranges . get ( "selection" ) ! ;
261
275
if ( ! selectionRange ) {
@@ -343,7 +357,19 @@ interface Array<T> {}`
343
357
}
344
358
}
345
359
346
- describe ( "unittests:: services:: convertToAsyncFunctions" , ( ) => {
360
+ const _testConvertToAsyncFunction = createTestWrapper ( ( it , caption : string , text : string ) => {
361
+ testConvertToAsyncFunction ( it , caption , text , "convertToAsyncFunction" , /*includeLib*/ true ) ;
362
+ } ) ;
363
+
364
+ const _testConvertToAsyncFunctionFailed = createTestWrapper ( ( it , caption : string , text : string ) => {
365
+ testConvertToAsyncFunction ( it , caption , text , "convertToAsyncFunction" , /*includeLib*/ true , /*expectFailure*/ true ) ;
366
+ } ) ;
367
+
368
+ const _testConvertToAsyncFunctionFailedSuggestion = createTestWrapper ( ( it , caption : string , text : string ) => {
369
+ testConvertToAsyncFunction ( it , caption , text , "convertToAsyncFunction" , /*includeLib*/ true , /*expectFailure*/ true , /*onlyProvideAction*/ true ) ;
370
+ } ) ;
371
+
372
+ describe ( "unittests:: services:: convertToAsyncFunction" , ( ) => {
347
373
_testConvertToAsyncFunction ( "convertToAsyncFunction_basic" , `
348
374
function [#|f|](): Promise<void>{
349
375
return fetch('https://typescriptlang.org').then(result => { console.log(result) });
@@ -1352,17 +1378,54 @@ function foo() {
1352
1378
})
1353
1379
}
1354
1380
` ) ;
1355
- } ) ;
1356
1381
1357
- function _testConvertToAsyncFunction ( caption : string , text : string ) {
1358
- testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true ) ;
1359
- }
1382
+ _testConvertToAsyncFunction ( "convertToAsyncFunction_thenTypeArgument1" , `
1383
+ type APIResponse<T> = { success: true, data: T } | { success: false };
1360
1384
1361
- function _testConvertToAsyncFunctionFailed ( caption : string , text : string ) {
1362
- testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true , /*expectFailure*/ true ) ;
1363
- }
1385
+ function wrapResponse<T>(response: T): APIResponse<T> {
1386
+ return { success: true, data: response } ;
1387
+ }
1364
1388
1365
- function _testConvertToAsyncFunctionFailedSuggestion ( caption : string , text : string ) {
1366
- testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true , /*expectFailure*/ true , /*onlyProvideAction*/ true ) ;
1367
- }
1389
+ function [#|get|]() {
1390
+ return Promise.resolve(undefined!).then<APIResponse<{ email: string }>>(wrapResponse);
1391
+ }
1392
+ ` ) ;
1393
+
1394
+ _testConvertToAsyncFunction ( "convertToAsyncFunction_thenTypeArgument2" , `
1395
+ type APIResponse<T> = { success: true, data: T } | { success: false };
1396
+
1397
+ function wrapResponse<T>(response: T): APIResponse<T> {
1398
+ return { success: true, data: response };
1399
+ }
1400
+
1401
+ function [#|get|]() {
1402
+ return Promise.resolve(undefined!).then<APIResponse<{ email: string }>>(d => wrapResponse(d));
1403
+ }
1404
+ ` ) ;
1405
+
1406
+ _testConvertToAsyncFunction ( "convertToAsyncFunction_thenTypeArgument3" , `
1407
+ type APIResponse<T> = { success: true, data: T } | { success: false };
1408
+
1409
+ function wrapResponse<T>(response: T): APIResponse<T> {
1410
+ return { success: true, data: response };
1411
+ }
1412
+
1413
+ function [#|get|]() {
1414
+ return Promise.resolve(undefined!).then<APIResponse<{ email: string }>>(d => {
1415
+ console.log(d);
1416
+ return wrapResponse(d);
1417
+ });
1418
+ }
1419
+ ` ) ;
1420
+
1421
+ _testConvertToAsyncFunction ( "convertToAsyncFunction_catchTypeArgument1" , `
1422
+ type APIResponse<T> = { success: true, data: T } | { success: false };
1423
+
1424
+ function [#|get|]() {
1425
+ return Promise
1426
+ .resolve<APIResponse<{ email: string }>>({ success: true, data: { email: "" } })
1427
+ .catch<APIResponse<{ email: string }>>(() => ({ success: false }));
1428
+ }
1429
+ ` ) ;
1430
+ } ) ;
1368
1431
}
0 commit comments