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

Side by Side Diff: pkg/analysis_server/test/services/refactoring/inline_method_test.dart

Issue 526583002: 'Inline Method' refactoring. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
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 test.services.refactoring.inline_method;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/src/protocol.dart' hide Element;
10 import 'package:analysis_server/src/services/refactoring/inline_method.dart';
11 import 'package:analysis_server/src/services/refactoring/refactoring.dart';
12 import 'package:analysis_testing/reflective_tests.dart';
13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:unittest/unittest.dart';
15
16 import 'abstract_refactoring.dart';
17
18
19 main() {
20 groupSep = ' | ';
21 runReflectiveTests(InlineMethodTest);
22 }
23
24
25 @ReflectiveTestCase()
26 class InlineMethodTest extends RefactoringTest {
27 InlineMethodRefactoringImpl refactoring;
28 bool deleteSource;
29 bool inlineAll;
30
31 test_access_FunctionElement() {
32 indexTestUnit(r'''
33 test(a, b) {
34 return a + b;
35 }
36 main() {
37 var res = test(1, 2);
38 }
39 ''');
40 _createRefactoring('test(a, b)');
41 // validate state
42 return refactoring.checkInitialConditions().then((_) {
43 expect(refactoring.refactoringName, 'Inline Function');
44 });
45 }
46
47 test_access_MethodElement() {
48 indexTestUnit(r'''
49 class A {
50 test(a, b) {
51 return a + b;
52 }
53 main() {
54 var res = test(1, 2);
55 }
56 }
57 ''');
58 _createRefactoring('test(a, b)');
59 // validate state
60 return refactoring.checkInitialConditions().then((_) {
61 expect(refactoring.refactoringName, 'Inline Method');
62 });
63 }
64
65 test_bad_cascadeInvocation() {
66 indexTestUnit(r'''
67 class A {
68 foo() {}
69 bar() {}
70 test() {}
71 }
72 main() {
73 A a = new A();
74 a..foo()..test()..bar();
75 }
76 ''');
77 _createRefactoring('test() {');
78 // error
79 return refactoring.checkAllConditions().then((status) {
80 var location = new SourceRange(findOffset('..test()'), '..test()'.length);
81 assertRefactoringStatus(
82 status,
83 RefactoringProblemSeverity.ERROR,
84 expectedMessage: 'Cannot inline cascade invocation.',
85 expectedContextRange: location);
86 });
87 }
88
89 test_bad_deleteSource_inlineOne() {
90 indexTestUnit(r'''
91 test(a, b) {
92 return a + b;
93 }
94 main() {
95 var res1 = test(1, 2);
96 var res2 = test(10, 20);
97 }
98 ''');
99 _createRefactoring('test(1, 2)');
100 // error
101 return refactoring.checkInitialConditions().then((status) {
102 assertRefactoringStatusOK(status);
103 refactoring.deleteSource = true;
104 refactoring.inlineAll = false;
105 return refactoring.checkFinalConditions().then((status) {
106 assertRefactoringStatus(
107 status,
108 RefactoringProblemSeverity.ERROR,
109 expectedMessage: 'All references must be inlined to remove the sourc e.');
110 });
111 });
112 }
113
114 test_bad_notExecutableElement() {
115 indexTestUnit(r'''
116 main() {
117 }
118 ''');
119 _createRefactoring(') {');
120 // error
121 return _assertConditionsFatal(
122 'Method declaration or reference must be selected to activate this refac toring.');
123 }
124
125 test_bad_notSimpleIdentifier() {
126 indexTestUnit(r'''
127 main() {
128 var test = 42;
129 var res = test;
130 }
131 ''');
132 _createRefactoring('test;');
133 // error
134 return _assertConditionsFatal(
135 'Method declaration or reference must be selected to activate this refac toring.');
136 }
137
138 test_bad_operator() {
139 indexTestUnit(r'''
140 class A {
141 operator -(other) => this;
142 }
143 ''');
144 _createRefactoring('-(other)');
145 // error
146 return _assertConditionsFatal('Cannot inline operator.');
147 }
148
149 test_bad_reference_toClassMethod() {
150 indexTestUnit(r'''
151 class A {
152 test(a, b) {
153 print(a);
154 print(b);
155 }
156 }
157 main() {
158 print(new A().test);
159 }
160 ''');
161 _createRefactoring('test(a, b)');
162 // error
163 return _assertConditionsFatal('Cannot inline class method reference.');
164 }
165
166 test_bad_severalReturns() {
167 indexTestUnit(r'''
168 test() {
169 if (true) {
170 return 1;
171 }
172 return 2;
173 }
174 main() {
175 var res = test();
176 }
177 ''');
178 _createRefactoring('test() {');
179 // error
180 return _assertConditionsError('Ambiguous return value.');
181 }
182
183 test_fieldAccessor_getter() {
184 indexTestUnit(r'''
185 class A {
186 var f;
187 get foo {
188 return f * 2;
189 }
190 }
191 main() {
192 A a = new A();
193 print(a.foo);
194 }
195 ''');
196 _createRefactoring('foo {');
197 // validate change
198 return _assertSuccessfulRefactoring(r'''
199 class A {
200 var f;
201 }
202 main() {
203 A a = new A();
204 print(a.f * 2);
205 }
206 ''');
207 }
208
209 test_fieldAccessor_getter_PropertyAccess() {
210 indexTestUnit(r'''
211 class A {
212 var f;
213 get foo {
214 return f * 2;
215 }
216 }
217 class B {
218 A a = new A();
219 }
220 main() {
221 B b = new B();
222 print(b.a.foo);
223 }
224 ''');
225 _createRefactoring('foo {');
226 // validate change
227 return _assertSuccessfulRefactoring(r'''
228 class A {
229 var f;
230 }
231 class B {
232 A a = new A();
233 }
234 main() {
235 B b = new B();
236 print(b.a.f * 2);
237 }
238 ''');
239 }
240
241 test_fieldAccessor_setter() {
242 indexTestUnit(r'''
243 class A {
244 var f;
245 set foo(x) {
246 f = x;
247 }
248 }
249 main() {
250 A a = new A();
251 a.foo = 0;
252 }
253 ''');
254 _createRefactoring('foo(x) {');
255 // validate change
256 return _assertSuccessfulRefactoring(r'''
257 class A {
258 var f;
259 }
260 main() {
261 A a = new A();
262 a.f = 0;
263 }
264 ''');
265 }
266
267 test_fieldAccessor_setter_PropertyAccess() {
268 indexTestUnit(r'''
269 class A {
270 var f;
271 set foo(x) {
272 f = x;
273 }
274 }
275 class B {
276 A a = new A();
277 }
278 main() {
279 B b = new B();
280 b.a.foo = 0;
281 }
282 ''');
283 _createRefactoring('foo(x) {');
284 // validate change
285 return _assertSuccessfulRefactoring(r'''
286 class A {
287 var f;
288 }
289 class B {
290 A a = new A();
291 }
292 main() {
293 B b = new B();
294 b.a.f = 0;
295 }
296 ''');
297 }
298
299 test_function_expressionFunctionBody() {
300 indexTestUnit(r'''
301 test(a, b) => a + b;
302 main() {
303 print(test(1, 2));
304 }
305 ''');
306 _createRefactoring('test(a, b)');
307 // validate change
308 return _assertSuccessfulRefactoring(r'''
309 main() {
310 print(1 + 2);
311 }
312 ''');
313 }
314
315 test_function_hasReturn_assign() {
316 indexTestUnit(r'''
317 test(a, b) {
318 print(a);
319 print(b);
320 return a + b;
321 }
322 main() {
323 var v;
324 v = test(1, 2);
325 }
326 ''');
327 _createRefactoring('test(a, b)');
328 // validate change
329 return _assertSuccessfulRefactoring(r'''
330 main() {
331 var v;
332 print(1);
333 print(2);
334 v = 1 + 2;
335 }
336 ''');
337 }
338
339 test_function_hasReturn_hasReturnType() {
340 indexTestUnit(r'''
341 int test(a, b) {
342 return a + b;
343 }
344 main() {
345 var v = test(1, 2);
346 }
347 ''');
348 _createRefactoring('test(a, b)');
349 // validate change
350 return _assertSuccessfulRefactoring(r'''
351 main() {
352 var v = 1 + 2;
353 }
354 ''');
355 }
356
357 test_function_hasReturn_noVars_oneUsage() {
358 indexTestUnit(r'''
359 test(a, b) {
360 print(a);
361 print(b);
362 return a + b;
363 }
364 main() {
365 var v = test(1, 2);
366 }
367 ''');
368 _createRefactoring('test(a, b)');
369 // validate change
370 return _assertSuccessfulRefactoring(r'''
371 main() {
372 print(1);
373 print(2);
374 var v = 1 + 2;
375 }
376 ''');
377 }
378
379 test_function_multilineString() {
380 indexTestUnit(r"""
381 main() {
382 {
383 test();
384 }
385 }
386 test() {
387 print('''
388 first line
389 second line
390 ''');
391 }
392 """);
393 _createRefactoring('test() {');
394 // validate change
395 return _assertSuccessfulRefactoring(r"""
396 main() {
397 {
398 print('''
399 first line
400 second line
401 ''');
402 }
403 }
404 """);
405 }
406
407 test_function_noReturn_hasVars_hasConflict_fieldSuperClass() {
408 indexTestUnit(r'''
409 class A {
410 var c;
411 }
412 class B extends A {
413 foo() {
414 test(1, 2);
415 }
416 }
417 test(a, b) {
418 var c = a + b;
419 print(c);
420 }
421 ''');
422 _createRefactoring('test(a, b)');
423 // validate change
424 return _assertSuccessfulRefactoring(r'''
425 class A {
426 var c;
427 }
428 class B extends A {
429 foo() {
430 var c2 = 1 + 2;
431 print(c2);
432 }
433 }
434 ''');
435 }
436
437 test_function_noReturn_hasVars_hasConflict_fieldThisClass() {
438 indexTestUnit(r'''
439 class A {
440 var c;
441 foo() {
442 test(1, 2);
443 }
444 }
445 test(a, b) {
446 var c = a + b;
447 print(c);
448 }
449 ''');
450 _createRefactoring('test(a, b)');
451 // validate change
452 return _assertSuccessfulRefactoring(r'''
453 class A {
454 var c;
455 foo() {
456 var c2 = 1 + 2;
457 print(c2);
458 }
459 }
460 ''');
461 }
462
463 test_function_noReturn_hasVars_hasConflict_localAfter() {
464 indexTestUnit(r'''
465 test(a, b) {
466 var c = a + b;
467 print(c);
468 }
469 main() {
470 test(1, 2);
471 var c = 0;
472 }
473 ''');
474 _createRefactoring('test(a, b)');
475 // validate change
476 return _assertSuccessfulRefactoring(r'''
477 main() {
478 var c2 = 1 + 2;
479 print(c2);
480 var c = 0;
481 }
482 ''');
483 }
484
485 test_function_noReturn_hasVars_hasConflict_localBefore() {
486 indexTestUnit(r'''
487 test(a, b) {
488 var c = a + b;
489 print(c);
490 }
491 main() {
492 var c = 0;
493 test(1, 2);
494 }
495 ''');
496 _createRefactoring('test(a, b)');
497 // validate change
498 return _assertSuccessfulRefactoring(r'''
499 main() {
500 var c = 0;
501 var c2 = 1 + 2;
502 print(c2);
503 }
504 ''');
505 }
506
507 test_function_noReturn_hasVars_noConflict() {
508 indexTestUnit(r'''
509 test(a, b) {
510 var c = a + b;
511 print(c);
512 }
513 main() {
514 test(1, 2);
515 }
516 ''');
517 _createRefactoring('test(a, b)');
518 // validate change
519 return _assertSuccessfulRefactoring(r'''
520 main() {
521 var c = 1 + 2;
522 print(c);
523 }
524 ''');
525 }
526
527 test_function_noReturn_noVars_oneUsage() {
528 indexTestUnit(r'''
529 test(a, b) {
530 print(a);
531 print(b);
532 }
533 main() {
534 test(1, 2);
535 }
536 ''');
537 _createRefactoring('test(a, b)');
538 // validate change
539 return _assertSuccessfulRefactoring(r'''
540 main() {
541 print(1);
542 print(2);
543 }
544 ''');
545 }
546
547 test_function_noReturn_noVars_useIndentation() {
548 indexTestUnit(r'''
549 test(a, b) {
550 print(a);
551 print(b);
552 }
553 main() {
554 {
555 test(1, 2);
556 }
557 }
558 ''');
559 _createRefactoring('test(a, b)');
560 // validate change
561 return _assertSuccessfulRefactoring(r'''
562 main() {
563 {
564 print(1);
565 print(2);
566 }
567 }
568 ''');
569 }
570
571 test_function_noReturn_voidReturnType() {
572 indexTestUnit(r'''
573 void test(a, b) {
574 print(a + b);
575 }
576 main() {
577 test(1, 2);
578 }
579 ''');
580 _createRefactoring('test(a, b)');
581 // validate change
582 return _assertSuccessfulRefactoring(r'''
583 main() {
584 print(1 + 2);
585 }
586 ''');
587 }
588
589 test_function_notStatement_oneStatement_assign() {
590 indexTestUnit(r'''
591 test(int p) {
592 print(p * 2);
593 }
594 main() {
595 var v;
596 v = test(0);
597 }
598 ''');
599 _createRefactoring('test(int p)');
600 // validate change
601 return _assertSuccessfulRefactoring(r'''
602 main() {
603 var v;
604 v = (int p) {
605 print(p * 2);
606 }(0);
607 }
608 ''');
609 }
610
611 test_function_notStatement_oneStatement_variableDeclaration() {
612 indexTestUnit(r'''
613 test(int p) {
614 print(p * 2);
615 }
616 main() {
617 var v = test(0);
618 }
619 ''');
620 _createRefactoring('test(int p)');
621 // validate change
622 return _assertSuccessfulRefactoring(r'''
623 main() {
624 var v = (int p) {
625 print(p * 2);
626 }(0);
627 }
628 ''');
629 }
630
631 test_function_notStatement_severalStatements() {
632 indexTestUnit(r'''
633 test(int p) {
634 print(p);
635 print(p * 2);
636 }
637 main() {
638 var v = test(0);
639 }
640 ''');
641 _createRefactoring('test(int p)');
642 // validate change
643 return _assertSuccessfulRefactoring(r'''
644 main() {
645 var v = (int p) {
646 print(p);
647 print(p * 2);
648 }(0);
649 }
650 ''');
651 }
652
653 test_function_notStatement_zeroStatements() {
654 indexTestUnit(r'''
655 test(int p) {
656 }
657 main() {
658 var v = test(0);
659 }
660 ''');
661 _createRefactoring('test(int p)');
662 // validate change
663 return _assertSuccessfulRefactoring(r'''
664 main() {
665 var v = (int p) {
666 }(0);
667 }
668 ''');
669 }
670
671 test_function_singleStatement() {
672 indexTestUnit(r'''
673 var topLevelField = 0;
674 test() {
675 print(topLevelField);
676 }
677 main() {
678 test();
679 }
680 ''');
681 _createRefactoring('test() {');
682 // validate change
683 return _assertSuccessfulRefactoring(r'''
684 var topLevelField = 0;
685 main() {
686 print(topLevelField);
687 }
688 ''');
689 }
690
691 test_initialMode_all() {
692 indexTestUnit(r'''
693 test(a, b) {
694 return a + b;
695 }
696 main() {
697 var res = test(1, 2);
698 }
699 ''');
700 _createRefactoring('test(a, b)');
701 // validate state
702 return refactoring.checkInitialConditions().then((_) {
703 expect(refactoring.deleteSource, true);
704 expect(refactoring.inlineAll, true);
705 });
706 }
707
708 test_initialMode_single() {
709 indexTestUnit(r'''
710 test(a, b) {
711 return a + b;
712 }
713 main() {
714 var res1 = test(1, 2);
715 var res2 = test(10, 20);
716 }
717 ''');
718 _createRefactoring('test(1, 2)');
719 deleteSource = false;
720 // validate state
721 return refactoring.checkInitialConditions().then((_) {
722 expect(refactoring.deleteSource, false);
723 expect(refactoring.inlineAll, false);
724 });
725 }
726
727 test_method_emptyBody() {
728 indexTestUnit(r'''
729 abstract class A {
730 test();
731 }
732 main(A a) {
733 print(a.test());
734 }
735 ''');
736 _createRefactoring('test();');
737 // error
738 return _assertConditionsFatal('Cannot inline method without body.');
739 }
740
741 test_method_fieldInstance() {
742 indexTestUnit(r'''
743 class A {
744 var fA;
745 }
746 class B extends A {
747 var fB;
748 test() {
749 print(fA);
750 print(fB);
751 print(this.fA);
752 print(this.fB);
753 }
754 }
755 main() {
756 B b = new B();
757 b.test();
758 }
759 ''');
760 _createRefactoring('test() {');
761 // validate change
762 return _assertSuccessfulRefactoring(r'''
763 class A {
764 var fA;
765 }
766 class B extends A {
767 var fB;
768 }
769 main() {
770 B b = new B();
771 print(b.fA);
772 print(b.fB);
773 print(b.fA);
774 print(b.fB);
775 }
776 ''');
777 }
778
779 test_method_fieldStatic() {
780 indexTestUnit(r'''
781 class A {
782 static var FA = 1;
783 }
784 class B extends A {
785 static var FB = 2;
786 test() {
787 print(FA);
788 print(FB);
789 print(A.FA);
790 print(B.FB);
791 }
792 }
793 main() {
794 B b = new B();
795 b.test();
796 }
797 ''');
798 _createRefactoring('test() {');
799 // validate change
800 return _assertSuccessfulRefactoring(r'''
801 class A {
802 static var FA = 1;
803 }
804 class B extends A {
805 static var FB = 2;
806 }
807 main() {
808 B b = new B();
809 print(A.FA);
810 print(B.FB);
811 print(A.FA);
812 print(B.FB);
813 }
814 ''');
815 }
816
817 test_method_fieldStatic_sameClass() {
818 indexTestUnit(r'''
819 class A {
820 static var F = 1;
821 foo() {
822 test();
823 }
824 test() {
825 print(A.F);
826 }
827 }
828 ''');
829 _createRefactoring('test() {');
830 // validate change
831 return _assertSuccessfulRefactoring(r'''
832 class A {
833 static var F = 1;
834 foo() {
835 print(A.F);
836 }
837 }
838 ''');
839 }
840
841 test_method_singleStatement() {
842 indexTestUnit(r'''
843 class A {
844 test() {
845 print(0);
846 }
847 foo() {
848 test();
849 }
850 }
851 ''');
852 _createRefactoring('test() {');
853 // validate change
854 return _assertSuccessfulRefactoring(r'''
855 class A {
856 foo() {
857 print(0);
858 }
859 }
860 ''');
861 }
862
863 test_method_unqualifiedUnvocation() {
864 indexTestUnit(r'''
865 class A {
866 test(a, b) {
867 print(a);
868 print(b);
869 return a + b;
870 }
871 foo() {
872 var v = test(1, 2);
873 }
874 }
875 ''');
876 _createRefactoring('test(a, b) {');
877 // validate change
878 return _assertSuccessfulRefactoring(r'''
879 class A {
880 foo() {
881 print(1);
882 print(2);
883 var v = 1 + 2;
884 }
885 }
886 ''');
887 }
888
889 test_namedArgument_inBody() {
890 indexTestUnit(r'''
891 fa(pa) => fb(pb: true);
892 fb({pb: false}) {}
893 main() {
894 fa(null);
895 }
896 ''');
897 _createRefactoring('fa(null)');
898 // validate change
899 return _assertSuccessfulRefactoring(r'''
900 fa(pa) => fb(pb: true);
901 fb({pb: false}) {}
902 main() {
903 fb(pb: true);
904 }
905 ''');
906 }
907
908 test_namedArguments() {
909 indexTestUnit(r'''
910 test({a: 0, b: 2}) {
911 print(a + b);
912 }
913 main() {
914 test(a: 10, b: 20);
915 test(b: 20, a: 10);
916 }
917 ''');
918 _createRefactoring('test({');
919 // validate change
920 return _assertSuccessfulRefactoring(r'''
921 main() {
922 print(10 + 20);
923 print(10 + 20);
924 }
925 ''');
926 }
927
928 test_reference_noStatement() {
929 indexTestUnit(r'''
930 test(a, b) {
931 return a || b;
932 }
933 foo(p1, p2, p3) => p1 && test(p2, p3);
934 bar() => {
935 'name' : baz(test)
936 };
937 baz(x) {}
938 ''');
939 _createRefactoring('test(a, b)');
940 // validate change
941 return _assertSuccessfulRefactoring(r'''
942 foo(p1, p2, p3) => p1 && (p2 || p3);
943 bar() => {
944 'name' : baz((a, b) {
945 return a || b;
946 })
947 };
948 baz(x) {}
949 ''');
950 }
951
952 test_reference_toLocal() {
953 indexTestUnit(r'''
954 main() {
955 test(a, b) {
956 print(a);
957 print(b);
958 }
959 print(test);
960 }
961 ''');
962 _createRefactoring('test(a, b)');
963 // validate change
964 return _assertSuccessfulRefactoring(r'''
965 main() {
966 print((a, b) {
967 print(a);
968 print(b);
969 });
970 }
971 ''');
972 }
973
974 test_reference_toTopLevel() {
975 indexTestUnit(r'''
976 test(a, b) {
977 print(a);
978 print(b);
979 }
980 main() {
981 print(test);
982 }
983 ''');
984 _createRefactoring('test(a, b)');
985 // validate change
986 return _assertSuccessfulRefactoring(r'''
987 main() {
988 print((a, b) {
989 print(a);
990 print(b);
991 });
992 }
993 ''');
994 }
995
996 test_singleExpression_oneUsage() {
997 indexTestUnit(r'''
998 test(a, b) {
999 return a + b;
1000 }
1001 main() {
1002 var res = test(1, 2);
1003 }
1004 ''');
1005 _createRefactoring('test(a, b)');
1006 // validate change
1007 return _assertSuccessfulRefactoring(r'''
1008 main() {
1009 var res = 1 + 2;
1010 }
1011 ''');
1012 }
1013
1014 test_singleExpression_oneUsage_keepMethod() {
1015 indexTestUnit(r'''
1016 test(a, b) {
1017 return a + b;
1018 }
1019 main() {
1020 var res = test(1, 2);
1021 }
1022 ''');
1023 _createRefactoring('test(a, b)');
1024 deleteSource = false;
1025 // validate change
1026 return _assertSuccessfulRefactoring(r'''
1027 test(a, b) {
1028 return a + b;
1029 }
1030 main() {
1031 var res = 1 + 2;
1032 }
1033 ''');
1034 }
1035
1036 test_singleExpression_twoUsages() {
1037 indexTestUnit(r'''
1038 test(a, b) {
1039 return a + b;
1040 }
1041 main() {
1042 var res1 = test(1, 2);
1043 var res2 = test(10, 20);
1044 }
1045 ''');
1046 _createRefactoring('test(a, b)');
1047 // validate change
1048 return _assertSuccessfulRefactoring(r'''
1049 main() {
1050 var res1 = 1 + 2;
1051 var res2 = 10 + 20;
1052 }
1053 ''');
1054 }
1055
1056 test_singleExpression_twoUsages_inlineOne() {
1057 indexTestUnit(r'''
1058 test(a, b) {
1059 return a + b;
1060 }
1061 main() {
1062 var res1 = test(1, 2);
1063 var res2 = test(10, 20);
1064 }
1065 ''');
1066 _createRefactoring('test(1, 2)');
1067 // validate change
1068 return _assertSuccessfulRefactoring(r'''
1069 test(a, b) {
1070 return a + b;
1071 }
1072 main() {
1073 var res1 = 1 + 2;
1074 var res2 = test(10, 20);
1075 }
1076 ''');
1077 }
1078
1079 test_singleExpression_wrapIntoParenthesized_alreadyInMethod() {
1080 indexTestUnit(r'''
1081 test(a, b) {
1082 return a * (b);
1083 }
1084 main() {
1085 var res = test(1, 2 + 3);
1086 }
1087 ''');
1088 _createRefactoring('test(a, b)');
1089 // validate change
1090 return _assertSuccessfulRefactoring(r'''
1091 main() {
1092 var res = 1 * (2 + 3);
1093 }
1094 ''');
1095 }
1096
1097 test_singleExpression_wrapIntoParenthesized_asNeeded() {
1098 indexTestUnit(r'''
1099 test(a, b) {
1100 return a * b;
1101 }
1102 main() {
1103 var res1 = test(1, 2 + 3);
1104 var res2 = test(1, (2 + 3));
1105 }
1106 ''');
1107 _createRefactoring('test(a, b)');
1108 // validate change
1109 return _assertSuccessfulRefactoring(r'''
1110 main() {
1111 var res1 = 1 * (2 + 3);
1112 var res2 = 1 * (2 + 3);
1113 }
1114 ''');
1115 }
1116
1117 test_singleExpression_wrapIntoParenthesized_bools() {
1118 indexTestUnit(r'''
1119 test(bool a, bool b) {
1120 return a || b;
1121 }
1122 main(bool p, bool p2, bool p3) {
1123 var res1 = p && test(p2, p3);
1124 var res2 = p || test(p2, p3);
1125 }
1126 ''');
1127 _createRefactoring('test(bool a, bool b)');
1128 // validate change
1129 return _assertSuccessfulRefactoring(r'''
1130 main(bool p, bool p2, bool p3) {
1131 var res1 = p && (p2 || p3);
1132 var res2 = p || p2 || p3;
1133 }
1134 ''');
1135 }
1136
1137 Future _assertConditionsError(String message) {
1138 return refactoring.checkAllConditions().then((status) {
1139 assertRefactoringStatus(
1140 status,
1141 RefactoringProblemSeverity.ERROR,
1142 expectedMessage: message);
1143 });
1144 }
1145
1146 Future _assertConditionsFatal(String message) {
1147 return refactoring.checkAllConditions().then((status) {
1148 assertRefactoringStatus(
1149 status,
1150 RefactoringProblemSeverity.FATAL,
1151 expectedMessage: message);
1152 });
1153 }
1154
1155 Future _assertSuccessfulRefactoring(String expectedCode) {
1156 return refactoring.checkInitialConditions().then((status) {
1157 assertRefactoringStatusOK(status);
1158 if (deleteSource != null) {
1159 refactoring.deleteSource = deleteSource;
1160 }
1161 if (inlineAll != null) {
1162 refactoring.inlineAll = inlineAll;
1163 }
1164 return refactoring.checkFinalConditions().then((status) {
1165 assertRefactoringStatusOK(status);
1166 return refactoring.createChange().then((SourceChange change) {
1167 this.refactoringChange = change;
1168 assertTestChangeResult(expectedCode);
1169 });
1170 });
1171 });
1172 }
1173
1174 void _createRefactoring(String search) {
1175 int offset = findOffset(search);
1176 refactoring = new InlineMethodRefactoring(searchEngine, testUnit, offset);
1177 }
1178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698