Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: pkg/analysis_services/test/correction/fix_test.dart

Issue 418203002: Implement more fixes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/analysis_services/lib/src/correction/util.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library test.services.correction.fix; 8 library test.services.correction.fix;
9 9
10 import 'package:analysis_services/correction/change.dart'; 10 import 'package:analysis_services/correction/change.dart';
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 if (group.id == id) { 52 if (group.id == id) {
53 expect(group.positions, unorderedEquals(expectedPositions)); 53 expect(group.positions, unorderedEquals(expectedPositions));
54 return; 54 return;
55 } 55 }
56 } 56 }
57 fail('No PositionGroup with id=$id found in $linkedPositionGroups'); 57 fail('No PositionGroup with id=$id found in $linkedPositionGroups');
58 } 58 }
59 59
60 void assertNoFix(FixKind kind) { 60 void assertNoFix(FixKind kind) {
61 AnalysisError error = _findErrorToFix(); 61 AnalysisError error = _findErrorToFix();
62 List<Fix> fixes = computeFixes(searchEngine, testFile, testUnit, error); 62 List<Fix> fixes = computeFixes(searchEngine, testUnit, error);
63 for (Fix fix in fixes) { 63 for (Fix fix in fixes) {
64 if (fix.kind == kind) { 64 if (fix.kind == kind) {
65 throw fail('Unexpected fix $kind in\n${fixes.join('\n')}'); 65 throw fail('Unexpected fix $kind in\n${fixes.join('\n')}');
66 } 66 }
67 } 67 }
68 } 68 }
69 69
70 Position expectedPosition(String search) { 70 Position expectedPosition(String search) {
71 int offset = resultCode.indexOf(search); 71 int offset = resultCode.indexOf(search);
72 int length = getLeadingIdentifierLength(search); 72 int length = getLeadingIdentifierLength(search);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 class A { 235 class A {
236 A._named(int p); 236 A._named(int p);
237 } 237 }
238 class B extends A { 238 class B extends A {
239 B() {} 239 B() {}
240 } 240 }
241 '''); 241 ''');
242 assertNoFix(FixKind.ADD_SUPER_CONSTRUCTOR_INVOCATION); 242 assertNoFix(FixKind.ADD_SUPER_CONSTRUCTOR_INVOCATION);
243 } 243 }
244 244
245 void test_createConstructorSuperImplicit() {
246 _indexTestUnit('''
247 class A {
248 A(p1, int p2, List<String> p3, [int p4]);
249 }
250 class B extends A {
251 int existingField;
252
253 void existingMethod() {}
254 }
255 ''');
256 assertHasFix(FixKind.CREATE_CONSTRUCTOR_SUPER, '''
257 class A {
258 A(p1, int p2, List<String> p3, [int p4]);
259 }
260 class B extends A {
261 int existingField;
262
263 B(p1, int p2, List<String> p3) : super(p1, p2, p3);
264
265 void existingMethod() {}
266 }
267 ''');
268 }
269
270 void test_createConstructorSuperImplicit_fieldInitializer() {
271 _indexTestUnit('''
272 class A {
273 int _field;
274 A(this._field);
275 }
276 class B extends A {
277 int existingField;
278
279 void existingMethod() {}
280 }
281 ''');
282 assertHasFix(FixKind.CREATE_CONSTRUCTOR_SUPER, '''
283 class A {
284 int _field;
285 A(this._field);
286 }
287 class B extends A {
288 int existingField;
289
290 B(int field) : super(field);
291
292 void existingMethod() {}
293 }
294 ''');
295 }
296
297 void test_createConstructorSuperImplicit_named() {
298 _indexTestUnit('''
299 class A {
300 A.named(p1, int p2);
301 }
302 class B extends A {
303 int existingField;
304
305 void existingMethod() {}
306 }
307 ''');
308 assertHasFix(FixKind.CREATE_CONSTRUCTOR_SUPER, '''
309 class A {
310 A.named(p1, int p2);
311 }
312 class B extends A {
313 int existingField;
314
315 B.named(p1, int p2) : super.named(p1, p2);
316
317 void existingMethod() {}
318 }
319 ''');
320 }
321
322 void test_createConstructorSuperImplicit_private() {
323 _indexTestUnit('''
324 class A {
325 A._named(p);
326 }
327 class B extends A {
328 }
329 ''');
330 assertNoFix(FixKind.CREATE_CONSTRUCTOR_SUPER);
331 }
332
245 void test_createConstructor_insteadOfSyntheticDefault() { 333 void test_createConstructor_insteadOfSyntheticDefault() {
246 _indexTestUnit(''' 334 _indexTestUnit('''
247 class A { 335 class A {
248 int field; 336 int field;
249 337
250 method() {} 338 method() {}
251 } 339 }
252 main() { 340 main() {
253 new A(1, 2.0); 341 new A(1, 2.0);
254 } 342 }
(...skipping 28 matching lines...) Expand all
283 } 371 }
284 372
285 method() {} 373 method() {}
286 } 374 }
287 main() { 375 main() {
288 new A.named(1, 2.0); 376 new A.named(1, 2.0);
289 } 377 }
290 '''); 378 ''');
291 } 379 }
292 380
381 void test_createMissingOverrides_functionType() {
382 _indexTestUnit('''
383 abstract class A {
384 forEach(int f(double p1, String p2));
385 }
386
387 class B extends A {
388 }
389 ''');
390 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
391 abstract class A {
392 forEach(int f(double p1, String p2));
393 }
394
395 class B extends A {
396 @override
397 forEach(int f(double p1, String p2)) {
398 // TODO: implement forEach
399 }
400 }
401 ''');
402 }
403
404 void test_createMissingOverrides_generics() {
405 _indexTestUnit('''
406 class Iterator<T> {
407 }
408
409 abstract class IterableMixin<T> {
410 Iterator<T> get iterator;
411 }
412
413 class Test extends IterableMixin<int> {
414 }
415 ''');
416 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
417 class Iterator<T> {
418 }
419
420 abstract class IterableMixin<T> {
421 Iterator<T> get iterator;
422 }
423
424 class Test extends IterableMixin<int> {
425 // TODO: implement iterator
426 @override
427 Iterator<int> get iterator => null;
428 }
429 ''');
430 }
431
432 void test_createMissingOverrides_getter() {
433 _indexTestUnit('''
434 abstract class A {
435 get g1;
436 int get g2;
437 }
438
439 class B extends A {
440 }
441 ''');
442 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
443 abstract class A {
444 get g1;
445 int get g2;
446 }
447
448 class B extends A {
449 // TODO: implement g1
450 @override
451 get g1 => null;
452
453 // TODO: implement g2
454 @override
455 int get g2 => null;
456 }
457 ''');
458 }
459
460 void test_createMissingOverrides_importPrefix() {
461 _indexTestUnit('''
462 import 'dart:async' as aaa;
463 abstract class A {
464 Map<aaa.Future, List<aaa.Future>> g(aaa.Future p);
465 }
466
467 class B extends A {
468 }
469 ''');
470 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
471 import 'dart:async' as aaa;
472 abstract class A {
473 Map<aaa.Future, List<aaa.Future>> g(aaa.Future p);
474 }
475
476 class B extends A {
477 @override
478 Map<aaa.Future, List<aaa.Future>> g(aaa.Future p) {
479 // TODO: implement g
480 }
481 }
482 ''');
483 }
484
485 void test_createMissingOverrides_method() {
486 _indexTestUnit('''
487 abstract class A {
488 m1();
489 int m2();
490 String m3(int p1, double p2, Map<int, List<String>> p3);
491 String m4(p1, p2);
492 String m5(p1, [int p2 = 2, int p3, p4 = 4]);
493 String m6(p1, {int p2: 2, int p3, p4: 4});
494 }
495
496 class B extends A {
497 }
498 ''');
499 String expectedCode = '''
500 abstract class A {
501 m1();
502 int m2();
503 String m3(int p1, double p2, Map<int, List<String>> p3);
504 String m4(p1, p2);
505 String m5(p1, [int p2 = 2, int p3, p4 = 4]);
506 String m6(p1, {int p2: 2, int p3, p4: 4});
507 }
508
509 class B extends A {
510 @override
511 m1() {
512 // TODO: implement m1
513 }
514
515 @override
516 int m2() {
517 // TODO: implement m2
518 }
519
520 @override
521 String m3(int p1, double p2, Map<int, List<String>> p3) {
522 // TODO: implement m3
523 }
524
525 @override
526 String m4(p1, p2) {
527 // TODO: implement m4
528 }
529
530 @override
531 String m5(p1, [int p2 = 2, int p3, p4 = 4]) {
532 // TODO: implement m5
533 }
534
535 @override
536 String m6(p1, {int p2: 2, int p3, p4: 4}) {
537 // TODO: implement m6
538 }
539 }
540 ''';
541 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, expectedCode);
542 // end position should be on "m1", not on "m2", "m3", etc
543 {
544 Position endPosition = change.endPosition;
545 expect(endPosition, isNotNull);
546 expect(endPosition.file, testFile);
547 int endOffset = endPosition.offset;
548 String endString = expectedCode.substring(endOffset, endOffset + 25);
549 expect(endString, contains('m1'));
550 expect(endString, isNot(contains('m2')));
551 expect(endString, isNot(contains('m3')));
552 expect(endString, isNot(contains('m4')));
553 expect(endString, isNot(contains('m5')));
554 expect(endString, isNot(contains('m6')));
555 }
556 }
557
558 void test_createMissingOverrides_operator() {
559 _indexTestUnit('''
560 abstract class A {
561 int operator [](int index);
562 void operator []=(int index, String value);
563 }
564
565 class B extends A {
566 }
567 ''');
568 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
569 abstract class A {
570 int operator [](int index);
571 void operator []=(int index, String value);
572 }
573
574 class B extends A {
575 @override
576 int operator [](int index) {
577 // TODO: implement []
578 }
579
580 @override
581 void operator []=(int index, String value) {
582 // TODO: implement []=
583 }
584 }
585 ''');
586 }
587
588 void test_createMissingOverrides_setter() {
589 _indexTestUnit('''
590 abstract class A {
591 set s1(x);
592 set s2(int x);
593 void set s3(String x);
594 }
595
596 class B extends A {
597 }
598 ''');
599 assertHasFix(FixKind.CREATE_MISSING_OVERRIDES, '''
600 abstract class A {
601 set s1(x);
602 set s2(int x);
603 void set s3(String x);
604 }
605
606 class B extends A {
607 @override
608 set s1(x) {
609 // TODO: implement s1
610 }
611
612 @override
613 set s2(int x) {
614 // TODO: implement s2
615 }
616
617 @override
618 void set s3(String x) {
619 // TODO: implement s3
620 }
621 }
622 ''');
623 }
624
625 void test_createNoSuchMethod() {
626 _indexTestUnit('''
627 abstract class A {
628 m1();
629 int m2();
630 }
631
632 class B extends A {
633 existing() {}
634 }
635 ''');
636 assertHasFix(FixKind.CREATE_NO_SUCH_METHOD, '''
637 abstract class A {
638 m1();
639 int m2();
640 }
641
642 class B extends A {
643 existing() {}
644
645 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
646 }
647 ''');
648 }
649
650 void test_creationFunction_forFunctionType_cascadeSecond() {
651 _indexTestUnit('''
652 class A {
653 B ma() => null;
654 }
655 class B {
656 useFunction(int g(double a, String b)) {}
657 }
658
659 main() {
660 A a = new A();
661 a..ma().useFunction(test);
662 }
663 ''');
664 assertHasFix(FixKind.CREATE_FUNCTION, '''
665 class A {
666 B ma() => null;
667 }
668 class B {
669 useFunction(int g(double a, String b)) {}
670 }
671
672 main() {
673 A a = new A();
674 a..ma().useFunction(test);
675 }
676
677 int test(double a, String b) {
678 }
679 ''');
680 }
681
682 void test_creationFunction_forFunctionType_dynamicArgument() {
683 _indexTestUnit('''
684 main() {
685 useFunction(test);
686 }
687 useFunction(int g(a, b)) {}
688 ''');
689 assertHasFix(FixKind.CREATE_FUNCTION, '''
690 main() {
691 useFunction(test);
692 }
693 useFunction(int g(a, b)) {}
694
695 int test(a, b) {
696 }
697 ''');
698 }
699
700 void test_creationFunction_forFunctionType_function() {
701 _indexTestUnit('''
702 main() {
703 useFunction(test);
704 }
705 useFunction(int g(double a, String b)) {}
706 ''');
707 assertHasFix(FixKind.CREATE_FUNCTION, '''
708 main() {
709 useFunction(test);
710 }
711 useFunction(int g(double a, String b)) {}
712
713 int test(double a, String b) {
714 }
715 ''');
716 }
717
718 void test_creationFunction_forFunctionType_method_enclosingClass_static() {
719 _indexTestUnit('''
720 class A {
721 static foo() {
722 useFunction(test);
723 }
724 }
725 useFunction(int g(double a, String b)) {}
726 ''');
727 assertHasFix(FixKind.CREATE_METHOD, '''
728 class A {
729 static foo() {
730 useFunction(test);
731 }
732
733 static int test(double a, String b) {
734 }
735 }
736 useFunction(int g(double a, String b)) {}
737 ''');
738 }
739
740 void test_creationFunction_forFunctionType_method_enclosingClass_static2() {
741 _indexTestUnit('''
742 class A {
743 var f;
744 A() : f = useFunction(test);
745 }
746 useFunction(int g(double a, String b)) {}
747 ''');
748 assertHasFix(FixKind.CREATE_METHOD, '''
749 class A {
750 var f;
751 A() : f = useFunction(test);
752
753 static int test(double a, String b) {
754 }
755 }
756 useFunction(int g(double a, String b)) {}
757 ''');
758 }
759
760 void test_creationFunction_forFunctionType_method_targetClass() {
761 _indexTestUnit('''
762 main(A a) {
763 useFunction(a.test);
764 }
765 class A {
766 }
767 useFunction(int g(double a, String b)) {}
768 ''');
769 assertHasFix(FixKind.CREATE_METHOD, '''
770 main(A a) {
771 useFunction(a.test);
772 }
773 class A {
774 int test(double a, String b) {
775 }
776 }
777 useFunction(int g(double a, String b)) {}
778 ''');
779 }
780
781 void
782 test_creationFunction_forFunctionType_method_targetClass_hasOtherMember() {
783 _indexTestUnit('''
784 main(A a) {
785 useFunction(a.test);
786 }
787 class A {
788 m() {}
789 }
790 useFunction(int g(double a, String b)) {}
791 ''');
792 assertHasFix(FixKind.CREATE_METHOD, '''
793 main(A a) {
794 useFunction(a.test);
795 }
796 class A {
797 m() {}
798
799 int test(double a, String b) {
800 }
801 }
802 useFunction(int g(double a, String b)) {}
803 ''');
804 }
805
806 void test_creationFunction_forFunctionType_notFunctionType() {
807 _indexTestUnit('''
808 main(A a) {
809 useFunction(a.test);
810 }
811 typedef A();
812 useFunction(g) {}
813 ''');
814 assertNoFix(FixKind.CREATE_METHOD);
815 assertNoFix(FixKind.CREATE_FUNCTION);
816 }
817
818 void test_creationFunction_forFunctionType_unknownTarget() {
819 _indexTestUnit('''
820 main(A a) {
821 useFunction(a.test);
822 }
823 class A {
824 }
825 useFunction(g) {}
826 ''');
827 assertNoFix(FixKind.CREATE_METHOD);
828 }
829
293 void test_expectedToken_semicolon() { 830 void test_expectedToken_semicolon() {
294 _indexTestUnit(''' 831 _indexTestUnit('''
295 main() { 832 main() {
296 print(0) 833 print(0)
297 } 834 }
298 '''); 835 ''');
299 assertHasFix(FixKind.INSERT_SEMICOLON, ''' 836 assertHasFix(FixKind.INSERT_SEMICOLON, '''
300 main() { 837 main() {
301 print(0); 838 print(0);
302 } 839 }
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 const a = new A(); 995 const a = new A();
459 '''); 996 ''');
460 assertHasFix(FixKind.USE_CONST, ''' 997 assertHasFix(FixKind.USE_CONST, '''
461 class A { 998 class A {
462 const A(); 999 const A();
463 } 1000 }
464 const a = const A(); 1001 const a = const A();
465 '''); 1002 ''');
466 } 1003 }
467 1004
1005 void test_undefinedMethod_createQualified_fromClass() {
1006 _indexTestUnit('''
1007 class A {
1008 }
1009 main() {
1010 A.myUndefinedMethod();
1011 }
1012 ''');
1013 assertHasFix(FixKind.CREATE_METHOD, '''
1014 class A {
1015 static void myUndefinedMethod() {
1016 }
1017 }
1018 main() {
1019 A.myUndefinedMethod();
1020 }
1021 ''');
1022 }
1023
1024 void test_undefinedMethod_createQualified_fromClass_hasOtherMember() {
1025 _indexTestUnit('''
1026 class A {
1027 foo() {}
1028 }
1029 main() {
1030 A.myUndefinedMethod();
1031 }
1032 ''');
1033 assertHasFix(FixKind.CREATE_METHOD, '''
1034 class A {
1035 foo() {}
1036
1037 static void myUndefinedMethod() {
1038 }
1039 }
1040 main() {
1041 A.myUndefinedMethod();
1042 }
1043 ''');
1044 }
1045
1046 void test_undefinedMethod_createQualified_fromClass_unresolved() {
1047 _indexTestUnit('''
1048 main() {
1049 NoSuchClass.myUndefinedMethod();
1050 }
1051 ''');
1052 assertNoFix(FixKind.CREATE_METHOD);
1053 }
1054
468 void test_useEffectiveIntegerDivision() { 1055 void test_useEffectiveIntegerDivision() {
469 _indexTestUnit(''' 1056 _indexTestUnit('''
470 main() { 1057 main() {
471 var a = 5; 1058 var a = 5;
472 var b = 2; 1059 var b = 2;
473 print((a / b).toInt()); 1060 print((a / b).toInt());
474 } 1061 }
475 '''); 1062 ''');
476 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, ''' 1063 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, '''
477 main() { 1064 main() {
(...skipping 11 matching lines...) Expand all
489 edit.replacement + 1076 edit.replacement +
490 code.substring(edit.end); 1077 code.substring(edit.end);
491 }); 1078 });
492 return code; 1079 return code;
493 } 1080 }
494 1081
495 /** 1082 /**
496 * Computes fixes and verifies that there is a fix of the given kind. 1083 * Computes fixes and verifies that there is a fix of the given kind.
497 */ 1084 */
498 Fix _assertHasFix(FixKind kind, AnalysisError error) { 1085 Fix _assertHasFix(FixKind kind, AnalysisError error) {
499 List<Fix> fixes = computeFixes(searchEngine, testFile, testUnit, error); 1086 List<Fix> fixes = computeFixes(searchEngine, testUnit, error);
500 for (Fix fix in fixes) { 1087 for (Fix fix in fixes) {
501 if (fix.kind == kind) { 1088 if (fix.kind == kind) {
502 return fix; 1089 return fix;
503 } 1090 }
504 } 1091 }
505 throw fail('Expected to find fix $kind in\n${fixes.join('\n')}'); 1092 throw fail('Expected to find fix $kind in\n${fixes.join('\n')}');
506 } 1093 }
507 1094
508 AnalysisError _findErrorToFix() { 1095 AnalysisError _findErrorToFix() {
509 List<AnalysisError> errors = context.computeErrors(testSource); 1096 List<AnalysisError> errors = context.computeErrors(testSource);
510 expect( 1097 expect(
511 errors, 1098 errors,
512 hasLength(1), 1099 hasLength(1),
513 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' + 1100 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' +
514 errors.join('\n')); 1101 errors.join('\n'));
515 return errors[0]; 1102 return errors[0];
516 } 1103 }
517 1104
518 void _indexTestUnit(String code) { 1105 void _indexTestUnit(String code) {
519 resolveTestUnit(code); 1106 resolveTestUnit(code);
520 index.indexUnit(context, testUnit); 1107 index.indexUnit(context, testUnit);
521 } 1108 }
522 } 1109 }
OLDNEW
« no previous file with comments | « pkg/analysis_services/lib/src/correction/util.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698