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

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

Issue 908463004: Convert refactoring tests to use 'await'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.services.refactoring.extract_local; 5 library test.services.refactoring.extract_local;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
10 import 'package:analysis_server/src/services/correction/status.dart';
10 import 'package:analysis_server/src/services/refactoring/extract_local.dart'; 11 import 'package:analysis_server/src/services/refactoring/extract_local.dart';
11 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; 12 import 'package:analysis_server/src/services/refactoring/refactoring.dart';
12 import 'package:unittest/unittest.dart'; 13 import 'package:unittest/unittest.dart';
13 14
14 import '../../reflective_tests.dart'; 15 import '../../reflective_tests.dart';
15 import 'abstract_refactoring.dart'; 16 import 'abstract_refactoring.dart';
16 17
17 18
18 main() { 19 main() {
19 groupSep = ' | '; 20 groupSep = ' | ';
20 runReflectiveTests(ExtractLocalTest); 21 runReflectiveTests(ExtractLocalTest);
21 } 22 }
22 23
23 24
24 @reflectiveTest 25 @reflectiveTest
25 class ExtractLocalTest extends RefactoringTest { 26 class ExtractLocalTest extends RefactoringTest {
26 ExtractLocalRefactoringImpl refactoring; 27 ExtractLocalRefactoringImpl refactoring;
27 28
28 test_checkFinalConditions_sameVariable_after() { 29 test_checkFinalConditions_sameVariable_after() async {
29 indexTestUnit(''' 30 indexTestUnit('''
30 main() { 31 main() {
31 int a = 1 + 2; 32 int a = 1 + 2;
32 var res; 33 var res;
33 } 34 }
34 '''); 35 ''');
35 _createRefactoringForString('1 + 2'); 36 _createRefactoringForString('1 + 2');
36 // conflicting name 37 // conflicting name
37 return refactoring.checkAllConditions().then((status) { 38 RefactoringStatus status = await refactoring.checkAllConditions();
38 assertRefactoringStatus( 39 assertRefactoringStatus(
39 status, 40 status,
40 RefactoringProblemSeverity.WARNING, 41 RefactoringProblemSeverity.WARNING,
41 expectedMessage: 42 expectedMessage:
42 "A variable with name 'res' is already defined in the visible scop e."); 43 "A variable with name 'res' is already defined in the visible scope. ");
43 });
44 } 44 }
45 45
46 test_checkFinalConditions_sameVariable_before() { 46 test_checkFinalConditions_sameVariable_before() async {
47 indexTestUnit(''' 47 indexTestUnit('''
48 main() { 48 main() {
49 var res; 49 var res;
50 int a = 1 + 2; 50 int a = 1 + 2;
51 } 51 }
52 '''); 52 ''');
53 _createRefactoringForString('1 + 2'); 53 _createRefactoringForString('1 + 2');
54 // conflicting name 54 // conflicting name
55 return refactoring.checkAllConditions().then((status) { 55 RefactoringStatus status = await refactoring.checkAllConditions();
56 assertRefactoringStatus( 56 assertRefactoringStatus(
57 status, 57 status,
58 RefactoringProblemSeverity.WARNING, 58 RefactoringProblemSeverity.WARNING,
59 expectedMessage: 59 expectedMessage:
60 "A variable with name 'res' is already defined in the visible scop e."); 60 "A variable with name 'res' is already defined in the visible scope. ");
61 });
62 } 61 }
63 62
64 test_checkInitialConditions_assignmentLeftHandSize() { 63 test_checkInitialConditions_assignmentLeftHandSize() async {
65 indexTestUnit(''' 64 indexTestUnit('''
66 main() { 65 main() {
67 var v = 0; 66 var v = 0;
68 v = 1; 67 v = 1;
69 } 68 }
70 '''); 69 ''');
71 _createRefactoringWithSuffix('v', ' = 1;'); 70 _createRefactoringWithSuffix('v', ' = 1;');
72 // check conditions 71 // check conditions
73 return refactoring.checkInitialConditions().then((status) { 72 RefactoringStatus status = await refactoring.checkAllConditions();
74 assertRefactoringStatus( 73 assertRefactoringStatus(
75 status, 74 status,
76 RefactoringProblemSeverity.FATAL, 75 RefactoringProblemSeverity.FATAL,
77 expectedMessage: 'Cannot extract the left-hand side of an assignment.' ); 76 expectedMessage: 'Cannot extract the left-hand side of an assignment.');
78 });
79 } 77 }
80 78
81 test_checkInitialConditions_methodName_reference() { 79 test_checkInitialConditions_methodName_reference() async {
82 indexTestUnit(''' 80 indexTestUnit('''
83 main() { 81 main() {
84 main(); 82 main();
85 } 83 }
86 '''); 84 ''');
87 _createRefactoringWithSuffix('main', '();'); 85 _createRefactoringWithSuffix('main', '();');
88 // check conditions 86 // check conditions
89 return refactoring.checkInitialConditions().then((status) { 87 RefactoringStatus status = await refactoring.checkAllConditions();
90 assertRefactoringStatus( 88 assertRefactoringStatus(
91 status, 89 status,
92 RefactoringProblemSeverity.FATAL, 90 RefactoringProblemSeverity.FATAL,
93 expectedMessage: 'Cannot extract a single method name.'); 91 expectedMessage: 'Cannot extract a single method name.');
94 });
95 } 92 }
96 93
97 test_checkInitialConditions_nameOfProperty_prefixedIdentifier() { 94 test_checkInitialConditions_nameOfProperty_prefixedIdentifier() async {
98 indexTestUnit(''' 95 indexTestUnit('''
99 main(p) { 96 main(p) {
100 p.value; // marker 97 p.value; // marker
101 } 98 }
102 '''); 99 ''');
103 _createRefactoringWithSuffix('value', '; // marker'); 100 _createRefactoringWithSuffix('value', '; // marker');
104 // check conditions 101 // check conditions
105 return refactoring.checkInitialConditions().then((status) { 102 RefactoringStatus status = await refactoring.checkAllConditions();
106 assertRefactoringStatus( 103 assertRefactoringStatus(
107 status, 104 status,
108 RefactoringProblemSeverity.FATAL, 105 RefactoringProblemSeverity.FATAL,
109 expectedMessage: 'Cannot extract name part of a property access.'); 106 expectedMessage: 'Cannot extract name part of a property access.');
110 });
111 } 107 }
112 108
113 test_checkInitialConditions_nameOfProperty_propertyAccess() { 109 test_checkInitialConditions_nameOfProperty_propertyAccess() async {
114 indexTestUnit(''' 110 indexTestUnit('''
115 main() { 111 main() {
116 foo().length; // marker 112 foo().length; // marker
117 } 113 }
118 String foo() => ''; 114 String foo() => '';
119 '''); 115 ''');
120 _createRefactoringWithSuffix('length', '; // marker'); 116 _createRefactoringWithSuffix('length', '; // marker');
121 // check conditions 117 // check conditions
122 return refactoring.checkInitialConditions().then((status) { 118 RefactoringStatus status = await refactoring.checkAllConditions();
123 assertRefactoringStatus( 119 assertRefactoringStatus(
124 status, 120 status,
125 RefactoringProblemSeverity.FATAL, 121 RefactoringProblemSeverity.FATAL,
126 expectedMessage: 'Cannot extract name part of a property access.'); 122 expectedMessage: 'Cannot extract name part of a property access.');
127 });
128 } 123 }
129 124
130 test_checkInitialConditions_namePartOfDeclaration_variable() { 125 test_checkInitialConditions_namePartOfDeclaration_variable() async {
131 indexTestUnit(''' 126 indexTestUnit('''
132 main() { 127 main() {
133 int vvv = 0; 128 int vvv = 0;
134 } 129 }
135 '''); 130 ''');
136 _createRefactoringWithSuffix('vvv', ' = 0;'); 131 _createRefactoringWithSuffix('vvv', ' = 0;');
137 // check conditions 132 // check conditions
138 return refactoring.checkInitialConditions().then((status) { 133 RefactoringStatus status = await refactoring.checkAllConditions();
139 assertRefactoringStatus( 134 assertRefactoringStatus(
140 status, 135 status,
141 RefactoringProblemSeverity.FATAL, 136 RefactoringProblemSeverity.FATAL,
142 expectedMessage: 'Cannot extract the name part of a declaration.'); 137 expectedMessage: 'Cannot extract the name part of a declaration.');
143 });
144 } 138 }
145 139
146 test_checkInitialConditions_notPartOfFunction() { 140 test_checkInitialConditions_notPartOfFunction() async {
147 indexTestUnit(''' 141 indexTestUnit('''
148 int a = 1 + 2; 142 int a = 1 + 2;
149 '''); 143 ''');
150 _createRefactoringForString('1 + 2'); 144 _createRefactoringForString('1 + 2');
151 // check conditions 145 // check conditions
152 return refactoring.checkInitialConditions().then((status) { 146 RefactoringStatus status = await refactoring.checkAllConditions();
153 assertRefactoringStatus( 147 assertRefactoringStatus(
154 status, 148 status,
155 RefactoringProblemSeverity.FATAL, 149 RefactoringProblemSeverity.FATAL,
156 expectedMessage: 150 expectedMessage:
157 'Expression inside of function must be selected to activate this r efactoring.'); 151 'Expression inside of function must be selected to activate this ref actoring.');
158 });
159 } 152 }
160 153
161 test_checkInitialConditions_stringSelection_leadingQuote() { 154 test_checkInitialConditions_stringSelection_leadingQuote() async {
162 indexTestUnit(''' 155 indexTestUnit('''
163 main() { 156 main() {
164 var vvv = 'abc'; 157 var vvv = 'abc';
165 } 158 }
166 '''); 159 ''');
167 _createRefactoringForString("'a"); 160 _createRefactoringForString("'a");
168 // check conditions 161 // check conditions
169 return refactoring.checkInitialConditions().then((status) { 162 RefactoringStatus status = await refactoring.checkAllConditions();
170 assertRefactoringStatus( 163 assertRefactoringStatus(
171 status, 164 status,
172 RefactoringProblemSeverity.FATAL, 165 RefactoringProblemSeverity.FATAL,
173 expectedMessage: 166 expectedMessage:
174 'Cannot extract only leading or trailing quote of string literal.' ); 167 'Cannot extract only leading or trailing quote of string literal.');
175 });
176 } 168 }
177 169
178 test_checkInitialConditions_stringSelection_trailingQuote() { 170 test_checkInitialConditions_stringSelection_trailingQuote() async {
179 indexTestUnit(''' 171 indexTestUnit('''
180 main() { 172 main() {
181 var vvv = 'abc'; 173 var vvv = 'abc';
182 } 174 }
183 '''); 175 ''');
184 _createRefactoringForString("c'"); 176 _createRefactoringForString("c'");
185 // check conditions 177 // check conditions
186 return refactoring.checkInitialConditions().then((status) { 178 RefactoringStatus status = await refactoring.checkAllConditions();
187 assertRefactoringStatus( 179 assertRefactoringStatus(
188 status, 180 status,
189 RefactoringProblemSeverity.FATAL, 181 RefactoringProblemSeverity.FATAL,
190 expectedMessage: 182 expectedMessage:
191 'Cannot extract only leading or trailing quote of string literal.' ); 183 'Cannot extract only leading or trailing quote of string literal.');
192 });
193 } 184 }
194 185
195 test_checkLocalName() { 186 test_checkLocalName() {
196 indexTestUnit(''' 187 indexTestUnit('''
197 main() { 188 main() {
198 int a = 1 + 2; 189 int a = 1 + 2;
199 } 190 }
200 '''); 191 ''');
201 _createRefactoringForString('1 + 2'); 192 _createRefactoringForString('1 + 2');
202 expect(refactoring.refactoringName, 'Extract Local Variable'); 193 expect(refactoring.refactoringName, 'Extract Local Variable');
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 _createRefactoringForString('2 + 3 '); 453 _createRefactoringForString('2 + 3 ');
463 // apply refactoring 454 // apply refactoring
464 return _assertSuccessfulRefactoring(''' 455 return _assertSuccessfulRefactoring('''
465 main() { 456 main() {
466 var res = 2 + 3 ; 457 var res = 2 + 3 ;
467 int a = 1 + res+ 4; 458 int a = 1 + res+ 4;
468 } 459 }
469 '''); 460 ''');
470 } 461 }
471 462
472 test_guessNames_fragmentExpression() { 463 test_guessNames_fragmentExpression() async {
473 indexTestUnit(''' 464 indexTestUnit('''
474 main() { 465 main() {
475 var a = 111 + 222 + 333 + 444; 466 var a = 111 + 222 + 333 + 444;
476 } 467 }
477 '''); 468 ''');
478 _createRefactoringForString('222 + 333'); 469 _createRefactoringForString('222 + 333');
479 // check guesses 470 // check guesses
480 return refactoring.checkInitialConditions().then((_) { 471 await refactoring.checkInitialConditions();
481 expect(refactoring.names, isEmpty); 472 expect(refactoring.names, isEmpty);
482 });
483 } 473 }
484 474
485 test_guessNames_singleExpression() { 475 test_guessNames_singleExpression() async {
486 indexTestUnit(''' 476 indexTestUnit('''
487 class TreeItem {} 477 class TreeItem {}
488 TreeItem getSelectedItem() => null; 478 TreeItem getSelectedItem() => null;
489 process(my) {} 479 process(my) {}
490 main() { 480 main() {
491 process(getSelectedItem()); // marker 481 process(getSelectedItem()); // marker
492 } 482 }
493 '''); 483 ''');
494 _createRefactoringWithSuffix('getSelectedItem()', '); // marker'); 484 _createRefactoringWithSuffix('getSelectedItem()', '); // marker');
495 // check guesses 485 // check guesses
496 return refactoring.checkInitialConditions().then((_) { 486 await refactoring.checkInitialConditions();
497 expect( 487 expect(
498 refactoring.names, 488 refactoring.names,
499 unorderedEquals(['selectedItem', 'item', 'my', 'treeItem'])); 489 unorderedEquals(['selectedItem', 'item', 'my', 'treeItem']));
500 });
501 } 490 }
502 491
503 test_guessNames_stringPart() { 492 test_guessNames_stringPart() async {
504 indexTestUnit(''' 493 indexTestUnit('''
505 main() { 494 main() {
506 var s = 'Hello Bob... welcome to Dart!'; 495 var s = 'Hello Bob... welcome to Dart!';
507 } 496 }
508 '''); 497 ''');
509 _createRefactoringForString('Hello Bob'); 498 _createRefactoringForString('Hello Bob');
510 // check guesses 499 // check guesses
511 return refactoring.checkInitialConditions().then((_) { 500 await refactoring.checkInitialConditions();
512 expect(refactoring.names, unorderedEquals(['helloBob', 'bob'])); 501 expect(refactoring.names, unorderedEquals(['helloBob', 'bob']));
513 });
514 } 502 }
515 503
516 test_occurences_differentVariable() { 504 test_occurences_differentVariable() {
517 indexTestUnit(''' 505 indexTestUnit('''
518 main() { 506 main() {
519 { 507 {
520 int v = 1; 508 int v = 1;
521 print(v + 1); // marker 509 print(v + 1); // marker
522 print(v + 1); 510 print(v + 1);
523 } 511 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 return _assertSuccessfulRefactoring(''' 673 return _assertSuccessfulRefactoring('''
686 int foo(String s) => 42; 674 int foo(String s) => 42;
687 main() { 675 main() {
688 var res = foo('has space'); 676 var res = foo('has space');
689 int a = 1 + res; 677 int a = 1 + res;
690 int b = 2 + res; // marker 678 int b = 2 + res; // marker
691 } 679 }
692 '''); 680 ''');
693 } 681 }
694 682
695 test_offsets_lengths() { 683 test_offsets_lengths() async {
696 indexTestUnit(''' 684 indexTestUnit('''
697 int foo() => 42; 685 int foo() => 42;
698 main() { 686 main() {
699 int a = 1 + foo(); // marker 687 int a = 1 + foo(); // marker
700 int b = 2 + foo( ); 688 int b = 2 + foo( );
701 } 689 }
702 '''); 690 ''');
703 _createRefactoringWithSuffix('foo()', '; // marker'); 691 _createRefactoringWithSuffix('foo()', '; // marker');
704 // apply refactoring 692 // check offsets
705 return refactoring.checkInitialConditions().then((_) { 693 await refactoring.checkInitialConditions();
706 expect( 694 expect(
707 refactoring.offsets, 695 refactoring.offsets,
708 unorderedEquals([findOffset('foo();'), findOffset('foo( );')])); 696 unorderedEquals([findOffset('foo();'), findOffset('foo( );')]));
709 expect(refactoring.lengths, unorderedEquals([5, 6])); 697 expect(refactoring.lengths, unorderedEquals([5, 6]));
710 });
711 } 698 }
712 699
713 test_singleExpression() { 700 test_singleExpression() {
714 indexTestUnit(''' 701 indexTestUnit('''
715 main() { 702 main() {
716 int a = 1 + 2; 703 int a = 1 + 2;
717 } 704 }
718 '''); 705 ''');
719 _createRefactoringForString('1 + 2'); 706 _createRefactoringForString('1 + 2');
720 // apply refactoring 707 // apply refactoring
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 return _assertSuccessfulRefactoring(r''' 935 return _assertSuccessfulRefactoring(r'''
949 main() { 936 main() {
950 int x = 1; 937 int x = 1;
951 int y = 2; 938 int y = 2;
952 var res = '$x+$y'; 939 var res = '$x+$y';
953 print('${res}=${x+y}'); 940 print('${res}=${x+y}');
954 } 941 }
955 '''); 942 ''');
956 } 943 }
957 944
958 Future _assertInitialConditions_fatal_selection() { 945 Future _assertInitialConditions_fatal_selection() async {
959 return refactoring.checkInitialConditions().then((status) { 946 RefactoringStatus status = await refactoring.checkInitialConditions();
960 assertRefactoringStatus( 947 assertRefactoringStatus(
961 status, 948 status,
962 RefactoringProblemSeverity.FATAL, 949 RefactoringProblemSeverity.FATAL,
963 expectedMessage: 'Expression must be selected to activate this refacto ring.'); 950 expectedMessage: 'Expression must be selected to activate this refactori ng.');
964 });
965 } 951 }
966 952
967 /** 953 /**
968 * Checks that all conditions are OK and the result of applying the [Change] 954 * Checks that all conditions are OK and the result of applying the [Change]
969 * to [testUnit] is [expectedCode]. 955 * to [testUnit] is [expectedCode].
970 */ 956 */
971 Future _assertSuccessfulRefactoring(String expectedCode) { 957 Future _assertSuccessfulRefactoring(String expectedCode) async {
972 return assertRefactoringConditionsOK().then((_) { 958 await assertRefactoringConditionsOK();
973 return refactoring.createChange().then((SourceChange refactoringChange) { 959 SourceChange refactoringChange = await refactoring.createChange();
974 this.refactoringChange = refactoringChange; 960 this.refactoringChange = refactoringChange;
975 assertTestChangeResult(expectedCode); 961 assertTestChangeResult(expectedCode);
976 });
977 });
978 } 962 }
979 963
980 void _createRefactoring(int offset, int length) { 964 void _createRefactoring(int offset, int length) {
981 refactoring = new ExtractLocalRefactoring(testUnit, offset, length); 965 refactoring = new ExtractLocalRefactoring(testUnit, offset, length);
982 refactoring.name = 'res'; 966 refactoring.name = 'res';
983 } 967 }
984 968
985 /** 969 /**
986 * Creates a new refactoring in [refactoring] for the selection range of the 970 * Creates a new refactoring in [refactoring] for the selection range of the
987 * given [search] pattern. 971 * given [search] pattern.
988 */ 972 */
989 void _createRefactoringForString(String search) { 973 void _createRefactoringForString(String search) {
990 int offset = findOffset(search); 974 int offset = findOffset(search);
991 int length = search.length; 975 int length = search.length;
992 _createRefactoring(offset, length); 976 _createRefactoring(offset, length);
993 } 977 }
994 978
995 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { 979 void _createRefactoringWithSuffix(String selectionSearch, String suffix) {
996 int offset = findOffset(selectionSearch + suffix); 980 int offset = findOffset(selectionSearch + suffix);
997 int length = selectionSearch.length; 981 int length = selectionSearch.length;
998 _createRefactoring(offset, length); 982 _createRefactoring(offset, length);
999 } 983 }
1000 } 984 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698