| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.services.correction.fix; | 5 library test.services.correction.fix; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; | 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; |
| 10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart'; | 10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart'; |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 | 456 |
| 457 test_addSync_blockFunctionBody() async { | 457 test_addSync_blockFunctionBody() async { |
| 458 resolveTestUnit(''' | 458 resolveTestUnit(''' |
| 459 foo() {} | 459 foo() {} |
| 460 main() { | 460 main() { |
| 461 await foo(); | 461 await foo(); |
| 462 } | 462 } |
| 463 '''); | 463 '''); |
| 464 List<AnalysisError> errors = context.computeErrors(testSource); | 464 List<AnalysisError> errors = context.computeErrors(testSource); |
| 465 expect(errors, hasLength(2)); | 465 expect(errors, hasLength(2)); |
| 466 String message1 = "Expected to find ';'"; | 466 errors.sort((a, b) => a.message.compareTo(b.message)); |
| 467 String message2 = "Undefined name 'await'"; | 467 // No fix for ";". |
| 468 expect(errors.map((e) => e.message), unorderedEquals([message1, message2])); | 468 { |
| 469 for (AnalysisError error in errors) { | 469 AnalysisError error = errors[0]; |
| 470 if (error.message == message1) { | 470 expect(error.message, "Expected to find ';'"); |
| 471 List<Fix> fixes = await _computeFixes(error); | 471 List<Fix> fixes = await _computeFixes(error); |
| 472 expect(fixes, isEmpty); | 472 expect(fixes, isEmpty); |
| 473 } | 473 } |
| 474 if (error.message == message2) { | 474 // Has fix for "await". |
| 475 List<Fix> fixes = await _computeFixes(error); | 475 { |
| 476 // has exactly one fix | 476 AnalysisError error = errors[1]; |
| 477 expect(fixes, hasLength(1)); | 477 expect(error.message, startsWith("Undefined name 'await';")); |
| 478 Fix fix = fixes[0]; | 478 List<Fix> fixes = await _computeFixes(error); |
| 479 expect(fix.kind, DartFixKind.ADD_ASYNC); | 479 // has exactly one fix |
| 480 // apply to "file" | 480 expect(fixes, hasLength(1)); |
| 481 List<SourceFileEdit> fileEdits = fix.change.edits; | 481 Fix fix = fixes[0]; |
| 482 expect(fileEdits, hasLength(1)); | 482 expect(fix.kind, DartFixKind.ADD_ASYNC); |
| 483 resultCode = SourceEdit.applySequence(testCode, fileEdits[0].edits); | 483 // apply to "file" |
| 484 // verify | 484 List<SourceFileEdit> fileEdits = fix.change.edits; |
| 485 expect( | 485 expect(fileEdits, hasLength(1)); |
| 486 resultCode, | 486 resultCode = SourceEdit.applySequence(testCode, fileEdits[0].edits); |
| 487 ''' | 487 // verify |
| 488 expect( |
| 489 resultCode, |
| 490 ''' |
| 488 foo() {} | 491 foo() {} |
| 489 main() async { | 492 main() async { |
| 490 await foo(); | 493 await foo(); |
| 491 } | 494 } |
| 492 '''); | 495 '''); |
| 493 } | |
| 494 } | 496 } |
| 495 } | 497 } |
| 496 | 498 |
| 497 test_addSync_expressionFunctionBody() async { | 499 test_addSync_expressionFunctionBody() async { |
| 498 errorFilter = (AnalysisError error) { | 500 errorFilter = (AnalysisError error) { |
| 499 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; | 501 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT; |
| 500 }; | 502 }; |
| 501 resolveTestUnit(''' | 503 resolveTestUnit(''' |
| 502 foo() {} | 504 foo() {} |
| 503 main() => await foo(); | 505 main() => await foo(); |
| 504 '''); | 506 '''); |
| 505 await assertHasFix( | 507 await assertHasFix( |
| 506 DartFixKind.ADD_ASYNC, | 508 DartFixKind.ADD_ASYNC, |
| 507 ''' | 509 ''' |
| 508 foo() {} | 510 foo() {} |
| 509 main() async => await foo(); | 511 main() async => await foo(); |
| 510 '''); | 512 '''); |
| 511 } | 513 } |
| 512 | 514 |
| 513 test_addSync_returnFuture() async { | 515 test_addSync_returnFuture() async { |
| 514 errorFilter = (AnalysisError error) { | 516 errorFilter = (AnalysisError error) { |
| 515 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; | 517 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT; |
| 516 }; | 518 }; |
| 517 resolveTestUnit(''' | 519 resolveTestUnit(''' |
| 518 foo() {} | 520 foo() {} |
| 519 int main() { | 521 int main() { |
| 520 await foo(); | 522 await foo(); |
| 521 return 42; | 523 return 42; |
| 522 } | 524 } |
| 523 '''); | 525 '''); |
| 524 await assertHasFix( | 526 await assertHasFix( |
| 525 DartFixKind.ADD_ASYNC, | 527 DartFixKind.ADD_ASYNC, |
| 526 ''' | 528 ''' |
| 527 import 'dart:async'; | 529 import 'dart:async'; |
| 528 | 530 |
| 529 foo() {} | 531 foo() {} |
| 530 Future<int> main() async { | 532 Future<int> main() async { |
| 531 await foo(); | 533 await foo(); |
| 532 return 42; | 534 return 42; |
| 533 } | 535 } |
| 534 '''); | 536 '''); |
| 535 } | 537 } |
| 536 | 538 |
| 537 test_addSync_returnFuture_alreadyFuture() async { | 539 test_addSync_returnFuture_alreadyFuture() async { |
| 538 errorFilter = (AnalysisError error) { | 540 errorFilter = (AnalysisError error) { |
| 539 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; | 541 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT; |
| 540 }; | 542 }; |
| 541 resolveTestUnit(''' | 543 resolveTestUnit(''' |
| 542 import 'dart:async'; | 544 import 'dart:async'; |
| 543 foo() {} | 545 foo() {} |
| 544 Future<int> main() { | 546 Future<int> main() { |
| 545 await foo(); | 547 await foo(); |
| 546 return 42; | 548 return 42; |
| 547 } | 549 } |
| 548 '''); | 550 '''); |
| 549 await assertHasFix( | 551 await assertHasFix( |
| 550 DartFixKind.ADD_ASYNC, | 552 DartFixKind.ADD_ASYNC, |
| 551 ''' | 553 ''' |
| 552 import 'dart:async'; | 554 import 'dart:async'; |
| 553 foo() {} | 555 foo() {} |
| 554 Future<int> main() async { | 556 Future<int> main() async { |
| 555 await foo(); | 557 await foo(); |
| 556 return 42; | 558 return 42; |
| 557 } | 559 } |
| 558 '''); | 560 '''); |
| 559 } | 561 } |
| 560 | 562 |
| 561 test_addSync_returnFuture_dynamic() async { | 563 test_addSync_returnFuture_dynamic() async { |
| 562 errorFilter = (AnalysisError error) { | 564 errorFilter = (AnalysisError error) { |
| 563 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; | 565 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT; |
| 564 }; | 566 }; |
| 565 resolveTestUnit(''' | 567 resolveTestUnit(''' |
| 566 foo() {} | 568 foo() {} |
| 567 dynamic main() { | 569 dynamic main() { |
| 568 await foo(); | 570 await foo(); |
| 569 return 42; | 571 return 42; |
| 570 } | 572 } |
| 571 '''); | 573 '''); |
| 572 await assertHasFix( | 574 await assertHasFix( |
| 573 DartFixKind.ADD_ASYNC, | 575 DartFixKind.ADD_ASYNC, |
| 574 ''' | 576 ''' |
| 575 foo() {} | 577 foo() {} |
| 576 dynamic main() async { | 578 dynamic main() async { |
| 577 await foo(); | 579 await foo(); |
| 578 return 42; | 580 return 42; |
| 579 } | 581 } |
| 580 '''); | 582 '''); |
| 581 } | 583 } |
| 582 | 584 |
| 583 test_addSync_returnFuture_noType() async { | 585 test_addSync_returnFuture_noType() async { |
| 584 errorFilter = (AnalysisError error) { | 586 errorFilter = (AnalysisError error) { |
| 585 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; | 587 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT; |
| 586 }; | 588 }; |
| 587 resolveTestUnit(''' | 589 resolveTestUnit(''' |
| 588 foo() {} | 590 foo() {} |
| 589 main() { | 591 main() { |
| 590 await foo(); | 592 await foo(); |
| 591 return 42; | 593 return 42; |
| 592 } | 594 } |
| 593 '''); | 595 '''); |
| 594 await assertHasFix( | 596 await assertHasFix( |
| 595 DartFixKind.ADD_ASYNC, | 597 DartFixKind.ADD_ASYNC, |
| (...skipping 4734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5330 @override | 5332 @override |
| 5331 void t() { } | 5333 void t() { } |
| 5332 } | 5334 } |
| 5333 '''); | 5335 '''); |
| 5334 } | 5336 } |
| 5335 | 5337 |
| 5336 void verifyResult(String expectedResult) { | 5338 void verifyResult(String expectedResult) { |
| 5337 expect(resultCode, expectedResult); | 5339 expect(resultCode, expectedResult); |
| 5338 } | 5340 } |
| 5339 } | 5341 } |
| OLD | NEW |