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

Side by Side Diff: pkg/analysis_server/test/edit/refactoring_test.dart

Issue 534123002: Integrate EXTRACT_METHOD refactoring into the server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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.edit.refactoring; 5 library test.edit.refactoring;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/edit/edit_domain.dart'; 9 import 'package:analysis_server/src/edit/edit_domain.dart';
10 import 'package:analysis_server/src/protocol.dart'; 10 import 'package:analysis_server/src/protocol.dart';
11 import 'package:analysis_server/src/services/index/index.dart'; 11 import 'package:analysis_server/src/services/index/index.dart';
12 import 'package:analysis_server/src/services/index/local_memory_index.dart'; 12 import 'package:analysis_server/src/services/index/local_memory_index.dart';
13 import 'package:analysis_server/src/services/json.dart';
13 import 'package:analysis_testing/reflective_tests.dart'; 14 import 'package:analysis_testing/reflective_tests.dart';
14 import 'package:unittest/unittest.dart' hide ERROR; 15 import 'package:unittest/unittest.dart' hide ERROR;
15 16
16 import '../analysis_abstract.dart'; 17 import '../analysis_abstract.dart';
17 18
18 19
19 main() { 20 main() {
20 groupSep = ' | '; 21 groupSep = ' | ';
21 runReflectiveTests(ExtractLocalVariableTest); 22 runReflectiveTests(ExtractLocalVariableTest);
23 runReflectiveTests(ExtractMethodTest);
22 runReflectiveTests(GetAvailableRefactoringsTest); 24 runReflectiveTests(GetAvailableRefactoringsTest);
23 runReflectiveTests(RenameTest); 25 runReflectiveTests(RenameTest);
24 } 26 }
25 27
26 28
27 @ReflectiveTestCase() 29 @ReflectiveTestCase()
28 class ExtractLocalVariableTest extends _AbstractGetRefactoring_Test { 30 class ExtractLocalVariableTest extends _AbstractGetRefactoring_Test {
29 Future<Response> sendExtractRequest(int offset, int length, String name, 31 Future<Response> sendExtractRequest(int offset, int length, String name,
30 bool extractAll) { 32 bool extractAll) {
31 RefactoringKind kind = RefactoringKind.EXTRACT_LOCAL_VARIABLE; 33 RefactoringKind kind = RefactoringKind.EXTRACT_LOCAL_VARIABLE;
32 Map options = name != null ? new ExtractLocalVariableOptions(name, 34 ExtractLocalVariableOptions options =
33 extractAll).toJson() : null; 35 name != null ? new ExtractLocalVariableOptions(name, extractAll) : null;
34 return sendRequest(kind, offset, length, options, false); 36 return sendRequest(kind, offset, length, options, false);
35 } 37 }
36 38
37 Future<Response> sendStringRequest(String search, String name, 39 Future<Response> sendStringRequest(String search, String name,
38 bool extractAll) { 40 bool extractAll) {
39 int offset = findOffset(search); 41 int offset = findOffset(search);
40 int length = search.length; 42 int length = search.length;
41 return sendExtractRequest(offset, length, name, extractAll); 43 return sendExtractRequest(offset, length, name, extractAll);
42 } 44 }
43 45
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 ExtractLocalVariableFeedback feedback = 141 ExtractLocalVariableFeedback feedback =
140 new ExtractLocalVariableFeedback.fromRefactoringResult(result); 142 new ExtractLocalVariableFeedback.fromRefactoringResult(result);
141 expect(feedback.offsets, [findOffset('1 + 2'), findOffset('1 + 2')]); 143 expect(feedback.offsets, [findOffset('1 + 2'), findOffset('1 + 2')]);
142 expect(feedback.lengths, [5, 6]); 144 expect(feedback.lengths, [5, 6]);
143 }); 145 });
144 } 146 }
145 } 147 }
146 148
147 149
148 @ReflectiveTestCase() 150 @ReflectiveTestCase()
151 class ExtractMethodTest extends _AbstractGetRefactoring_Test {
152 int offset;
153 int length;
154 String name = 'res';
155 ExtractMethodOptions options;
156
157 test_expression() {
158 addTestFile('''
159 main() {
160 print(1 + 2);
161 print(1 + 2);
162 }
163 ''');
164 _setOffsetLengthForString('1 + 2');
165 return assertSuccessfulRefactoring(_computeChange, '''
166 main() {
167 print(res());
168 print(res());
169 }
170
171 int res() => 1 + 2;
172 ''');
173 }
174
175 test_expression_hasParameters() {
176 addTestFile('''
177 main() {
178 int a = 1;
179 int b = 2;
180 print(a + b);
181 print(a + b);
182 }
183 ''');
184 _setOffsetLengthForString('a + b');
185 return assertSuccessfulRefactoring(_computeChange, '''
186 main() {
187 int a = 1;
188 int b = 2;
189 print(res(a, b));
190 print(res(a, b));
191 }
192
193 int res(int a, int b) => a + b;
194 ''');
195 }
196
197 test_expression_updateParameters() {
198 addTestFile('''
199 main() {
200 int a = 1;
201 int b = 2;
202 print(a + b);
203 print(a + b);
204 }
205 ''');
206 _setOffsetLengthForString('a + b');
207 return getRefactoringResult(_computeChange).then((result) {
208 ExtractMethodFeedback feedback =
209 new ExtractMethodFeedback.fromRefactoringResult(result);
210 List<RefactoringMethodParameter> parameters = feedback.parameters;
211 parameters[0].name = 'aaa';
212 parameters[1].name = 'bbb';
213 parameters[1].type = 'num';
214 parameters.insert(0, parameters.removeLast());
215 options.parameters = parameters;
216 return assertSuccessfulRefactoring(_sendExtractRequest, '''
217 main() {
218 int a = 1;
219 int b = 2;
220 print(res(b, a));
221 print(res(b, a));
222 }
223
224 int res(num bbb, int aaa) => aaa + bbb;
225 ''');
226 });
227 }
228
229 test_names() {
230 addTestFile('''
231 class TreeItem {}
232 TreeItem getSelectedItem() => null;
233 main() {
234 var a = getSelectedItem( );
235 }
236 ''');
237 _setOffsetLengthForString('getSelectedItem( )');
238 return _computeInitialFeedback().then((feedback) {
239 expect(
240 feedback.names,
241 unorderedEquals(['treeItem', 'item', 'selectedItem']));
242 expect(feedback.returnType, 'TreeItem');
243 });
244 }
245
246 test_offsetsLengths() {
247 addTestFile('''
248 class TreeItem {}
249 TreeItem getSelectedItem() => null;
250 main() {
251 var a = 1 + 2;
252 var b = 1 + 2;
253 }
254 ''');
255 _setOffsetLengthForString('1 + 2');
256 return _computeInitialFeedback().then((feedback) {
257 expect(feedback.offsets, [findOffset('1 + 2'), findOffset('1 + 2')]);
258 expect(feedback.lengths, [5, 6]);
259 });
260 }
261
262 test_statements() {
263 addTestFile('''
264 main() {
265 int a = 1;
266 int b = 2;
267 // start
268 print(a + b);
269 // end
270 print(a + b);
271 }
272 ''');
273 _setOffsetLengthForStartEnd();
274 return assertSuccessfulRefactoring(_computeChange, '''
275 main() {
276 int a = 1;
277 int b = 2;
278 // start
279 res(a, b);
280 // end
281 res(a, b);
282 }
283
284 void res(int a, int b) {
285 print(a + b);
286 }
287 ''');
288 }
289
290 Future<Response> _computeChange() {
291 return _prepareOptions().then((_) {
292 // send request with the options
293 return _sendExtractRequest();
294 });
295 }
296
297 Future<ExtractMethodFeedback> _computeInitialFeedback() {
298 return waitForTasksFinished().then((_) {
299 return _sendExtractRequest();
300 }).then((Response response) {
301 var result = new EditGetRefactoringResult.fromResponse(response);
302 return new ExtractMethodFeedback.fromRefactoringResult(result);
303 });
304 }
305
306 Future _prepareOptions() {
307 return getRefactoringResult(() {
308 // get initial feedback
309 return _sendExtractRequest();
310 }).then((result) {
311 assertResultProblemsOK(result);
312 // fill options from results
313 var feedback = new ExtractMethodFeedback.fromRefactoringResult(result);
314 options = new ExtractMethodOptions(
315 feedback.returnType,
316 false,
317 name,
318 feedback.parameters,
319 true);
320 // done
321 return new Future.value();
322 });
323 }
324
325 Future<Response> _sendExtractRequest() {
326 RefactoringKind kind = RefactoringKind.EXTRACT_METHOD;
327 return sendRequest(kind, offset, length, options, false);
328 }
329
330 void _setOffsetLengthForStartEnd() {
331 offset = findOffset('// start') + '// start\n'.length;
332 length = findOffset('// end') - offset;
333 }
334
335 void _setOffsetLengthForString(String search) {
336 offset = findOffset(search);
337 length = search.length;
338 }
339 }
340
341
342 @ReflectiveTestCase()
149 class GetAvailableRefactoringsTest extends AbstractAnalysisTest { 343 class GetAvailableRefactoringsTest extends AbstractAnalysisTest {
150 /** 344 /**
151 * Tests that there is a RENAME refactoring available at the [search] offset. 345 * Tests that there is a RENAME refactoring available at the [search] offset.
152 */ 346 */
153 Future assertHasRenameRefactoring(String code, String search) { 347 Future assertHasRenameRefactoring(String code, String search) {
154 addTestFile(code); 348 addTestFile(code);
155 return waitForTasksFinished().then((_) { 349 return waitForTasksFinished().then((_) {
156 List<RefactoringKind> kinds = getRefactoringsAtString(search); 350 List<RefactoringKind> kinds = getRefactoringsAtString(search);
157 expect(kinds, contains(RefactoringKind.RENAME)); 351 expect(kinds, contains(RefactoringKind.RENAME));
158 }); 352 });
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 Future<EditGetRefactoringResult> getRefactoringResult(Future<Response> 885 Future<EditGetRefactoringResult> getRefactoringResult(Future<Response>
692 requestSender()) { 886 requestSender()) {
693 return waitForTasksFinished().then((_) { 887 return waitForTasksFinished().then((_) {
694 return requestSender().then((Response response) { 888 return requestSender().then((Response response) {
695 return new EditGetRefactoringResult.fromResponse(response); 889 return new EditGetRefactoringResult.fromResponse(response);
696 }); 890 });
697 }); 891 });
698 } 892 }
699 893
700 Future<Response> sendRequest(RefactoringKind kind, int offset, int length, 894 Future<Response> sendRequest(RefactoringKind kind, int offset, int length,
701 Map options, [bool validateOnly = false]) { 895 HasToJson options, [bool validateOnly = false]) {
896 Map optionsJson = options != null ? options.toJson() : null;
702 Request request = new EditGetRefactoringParams( 897 Request request = new EditGetRefactoringParams(
703 kind, 898 kind,
704 testFile, 899 testFile,
705 offset, 900 offset,
706 length, 901 length,
707 validateOnly, 902 validateOnly,
708 options: options).toRequest('0'); 903 options: optionsJson).toRequest('0');
709 return serverChannel.sendRequest(request); 904 return serverChannel.sendRequest(request);
710 } 905 }
711 906
712 @override 907 @override
713 void setUp() { 908 void setUp() {
714 super.setUp(); 909 super.setUp();
715 server.handlers = [new EditDomainHandler(server),]; 910 server.handlers = [new EditDomainHandler(server),];
716 createProject(); 911 createProject();
717 handler = new EditDomainHandler(server); 912 handler = new EditDomainHandler(server);
718 } 913 }
719 } 914 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698