| OLD | NEW |
| 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' hide RefactoringKind; | 9 import 'package:analysis_server/src/edit/edit_domain.dart' hide RefactoringKind; |
| 10 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 | 25 |
| 26 @ReflectiveTestCase() | 26 @ReflectiveTestCase() |
| 27 class GetAvailableRefactoringsTest extends AbstractAnalysisTest { | 27 class GetAvailableRefactoringsTest extends AbstractAnalysisTest { |
| 28 /** | 28 /** |
| 29 * Tests that there is a RENAME refactoring available at the [search] offset. | 29 * Tests that there is a RENAME refactoring available at the [search] offset. |
| 30 */ | 30 */ |
| 31 Future assertHasRenameRefactoring(String code, String search) { | 31 Future assertHasRenameRefactoring(String code, String search) { |
| 32 addTestFile(code); | 32 addTestFile(code); |
| 33 return waitForTasksFinished().then((_) { | 33 return waitForTasksFinished().then((_) { |
| 34 List<RefactoringKind> kinds = getAvailableRefactorings(search); | 34 List<RefactoringKind> kinds = getRefactoringsAtString(search); |
| 35 expect(kinds, contains(RefactoringKind.RENAME)); | 35 expect(kinds, contains(RefactoringKind.RENAME)); |
| 36 }); | 36 }); |
| 37 } | 37 } |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Returns the list of available refactorings of the offset of [search] with | 40 * Returns the list of available refactorings for the given [offset] and |
| 41 * [length] characters selected. | 41 * [length]. |
| 42 */ | 42 */ |
| 43 List<RefactoringKind> getAvailableRefactorings(String search, [int length = 0]
) { | 43 List<RefactoringKind> getRefactorings(int offset, int length) { |
| 44 Request request = new EditGetAvailableRefactoringsParams(testFile, | 44 Request request = new EditGetAvailableRefactoringsParams( |
| 45 findOffset(search), length).toRequest('0'); | 45 testFile, |
| 46 offset, |
| 47 length).toRequest('0'); |
| 46 Response response = handleSuccessfulRequest(request); | 48 Response response = handleSuccessfulRequest(request); |
| 47 var result = new EditGetAvailableRefactoringsResult.fromResponse(response); | 49 var result = new EditGetAvailableRefactoringsResult.fromResponse(response); |
| 48 return result.kinds; | 50 return result.kinds; |
| 49 } | 51 } |
| 50 | 52 |
| 53 /** |
| 54 * Returns the list of available refactorings at the offset of [search]. |
| 55 */ |
| 56 List<RefactoringKind> getRefactoringsAtString(String search) { |
| 57 int offset = findOffset(search); |
| 58 return getRefactorings(offset, 0); |
| 59 } |
| 60 |
| 61 List<RefactoringKind> getRefactoringsForString(String search) { |
| 62 int offset = findOffset(search); |
| 63 return getRefactorings(offset, search.length); |
| 64 } |
| 65 |
| 51 @override | 66 @override |
| 52 void setUp() { | 67 void setUp() { |
| 53 super.setUp(); | 68 super.setUp(); |
| 54 createProject(); | 69 createProject(); |
| 55 handler = new EditDomainHandler(server); | 70 handler = new EditDomainHandler(server); |
| 56 } | 71 } |
| 57 | 72 |
| 73 Future test_extractLocal() { |
| 74 addTestFile(''' |
| 75 main() { |
| 76 var a = 1 + 2; |
| 77 } |
| 78 '''); |
| 79 return waitForTasksFinished().then((_) { |
| 80 var search = '1 + 2'; |
| 81 List<RefactoringKind> kinds = getRefactoringsForString(search); |
| 82 expect(kinds, contains(RefactoringKind.EXTRACT_LOCAL_VARIABLE)); |
| 83 expect(kinds, contains(RefactoringKind.EXTRACT_METHOD)); |
| 84 }); |
| 85 } |
| 86 |
| 58 Future test_rename_hasElement_class() { | 87 Future test_rename_hasElement_class() { |
| 59 return assertHasRenameRefactoring(''' | 88 return assertHasRenameRefactoring(''' |
| 60 class Test {} | 89 class Test {} |
| 61 main() { | 90 main() { |
| 62 Test v; | 91 Test v; |
| 63 } | 92 } |
| 64 ''', 'Test v'); | 93 ''', 'Test v'); |
| 65 } | 94 } |
| 66 | 95 |
| 67 Future test_rename_hasElement_constructor() { | 96 Future test_rename_hasElement_constructor() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 ''', 'test();'); | 189 ''', 'test();'); |
| 161 } | 190 } |
| 162 | 191 |
| 163 Future test_rename_noElement() { | 192 Future test_rename_noElement() { |
| 164 addTestFile(''' | 193 addTestFile(''' |
| 165 main() { | 194 main() { |
| 166 // not an element | 195 // not an element |
| 167 } | 196 } |
| 168 '''); | 197 '''); |
| 169 return waitForTasksFinished().then((_) { | 198 return waitForTasksFinished().then((_) { |
| 170 List<RefactoringKind> kinds = getAvailableRefactorings('// not an element'
); | 199 List<RefactoringKind> kinds = |
| 200 getRefactoringsAtString('// not an element'); |
| 171 expect(kinds, isNot(contains(RefactoringKind.RENAME))); | 201 expect(kinds, isNot(contains(RefactoringKind.RENAME))); |
| 172 }); | 202 }); |
| 173 } | 203 } |
| 174 } | 204 } |
| OLD | NEW |