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

Side by Side Diff: packages/analyzer/test/generated/incremental_resolver_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library engine.incremental_resolver_test;
6
7 import 'package:analyzer/src/context/cache.dart' as task;
8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:analyzer/src/generated/element.dart';
10 import 'package:analyzer/src/generated/engine.dart';
11 import 'package:analyzer/src/generated/error.dart';
12 import 'package:analyzer/src/generated/incremental_logger.dart' as log;
13 import 'package:analyzer/src/generated/incremental_resolution_validator.dart';
14 import 'package:analyzer/src/generated/incremental_resolver.dart';
15 import 'package:analyzer/src/generated/java_engine.dart';
16 import 'package:analyzer/src/generated/parser.dart';
17 import 'package:analyzer/src/generated/resolver.dart';
18 import 'package:analyzer/src/generated/scanner.dart';
19 import 'package:analyzer/src/generated/source_io.dart';
20 import 'package:analyzer/src/generated/testing/ast_factory.dart';
21 import 'package:analyzer/src/generated/testing/element_factory.dart';
22 import 'package:analyzer/task/dart.dart';
23 import 'package:unittest/unittest.dart';
24
25 import '../reflective_tests.dart';
26 import 'parser_test.dart';
27 import 'resolver_test.dart';
28 import 'test_support.dart';
29
30 main() {
31 initializeTestEnvironment();
32 runReflectiveTests(DeclarationMatcherTest);
33 runReflectiveTests(IncrementalResolverTest);
34 runReflectiveTests(PoorMansIncrementalResolutionTest);
35 runReflectiveTests(ResolutionContextBuilderTest);
36 }
37
38 void initializeTestEnvironment() {}
39
40 void _assertEqualError(AnalysisError incrError, AnalysisError fullError) {
41 if (incrError.errorCode != fullError.errorCode ||
42 incrError.source != fullError.source ||
43 incrError.offset != fullError.offset ||
44 incrError.length != fullError.length ||
45 incrError.message != fullError.message) {
46 StringBuffer buffer = new StringBuffer();
47 buffer.writeln('Found error does not match expected error:');
48 if (incrError.errorCode == fullError.errorCode) {
49 buffer.write(' errorCode = ');
50 buffer.write(fullError.errorCode.uniqueName);
51 } else {
52 buffer.write(' Expected errorCode = ');
53 buffer.write(fullError.errorCode.uniqueName);
54 buffer.write(' found ');
55 buffer.write(incrError.errorCode.uniqueName);
56 }
57 buffer.writeln();
58 if (incrError.source == fullError.source) {
59 buffer.write(' source = ');
60 buffer.write(fullError.source);
61 } else {
62 buffer.write(' Expected source = ');
63 buffer.write(fullError.source);
64 buffer.write(' found ');
65 buffer.write(incrError.source);
66 }
67 buffer.writeln();
68 if (incrError.offset == fullError.offset) {
69 buffer.write(' offset = ');
70 buffer.write(fullError.offset);
71 } else {
72 buffer.write(' Expected offset = ');
73 buffer.write(fullError.offset);
74 buffer.write(' found ');
75 buffer.write(incrError.offset);
76 }
77 buffer.writeln();
78 if (incrError.length == fullError.length) {
79 buffer.write(' length = ');
80 buffer.write(fullError.length);
81 } else {
82 buffer.write(' Expected length = ');
83 buffer.write(fullError.length);
84 buffer.write(' found ');
85 buffer.write(incrError.length);
86 }
87 buffer.writeln();
88 if (incrError.message == fullError.message) {
89 buffer.write(' message = ');
90 buffer.write(fullError.message);
91 } else {
92 buffer.write(' Expected message = ');
93 buffer.write(fullError.message);
94 buffer.write(' found ');
95 buffer.write(incrError.message);
96 }
97 fail(buffer.toString());
98 }
99 }
100
101 void _assertEqualErrors(
102 List<AnalysisError> incrErrors, List<AnalysisError> fullErrors) {
103 expect(incrErrors, hasLength(fullErrors.length));
104 if (incrErrors.isNotEmpty) {
105 incrErrors.sort((a, b) => a.offset - b.offset);
106 }
107 if (fullErrors.isNotEmpty) {
108 fullErrors.sort((a, b) => a.offset - b.offset);
109 }
110 int length = incrErrors.length;
111 for (int i = 0; i < length; i++) {
112 AnalysisError incrError = incrErrors[i];
113 AnalysisError fullError = fullErrors[i];
114 _assertEqualError(incrError, fullError);
115 }
116 }
117
118 @reflectiveTest
119 class DeclarationMatcherTest extends ResolverTestCase {
120 void setUp() {
121 super.setUp();
122 test_resolveApiChanges = true;
123 }
124
125 void test_false_class_annotation_accessor_edit() {
126 _assertDoesNotMatch(
127 r'''
128 const my_annotationA = const Object();
129 const my_annotationB = const Object();
130 @my_annotationA
131 class A {
132 }
133 ''',
134 r'''
135 const my_annotationA = const Object();
136 const my_annotationB = const Object();
137 @my_annotationB
138 class A {
139 }
140 ''');
141 }
142
143 void test_false_class_annotation_constructor_edit() {
144 _assertDoesNotMatch(
145 r'''
146 class MyAnnotationA {
147 const MyAnnotationA();
148 }
149 class MyAnnotationB {
150 const MyAnnotationB();
151 }
152 @MyAnnotationA()
153 class A {
154 }
155 ''',
156 r'''
157 class MyAnnotationA {
158 const MyAnnotationA();
159 }
160 class MyAnnotationB {
161 const MyAnnotationB();
162 }
163 @MyAnnotationB()
164 class A {
165 }
166 ''');
167 }
168
169 void test_false_class_annotations_add() {
170 _assertDoesNotMatch(
171 r'''
172 const my_annotation = const Object();
173 class A {
174 }
175 ''',
176 r'''
177 const my_annotation = const Object();
178 @my_annotation
179 class A {
180 }
181 ''');
182 }
183
184 void test_false_class_annotations_remove() {
185 _assertDoesNotMatch(
186 r'''
187 const my_annotation = const Object();
188 @my_annotation
189 class A {
190 }
191 ''',
192 r'''
193 const my_annotation = const Object();
194 class A {
195 }
196 ''');
197 }
198
199 void test_false_class_list_add() {
200 _assertDoesNotMatch(
201 r'''
202 class A {}
203 class B {}
204 ''',
205 r'''
206 class A {}
207 class B {}
208 class C {}
209 ''');
210 }
211
212 void test_false_class_list_remove() {
213 _assertDoesNotMatch(
214 r'''
215 class A {}
216 class B {}
217 class C {}
218 ''',
219 r'''
220 class A {}
221 class B {}
222 ''');
223 }
224
225 void test_false_class_typeParameters_bounds_add() {
226 _assertDoesNotMatch(
227 r'''
228 class A {}
229 class B<T> {
230 T f;
231 }
232 ''',
233 r'''
234 class A {}
235 class B<T extends A> {
236 T f;
237 }
238 ''');
239 }
240
241 void test_false_class_typeParameters_bounds_remove() {
242 _assertDoesNotMatch(
243 r'''
244 class A {}
245 class B<T extends A> {
246 T f;
247 }
248 ''',
249 r'''
250 class A {}
251 class B<T> {
252 T f;
253 }
254 ''');
255 }
256
257 void test_false_classMemberAccessor_list_add() {
258 _assertDoesNotMatchOK(
259 r'''
260 class A {
261 get a => 1;
262 get b => 2;
263 }
264 ''',
265 r'''
266 class A {
267 get a => 1;
268 get b => 2;
269 get c => 3;
270 }
271 ''');
272 }
273
274 void test_false_classMemberAccessor_list_remove() {
275 _assertDoesNotMatch(
276 r'''
277 class A {
278 get a => 1;
279 get b => 2;
280 get c => 3;
281 }
282 ''',
283 r'''
284 class A {
285 get a => 1;
286 get b => 2;
287 }
288 ''');
289 }
290
291 void test_false_classMemberAccessor_wasGetter() {
292 _assertDoesNotMatchOK(
293 r'''
294 class A {
295 get a => 1;
296 }
297 ''',
298 r'''
299 class A {
300 set a(x) {}
301 }
302 ''');
303 }
304
305 void test_false_classMemberAccessor_wasInstance() {
306 _assertDoesNotMatchOK(
307 r'''
308 class A {
309 get a => 1;
310 }
311 ''',
312 r'''
313 class A {
314 static get a => 1;
315 }
316 ''');
317 }
318
319 void test_false_classMemberAccessor_wasSetter() {
320 _assertDoesNotMatchOK(
321 r'''
322 class A {
323 set a(x) {}
324 }
325 ''',
326 r'''
327 class A {
328 get a => 1;
329 }
330 ''');
331 }
332
333 void test_false_classMemberAccessor_wasStatic() {
334 _assertDoesNotMatchOK(
335 r'''
336 class A {
337 static get a => 1;
338 }
339 ''',
340 r'''
341 class A {
342 get a => 1;
343 }
344 ''');
345 }
346
347 void test_false_classTypeAlias_list_add() {
348 _assertDoesNotMatch(
349 r'''
350 class M {}
351 class A = Object with M;
352 ''',
353 r'''
354 class M {}
355 class A = Object with M;
356 class B = Object with M;
357 ''');
358 }
359
360 void test_false_classTypeAlias_list_remove() {
361 _assertDoesNotMatch(
362 r'''
363 class M {}
364 class A = Object with M;
365 class B = Object with M;
366 ''',
367 r'''
368 class M {}
369 class A = Object with M;
370 ''');
371 }
372
373 void test_false_classTypeAlias_typeParameters_bounds_add() {
374 _assertDoesNotMatch(
375 r'''
376 class M<T> {}
377 class A {}
378 class B<T> = Object with M<T>;
379 ''',
380 r'''
381 class M<T> {}
382 class A {}
383 class B<T extends A> = Object with M<T>;
384 ''');
385 }
386
387 void test_false_classTypeAlias_typeParameters_bounds_remove() {
388 _assertDoesNotMatch(
389 r'''
390 class M<T> {}
391 class A {}
392 class B<T extends A> = Object with M<T>;
393 ''',
394 r'''
395 class M<T> {}
396 class A {}
397 class B<T> = Object with M<T>;
398 ''');
399 }
400
401 void test_false_constructor_keywordConst_add() {
402 _assertDoesNotMatch(
403 r'''
404 class A {
405 A();
406 }
407 ''',
408 r'''
409 class A {
410 const A();
411 }
412 ''');
413 }
414
415 void test_false_constructor_keywordConst_remove() {
416 _assertDoesNotMatch(
417 r'''
418 class A {
419 const A();
420 }
421 ''',
422 r'''
423 class A {
424 A();
425 }
426 ''');
427 }
428
429 void test_false_constructor_keywordFactory_add() {
430 _assertDoesNotMatch(
431 r'''
432 class A {
433 A();
434 A.foo() {
435 return new A();
436 }
437 }
438 ''',
439 r'''
440 class A {
441 A();
442 factory A.foo() {
443 return new A();
444 }
445 }
446 ''');
447 }
448
449 void test_false_constructor_keywordFactory_remove() {
450 _assertDoesNotMatch(
451 r'''
452 class A {
453 A();
454 factory A.foo() {
455 return new A();
456 }
457 }
458 ''',
459 r'''
460 class A {
461 A();
462 A.foo() {
463 return new A();
464 }
465 }
466 ''');
467 }
468
469 void test_false_constructor_parameters_list_add() {
470 _assertDoesNotMatch(
471 r'''
472 class A {
473 A();
474 }
475 ''',
476 r'''
477 class A {
478 A(int p);
479 }
480 ''');
481 }
482
483 void test_false_constructor_parameters_list_remove() {
484 _assertDoesNotMatch(
485 r'''
486 class A {
487 A(int p);
488 }
489 ''',
490 r'''
491 class A {
492 A();
493 }
494 ''');
495 }
496
497 void test_false_constructor_parameters_type_edit() {
498 _assertDoesNotMatch(
499 r'''
500 class A {
501 A(int p);
502 }
503 ''',
504 r'''
505 class A {
506 A(String p);
507 }
508 ''');
509 }
510
511 void test_false_constructor_unnamed_add_hadParameters() {
512 _assertDoesNotMatch(
513 r'''
514 class A {
515 }
516 ''',
517 r'''
518 class A {
519 A(int p) {}
520 }
521 ''');
522 }
523
524 void test_false_constructor_unnamed_remove_hadParameters() {
525 _assertDoesNotMatch(
526 r'''
527 class A {
528 A(int p) {}
529 }
530 ''',
531 r'''
532 class A {
533 }
534 ''');
535 }
536
537 void test_false_defaultFieldFormalParameterElement_wasSimple() {
538 _assertDoesNotMatch(
539 r'''
540 class A {
541 int field;
542 A(int field);
543 }
544 ''',
545 r'''
546 class A {
547 int field;
548 A([this.field = 0]);
549 }
550 ''');
551 }
552
553 void test_false_enum_constants_add() {
554 _assertDoesNotMatch(
555 r'''
556 enum E {A, B}
557 ''',
558 r'''
559 enum E {A, B, C}
560 ''');
561 }
562
563 void test_false_enum_constants_remove() {
564 _assertDoesNotMatch(
565 r'''
566 enum E {A, B, C}
567 ''',
568 r'''
569 enum E {A, B}
570 ''');
571 }
572
573 void test_false_export_hide_add() {
574 _assertDoesNotMatch(
575 r'''
576 export 'dart:async' hide Future;
577 ''',
578 r'''
579 export 'dart:async' hide Future, Stream;
580 ''');
581 }
582
583 void test_false_export_hide_remove() {
584 _assertDoesNotMatch(
585 r'''
586 export 'dart:async' hide Future, Stream;
587 ''',
588 r'''
589 export 'dart:async' hide Future;
590 ''');
591 }
592
593 void test_false_export_list_add() {
594 _assertDoesNotMatch(
595 r'''
596 export 'dart:async';
597 ''',
598 r'''
599 export 'dart:async';
600 export 'dart:math';
601 ''');
602 }
603
604 void test_false_export_list_remove() {
605 _assertDoesNotMatch(
606 r'''
607 export 'dart:async';
608 export 'dart:math';
609 ''',
610 r'''
611 export 'dart:async';
612 ''');
613 }
614
615 void test_false_export_show_add() {
616 _assertDoesNotMatch(
617 r'''
618 export 'dart:async' show Future;
619 ''',
620 r'''
621 export 'dart:async' show Future, Stream;
622 ''');
623 }
624
625 void test_false_export_show_remove() {
626 _assertDoesNotMatch(
627 r'''
628 export 'dart:async' show Future, Stream;
629 ''',
630 r'''
631 export 'dart:async' show Future;
632 ''');
633 }
634
635 void test_false_extendsClause_add() {
636 _assertDoesNotMatch(
637 r'''
638 class A {}
639 class B {}
640 ''',
641 r'''
642 class A {}
643 class B extends A {}
644 ''');
645 }
646
647 void test_false_extendsClause_different() {
648 _assertDoesNotMatch(
649 r'''
650 class A {}
651 class B {}
652 class C extends A {}
653 ''',
654 r'''
655 class A {}
656 class B {}
657 class C extends B {}
658 ''');
659 }
660
661 void test_false_extendsClause_remove() {
662 _assertDoesNotMatch(
663 r'''
664 class A {}
665 class B extends A{}
666 ''',
667 r'''
668 class A {}
669 class B {}
670 ''');
671 }
672
673 void test_false_field_list_add() {
674 _assertDoesNotMatch(
675 r'''
676 class T {
677 int A = 1;
678 int C = 3;
679 }
680 ''',
681 r'''
682 class T {
683 int A = 1;
684 int B = 2;
685 int C = 3;
686 }
687 ''');
688 }
689
690 void test_false_field_list_remove() {
691 _assertDoesNotMatch(
692 r'''
693 class T {
694 int A = 1;
695 int B = 2;
696 int C = 3;
697 }
698 ''',
699 r'''
700 class T {
701 int A = 1;
702 int C = 3;
703 }
704 ''');
705 }
706
707 void test_false_field_modifier_isConst() {
708 _assertDoesNotMatch(
709 r'''
710 class T {
711 static final A = 1;
712 }
713 ''',
714 r'''
715 class T {
716 static const A = 1;
717 }
718 ''');
719 }
720
721 void test_false_field_modifier_isFinal() {
722 _assertDoesNotMatch(
723 r'''
724 class T {
725 int A = 1;
726 }
727 ''',
728 r'''
729 class T {
730 final int A = 1;
731 }
732 ''');
733 }
734
735 void test_false_field_modifier_isStatic() {
736 _assertDoesNotMatch(
737 r'''
738 class T {
739 int A = 1;
740 }
741 ''',
742 r'''
743 class T {
744 static int A = 1;
745 }
746 ''');
747 }
748
749 void test_false_field_modifier_wasConst() {
750 _assertDoesNotMatch(
751 r'''
752 class T {
753 static const A = 1;
754 }
755 ''',
756 r'''
757 class T {
758 static final A = 1;
759 }
760 ''');
761 }
762
763 void test_false_field_modifier_wasFinal() {
764 _assertDoesNotMatch(
765 r'''
766 class T {
767 final int A = 1;
768 }
769 ''',
770 r'''
771 class T {
772 int A = 1;
773 }
774 ''');
775 }
776
777 void test_false_field_modifier_wasStatic() {
778 _assertDoesNotMatch(
779 r'''
780 class T {
781 static int A = 1;
782 }
783 ''',
784 r'''
785 class T {
786 int A = 1;
787 }
788 ''');
789 }
790
791 void test_false_field_type_differentArgs() {
792 _assertDoesNotMatch(
793 r'''
794 class T {
795 List<int> A;
796 }
797 ''',
798 r'''
799 class T {
800 List<String> A;
801 }
802 ''');
803 }
804
805 void test_false_fieldFormalParameter_add() {
806 _assertDoesNotMatch(
807 r'''
808 class A {
809 final field;
810 A(field);
811 }
812 ''',
813 r'''
814 class A {
815 final field;
816 A(this.field);
817 }
818 ''');
819 }
820
821 void test_false_fieldFormalParameter_add_function() {
822 _assertDoesNotMatch(
823 r'''
824 class A {
825 final field;
826 A(field(a));
827 }
828 ''',
829 r'''
830 class A {
831 final field;
832 A(this.field(a));
833 }
834 ''');
835 }
836
837 void test_false_fieldFormalParameter_differentField() {
838 _assertDoesNotMatch(
839 r'''
840 class A {
841 final aaa;
842 final bbb;
843 A(this.aaa, this.bbb);
844 }
845 ''',
846 r'''
847 class A {
848 final aaa;
849 final bbb;
850 A(this.bbb, this.aaa);
851 }
852 ''');
853 }
854
855 void test_false_fieldFormalParameter_parameters_add() {
856 _assertDoesNotMatch(
857 r'''
858 class A {
859 final field;
860 A(this.field(a));
861 }
862 ''',
863 r'''
864 class A {
865 final field;
866 A(this.field(a, b));
867 }
868 ''');
869 }
870
871 void test_false_fieldFormalParameter_parameters_remove() {
872 _assertDoesNotMatch(
873 r'''
874 class A {
875 final field;
876 A(this.field(a, b));
877 }
878 ''',
879 r'''
880 class A {
881 final field;
882 A(this.field(a));
883 }
884 ''');
885 }
886
887 void test_false_fieldFormalParameter_parameters_typeEdit() {
888 _assertDoesNotMatch(
889 r'''
890 class A {
891 final field;
892 A(this.field(int p));
893 }
894 ''',
895 r'''
896 class A {
897 final field;
898 A(this.field(String p));
899 }
900 ''');
901 }
902
903 void test_false_fieldFormalParameter_remove_default() {
904 _assertDoesNotMatch(
905 r'''
906 class A {
907 final field;
908 A([this.field = 0]);
909 }
910 ''',
911 r'''
912 class A {
913 final field;
914 A([field = 0]);
915 }
916 ''');
917 }
918
919 void test_false_fieldFormalParameter_remove_function() {
920 _assertDoesNotMatch(
921 r'''
922 class A {
923 final field;
924 A(this.field(a));
925 }
926 ''',
927 r'''
928 class A {
929 final field;
930 A(field(a));
931 }
932 ''');
933 }
934
935 void test_false_fieldFormalParameter_remove_normal() {
936 _assertDoesNotMatch(
937 r'''
938 class A {
939 final field;
940 A(this.field);
941 }
942 ''',
943 r'''
944 class A {
945 final field;
946 A(field);
947 }
948 ''');
949 }
950
951 void test_false_fieldFormalParameter_typeAdd() {
952 _assertDoesNotMatch(
953 r'''
954 class A {
955 final fff;
956 A(this.fff);
957 }
958 ''',
959 r'''
960 class A {
961 final fff;
962 A(int this.fff);
963 }
964 ''');
965 }
966
967 void test_false_fieldFormalParameter_typeEdit() {
968 _assertDoesNotMatch(
969 r'''
970 class A {
971 final fff;
972 A(int this.fff);
973 }
974 ''',
975 r'''
976 class A {
977 final fff;
978 A(String this.fff);
979 }
980 ''');
981 }
982
983 void test_false_fieldFormalParameter_typeRemove() {
984 _assertDoesNotMatch(
985 r'''
986 class A {
987 final fff;
988 A(int this.fff);
989 }
990 ''',
991 r'''
992 class A {
993 final fff;
994 A(this.fff);
995 }
996 ''');
997 }
998
999 void test_false_fieldFormalParameterElement_wasSimple() {
1000 _assertDoesNotMatch(
1001 r'''
1002 class A {
1003 int field;
1004 A(int field);
1005 }
1006 ''',
1007 r'''
1008 class A {
1009 int field;
1010 A(this.field);
1011 }
1012 ''');
1013 }
1014
1015 void test_false_final_type_different() {
1016 _assertDoesNotMatch(
1017 r'''
1018 class T {
1019 int A;
1020 }
1021 ''',
1022 r'''
1023 class T {
1024 String A;
1025 }
1026 ''');
1027 }
1028
1029 void test_false_function_async_add() {
1030 _assertDoesNotMatch(
1031 r'''
1032 main() {}
1033 ''',
1034 r'''
1035 main() async {}
1036 ''');
1037 }
1038
1039 void test_false_function_async_remove() {
1040 _assertDoesNotMatch(
1041 r'''
1042 main() async {}
1043 ''',
1044 r'''
1045 main() {}
1046 ''');
1047 }
1048
1049 void test_false_function_generator_add() {
1050 _assertDoesNotMatch(
1051 r'''
1052 main() async {}
1053 ''',
1054 r'''
1055 main() async* {}
1056 ''');
1057 }
1058
1059 void test_false_function_generator_remove() {
1060 _assertDoesNotMatch(
1061 r'''
1062 main() async* {}
1063 ''',
1064 r'''
1065 main() async {}
1066 ''');
1067 }
1068
1069 void test_false_functionTypeAlias_list_add() {
1070 _assertDoesNotMatch(
1071 r'''
1072 typedef A(int pa);
1073 typedef B(String pb);
1074 ''',
1075 r'''
1076 typedef A(int pa);
1077 typedef B(String pb);
1078 typedef C(pc);
1079 ''');
1080 }
1081
1082 void test_false_functionTypeAlias_list_remove() {
1083 _assertDoesNotMatch(
1084 r'''
1085 typedef A(int pa);
1086 typedef B(String pb);
1087 typedef C(pc);
1088 ''',
1089 r'''
1090 typedef A(int pa);
1091 typedef B(String pb);
1092 ''');
1093 }
1094
1095 void test_false_functionTypeAlias_parameters_list_add() {
1096 _assertDoesNotMatch(
1097 r'''
1098 typedef A(a);
1099 ''',
1100 r'''
1101 typedef A(a, b);
1102 ''');
1103 }
1104
1105 void test_false_functionTypeAlias_parameters_list_remove() {
1106 _assertDoesNotMatch(
1107 r'''
1108 typedef A(a, b);
1109 ''',
1110 r'''
1111 typedef A(a);
1112 ''');
1113 }
1114
1115 void test_false_functionTypeAlias_parameters_type_edit() {
1116 _assertDoesNotMatch(
1117 r'''
1118 typedef A(int p);
1119 ''',
1120 r'''
1121 typedef A(String p);
1122 ''');
1123 }
1124
1125 void test_false_functionTypeAlias_returnType_edit() {
1126 _assertDoesNotMatch(
1127 r'''
1128 typedef int A();
1129 ''',
1130 r'''
1131 typedef String A();
1132 ''');
1133 }
1134
1135 void test_false_functionTypeAlias_typeParameters_bounds_add() {
1136 _assertDoesNotMatch(
1137 r'''
1138 class A {}
1139 typedef F<T>();
1140 ''',
1141 r'''
1142 class A {}
1143 typedef F<T extends A>();
1144 ''');
1145 }
1146
1147 void test_false_functionTypeAlias_typeParameters_bounds_edit() {
1148 _assertDoesNotMatch(
1149 r'''
1150 class A {}
1151 class B {}
1152 typedef F<T extends A>();
1153 ''',
1154 r'''
1155 class A {}
1156 typedef F<T extends B>();
1157 ''');
1158 }
1159
1160 void test_false_functionTypeAlias_typeParameters_bounds_remove() {
1161 _assertDoesNotMatch(
1162 r'''
1163 class A {}
1164 typedef F<T extends A>();
1165 ''',
1166 r'''
1167 class A {}
1168 typedef F<T>();
1169 ''');
1170 }
1171
1172 void test_false_functionTypeAlias_typeParameters_list_add() {
1173 _assertDoesNotMatch(
1174 r'''
1175 typedef F<A>();
1176 ''',
1177 r'''
1178 typedef F<A, B>();
1179 ''');
1180 }
1181
1182 void test_false_functionTypeAlias_typeParameters_list_remove() {
1183 _assertDoesNotMatch(
1184 r'''
1185 typedef F<A, B>();
1186 ''',
1187 r'''
1188 typedef F<A>();
1189 ''');
1190 }
1191
1192 void test_false_FunctionTypedFormalParameter_parameters_list_add() {
1193 _assertDoesNotMatch(
1194 r'''
1195 main(int callback(int a)) {
1196 }
1197 ''',
1198 r'''
1199 main(int callback(int a, String b)) {
1200 }
1201 ''');
1202 }
1203
1204 void test_false_FunctionTypedFormalParameter_parameters_list_remove() {
1205 _assertDoesNotMatch(
1206 r'''
1207 main(int callback(int a, String b)) {
1208 }
1209 ''',
1210 r'''
1211 main(int callback(int a)) {
1212 }
1213 ''');
1214 }
1215
1216 void test_false_FunctionTypedFormalParameter_parameterType() {
1217 _assertDoesNotMatch(
1218 r'''
1219 main(int callback(int p)) {
1220 }
1221 ''',
1222 r'''
1223 main(int callback(String p)) {
1224 }
1225 ''');
1226 }
1227
1228 void test_false_FunctionTypedFormalParameter_returnType() {
1229 _assertDoesNotMatch(
1230 r'''
1231 main(int callback()) {
1232 }
1233 ''',
1234 r'''
1235 main(String callback()) {
1236 }
1237 ''');
1238 }
1239
1240 void test_false_FunctionTypedFormalParameter_wasSimple() {
1241 _assertDoesNotMatch(
1242 r'''
1243 main(int callback) {
1244 }
1245 ''',
1246 r'''
1247 main(int callback(int a, String b)) {
1248 }
1249 ''');
1250 }
1251
1252 void test_false_getter_body_add() {
1253 _assertDoesNotMatchOK(
1254 r'''
1255 class A {
1256 int get foo;
1257 }
1258 ''',
1259 r'''
1260 class A {
1261 int get foo => 0;
1262 }
1263 ''');
1264 }
1265
1266 void test_false_getter_body_remove() {
1267 _assertDoesNotMatchOK(
1268 r'''
1269 class A {
1270 int get foo => 0;
1271 }
1272 ''',
1273 r'''
1274 class A {
1275 int get foo;
1276 }
1277 ''');
1278 }
1279
1280 void test_false_implementsClause_add() {
1281 _assertDoesNotMatch(
1282 r'''
1283 class A {}
1284 class B {}
1285 ''',
1286 r'''
1287 class A {}
1288 class B implements A {}
1289 ''');
1290 }
1291
1292 void test_false_implementsClause_remove() {
1293 _assertDoesNotMatch(
1294 r'''
1295 class A {}
1296 class B implements A {}
1297 ''',
1298 r'''
1299 class A {}
1300 class B {}
1301 ''');
1302 }
1303
1304 void test_false_implementsClause_reorder() {
1305 _assertDoesNotMatch(
1306 r'''
1307 class A {}
1308 class B {}
1309 class C implements A, B {}
1310 ''',
1311 r'''
1312 class A {}
1313 class B {}
1314 class C implements B, A {}
1315 ''');
1316 }
1317
1318 void test_false_import_hide_add() {
1319 _assertDoesNotMatch(
1320 r'''
1321 import 'dart:async' hide Future;
1322 ''',
1323 r'''
1324 import 'dart:async' hide Future, Stream;
1325 ''');
1326 }
1327
1328 void test_false_import_hide_remove() {
1329 _assertDoesNotMatch(
1330 r'''
1331 import 'dart:async' hide Future, Stream;
1332 ''',
1333 r'''
1334 import 'dart:async' hide Future;
1335 ''');
1336 }
1337
1338 void test_false_import_list_add() {
1339 _assertDoesNotMatch(
1340 r'''
1341 import 'dart:async';
1342 ''',
1343 r'''
1344 import 'dart:async';
1345 import 'dart:math';
1346 ''');
1347 }
1348
1349 void test_false_import_list_remove() {
1350 _assertDoesNotMatch(
1351 r'''
1352 import 'dart:async';
1353 import 'dart:math';
1354 ''',
1355 r'''
1356 import 'dart:async';
1357 ''');
1358 }
1359
1360 void test_false_import_prefix_add() {
1361 _assertDoesNotMatch(
1362 r'''
1363 import 'dart:async';
1364 ''',
1365 r'''
1366 import 'dart:async' as async;
1367 ''');
1368 }
1369
1370 void test_false_import_prefix_edit() {
1371 _assertDoesNotMatch(
1372 r'''
1373 import 'dart:async' as oldPrefix;
1374 ''',
1375 r'''
1376 import 'dart:async' as newPrefix;
1377 ''');
1378 }
1379
1380 void test_false_import_prefix_remove() {
1381 _assertDoesNotMatch(
1382 r'''
1383 import 'dart:async' as async;
1384 ''',
1385 r'''
1386 import 'dart:async';
1387 ''');
1388 }
1389
1390 void test_false_import_show_add() {
1391 _assertDoesNotMatch(
1392 r'''
1393 import 'dart:async' show Future;
1394 ''',
1395 r'''
1396 import 'dart:async' show Future, Stream;
1397 ''');
1398 }
1399
1400 void test_false_import_show_remove() {
1401 _assertDoesNotMatch(
1402 r'''
1403 import 'dart:async' show Future, Stream;
1404 ''',
1405 r'''
1406 import 'dart:async' show Future;
1407 ''');
1408 }
1409
1410 void test_false_method_annotation_edit() {
1411 _assertDoesNotMatchOK(
1412 r'''
1413 const my_annotationA = const Object();
1414 const my_annotationB = const Object();
1415 class A {
1416 @my_annotationA
1417 void m() {}
1418 }
1419 ''',
1420 r'''
1421 const my_annotationA = const Object();
1422 const my_annotationB = const Object();
1423 class A {
1424 @my_annotationB
1425 void m() {}
1426 }
1427 ''');
1428 }
1429
1430 void test_false_method_annotations_add() {
1431 _assertDoesNotMatchOK(
1432 r'''
1433 const my_annotation = const Object();
1434 class A {
1435 void m() {}
1436 }
1437 ''',
1438 r'''
1439 const my_annotation = const Object();
1440 class A {
1441 @my_annotation
1442 void m() {}
1443 }
1444 ''');
1445 }
1446
1447 void test_false_method_annotations_remove() {
1448 _assertDoesNotMatchOK(
1449 r'''
1450 const my_annotation = const Object();
1451 class A {
1452 @my_annotation
1453 void m() {}
1454 }
1455 ''',
1456 r'''
1457 const my_annotation = const Object();
1458 class A {
1459 void m() {}
1460 }
1461 ''');
1462 }
1463
1464 void test_false_method_async_add() {
1465 _assertDoesNotMatchOK(
1466 r'''
1467 class A {
1468 m() {}
1469 }
1470 ''',
1471 r'''
1472 class A {
1473 m() async {}
1474 }
1475 ''');
1476 }
1477
1478 void test_false_method_async_remove() {
1479 _assertDoesNotMatchOK(
1480 r'''
1481 class A {
1482 m() async {}
1483 }
1484 ''',
1485 r'''
1486 class A {
1487 m() {}
1488 }
1489 ''');
1490 }
1491
1492 void test_false_method_body_add() {
1493 _assertDoesNotMatchOK(
1494 r'''
1495 class A {
1496 void foo();
1497 }
1498 ''',
1499 r'''
1500 class A {
1501 void foo() {}
1502 }
1503 ''');
1504 }
1505
1506 void test_false_method_body_remove() {
1507 _assertDoesNotMatchOK(
1508 r'''
1509 class A {
1510 void foo() {}
1511 }
1512 ''',
1513 r'''
1514 class A {
1515 void foo();
1516 }
1517 ''');
1518 }
1519
1520 void test_false_method_generator_add() {
1521 _assertDoesNotMatchOK(
1522 r'''
1523 class A {
1524 m() async {}
1525 }
1526 ''',
1527 r'''
1528 class A {
1529 m() async* {}
1530 }
1531 ''');
1532 }
1533
1534 void test_false_method_generator_remove() {
1535 _assertDoesNotMatchOK(
1536 r'''
1537 class A {
1538 m() async* {}
1539 }
1540 ''',
1541 r'''
1542 class A {
1543 m() async {}
1544 }
1545 ''');
1546 }
1547
1548 void test_false_method_list_add() {
1549 _assertDoesNotMatchOK(
1550 r'''
1551 class A {
1552 a() {}
1553 b() {}
1554 }
1555 ''',
1556 r'''
1557 class A {
1558 a() {}
1559 b() {}
1560 c() {}
1561 }
1562 ''');
1563 }
1564
1565 void test_false_method_list_remove() {
1566 _assertDoesNotMatch(
1567 r'''
1568 class A {
1569 a() {}
1570 b() {}
1571 c() {}
1572 }
1573 ''',
1574 r'''
1575 class A {
1576 a() {}
1577 b() {}
1578 }
1579 ''');
1580 }
1581
1582 void test_false_method_parameters_type_edit() {
1583 _assertDoesNotMatchOK(
1584 r'''
1585 class A {
1586 m(int p) {
1587 }
1588 }
1589 ''',
1590 r'''
1591 class A {
1592 m(String p) {
1593 }
1594 }
1595 ''');
1596 }
1597
1598 void test_false_method_parameters_type_edit_insertImportPrefix() {
1599 _assertDoesNotMatchOK(
1600 r'''
1601 import 'dart:async' as a;
1602
1603 class C {
1604 void foo(Future f) {}
1605 }
1606
1607 class Future {}
1608
1609 bar(C c, a.Future f) {
1610 c.foo(f);
1611 }
1612 ''',
1613 r'''
1614 import 'dart:async' as a;
1615
1616 class C {
1617 void foo(a.Future f) {}
1618 }
1619
1620 class Future {}
1621
1622 bar(C c, a.Future f) {
1623 c.foo(f);
1624 }
1625 ''');
1626 }
1627
1628 void test_false_method_returnType_edit() {
1629 _assertDoesNotMatchOK(
1630 r'''
1631 class A {
1632 int m() {}
1633 }
1634 ''',
1635 r'''
1636 class A {
1637 String m() {}
1638 }
1639 ''');
1640 }
1641
1642 void test_false_part_list_add() {
1643 addNamedSource('/unitA.dart', 'part of lib; class A {}');
1644 addNamedSource('/unitB.dart', 'part of lib; class B {}');
1645 _assertDoesNotMatch(
1646 r'''
1647 library lib;
1648 part 'unitA.dart';
1649 ''',
1650 r'''
1651 library lib;
1652 part 'unitA.dart';
1653 part 'unitB.dart';
1654 ''');
1655 }
1656
1657 void test_false_part_list_remove() {
1658 addNamedSource('/unitA.dart', 'part of lib; class A {}');
1659 addNamedSource('/unitB.dart', 'part of lib; class B {}');
1660 _assertDoesNotMatch(
1661 r'''
1662 library lib;
1663 part 'unitA.dart';
1664 part 'unitB.dart';
1665 ''',
1666 r'''
1667 library lib;
1668 part 'unitA.dart';
1669 ''');
1670 }
1671
1672 void test_false_SimpleFormalParameter_named_differentName() {
1673 _assertDoesNotMatch(
1674 r'''
1675 main({int oldName}) {
1676 }
1677 ''',
1678 r'''
1679 main({int newName}) {
1680 }
1681 ''');
1682 }
1683
1684 void test_false_SimpleFormalParameter_namedDefault_addValue() {
1685 _assertDoesNotMatch(
1686 r'''
1687 main({int p}) {
1688 }
1689 ''',
1690 r'''
1691 main({int p: 2}) {
1692 }
1693 ''');
1694 }
1695
1696 void test_false_SimpleFormalParameter_namedDefault_differentValue() {
1697 _assertDoesNotMatch(
1698 r'''
1699 main({int p: 1}) {
1700 }
1701 ''',
1702 r'''
1703 main({int p: 2}) {
1704 }
1705 ''');
1706 }
1707
1708 void test_false_SimpleFormalParameter_namedDefault_removeValue() {
1709 _assertDoesNotMatch(
1710 r'''
1711 main({int p: 1}) {
1712 }
1713 ''',
1714 r'''
1715 main({int p}) {
1716 }
1717 ''');
1718 }
1719
1720 void test_false_SimpleFormalParameter_optionalDefault_addValue() {
1721 _assertDoesNotMatch(
1722 r'''
1723 main([int p]) {
1724 }
1725 ''',
1726 r'''
1727 main([int p = 2]) {
1728 }
1729 ''');
1730 }
1731
1732 void test_false_SimpleFormalParameter_optionalDefault_differentValue() {
1733 _assertDoesNotMatch(
1734 r'''
1735 main([int p = 1]) {
1736 }
1737 ''',
1738 r'''
1739 main([int p = 2]) {
1740 }
1741 ''');
1742 }
1743
1744 void test_false_SimpleFormalParameter_optionalDefault_removeValue() {
1745 _assertDoesNotMatch(
1746 r'''
1747 main([int p = 1]) {
1748 }
1749 ''',
1750 r'''
1751 main([int p]) {
1752 }
1753 ''');
1754 }
1755
1756 void test_false_topLevelAccessor_list_add() {
1757 _assertDoesNotMatch(
1758 r'''
1759 get a => 1;
1760 get b => 2;
1761 ''',
1762 r'''
1763 get a => 1;
1764 get b => 2;
1765 get c => 3;
1766 ''');
1767 }
1768
1769 void test_false_topLevelAccessor_list_remove() {
1770 _assertDoesNotMatch(
1771 r'''
1772 get a => 1;
1773 get b => 2;
1774 get c => 3;
1775 ''',
1776 r'''
1777 get a => 1;
1778 get b => 2;
1779 ''');
1780 }
1781
1782 void test_false_topLevelAccessor_wasGetter() {
1783 _assertDoesNotMatch(
1784 r'''
1785 get a => 1;
1786 ''',
1787 r'''
1788 set a(x) {}
1789 ''');
1790 }
1791
1792 void test_false_topLevelAccessor_wasSetter() {
1793 _assertDoesNotMatch(
1794 r'''
1795 set a(x) {}
1796 ''',
1797 r'''
1798 get a => 1;
1799 ''');
1800 }
1801
1802 void test_false_topLevelFunction_list_add() {
1803 _assertDoesNotMatch(
1804 r'''
1805 a() {}
1806 b() {}
1807 ''',
1808 r'''
1809 a() {}
1810 b() {}
1811 c() {}
1812 ''');
1813 }
1814
1815 void test_false_topLevelFunction_list_remove() {
1816 _assertDoesNotMatch(
1817 r'''
1818 a() {}
1819 b() {}
1820 c() {}
1821 ''',
1822 r'''
1823 a() {}
1824 b() {}
1825 ''');
1826 }
1827
1828 void test_false_topLevelFunction_parameters_list_add() {
1829 _assertDoesNotMatch(
1830 r'''
1831 main(int a, int b) {
1832 }
1833 ''',
1834 r'''
1835 main(int a, int b, int c) {
1836 }
1837 ''');
1838 }
1839
1840 void test_false_topLevelFunction_parameters_list_remove() {
1841 _assertDoesNotMatch(
1842 r'''
1843 main(int a, int b, int c) {
1844 }
1845 ''',
1846 r'''
1847 main(int a, int b) {
1848 }
1849 ''');
1850 }
1851
1852 void test_false_topLevelFunction_parameters_type_edit() {
1853 _assertDoesNotMatch(
1854 r'''
1855 main(int a, int b, int c) {
1856 }
1857 ''',
1858 r'''
1859 main(int a, String b, int c) {
1860 }
1861 ''');
1862 }
1863
1864 void test_false_topLevelFunction_returnType_edit() {
1865 _assertDoesNotMatch(
1866 r'''
1867 int a() {}
1868 ''',
1869 r'''
1870 String a() {}
1871 ''');
1872 }
1873
1874 void test_false_topLevelVariable_list_add() {
1875 _assertDoesNotMatch(
1876 r'''
1877 const int A = 1;
1878 const int C = 3;
1879 ''',
1880 r'''
1881 const int A = 1;
1882 const int B = 2;
1883 const int C = 3;
1884 ''');
1885 }
1886
1887 void test_false_topLevelVariable_list_remove() {
1888 _assertDoesNotMatch(
1889 r'''
1890 const int A = 1;
1891 const int B = 2;
1892 const int C = 3;
1893 ''',
1894 r'''
1895 const int A = 1;
1896 const int C = 3;
1897 ''');
1898 }
1899
1900 void test_false_topLevelVariable_modifier_isConst() {
1901 _assertDoesNotMatch(
1902 r'''
1903 final int A = 1;
1904 ''',
1905 r'''
1906 const int A = 1;
1907 ''');
1908 }
1909
1910 void test_false_topLevelVariable_modifier_isFinal() {
1911 _assertDoesNotMatch(
1912 r'''
1913 int A = 1;
1914 ''',
1915 r'''
1916 final int A = 1;
1917 ''');
1918 }
1919
1920 void test_false_topLevelVariable_modifier_wasConst() {
1921 _assertDoesNotMatch(
1922 r'''
1923 const int A = 1;
1924 ''',
1925 r'''
1926 final int A = 1;
1927 ''');
1928 }
1929
1930 void test_false_topLevelVariable_modifier_wasFinal() {
1931 _assertDoesNotMatch(
1932 r'''
1933 final int A = 1;
1934 ''',
1935 r'''
1936 int A = 1;
1937 ''');
1938 }
1939
1940 void test_false_topLevelVariable_synthetic_wasGetter() {
1941 _assertDoesNotMatch(
1942 r'''
1943 int get A => 1;
1944 ''',
1945 r'''
1946 final int A = 1;
1947 ''');
1948 }
1949
1950 void test_false_topLevelVariable_type_different() {
1951 _assertDoesNotMatch(
1952 r'''
1953 int A;
1954 ''',
1955 r'''
1956 String A;
1957 ''');
1958 }
1959
1960 void test_false_topLevelVariable_type_differentArgs() {
1961 _assertDoesNotMatch(
1962 r'''
1963 List<int> A;
1964 ''',
1965 r'''
1966 List<String> A;
1967 ''');
1968 }
1969
1970 void test_false_type_noTypeArguments_hadTypeArguments() {
1971 _assertDoesNotMatch(
1972 r'''
1973 class A<T> {}
1974 A<int> main() {
1975 }
1976 ''',
1977 r'''
1978 class A<T> {}
1979 A main() {
1980 }
1981 ''');
1982 }
1983
1984 void test_false_withClause_add() {
1985 _assertDoesNotMatch(
1986 r'''
1987 class A {}
1988 class B {}
1989 ''',
1990 r'''
1991 class A {}
1992 class B extends Object with A {}
1993 ''');
1994 }
1995
1996 void test_false_withClause_remove() {
1997 _assertDoesNotMatch(
1998 r'''
1999 class A {}
2000 class B extends Object with A {}
2001 ''',
2002 r'''
2003 class A {}
2004 class B {}
2005 ''');
2006 }
2007
2008 void test_false_withClause_reorder() {
2009 _assertDoesNotMatch(
2010 r'''
2011 class A {}
2012 class B {}
2013 class C extends Object with A, B {}
2014 ''',
2015 r'''
2016 class A {}
2017 class B {}
2018 class C extends Object with B, A {}
2019 ''');
2020 }
2021
2022 void test_true_class_annotations_same() {
2023 _assertMatches(
2024 r'''
2025 const my_annotation = const Object();
2026 @my_annotation
2027 class A {
2028 }
2029 ''',
2030 r'''
2031 const my_annotation = const Object();
2032 @my_annotation
2033 class A {
2034 }
2035 ''');
2036 }
2037
2038 void test_true_class_list_reorder() {
2039 _assertMatches(
2040 r'''
2041 class A {}
2042 class B {}
2043 class C {}
2044 ''',
2045 r'''
2046 class C {}
2047 class A {}
2048 class B {}
2049 ''');
2050 }
2051
2052 void test_true_class_list_same() {
2053 _assertMatches(
2054 r'''
2055 class A {}
2056 class B {}
2057 class C {}
2058 ''',
2059 r'''
2060 class A {}
2061 class B {}
2062 class C {}
2063 ''');
2064 }
2065
2066 void test_true_class_typeParameters_same() {
2067 _assertMatches(
2068 r'''
2069 class A<T> {}
2070 ''',
2071 r'''
2072 class A<T> {}
2073 ''');
2074 }
2075
2076 void test_true_classMemberAccessor_getterSetter() {
2077 _assertMatches(
2078 r'''
2079 class A {
2080 int _test;
2081 get test => _test;
2082 set test(v) {
2083 _test = v;
2084 }
2085 }
2086 ''',
2087 r'''
2088 class A {
2089 int _test;
2090 get test => _test;
2091 set test(v) {
2092 _test = v;
2093 }
2094 }
2095 ''');
2096 }
2097
2098 void test_true_classMemberAccessor_list_reorder() {
2099 _assertMatches(
2100 r'''
2101 class A {
2102 get a => 1;
2103 get b => 2;
2104 get c => 3;
2105 }
2106 ''',
2107 r'''
2108 class A {
2109 get c => 3;
2110 get a => 1;
2111 get b => 2;
2112 }
2113 ''');
2114 }
2115
2116 void test_true_classMemberAccessor_list_same() {
2117 _assertMatches(
2118 r'''
2119 class A {
2120 get a => 1;
2121 get b => 2;
2122 get c => 3;
2123 }
2124 ''',
2125 r'''
2126 class A {
2127 get a => 1;
2128 get b => 2;
2129 get c => 3;
2130 }
2131 ''');
2132 }
2133
2134 void test_true_classTypeAlias_list_reorder() {
2135 _assertMatches(
2136 r'''
2137 class M {}
2138 class A = Object with M;
2139 class B = Object with M;
2140 class C = Object with M;
2141 ''',
2142 r'''
2143 class M {}
2144 class C = Object with M;
2145 class A = Object with M;
2146 class B = Object with M;
2147 ''');
2148 }
2149
2150 void test_true_classTypeAlias_list_same() {
2151 _assertMatches(
2152 r'''
2153 class M {}
2154 class A = Object with M;
2155 class B = Object with M;
2156 class C = Object with M;
2157 ''',
2158 r'''
2159 class M {}
2160 class A = Object with M;
2161 class B = Object with M;
2162 class C = Object with M;
2163 ''');
2164 }
2165
2166 void test_true_classTypeAlias_typeParameters_same() {
2167 _assertMatches(
2168 r'''
2169 class M<T> {}
2170 class A<T> {}
2171 class B<T> = A<T> with M<T>;
2172 ''',
2173 r'''
2174 class M<T> {}
2175 class A<T> {}
2176 class B<T> = A<T> with M<T>;
2177 ''');
2178 }
2179
2180 void test_true_constructor_body_add() {
2181 _assertMatches(
2182 r'''
2183 class A {
2184 A(int p);
2185 }
2186 ''',
2187 r'''
2188 class A {
2189 A(int p) {}
2190 }
2191 ''');
2192 }
2193
2194 void test_true_constructor_body_remove() {
2195 _assertMatches(
2196 r'''
2197 class A {
2198 A(int p) {}
2199 }
2200 ''',
2201 r'''
2202 class A {
2203 A(int p);
2204 }
2205 ''');
2206 }
2207
2208 void test_true_constructor_named_same() {
2209 _assertMatches(
2210 r'''
2211 class A {
2212 A.name(int p);
2213 }
2214 ''',
2215 r'''
2216 class A {
2217 A.name(int p);
2218 }
2219 ''');
2220 }
2221
2222 void test_true_constructor_unnamed_add_noParameters() {
2223 _assertMatches(
2224 r'''
2225 class A {
2226 }
2227 ''',
2228 r'''
2229 class A {
2230 A() {}
2231 }
2232 ''');
2233 }
2234
2235 void test_true_constructor_unnamed_remove_noParameters() {
2236 _assertMatches(
2237 r'''
2238 class A {
2239 A() {}
2240 }
2241 ''',
2242 r'''
2243 class A {
2244 }
2245 ''');
2246 }
2247
2248 void test_true_constructor_unnamed_same() {
2249 _assertMatches(
2250 r'''
2251 class A {
2252 A(int p);
2253 }
2254 ''',
2255 r'''
2256 class A {
2257 A(int p);
2258 }
2259 ''');
2260 }
2261
2262 void test_true_defaultFieldFormalParameterElement() {
2263 _assertMatches(
2264 r'''
2265 class A {
2266 int field;
2267 A([this.field = 0]);
2268 }
2269 ''',
2270 r'''
2271 class A {
2272 int field;
2273 A([this.field = 0]);
2274 }
2275 ''');
2276 }
2277
2278 void test_true_enum_constants_reorder() {
2279 _assertMatches(
2280 r'''
2281 enum E {A, B, C}
2282 ''',
2283 r'''
2284 enum E {C, A, B}
2285 ''');
2286 }
2287
2288 void test_true_enum_list_reorder() {
2289 _assertMatches(
2290 r'''
2291 enum A {A1, A2, A3}
2292 enum B {B1, B2, B3}
2293 enum C {C1, C2, C3}
2294 ''',
2295 r'''
2296 enum C {C1, C2, C3}
2297 enum A {A1, A2, A3}
2298 enum B {B1, B2, B3}
2299 ''');
2300 }
2301
2302 void test_true_enum_list_same() {
2303 _assertMatches(
2304 r'''
2305 enum A {A1, A2, A3}
2306 enum B {B1, B2, B3}
2307 enum C {C1, C2, C3}
2308 ''',
2309 r'''
2310 enum A {A1, A2, A3}
2311 enum B {B1, B2, B3}
2312 enum C {C1, C2, C3}
2313 ''');
2314 }
2315
2316 void test_true_executable_same_hasLabel() {
2317 _assertMatches(
2318 r'''
2319 main() {
2320 label: return 42;
2321 }
2322 ''',
2323 r'''
2324 main() {
2325 label: return 42;
2326 }
2327 ''');
2328 }
2329
2330 void test_true_executable_same_hasLocalVariable() {
2331 _assertMatches(
2332 r'''
2333 main() {
2334 int a = 42;
2335 }
2336 ''',
2337 r'''
2338 main() {
2339 int a = 42;
2340 }
2341 ''');
2342 }
2343
2344 void test_true_export_hide_reorder() {
2345 _assertMatches(
2346 r'''
2347 export 'dart:async' hide Future, Stream;
2348 ''',
2349 r'''
2350 export 'dart:async' hide Stream, Future;
2351 ''');
2352 }
2353
2354 void test_true_export_list_reorder() {
2355 _assertMatches(
2356 r'''
2357 export 'dart:async';
2358 export 'dart:math';
2359 ''',
2360 r'''
2361 export 'dart:math';
2362 export 'dart:async';
2363 ''');
2364 }
2365
2366 void test_true_export_list_same() {
2367 _assertMatches(
2368 r'''
2369 export 'dart:async';
2370 export 'dart:math';
2371 ''',
2372 r'''
2373 export 'dart:async';
2374 export 'dart:math';
2375 ''');
2376 }
2377
2378 void test_true_export_show_reorder() {
2379 _assertMatches(
2380 r'''
2381 export 'dart:async' show Future, Stream;
2382 ''',
2383 r'''
2384 export 'dart:async' show Stream, Future;
2385 ''');
2386 }
2387
2388 void test_true_extendsClause_same() {
2389 _assertMatches(
2390 r'''
2391 class A {}
2392 class B extends A {}
2393 ''',
2394 r'''
2395 class A {}
2396 class B extends A {}
2397 ''');
2398 }
2399
2400 void test_true_field_list_reorder() {
2401 _assertMatches(
2402 r'''
2403 class T {
2404 int A = 1;
2405 int B = 2;
2406 int C = 3;
2407 }
2408 ''',
2409 r'''
2410 class T {
2411 int C = 3;
2412 int A = 1;
2413 int B = 2;
2414 }
2415 ''');
2416 }
2417
2418 void test_true_field_list_same() {
2419 _assertMatches(
2420 r'''
2421 class T {
2422 int A = 1;
2423 int B = 2;
2424 int C = 3;
2425 }
2426 ''',
2427 r'''
2428 class T {
2429 int A = 1;
2430 int B = 2;
2431 int C = 3;
2432 }
2433 ''');
2434 }
2435
2436 void test_true_fieldFormalParameter() {
2437 _assertMatches(
2438 r'''
2439 class A {
2440 int field;
2441 A(this.field);
2442 }
2443 ''',
2444 r'''
2445 class A {
2446 int field;
2447 A(this.field);
2448 }
2449 ''');
2450 }
2451
2452 void test_true_fieldFormalParameter_changeName_wasUnresolvedField() {
2453 _assertMatches(
2454 r'''
2455 class A {
2456 final fff;
2457 A(this.unresolved);
2458 }
2459 ''',
2460 r'''
2461 class A {
2462 final fff;
2463 A(this.fff);
2464 }
2465 ''');
2466 }
2467
2468 void test_true_fieldFormalParameter_function() {
2469 _assertMatches(
2470 r'''
2471 class A {
2472 final field;
2473 A(this.field(int a, String b));
2474 }
2475 ''',
2476 r'''
2477 class A {
2478 final field;
2479 A(this.field(int a, String b));
2480 }
2481 ''');
2482 }
2483
2484 void test_true_functionTypeAlias_list_reorder() {
2485 _assertMatches(
2486 r'''
2487 typedef A(int pa);
2488 typedef B(String pb);
2489 typedef C(pc);
2490 ''',
2491 r'''
2492 typedef C(pc);
2493 typedef A(int pa);
2494 typedef B(String pb);
2495 ''');
2496 }
2497
2498 void test_true_functionTypeAlias_list_same() {
2499 _assertMatches(
2500 r'''
2501 typedef String A(int pa);
2502 typedef int B(String pb);
2503 typedef C(pc);
2504 ''',
2505 r'''
2506 typedef String A(int pa);
2507 typedef int B(String pb);
2508 typedef C(pc);
2509 ''');
2510 }
2511
2512 void test_true_functionTypeAlias_typeParameters_list_same() {
2513 _assertMatches(
2514 r'''
2515 typedef F<A, B, C>();
2516 ''',
2517 r'''
2518 typedef F<A, B, C>();
2519 ''');
2520 }
2521
2522 void test_true_FunctionTypedFormalParameter() {
2523 _assertMatches(
2524 r'''
2525 main(int callback(int a, String b)) {
2526 }
2527 ''',
2528 r'''
2529 main(int callback(int a, String b)) {
2530 }
2531 ''');
2532 }
2533
2534 void test_true_implementsClause_same() {
2535 _assertMatches(
2536 r'''
2537 class A {}
2538 class B implements A {}
2539 ''',
2540 r'''
2541 class A {}
2542 class B implements A {}
2543 ''');
2544 }
2545
2546 void test_true_import_hide_reorder() {
2547 _assertMatches(
2548 r'''
2549 import 'dart:async' hide Future, Stream;
2550 ''',
2551 r'''
2552 import 'dart:async' hide Stream, Future;
2553 ''');
2554 }
2555
2556 void test_true_import_list_reorder() {
2557 _assertMatches(
2558 r'''
2559 import 'dart:async';
2560 import 'dart:math';
2561 ''',
2562 r'''
2563 import 'dart:math';
2564 import 'dart:async';
2565 ''');
2566 }
2567
2568 void test_true_import_list_same() {
2569 _assertMatches(
2570 r'''
2571 import 'dart:async';
2572 import 'dart:math';
2573 ''',
2574 r'''
2575 import 'dart:async';
2576 import 'dart:math';
2577 ''');
2578 }
2579
2580 void test_true_import_prefix() {
2581 _assertMatches(
2582 r'''
2583 import 'dart:async' as async;
2584 ''',
2585 r'''
2586 import 'dart:async' as async;
2587 ''');
2588 }
2589
2590 void test_true_import_show_reorder() {
2591 _assertMatches(
2592 r'''
2593 import 'dart:async' show Future, Stream;
2594 ''',
2595 r'''
2596 import 'dart:async' show Stream, Future;
2597 ''');
2598 }
2599
2600 void test_true_method_annotation_accessor_same() {
2601 _assertMatches(
2602 r'''
2603 const my_annotation = const Object();
2604 class A {
2605 @my_annotation
2606 void m() {}
2607 }
2608 ''',
2609 r'''
2610 const my_annotation = const Object();
2611 class A {
2612 @my_annotation
2613 void m() {}
2614 }
2615 ''');
2616 }
2617
2618 void test_true_method_annotation_constructor_same() {
2619 _assertMatches(
2620 r'''
2621 class MyAnnotation {
2622 const MyAnnotation();
2623 }
2624 class A {
2625 @MyAnnotation()
2626 void m() {}
2627 }
2628 ''',
2629 r'''
2630 class MyAnnotation {
2631 const MyAnnotation();
2632 }
2633 class A {
2634 @MyAnnotation()
2635 void m() {}
2636 }
2637 ''');
2638 }
2639
2640 void test_true_method_async() {
2641 _assertMatches(
2642 r'''
2643 class A {
2644 m() async {}
2645 }
2646 ''',
2647 r'''
2648 class A {
2649 m() async {}
2650 }
2651 ''');
2652 }
2653
2654 void test_true_method_list_reorder() {
2655 _assertMatches(
2656 r'''
2657 class A {
2658 a() {}
2659 b() {}
2660 c() {}
2661 }
2662 ''',
2663 r'''
2664 class A {
2665 c() {}
2666 a() {}
2667 b() {}
2668 }
2669 ''');
2670 }
2671
2672 void test_true_method_list_same() {
2673 _assertMatches(
2674 r'''
2675 class A {
2676 a() {}
2677 b() {}
2678 c() {}
2679 }
2680 ''',
2681 r'''
2682 class A {
2683 a() {}
2684 b() {}
2685 c() {}
2686 }
2687 ''');
2688 }
2689
2690 void test_true_method_operator_minus() {
2691 _assertMatches(
2692 r'''
2693 class A {
2694 operator -(other) {}
2695 }
2696 ''',
2697 r'''
2698 class A {
2699 operator -(other) {}
2700 }
2701 ''');
2702 }
2703
2704 void test_true_method_operator_minusUnary() {
2705 _assertMatches(
2706 r'''
2707 class A {
2708 operator -() {}
2709 }
2710 ''',
2711 r'''
2712 class A {
2713 operator -() {}
2714 }
2715 ''');
2716 }
2717
2718 void test_true_method_operator_plus() {
2719 _assertMatches(
2720 r'''
2721 class A {
2722 operator +(other) {}
2723 }
2724 ''',
2725 r'''
2726 class A {
2727 operator +(other) {}
2728 }
2729 ''');
2730 }
2731
2732 void test_true_method_parameters_type_functionType() {
2733 _assertMatches(
2734 r'''
2735 typedef F();
2736 class A {
2737 m(F p) {}
2738 }
2739 ''',
2740 r'''
2741 typedef F();
2742 class A {
2743 m(F p) {}
2744 }
2745 ''');
2746 }
2747
2748 void test_true_method_parameters_type_sameImportPrefix() {
2749 _assertMatches(
2750 r'''
2751 import 'dart:async' as a;
2752
2753 bar(a.Future f) {
2754 print(f);
2755 }
2756 ''',
2757 r'''
2758 import 'dart:async' as a;
2759
2760 bar(a.Future ff) {
2761 print(ff);
2762 }
2763 ''');
2764 }
2765
2766 void test_true_part_list_reorder() {
2767 addNamedSource('/unitA.dart', 'part of lib; class A {}');
2768 addNamedSource('/unitB.dart', 'part of lib; class B {}');
2769 _assertMatches(
2770 r'''
2771 library lib;
2772 part 'unitA.dart';
2773 part 'unitB.dart';
2774 ''',
2775 r'''
2776 library lib;
2777 part 'unitB.dart';
2778 part 'unitA.dart';
2779 ''');
2780 }
2781
2782 void test_true_part_list_same() {
2783 addNamedSource('/unitA.dart', 'part of lib; class A {}');
2784 addNamedSource('/unitB.dart', 'part of lib; class B {}');
2785 _assertMatches(
2786 r'''
2787 library lib;
2788 part 'unitA.dart';
2789 part 'unitB.dart';
2790 ''',
2791 r'''
2792 library lib;
2793 part 'unitA.dart';
2794 part 'unitB.dart';
2795 ''');
2796 }
2797
2798 void test_true_SimpleFormalParameter_optional_differentName() {
2799 _assertMatches(
2800 r'''
2801 main([int oldName]) {
2802 }
2803 ''',
2804 r'''
2805 main([int newName]) {
2806 }
2807 ''');
2808 }
2809
2810 void test_true_SimpleFormalParameter_optionalDefault_differentName() {
2811 _assertMatches(
2812 r'''
2813 main([int oldName = 1]) {
2814 }
2815 ''',
2816 r'''
2817 main([int newName = 1]) {
2818 }
2819 ''');
2820 }
2821
2822 void test_true_SimpleFormalParameter_required_differentName() {
2823 _assertMatches(
2824 r'''
2825 main(int oldName) {
2826 }
2827 ''',
2828 r'''
2829 main(int newName) {
2830 }
2831 ''');
2832 }
2833
2834 void test_true_topLevelAccessor_list_reorder() {
2835 _assertMatches(
2836 r'''
2837 set a(x) {}
2838 set b(x) {}
2839 set c(x) {}
2840 ''',
2841 r'''
2842 set c(x) {}
2843 set a(x) {}
2844 set b(x) {}
2845 ''');
2846 }
2847
2848 void test_true_topLevelAccessor_list_same() {
2849 _assertMatches(
2850 r'''
2851 get a => 1;
2852 get b => 2;
2853 get c => 3;
2854 ''',
2855 r'''
2856 get a => 1;
2857 get b => 2;
2858 get c => 3;
2859 ''');
2860 }
2861
2862 void test_true_topLevelFunction_list_reorder() {
2863 _assertMatches(
2864 r'''
2865 a() {}
2866 b() {}
2867 c() {}
2868 ''',
2869 r'''
2870 c() {}
2871 a() {}
2872 b() {}
2873 ''');
2874 }
2875
2876 void test_true_topLevelFunction_list_same() {
2877 _assertMatches(
2878 r'''
2879 a() {}
2880 b() {}
2881 c() {}
2882 ''',
2883 r'''
2884 a() {}
2885 b() {}
2886 c() {}
2887 ''');
2888 }
2889
2890 void test_true_topLevelVariable_list_reorder() {
2891 _assertMatches(
2892 r'''
2893 const int A = 1;
2894 const int B = 2;
2895 const int C = 3;
2896 ''',
2897 r'''
2898 const int C = 3;
2899 const int A = 1;
2900 const int B = 2;
2901 ''');
2902 }
2903
2904 void test_true_topLevelVariable_list_same() {
2905 _assertMatches(
2906 r'''
2907 const int A = 1;
2908 const int B = 2;
2909 const int C = 3;
2910 ''',
2911 r'''
2912 const int A = 1;
2913 const int B = 2;
2914 const int C = 3;
2915 ''');
2916 }
2917
2918 void test_true_topLevelVariable_type_sameArgs() {
2919 _assertMatches(
2920 r'''
2921 Map<int, String> A;
2922 ''',
2923 r'''
2924 Map<int, String> A;
2925 ''');
2926 }
2927
2928 void test_true_type_dynamic() {
2929 _assertMatches(
2930 r'''
2931 dynamic a() {}
2932 ''',
2933 r'''
2934 dynamic a() {}
2935 ''');
2936 }
2937
2938 void test_true_type_hasImportPrefix() {
2939 _assertMatches(
2940 r'''
2941 import 'dart:async' as async;
2942 async.Future F;
2943 ''',
2944 r'''
2945 import 'dart:async' as async;
2946 async.Future F;
2947 ''');
2948 }
2949
2950 void test_true_type_noTypeArguments_implyAllDynamic() {
2951 _assertMatches(
2952 r'''
2953 class A<T> {}
2954 A main() {
2955 }
2956 ''',
2957 r'''
2958 class A<T> {}
2959 A main() {
2960 }
2961 ''');
2962 }
2963
2964 void test_true_type_void() {
2965 _assertMatches(
2966 r'''
2967 void a() {}
2968 ''',
2969 r'''
2970 void a() {}
2971 ''');
2972 }
2973
2974 void test_true_withClause_same() {
2975 _assertMatches(
2976 r'''
2977 class A {}
2978 class B extends Object with A {}
2979 ''',
2980 r'''
2981 class A {}
2982 class B extends Object with A {}
2983 ''');
2984 }
2985
2986 void _assertDoesNotMatch(String oldContent, String newContent) {
2987 _assertMatchKind(DeclarationMatchKind.MISMATCH, oldContent, newContent);
2988 }
2989
2990 void _assertDoesNotMatchOK(String oldContent, String newContent) {
2991 _assertMatchKind(DeclarationMatchKind.MISMATCH_OK, oldContent, newContent);
2992 }
2993
2994 void _assertMatches(String oldContent, String newContent) {
2995 _assertMatchKind(DeclarationMatchKind.MATCH, oldContent, newContent);
2996 }
2997
2998 void _assertMatchKind(
2999 DeclarationMatchKind expectMatch, String oldContent, String newContent) {
3000 Source source = addSource(oldContent);
3001 LibraryElement library = resolve2(source);
3002 CompilationUnit oldUnit = resolveCompilationUnit(source, library);
3003 // parse
3004 CompilationUnit newUnit = ParserTestCase.parseCompilationUnit(newContent);
3005 // build elements
3006 {
3007 ElementHolder holder = new ElementHolder();
3008 ElementBuilder builder = new ElementBuilder(holder);
3009 newUnit.accept(builder);
3010 }
3011 // match
3012 DeclarationMatcher matcher = new DeclarationMatcher();
3013 DeclarationMatchKind matchKind = matcher.matches(newUnit, oldUnit.element);
3014 expect(matchKind, same(expectMatch));
3015 }
3016 }
3017
3018 @reflectiveTest
3019 class IncrementalResolverTest extends ResolverTestCase {
3020 Source source;
3021 String code;
3022 LibraryElement library;
3023 CompilationUnit unit;
3024
3025 @override
3026 void reset() {
3027 if (AnalysisEngine.instance.useTaskModel) {
3028 analysisContext2 = AnalysisContextFactory.contextWithCore();
3029 } else {
3030 analysisContext2 = AnalysisContextFactory.oldContextWithCore();
3031 }
3032 }
3033
3034 @override
3035 void resetWithOptions(AnalysisOptions options) {
3036 if (AnalysisEngine.instance.useTaskModel) {
3037 analysisContext2 =
3038 AnalysisContextFactory.contextWithCoreAndOptions(options);
3039 } else {
3040 analysisContext2 =
3041 AnalysisContextFactory.oldContextWithCoreAndOptions(options);
3042 }
3043 }
3044
3045 void setUp() {
3046 super.setUp();
3047 test_resolveApiChanges = true;
3048 log.logger = log.NULL_LOGGER;
3049 }
3050
3051 void test_classMemberAccessor_body() {
3052 _resolveUnit(r'''
3053 class A {
3054 int get test {
3055 return 1 + 2;
3056 }
3057 }''');
3058 _resolve(_editString('+', '*'), _isFunctionBody);
3059 }
3060
3061 void test_constructor_body() {
3062 _resolveUnit(r'''
3063 class A {
3064 int f;
3065 A(int a, int b) {
3066 f = a + b;
3067 }
3068 }''');
3069 _resolve(_editString('+', '*'), _isFunctionBody);
3070 }
3071
3072 void test_constructor_fieldInitializer_add() {
3073 _resolveUnit(r'''
3074 class A {
3075 int f;
3076 A(int a, int b);
3077 }''');
3078 _resolve(_editString(');', ') : f = a + b;'), _isClassMember);
3079 }
3080
3081 void test_constructor_fieldInitializer_edit() {
3082 _resolveUnit(r'''
3083 class A {
3084 int f;
3085 A(int a, int b) : f = a + b {
3086 int a = 42;
3087 }
3088 }''');
3089 _resolve(_editString('+', '*'), _isExpression);
3090 }
3091
3092 void test_constructor_label_add() {
3093 _resolveUnit(r'''
3094 class A {
3095 A() {
3096 return 42;
3097 }
3098 }
3099 ''');
3100 _resolve(_editString('return', 'label: return'), _isBlock);
3101 }
3102
3103 void test_constructor_localVariable_add() {
3104 _resolveUnit(r'''
3105 class A {
3106 A() {
3107 42;
3108 }
3109 }
3110 ''');
3111 _resolve(_editString('42;', 'var res = 42;'), _isBlock);
3112 }
3113
3114 void test_constructor_superConstructorInvocation() {
3115 _resolveUnit(r'''
3116 class A {
3117 A(int p);
3118 }
3119 class B extends A {
3120 B(int a, int b) : super(a + b);
3121 }
3122 ''');
3123 _resolve(_editString('+', '*'), _isExpression);
3124 }
3125
3126 void test_fieldFormalParameter() {
3127 _resolveUnit(r'''
3128 class A {
3129 int xy;
3130 A(this.x);
3131 }''');
3132 _resolve(_editString('this.x', 'this.xy'), _isDeclaration);
3133 }
3134
3135 void test_function_localFunction_add() {
3136 _resolveUnit(r'''
3137 int main() {
3138 return 0;
3139 }
3140 callIt(f) {}
3141 ''');
3142 _resolve(_editString('return 0;', 'callIt((p) {});'), _isBlock);
3143 }
3144
3145 void test_functionBody_body() {
3146 _resolveUnit(r'''
3147 main(int a, int b) {
3148 return a + b;
3149 }''');
3150 _resolve(_editString('+', '*'), _isFunctionBody);
3151 }
3152
3153 void test_functionBody_expression() {
3154 _resolveUnit(r'''
3155 main(int a, int b) => a + b;
3156 ''');
3157 _resolve(_editString('+', '*'), _isExpression);
3158 }
3159
3160 void test_functionBody_statement() {
3161 _resolveUnit(r'''
3162 main(int a, int b) {
3163 return a + b;
3164 }''');
3165 _resolve(_editString('+', '*'), _isStatement);
3166 }
3167
3168 void test_method_body() {
3169 _resolveUnit(r'''
3170 class A {
3171 m(int a, int b) {
3172 return a + b;
3173 }
3174 }''');
3175 _resolve(_editString('+', '*'), _isFunctionBody);
3176 }
3177
3178 void test_method_label_add() {
3179 _resolveUnit(r'''
3180 class A {
3181 int m(int a, int b) {
3182 return a + b;
3183 }
3184 }
3185 ''');
3186 _resolve(_editString('return', 'label: return'), _isBlock);
3187 }
3188
3189 void test_method_localFunction_add() {
3190 _resolveUnit(r'''
3191 class A {
3192 int m() {
3193 return 0;
3194 }
3195 }
3196 callIt(f) {}
3197 ''');
3198 _resolve(_editString('return 0;', 'callIt((p) {});'), _isBlock);
3199 }
3200
3201 void test_method_localVariable_add() {
3202 _resolveUnit(r'''
3203 class A {
3204 int m(int a, int b) {
3205 return a + b;
3206 }
3207 }
3208 ''');
3209 _resolve(
3210 _editString(
3211 ' return a + b;',
3212 r'''
3213 int res = a + b;
3214 return res;
3215 '''),
3216 _isBlock);
3217 }
3218
3219 void test_method_parameter_rename() {
3220 _resolveUnit(r'''
3221 class A {
3222 int m(int a, int b, int c) {
3223 return a + b + c;
3224 }
3225 }
3226 ''');
3227 _resolve(
3228 _editString(
3229 r'''(int a, int b, int c) {
3230 return a + b + c;''',
3231 r'''(int a, int second, int c) {
3232 return a + second + c;'''),
3233 _isDeclaration);
3234 }
3235
3236 void test_superInvocation() {
3237 _resolveUnit(r'''
3238 class A {
3239 foo(p) {}
3240 }
3241 class B extends A {
3242 bar() {
3243 super.foo(1 + 2);
3244 }
3245 }''');
3246 _resolve(_editString('+', '*'), _isFunctionBody);
3247 }
3248
3249 void test_topLevelAccessor_body() {
3250 _resolveUnit(r'''
3251 int get test {
3252 return 1 + 2;
3253 }''');
3254 _resolve(_editString('+', '*'), _isFunctionBody);
3255 }
3256
3257 void test_topLevelFunction_label_add() {
3258 _resolveUnit(r'''
3259 int main(int a, int b) {
3260 return a + b;
3261 }
3262 ''');
3263 _resolve(_editString(' return', 'label: return a + b;'), _isBlock);
3264 }
3265
3266 void test_topLevelFunction_label_remove() {
3267 _resolveUnit(r'''
3268 int main(int a, int b) {
3269 label: return a + b;
3270 }
3271 ''');
3272 _resolve(_editString('label: ', ''), _isBlock);
3273 }
3274
3275 void test_topLevelFunction_localVariable_add() {
3276 _resolveUnit(r'''
3277 int main(int a, int b) {
3278 return a + b;
3279 }
3280 ''');
3281 _resolve(
3282 _editString(
3283 ' return a + b;',
3284 r'''
3285 int res = a + b;
3286 return res;
3287 '''),
3288 _isBlock);
3289 }
3290
3291 void test_topLevelFunction_localVariable_remove() {
3292 _resolveUnit(r'''
3293 int main(int a, int b) {
3294 int res = a * b;
3295 return a + b;
3296 }
3297 ''');
3298 _resolve(_editString('int res = a * b;', ''), _isBlock);
3299 }
3300
3301 void test_topLevelFunction_parameter_inFunctionTyped_rename() {
3302 _resolveUnit(r'''
3303 test(f(int a, int b)) {
3304 }
3305 ''');
3306 _resolve(_editString('test(f(int a', 'test(f2(int a2'), _isDeclaration);
3307 }
3308
3309 void test_topLevelFunction_parameter_rename() {
3310 _resolveUnit(r'''
3311 int main(int a, int b) {
3312 return a + b;
3313 }
3314 ''');
3315 _resolve(
3316 _editString(
3317 r'''(int a, int b) {
3318 return a + b;''',
3319 r'''(int first, int b) {
3320 return first + b;'''),
3321 _isDeclaration);
3322 }
3323
3324 void test_topLevelVariable_initializer() {
3325 _resolveUnit(r'''
3326 int C = 1 + 2;
3327 ''');
3328 _resolve(_editString('+', '*'), _isExpression);
3329 }
3330
3331 void test_updateElementOffset() {
3332 _resolveUnit(r'''
3333 class A {
3334 int am(String ap) {
3335 int av = 1;
3336 return av;
3337 }
3338 }
3339 main(int a, int b) {
3340 return a + b;
3341 }
3342 class B {
3343 int bm(String bp) {
3344 int bv = 1;
3345 return bv;
3346 }
3347 }
3348 ''');
3349 _resolve(_editString('+', ' + '), _isStatement);
3350 }
3351
3352 _Edit _editString(String search, String replacement, [int length]) {
3353 int offset = code.indexOf(search);
3354 expect(offset, isNot(-1));
3355 if (length == null) {
3356 length = search.length;
3357 }
3358 return new _Edit(offset, length, replacement);
3359 }
3360
3361 /**
3362 * Applies [edit] to [code], find the [AstNode] specified by [predicate]
3363 * and incrementally resolves it.
3364 *
3365 * Then resolves the new code from scratch and validates that results of
3366 * the incremental resolution and non-incremental resolutions are the same.
3367 */
3368 void _resolve(_Edit edit, Predicate<AstNode> predicate) {
3369 int offset = edit.offset;
3370 // parse "newCode"
3371 String newCode = code.substring(0, offset) +
3372 edit.replacement +
3373 code.substring(offset + edit.length);
3374 CompilationUnit newUnit = _parseUnit(newCode);
3375 // replace the node
3376 AstNode oldNode = _findNodeAt(unit, offset, predicate);
3377 AstNode newNode = _findNodeAt(newUnit, offset, predicate);
3378 {
3379 bool success = NodeReplacer.replace(oldNode, newNode);
3380 expect(success, isTrue);
3381 }
3382 // update tokens
3383 {
3384 int delta = edit.replacement.length - edit.length;
3385 _shiftTokens(unit.beginToken, offset, delta);
3386 }
3387 // do incremental resolution
3388 int updateOffset = edit.offset;
3389 int updateEndOld = updateOffset + edit.length;
3390 int updateOldNew = updateOffset + edit.replacement.length;
3391 IncrementalResolver resolver;
3392 if (AnalysisEngine.instance.useTaskModel) {
3393 LibrarySpecificUnit lsu = new LibrarySpecificUnit(source, source);
3394 task.AnalysisCache cache = analysisContext2.analysisCache;
3395 resolver = new IncrementalResolver(
3396 null,
3397 cache.get(source),
3398 cache.get(lsu),
3399 unit.element,
3400 updateOffset,
3401 updateEndOld,
3402 updateOldNew);
3403 } else {
3404 resolver = new IncrementalResolver(
3405 (analysisContext2 as AnalysisContextImpl)
3406 .getReadableSourceEntryOrNull(source),
3407 null,
3408 null,
3409 unit.element,
3410 updateOffset,
3411 updateEndOld,
3412 updateOldNew);
3413 }
3414 bool success = resolver.resolve(newNode);
3415 expect(success, isTrue);
3416 List<AnalysisError> newErrors = analysisContext.computeErrors(source);
3417 // resolve "newCode" from scratch
3418 CompilationUnit fullNewUnit;
3419 {
3420 source = addSource(newCode);
3421 _runTasks();
3422 LibraryElement library = resolve2(source);
3423 fullNewUnit = resolveCompilationUnit(source, library);
3424 }
3425 try {
3426 assertSameResolution(unit, fullNewUnit);
3427 } on IncrementalResolutionMismatch catch (mismatch) {
3428 fail(mismatch.message);
3429 }
3430 // errors
3431 List<AnalysisError> newFullErrors =
3432 analysisContext.getErrors(source).errors;
3433 _assertEqualErrors(newErrors, newFullErrors);
3434 // prepare for the next cycle
3435 code = newCode;
3436 }
3437
3438 void _resolveUnit(String code) {
3439 this.code = code;
3440 source = addSource(code);
3441 library = resolve2(source);
3442 unit = resolveCompilationUnit(source, library);
3443 _runTasks();
3444 }
3445
3446 void _runTasks() {
3447 AnalysisResult result = analysisContext.performAnalysisTask();
3448 while (result.changeNotices != null) {
3449 result = analysisContext.performAnalysisTask();
3450 }
3451 }
3452
3453 static AstNode _findNodeAt(
3454 CompilationUnit oldUnit, int offset, Predicate<AstNode> predicate) {
3455 NodeLocator locator = new NodeLocator(offset);
3456 AstNode node = locator.searchWithin(oldUnit);
3457 return node.getAncestor(predicate);
3458 }
3459
3460 static bool _isBlock(AstNode node) => node is Block;
3461
3462 static bool _isClassMember(AstNode node) => node is ClassMember;
3463
3464 static bool _isDeclaration(AstNode node) => node is Declaration;
3465
3466 static bool _isExpression(AstNode node) => node is Expression;
3467
3468 static bool _isFunctionBody(AstNode node) => node is FunctionBody;
3469
3470 static bool _isStatement(AstNode node) => node is Statement;
3471
3472 static CompilationUnit _parseUnit(String code) {
3473 var errorListener = new BooleanErrorListener();
3474 var reader = new CharSequenceReader(code);
3475 var scanner = new Scanner(null, reader, errorListener);
3476 var token = scanner.tokenize();
3477 var parser = new Parser(null, errorListener);
3478 return parser.parseCompilationUnit(token);
3479 }
3480
3481 static void _shiftTokens(Token token, int afterOffset, int delta) {
3482 while (token.type != TokenType.EOF) {
3483 if (token.offset >= afterOffset) {
3484 token.applyDelta(delta);
3485 }
3486 token = token.next;
3487 }
3488 }
3489 }
3490
3491 /**
3492 * The test for [poorMansIncrementalResolution] function and its integration
3493 * into [AnalysisContext].
3494 */
3495 @reflectiveTest
3496 class PoorMansIncrementalResolutionTest extends ResolverTestCase {
3497 Source source;
3498 String code;
3499 LibraryElement oldLibrary;
3500 CompilationUnit oldUnit;
3501 CompilationUnitElement oldUnitElement;
3502
3503 void fail_updateErrors_removeExisting_duplicateMethodDeclaration() {
3504 // TODO(scheglov) We fail to remove the second "foo" declaration.
3505 // So, we still have the same duplicate declaration problem.
3506 _resolveUnit(r'''
3507 class A {
3508 void foo() {}
3509 void foo() {}
3510 }
3511 ''');
3512 _updateAndValidate(r'''
3513 class A {
3514 void foo() {}
3515 void foo2() {}
3516 }
3517 ''');
3518 }
3519
3520 @override
3521 void setUp() {
3522 AnalysisEngine.instance.useTaskModel = true;
3523 super.setUp();
3524 _resetWithIncremental(true);
3525 }
3526
3527 @override
3528 void tearDown() {
3529 super.tearDown();
3530 AnalysisEngine.instance.useTaskModel = false;
3531 }
3532
3533 void test_computeConstants() {
3534 _resolveUnit(r'''
3535 int f() => 0;
3536 main() {
3537 const x = f();
3538 print(x + 1);
3539 }
3540 ''');
3541 _updateAndValidate(r'''
3542 int f() => 0;
3543 main() {
3544 const x = f();
3545 print(x + 2);
3546 }
3547 ''');
3548 }
3549
3550 void test_dartDoc_beforeField() {
3551 _resolveUnit(r'''
3552 class A {
3553 /**
3554 * A field [field] of type [int] in class [A].
3555 */
3556 int field;
3557 }
3558 ''');
3559 _updateAndValidate(r'''
3560 class A {
3561 /**
3562 * A field [field] of the type [int] in the class [A].
3563 * Updated, with a reference to the [String] type.
3564 */
3565 int field;
3566 }
3567 ''');
3568 }
3569
3570 void test_dartDoc_clumsy_addReference() {
3571 _resolveUnit(r'''
3572 /**
3573 * aaa bbbb
3574 */
3575 main() {
3576 }
3577 ''');
3578 _updateAndValidate(r'''
3579 /**
3580 * aaa [main] bbbb
3581 */
3582 main() {
3583 }
3584 ''');
3585 }
3586
3587 void test_dartDoc_clumsy_removeReference() {
3588 _resolveUnit(r'''
3589 /**
3590 * aaa [main] bbbb
3591 */
3592 main() {
3593 }
3594 ''');
3595 _updateAndValidate(r'''
3596 /**
3597 * aaa bbbb
3598 */
3599 main() {
3600 }
3601 ''');
3602 }
3603
3604 void test_dartDoc_clumsy_updateText_beforeKeywordToken() {
3605 _resolveUnit(r'''
3606 /**
3607 * A comment with the [int] type reference.
3608 */
3609 class A {}
3610 ''');
3611 _updateAndValidate(r'''
3612 /**
3613 * A comment with the [int] type reference.
3614 * Plus reference to [A] itself.
3615 */
3616 class A {}
3617 ''');
3618 }
3619
3620 void test_dartDoc_clumsy_updateText_insert() {
3621 _resolveUnit(r'''
3622 /**
3623 * A function [main] with a parameter [p] of type [int].
3624 */
3625 main(int p) {
3626 unresolvedFunctionProblem();
3627 }
3628 /**
3629 * Other comment with [int] reference.
3630 */
3631 foo() {}
3632 ''');
3633 _updateAndValidate(r'''
3634 /**
3635 * A function [main] with a parameter [p] of type [int].
3636 * Inserted text with [String] reference.
3637 */
3638 main(int p) {
3639 unresolvedFunctionProblem();
3640 }
3641 /**
3642 * Other comment with [int] reference.
3643 */
3644 foo() {}
3645 ''');
3646 }
3647
3648 void test_dartDoc_clumsy_updateText_remove() {
3649 _resolveUnit(r'''
3650 /**
3651 * A function [main] with a parameter [p] of type [int].
3652 * Some text with [String] reference to remove.
3653 */
3654 main(int p) {
3655 }
3656 /**
3657 * Other comment with [int] reference.
3658 */
3659 foo() {}
3660 ''');
3661 _updateAndValidate(r'''
3662 /**
3663 * A function [main] with a parameter [p] of type [int].
3664 */
3665 main(int p) {
3666 }
3667 /**
3668 * Other comment with [int] reference.
3669 */
3670 foo() {}
3671 ''');
3672 }
3673
3674 void test_dartDoc_elegant_addReference() {
3675 _resolveUnit(r'''
3676 /// aaa bbb
3677 main() {
3678 return 1;
3679 }
3680 ''');
3681 _updateAndValidate(r'''
3682 /// aaa [main] bbb
3683 /// ccc [int] ddd
3684 main() {
3685 return 1;
3686 }
3687 ''');
3688 }
3689
3690 void test_dartDoc_elegant_removeReference() {
3691 _resolveUnit(r'''
3692 /// aaa [main] bbb
3693 /// ccc [int] ddd
3694 main() {
3695 return 1;
3696 }
3697 ''');
3698 _updateAndValidate(r'''
3699 /// aaa bbb
3700 main() {
3701 return 1;
3702 }
3703 ''');
3704 }
3705
3706 void test_dartDoc_elegant_updateText_insertToken() {
3707 _resolveUnit(r'''
3708 /// A
3709 /// [int]
3710 class Test {
3711 }
3712 ''');
3713 _updateAndValidate(r'''
3714 /// A
3715 ///
3716 /// [int]
3717 class Test {
3718 }
3719 ''');
3720 }
3721
3722 void test_dartDoc_elegant_updateText_removeToken() {
3723 _resolveUnit(r'''
3724 /// A
3725 ///
3726 /// [int]
3727 class Test {
3728 }
3729 ''');
3730 _updateAndValidate(r'''
3731 /// A
3732 /// [int]
3733 class Test {
3734 }
3735 ''');
3736 }
3737
3738 void test_endOfLineComment_add_beforeKeywordToken() {
3739 _resolveUnit(r'''
3740 main() {
3741 var v = 42;
3742 }
3743 ''');
3744 _updateAndValidate(r'''
3745 main() {
3746 // some comment
3747 var v = 42;
3748 }
3749 ''');
3750 }
3751
3752 void test_endOfLineComment_add_beforeStringToken() {
3753 _resolveUnit(r'''
3754 main() {
3755 print(0);
3756 }
3757 ''');
3758 _updateAndValidate(r'''
3759 main() {
3760 // some comment
3761 print(0);
3762 }
3763 ''');
3764 }
3765
3766 void test_endOfLineComment_edit() {
3767 _resolveUnit(r'''
3768 main() {
3769 // some comment
3770 print(0);
3771 }
3772 ''');
3773 _updateAndValidate(r'''
3774 main() {
3775 // edited comment text
3776 print(0);
3777 }
3778 ''');
3779 }
3780
3781 void test_endOfLineComment_localFunction_inTopLevelVariable() {
3782 _resolveUnit(r'''
3783 typedef int Binary(one, two, three);
3784
3785 int Global = f((a, b, c) {
3786 return 0; // Some comment
3787 });
3788 ''');
3789 _updateAndValidate(r'''
3790 typedef int Binary(one, two, three);
3791
3792 int Global = f((a, b, c) {
3793 return 0; // Some comment
3794 });
3795 ''');
3796 }
3797
3798 void test_endOfLineComment_outBody_add() {
3799 _resolveUnit(r'''
3800 main() {
3801 Object x;
3802 x.foo();
3803 }
3804 ''');
3805 _updateAndValidate(
3806 r'''
3807 // 000
3808 main() {
3809 Object x;
3810 x.foo();
3811 }
3812 ''',
3813 expectedSuccess: false);
3814 }
3815
3816 void test_endOfLineComment_outBody_remove() {
3817 _resolveUnit(r'''
3818 // 000
3819 main() {
3820 Object x;
3821 x.foo();
3822 }
3823 ''');
3824 _updateAndValidate(
3825 r'''
3826 main() {
3827 Object x;
3828 x.foo();
3829 }
3830 ''',
3831 expectedSuccess: false);
3832 }
3833
3834 void test_endOfLineComment_outBody_update() {
3835 _resolveUnit(r'''
3836 // 000
3837 main() {
3838 Object x;
3839 x.foo();
3840 }
3841 ''');
3842 _updateAndValidate(
3843 r'''
3844 // 10
3845 main() {
3846 Object x;
3847 x.foo();
3848 }
3849 ''',
3850 expectedSuccess: false);
3851 }
3852
3853 void test_endOfLineComment_remove() {
3854 _resolveUnit(r'''
3855 main() {
3856 // some comment
3857 print(0);
3858 }
3859 ''');
3860 _updateAndValidate(r'''
3861 main() {
3862 print(0);
3863 }
3864 ''');
3865 }
3866
3867 void test_false_constConstructor_initializer() {
3868 _resolveUnit(r'''
3869 class C {
3870 final int x;
3871 const C(this.x);
3872 const C.foo() : x = 0;
3873 }
3874 main() {
3875 const {const C(0): 0, const C.foo(): 1};
3876 }
3877 ''');
3878 _updateAndValidate(
3879 r'''
3880 class C {
3881 final int x;
3882 const C(this.x);
3883 const C.foo() : x = 1;
3884 }
3885 main() {
3886 const {const C(0): 0, const C.foo(): 1};
3887 }
3888 ''',
3889 expectedSuccess: false);
3890 }
3891
3892 void test_false_expressionBody() {
3893 _resolveUnit(r'''
3894 class A {
3895 final f = (() => 1)();
3896 }
3897 ''');
3898 _updateAndValidate(
3899 r'''
3900 class A {
3901 final f = (() => 2)();
3902 }
3903 ''',
3904 expectedSuccess: false);
3905 }
3906
3907 void test_false_topLevelFunction_name() {
3908 _resolveUnit(r'''
3909 a() {}
3910 b() {}
3911 ''');
3912 _updateAndValidate(
3913 r'''
3914 a() {}
3915 bb() {}
3916 ''',
3917 expectedSuccess: false);
3918 }
3919
3920 void test_false_unbalancedCurlyBrackets_inNew() {
3921 _resolveUnit(r'''
3922 class A {
3923 aaa() {
3924 if (true) {
3925 1;
3926 }
3927 }
3928
3929 bbb() {
3930 print(0123456789);
3931 }
3932 }''');
3933 _updateAndValidate(
3934 r'''
3935 class A {
3936 aaa() {
3937 1;
3938 }
3939 }
3940
3941 bbb() {
3942 print(0123456789);
3943 }
3944 }''',
3945 expectedSuccess: false);
3946 }
3947
3948 void test_false_unbalancedCurlyBrackets_inOld() {
3949 _resolveUnit(r'''
3950 class A {
3951 aaa() {
3952 1;
3953 }
3954 }
3955
3956 bbb() {
3957 print(0123456789);
3958 }
3959 }''');
3960 _updateAndValidate(
3961 r'''
3962 class A {
3963 aaa() {
3964 if (true) {
3965 1;
3966 }
3967 }
3968
3969 bbb() {
3970 print(0123456789);
3971 }
3972 }''',
3973 expectedSuccess: false);
3974 }
3975
3976 void test_fieldClassField_propagatedType() {
3977 _resolveUnit(r'''
3978 class A {
3979 static const A b = const B();
3980 const A();
3981 }
3982
3983 class B extends A {
3984 const B();
3985 }
3986
3987 main() {
3988 print(12);
3989 A.b;
3990 }
3991 ''');
3992 _updateAndValidate(r'''
3993 class A {
3994 static const A b = const B();
3995 const A();
3996 }
3997
3998 class B extends A {
3999 const B();
4000 }
4001
4002 main() {
4003 print(123);
4004 A.b;
4005 }
4006 ''');
4007 }
4008
4009 void test_inBody_expression() {
4010 _resolveUnit(r'''
4011 class A {
4012 m() {
4013 print(1);
4014 }
4015 }
4016 ''');
4017 _updateAndValidate(r'''
4018 class A {
4019 m() {
4020 print(2 + 3);
4021 }
4022 }
4023 ''');
4024 }
4025
4026 void test_inBody_insertStatement() {
4027 _resolveUnit(r'''
4028 main() {
4029 print(1);
4030 }
4031 ''');
4032 _updateAndValidate(r'''
4033 main() {
4034 print(0);
4035 print(1);
4036 }
4037 ''');
4038 }
4039
4040 void test_inBody_tokenToNode() {
4041 _resolveUnit(r'''
4042 main() {
4043 var v = 42;
4044 print(v);
4045 }
4046 ''');
4047 _updateAndValidate(r'''
4048 main() {
4049 int v = 42;
4050 print(v);
4051 }
4052 ''');
4053 }
4054
4055 void test_multiple_emptyLine() {
4056 _resolveUnit(r'''
4057 class A {
4058 m() {
4059 return true;
4060 }
4061 }''');
4062 for (int i = 0; i < 6; i++) {
4063 if (i.isEven) {
4064 _updateAndValidate(
4065 r'''
4066 class A {
4067 m() {
4068 return true;
4069
4070 }
4071 }''',
4072 compareWithFull: false);
4073 } else {
4074 _updateAndValidate(
4075 r'''
4076 class A {
4077 m() {
4078 return true;
4079 }
4080 }''',
4081 compareWithFull: false);
4082 }
4083 }
4084 }
4085
4086 void test_multiple_expression() {
4087 _resolveUnit(r'''
4088 main() {
4089 print(1);
4090 }''');
4091 for (int i = 0; i < 6; i++) {
4092 if (i.isEven) {
4093 _updateAndValidate(
4094 r'''
4095 main() {
4096 print(12);
4097 }''',
4098 compareWithFull: false);
4099 } else {
4100 _updateAndValidate(
4101 r'''
4102 main() {
4103 print(1);
4104 }''',
4105 compareWithFull: false);
4106 }
4107 }
4108 }
4109
4110 void test_true_emptyLine_betweenClassMembers_insert() {
4111 _resolveUnit(r'''
4112 class A {
4113 a() {}
4114 b() {}
4115 }
4116 ''');
4117 _updateAndValidate(r'''
4118 class A {
4119 a() {}
4120
4121 b() {}
4122 }
4123 ''');
4124 }
4125
4126 void test_true_emptyLine_betweenClassMembers_remove() {
4127 _resolveUnit(r'''
4128 class A {
4129 a() {}
4130
4131 b() {}
4132 }
4133 ''');
4134 _updateAndValidate(r'''
4135 class A {
4136 a() {}
4137 b() {}
4138 }
4139 ''');
4140 }
4141
4142 void test_true_emptyLine_betweenCompilationUnitMembers_insert() {
4143 _resolveUnit(r'''
4144 a() {}
4145 b() {}
4146 ''');
4147 _updateAndValidate(r'''
4148 a() {}
4149
4150 b() {}
4151 ''');
4152 }
4153
4154 void test_true_emptyLine_betweenCompilationUnitMembers_remove() {
4155 _resolveUnit(r'''
4156 a() {
4157 print(1)
4158 }
4159
4160 b() {
4161 foo(42);
4162 }
4163 foo(String p) {}
4164 ''');
4165 _updateAndValidate(r'''
4166 a() {
4167 print(1)
4168 }
4169 b() {
4170 foo(42);
4171 }
4172 foo(String p) {}
4173 ''');
4174 }
4175
4176 void test_true_todoHint() {
4177 _resolveUnit(r'''
4178 main() {
4179 print(1);
4180 }
4181 foo() {
4182 // TODO
4183 }
4184 ''');
4185 List<AnalysisError> oldErrors = analysisContext.computeErrors(source);
4186 _updateAndValidate(r'''
4187 main() {
4188 print(2);
4189 }
4190 foo() {
4191 // TODO
4192 }
4193 ''');
4194 List<AnalysisError> newErrors = analysisContext.computeErrors(source);
4195 _assertEqualErrors(newErrors, oldErrors);
4196 }
4197
4198 void test_true_wholeConstructor() {
4199 _resolveUnit(r'''
4200 class A {
4201 A(int a) {
4202 print(a);
4203 }
4204 }
4205 ''');
4206 _updateAndValidate(r'''
4207 class A {
4208 A(int b) {
4209 print(b);
4210 }
4211 }
4212 ''');
4213 }
4214
4215 void test_true_wholeConstructor_addInitializer() {
4216 _resolveUnit(r'''
4217 class A {
4218 int field;
4219 A();
4220 }
4221 ''');
4222 _updateAndValidate(r'''
4223 class A {
4224 int field;
4225 A() : field = 5;
4226 }
4227 ''');
4228 }
4229
4230 void test_true_wholeFunction() {
4231 _resolveUnit(r'''
4232 foo() {}
4233 main(int a) {
4234 print(a);
4235 }
4236 ''');
4237 _updateAndValidate(r'''
4238 foo() {}
4239 main(int b) {
4240 print(b);
4241 }
4242 ''');
4243 }
4244
4245 void test_true_wholeFunction_firstTokenInUnit() {
4246 _resolveUnit(r'''
4247 main(int a) {
4248 print(a);
4249 }
4250 ''');
4251 _updateAndValidate(r'''
4252 main(int b) {
4253 print(b);
4254 }
4255 ''');
4256 }
4257
4258 void test_true_wholeMethod() {
4259 _resolveUnit(r'''
4260 class A {
4261 main(int a) {
4262 print(a);
4263 }
4264 }
4265 ''');
4266 _updateAndValidate(r'''
4267 class A {
4268 main(int b) {
4269 print(b);
4270 }
4271 }
4272 ''');
4273 }
4274
4275 void test_unusedHint_add_wasUsedOnlyInPart() {
4276 Source partSource = addNamedSource(
4277 '/my_unit.dart',
4278 r'''
4279 part of lib;
4280
4281 f(A a) {
4282 a._foo();
4283 }
4284 ''');
4285 _resolveUnit(r'''
4286 library lib;
4287 part 'my_unit.dart';
4288 class A {
4289 _foo() {
4290 print(1);
4291 }
4292 }
4293 ''');
4294 _runTasks();
4295 // perform incremental resolution
4296 _resetWithIncremental(true);
4297 analysisContext2.setContents(
4298 partSource,
4299 r'''
4300 part of lib;
4301
4302 f(A a) {
4303 // a._foo();
4304 }
4305 ''');
4306 // no hints right now, because we delay hints computing
4307 {
4308 List<AnalysisError> errors = analysisContext.getErrors(source).errors;
4309 expect(errors, isEmpty);
4310 }
4311 // a new hint should be added
4312 List<AnalysisError> errors = analysisContext.computeErrors(source);
4313 expect(errors, hasLength(1));
4314 expect(errors[0].errorCode.type, ErrorType.HINT);
4315 // the same hint should be reported using a ChangeNotice
4316 bool noticeFound = false;
4317 AnalysisResult result = analysisContext2.performAnalysisTask();
4318 for (ChangeNotice notice in result.changeNotices) {
4319 if (notice.source == source) {
4320 expect(notice.errors, contains(errors[0]));
4321 noticeFound = true;
4322 }
4323 }
4324 expect(noticeFound, isTrue);
4325 }
4326
4327 void test_unusedHint_false_stillUsedInPart() {
4328 addNamedSource(
4329 '/my_unit.dart',
4330 r'''
4331 part of lib;
4332
4333 f(A a) {
4334 a._foo();
4335 }
4336 ''');
4337 _resolveUnit(r'''
4338 library lib;
4339 part 'my_unit.dart';
4340 class A {
4341 _foo() {
4342 print(1);
4343 }
4344 }
4345 ''');
4346 // perform incremental resolution
4347 _resetWithIncremental(true);
4348 analysisContext2.setContents(
4349 source,
4350 r'''
4351 library lib;
4352 part 'my_unit.dart';
4353 class A {
4354 _foo() {
4355 print(12);
4356 }
4357 }
4358 ''');
4359 // no hints
4360 List<AnalysisError> errors = analysisContext.getErrors(source).errors;
4361 expect(errors, isEmpty);
4362 }
4363
4364 void test_updateErrors_addNew_hint1() {
4365 _resolveUnit(r'''
4366 int main() {
4367 return 42;
4368 }
4369 ''');
4370 _updateAndValidate(r'''
4371 int main() {
4372 }
4373 ''');
4374 }
4375
4376 void test_updateErrors_addNew_hint2() {
4377 _resolveUnit(r'''
4378 main() {
4379 int v = 0;
4380 print(v);
4381 }
4382 ''');
4383 _updateAndValidate(r'''
4384 main() {
4385 int v = 0;
4386 }
4387 ''');
4388 }
4389
4390 void test_updateErrors_addNew_parse() {
4391 _resolveUnit(r'''
4392 main() {
4393 print(42);
4394 }
4395 ''');
4396 _updateAndValidate(r'''
4397 main() {
4398 print(42)
4399 }
4400 ''');
4401 }
4402
4403 void test_updateErrors_addNew_resolve() {
4404 _resolveUnit(r'''
4405 main() {
4406 foo();
4407 }
4408 foo() {}
4409 ''');
4410 _updateAndValidate(r'''
4411 main() {
4412 bar();
4413 }
4414 foo() {}
4415 ''');
4416 }
4417
4418 void test_updateErrors_addNew_resolve2() {
4419 _resolveUnit(r'''
4420 // this comment is important to reproduce the problem
4421 main() {
4422 int vvv = 42;
4423 print(vvv);
4424 }
4425 ''');
4426 _updateAndValidate(r'''
4427 // this comment is important to reproduce the problem
4428 main() {
4429 int vvv = 42;
4430 print(vvv2);
4431 }
4432 ''');
4433 }
4434
4435 void test_updateErrors_addNew_scan() {
4436 _resolveUnit(r'''
4437 main() {
4438 1;
4439 }
4440 ''');
4441 _updateAndValidate(r'''
4442 main() {
4443 1e;
4444 }
4445 ''');
4446 }
4447
4448 void test_updateErrors_addNew_verify() {
4449 _resolveUnit(r'''
4450 main() {
4451 foo(0);
4452 }
4453 foo(int p) {}
4454 ''');
4455 _updateAndValidate(r'''
4456 main() {
4457 foo('abc');
4458 }
4459 foo(int p) {}
4460 ''');
4461 }
4462
4463 void test_updateErrors_removeExisting_hint() {
4464 _resolveUnit(r'''
4465 int main() {
4466 }
4467 ''');
4468 _updateAndValidate(r'''
4469 int main() {
4470 return 42;
4471 }
4472 ''');
4473 }
4474
4475 void test_updateErrors_removeExisting_verify() {
4476 _resolveUnit(r'''
4477 f1() {
4478 print(1)
4479 }
4480 f2() {
4481 print(22)
4482 }
4483 f3() {
4484 print(333)
4485 }
4486 ''');
4487 _updateAndValidate(r'''
4488 f1() {
4489 print(1)
4490 }
4491 f2() {
4492 print(22);
4493 }
4494 f3() {
4495 print(333)
4496 }
4497 ''');
4498 }
4499
4500 void test_updateErrors_shiftExisting() {
4501 _resolveUnit(r'''
4502 f1() {
4503 print(1)
4504 }
4505 f2() {
4506 print(2);
4507 }
4508 f3() {
4509 print(333)
4510 }
4511 ''');
4512 _updateAndValidate(r'''
4513 f1() {
4514 print(1)
4515 }
4516 f2() {
4517 print(22);
4518 }
4519 f3() {
4520 print(333)
4521 }
4522 ''');
4523 }
4524
4525 void test_whitespace_getElementAt() {
4526 _resolveUnit(r'''
4527 class A {}
4528 class B extends A {}
4529 ''');
4530 {
4531 ClassElement typeA = oldUnitElement.getType('A');
4532 expect(oldUnitElement.getElementAt(typeA.nameOffset), typeA);
4533 }
4534 {
4535 ClassElement typeB = oldUnitElement.getType('B');
4536 expect(oldUnitElement.getElementAt(typeB.nameOffset), typeB);
4537 }
4538 _updateAndValidate(r'''
4539 class A {}
4540
4541 class B extends A {}
4542 ''');
4543 // getElementAt() caches results, it should be notified when offset
4544 // are changed.
4545 {
4546 ClassElement typeA = oldUnitElement.getType('A');
4547 expect(oldUnitElement.getElementAt(typeA.nameOffset), typeA);
4548 }
4549 {
4550 ClassElement typeB = oldUnitElement.getType('B');
4551 expect(oldUnitElement.getElementAt(typeB.nameOffset), typeB);
4552 }
4553 }
4554
4555 void _assertEqualLineInfo(LineInfo incrLineInfo, LineInfo fullLineInfo) {
4556 for (int offset = 0; offset < 1000; offset++) {
4557 LineInfo_Location incrLocation = incrLineInfo.getLocation(offset);
4558 LineInfo_Location fullLocation = fullLineInfo.getLocation(offset);
4559 if (incrLocation.lineNumber != fullLocation.lineNumber ||
4560 incrLocation.columnNumber != fullLocation.columnNumber) {
4561 fail('At offset $offset ' +
4562 '(${incrLocation.lineNumber}, ${incrLocation.columnNumber})' +
4563 ' != ' +
4564 '(${fullLocation.lineNumber}, ${fullLocation.columnNumber})');
4565 }
4566 }
4567 }
4568
4569 /**
4570 * Reset the analysis context to have the 'incremental' option set to the
4571 * given value.
4572 */
4573 void _resetWithIncremental(bool enable) {
4574 AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl();
4575 analysisOptions.incremental = enable;
4576 analysisOptions.incrementalApi = enable;
4577 // log.logger = log.PRINT_LOGGER;
4578 log.logger = log.NULL_LOGGER;
4579 analysisContext2.analysisOptions = analysisOptions;
4580 }
4581
4582 void _resolveUnit(String code) {
4583 this.code = code;
4584 source = addSource(code);
4585 oldLibrary = resolve2(source);
4586 oldUnit = resolveCompilationUnit(source, oldLibrary);
4587 oldUnitElement = oldUnit.element;
4588 }
4589
4590 void _runTasks() {
4591 AnalysisResult result = analysisContext.performAnalysisTask();
4592 while (result.changeNotices != null) {
4593 result = analysisContext.performAnalysisTask();
4594 }
4595 }
4596
4597 void _updateAndValidate(String newCode,
4598 {bool expectedSuccess: true, bool compareWithFull: true}) {
4599 // Run any pending tasks tasks.
4600 _runTasks();
4601 // Update the source - currently this may cause incremental resolution.
4602 // Then request the updated resolved unit.
4603 _resetWithIncremental(true);
4604 analysisContext2.setContents(source, newCode);
4605 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary);
4606 List<AnalysisError> newErrors = analysisContext.computeErrors(source);
4607 LineInfo newLineInfo = analysisContext.getLineInfo(source);
4608 // check for expected failure
4609 if (!expectedSuccess) {
4610 expect(newUnit.element, isNot(same(oldUnitElement)));
4611 return;
4612 }
4613 // The existing CompilationUnit[Element] should be updated.
4614 expect(newUnit, same(oldUnit));
4615 expect(newUnit.element, same(oldUnitElement));
4616 expect(analysisContext.parseCompilationUnit(source), same(oldUnit));
4617 // The only expected pending task should return the same resolved
4618 // "newUnit", so all clients will get it using the usual way.
4619 AnalysisResult analysisResult = analysisContext.performAnalysisTask();
4620 ChangeNotice notice = analysisResult.changeNotices[0];
4621 expect(notice.resolvedDartUnit, same(newUnit));
4622 // Resolve "newCode" from scratch.
4623 if (compareWithFull) {
4624 _resetWithIncremental(false);
4625 changeSource(source, newCode);
4626 _runTasks();
4627 LibraryElement library = resolve2(source);
4628 CompilationUnit fullNewUnit = resolveCompilationUnit(source, library);
4629 // Validate tokens.
4630 _assertEqualTokens(newUnit, fullNewUnit);
4631 // Validate LineInfo
4632 _assertEqualLineInfo(newLineInfo, analysisContext.getLineInfo(source));
4633 // Validate that "incremental" and "full" units have the same resolution.
4634 try {
4635 assertSameResolution(newUnit, fullNewUnit, validateTypes: true);
4636 } on IncrementalResolutionMismatch catch (mismatch) {
4637 fail(mismatch.message);
4638 }
4639 List<AnalysisError> newFullErrors =
4640 analysisContext.getErrors(source).errors;
4641 _assertEqualErrors(newErrors, newFullErrors);
4642 }
4643 }
4644
4645 static void _assertEqualToken(Token incrToken, Token fullToken) {
4646 // print('[${incrToken.offset}] |$incrToken| vs. [${fullToken.offset}] |$full Token|');
4647 expect(incrToken.type, fullToken.type);
4648 expect(incrToken.offset, fullToken.offset);
4649 expect(incrToken.length, fullToken.length);
4650 expect(incrToken.lexeme, fullToken.lexeme);
4651 }
4652
4653 static void _assertEqualTokens(
4654 CompilationUnit incrUnit, CompilationUnit fullUnit) {
4655 Token incrToken = incrUnit.beginToken;
4656 Token fullToken = fullUnit.beginToken;
4657 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) {
4658 _assertEqualToken(incrToken, fullToken);
4659 // comments
4660 {
4661 Token incrComment = incrToken.precedingComments;
4662 Token fullComment = fullToken.precedingComments;
4663 while (true) {
4664 if (fullComment == null) {
4665 expect(incrComment, isNull);
4666 break;
4667 }
4668 expect(incrComment, isNotNull);
4669 _assertEqualToken(incrComment, fullComment);
4670 incrComment = incrComment.next;
4671 fullComment = fullComment.next;
4672 }
4673 }
4674 // next tokens
4675 incrToken = incrToken.next;
4676 fullToken = fullToken.next;
4677 }
4678 }
4679 }
4680
4681 @reflectiveTest
4682 class ResolutionContextBuilderTest extends EngineTestCase {
4683 GatheringErrorListener listener = new GatheringErrorListener();
4684
4685 void test_scopeFor_ClassDeclaration() {
4686 Scope scope = _scopeFor(_createResolvedClassDeclaration());
4687 EngineTestCase.assertInstanceOf(
4688 (obj) => obj is LibraryScope, LibraryScope, scope);
4689 }
4690
4691 void test_scopeFor_ClassTypeAlias() {
4692 Scope scope = _scopeFor(_createResolvedClassTypeAlias());
4693 EngineTestCase.assertInstanceOf(
4694 (obj) => obj is LibraryScope, LibraryScope, scope);
4695 }
4696
4697 void test_scopeFor_CompilationUnit() {
4698 Scope scope = _scopeFor(_createResolvedCompilationUnit());
4699 EngineTestCase.assertInstanceOf(
4700 (obj) => obj is LibraryScope, LibraryScope, scope);
4701 }
4702
4703 void test_scopeFor_ConstructorDeclaration() {
4704 Scope scope = _scopeFor(_createResolvedConstructorDeclaration());
4705 EngineTestCase.assertInstanceOf(
4706 (obj) => obj is ClassScope, ClassScope, scope);
4707 }
4708
4709 void test_scopeFor_ConstructorDeclaration_parameters() {
4710 Scope scope = _scopeFor(_createResolvedConstructorDeclaration().parameters);
4711 EngineTestCase.assertInstanceOf(
4712 (obj) => obj is FunctionScope, FunctionScope, scope);
4713 }
4714
4715 void test_scopeFor_FunctionDeclaration() {
4716 Scope scope = _scopeFor(_createResolvedFunctionDeclaration());
4717 EngineTestCase.assertInstanceOf(
4718 (obj) => obj is LibraryScope, LibraryScope, scope);
4719 }
4720
4721 void test_scopeFor_FunctionDeclaration_parameters() {
4722 Scope scope = _scopeFor(
4723 _createResolvedFunctionDeclaration().functionExpression.parameters);
4724 EngineTestCase.assertInstanceOf(
4725 (obj) => obj is FunctionScope, FunctionScope, scope);
4726 }
4727
4728 void test_scopeFor_FunctionTypeAlias() {
4729 Scope scope = _scopeFor(_createResolvedFunctionTypeAlias());
4730 EngineTestCase.assertInstanceOf(
4731 (obj) => obj is LibraryScope, LibraryScope, scope);
4732 }
4733
4734 void test_scopeFor_FunctionTypeAlias_parameters() {
4735 Scope scope = _scopeFor(_createResolvedFunctionTypeAlias().parameters);
4736 EngineTestCase.assertInstanceOf(
4737 (obj) => obj is FunctionTypeScope, FunctionTypeScope, scope);
4738 }
4739
4740 void test_scopeFor_MethodDeclaration() {
4741 Scope scope = _scopeFor(_createResolvedMethodDeclaration());
4742 EngineTestCase.assertInstanceOf(
4743 (obj) => obj is ClassScope, ClassScope, scope);
4744 }
4745
4746 void test_scopeFor_MethodDeclaration_body() {
4747 Scope scope = _scopeFor(_createResolvedMethodDeclaration().body);
4748 EngineTestCase.assertInstanceOf(
4749 (obj) => obj is FunctionScope, FunctionScope, scope);
4750 }
4751
4752 void test_scopeFor_notInCompilationUnit() {
4753 try {
4754 _scopeFor(AstFactory.identifier3("x"));
4755 fail("Expected AnalysisException");
4756 } on AnalysisException {
4757 // Expected
4758 }
4759 }
4760
4761 void test_scopeFor_null() {
4762 try {
4763 _scopeFor(null);
4764 fail("Expected AnalysisException");
4765 } on AnalysisException {
4766 // Expected
4767 }
4768 }
4769
4770 void test_scopeFor_unresolved() {
4771 try {
4772 _scopeFor(AstFactory.compilationUnit());
4773 fail("Expected AnalysisException");
4774 } on AnalysisException {
4775 // Expected
4776 }
4777 }
4778
4779 ClassDeclaration _createResolvedClassDeclaration() {
4780 CompilationUnit unit = _createResolvedCompilationUnit();
4781 String className = "C";
4782 ClassDeclaration classNode = AstFactory.classDeclaration(
4783 null, className, AstFactory.typeParameterList(), null, null, null);
4784 unit.declarations.add(classNode);
4785 ClassElement classElement = ElementFactory.classElement2(className);
4786 classNode.name.staticElement = classElement;
4787 (unit.element as CompilationUnitElementImpl).types = <ClassElement>[
4788 classElement
4789 ];
4790 return classNode;
4791 }
4792
4793 ClassTypeAlias _createResolvedClassTypeAlias() {
4794 CompilationUnit unit = _createResolvedCompilationUnit();
4795 String className = "C";
4796 ClassTypeAlias classNode = AstFactory.classTypeAlias(
4797 className, AstFactory.typeParameterList(), null, null, null, null);
4798 unit.declarations.add(classNode);
4799 ClassElement classElement = ElementFactory.classElement2(className);
4800 classNode.name.staticElement = classElement;
4801 (unit.element as CompilationUnitElementImpl).types = <ClassElement>[
4802 classElement
4803 ];
4804 return classNode;
4805 }
4806
4807 CompilationUnit _createResolvedCompilationUnit() {
4808 CompilationUnit unit = AstFactory.compilationUnit();
4809 LibraryElementImpl library =
4810 ElementFactory.library(AnalysisContextFactory.contextWithCore(), "lib");
4811 unit.element = library.definingCompilationUnit;
4812 return unit;
4813 }
4814
4815 ConstructorDeclaration _createResolvedConstructorDeclaration() {
4816 ClassDeclaration classNode = _createResolvedClassDeclaration();
4817 String constructorName = "f";
4818 ConstructorDeclaration constructorNode = AstFactory.constructorDeclaration(
4819 AstFactory.identifier3(constructorName),
4820 null,
4821 AstFactory.formalParameterList(),
4822 null);
4823 classNode.members.add(constructorNode);
4824 ConstructorElement constructorElement =
4825 ElementFactory.constructorElement2(classNode.element, null);
4826 constructorNode.element = constructorElement;
4827 (classNode.element as ClassElementImpl).constructors = <ConstructorElement>[
4828 constructorElement
4829 ];
4830 return constructorNode;
4831 }
4832
4833 FunctionDeclaration _createResolvedFunctionDeclaration() {
4834 CompilationUnit unit = _createResolvedCompilationUnit();
4835 String functionName = "f";
4836 FunctionDeclaration functionNode = AstFactory.functionDeclaration(
4837 null, null, functionName, AstFactory.functionExpression());
4838 unit.declarations.add(functionNode);
4839 FunctionElement functionElement =
4840 ElementFactory.functionElement(functionName);
4841 functionNode.name.staticElement = functionElement;
4842 (unit.element as CompilationUnitElementImpl).functions = <FunctionElement>[
4843 functionElement
4844 ];
4845 return functionNode;
4846 }
4847
4848 FunctionTypeAlias _createResolvedFunctionTypeAlias() {
4849 CompilationUnit unit = _createResolvedCompilationUnit();
4850 FunctionTypeAlias aliasNode = AstFactory.typeAlias(
4851 AstFactory.typeName4("A"),
4852 "F",
4853 AstFactory.typeParameterList(),
4854 AstFactory.formalParameterList());
4855 unit.declarations.add(aliasNode);
4856 SimpleIdentifier aliasName = aliasNode.name;
4857 FunctionTypeAliasElement aliasElement =
4858 new FunctionTypeAliasElementImpl.forNode(aliasName);
4859 aliasName.staticElement = aliasElement;
4860 (unit.element as CompilationUnitElementImpl).typeAliases =
4861 <FunctionTypeAliasElement>[aliasElement];
4862 return aliasNode;
4863 }
4864
4865 MethodDeclaration _createResolvedMethodDeclaration() {
4866 ClassDeclaration classNode = _createResolvedClassDeclaration();
4867 String methodName = "f";
4868 MethodDeclaration methodNode = AstFactory.methodDeclaration(
4869 null,
4870 null,
4871 null,
4872 null,
4873 AstFactory.identifier3(methodName),
4874 AstFactory.formalParameterList());
4875 classNode.members.add(methodNode);
4876 MethodElement methodElement =
4877 ElementFactory.methodElement(methodName, null);
4878 methodNode.name.staticElement = methodElement;
4879 (classNode.element as ClassElementImpl).methods = <MethodElement>[
4880 methodElement
4881 ];
4882 return methodNode;
4883 }
4884
4885 Scope _scopeFor(AstNode node) {
4886 return ResolutionContextBuilder.contextFor(node, listener).scope;
4887 }
4888 }
4889
4890 class _Edit {
4891 final int offset;
4892 final int length;
4893 final String replacement;
4894 _Edit(this.offset, this.length, this.replacement);
4895 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/generated/engine_test.dart ('k') | packages/analyzer/test/generated/incremental_scanner_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698