| Index: pkg/analysis_server/test/edit/refactoring_test.dart
|
| diff --git a/pkg/analysis_server/test/edit/refactoring_test.dart b/pkg/analysis_server/test/edit/refactoring_test.dart
|
| index 273d501ec5ea591ef84cf287722d9498f391df8d..222d7fd53b98347d2b4720e1aac8e14805cc4414 100644
|
| --- a/pkg/analysis_server/test/edit/refactoring_test.dart
|
| +++ b/pkg/analysis_server/test/edit/refactoring_test.dart
|
| @@ -10,6 +10,7 @@ import 'package:analysis_server/src/edit/edit_domain.dart';
|
| import 'package:analysis_server/src/protocol.dart';
|
| import 'package:analysis_server/src/services/index/index.dart';
|
| import 'package:analysis_server/src/services/index/local_memory_index.dart';
|
| +import 'package:analysis_server/src/services/json.dart';
|
| import 'package:analysis_testing/reflective_tests.dart';
|
| import 'package:unittest/unittest.dart' hide ERROR;
|
|
|
| @@ -19,6 +20,7 @@ import '../analysis_abstract.dart';
|
| main() {
|
| groupSep = ' | ';
|
| runReflectiveTests(ExtractLocalVariableTest);
|
| + runReflectiveTests(ExtractMethodTest);
|
| runReflectiveTests(GetAvailableRefactoringsTest);
|
| runReflectiveTests(RenameTest);
|
| }
|
| @@ -29,8 +31,8 @@ class ExtractLocalVariableTest extends _AbstractGetRefactoring_Test {
|
| Future<Response> sendExtractRequest(int offset, int length, String name,
|
| bool extractAll) {
|
| RefactoringKind kind = RefactoringKind.EXTRACT_LOCAL_VARIABLE;
|
| - Map options = name != null ? new ExtractLocalVariableOptions(name,
|
| - extractAll).toJson() : null;
|
| + ExtractLocalVariableOptions options =
|
| + name != null ? new ExtractLocalVariableOptions(name, extractAll) : null;
|
| return sendRequest(kind, offset, length, options, false);
|
| }
|
|
|
| @@ -146,6 +148,198 @@ main() {
|
|
|
|
|
| @ReflectiveTestCase()
|
| +class ExtractMethodTest extends _AbstractGetRefactoring_Test {
|
| + int offset;
|
| + int length;
|
| + String name = 'res';
|
| + ExtractMethodOptions options;
|
| +
|
| + test_expression() {
|
| + addTestFile('''
|
| +main() {
|
| + print(1 + 2);
|
| + print(1 + 2);
|
| +}
|
| +''');
|
| + _setOffsetLengthForString('1 + 2');
|
| + return assertSuccessfulRefactoring(_computeChange, '''
|
| +main() {
|
| + print(res());
|
| + print(res());
|
| +}
|
| +
|
| +int res() => 1 + 2;
|
| +''');
|
| + }
|
| +
|
| + test_expression_hasParameters() {
|
| + addTestFile('''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| + print(a + b);
|
| + print(a + b);
|
| +}
|
| +''');
|
| + _setOffsetLengthForString('a + b');
|
| + return assertSuccessfulRefactoring(_computeChange, '''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| + print(res(a, b));
|
| + print(res(a, b));
|
| +}
|
| +
|
| +int res(int a, int b) => a + b;
|
| +''');
|
| + }
|
| +
|
| + test_expression_updateParameters() {
|
| + addTestFile('''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| + print(a + b);
|
| + print(a + b);
|
| +}
|
| +''');
|
| + _setOffsetLengthForString('a + b');
|
| + return getRefactoringResult(_computeChange).then((result) {
|
| + ExtractMethodFeedback feedback =
|
| + new ExtractMethodFeedback.fromRefactoringResult(result);
|
| + List<RefactoringMethodParameter> parameters = feedback.parameters;
|
| + parameters[0].name = 'aaa';
|
| + parameters[1].name = 'bbb';
|
| + parameters[1].type = 'num';
|
| + parameters.insert(0, parameters.removeLast());
|
| + options.parameters = parameters;
|
| + return assertSuccessfulRefactoring(_sendExtractRequest, '''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| + print(res(b, a));
|
| + print(res(b, a));
|
| +}
|
| +
|
| +int res(num bbb, int aaa) => aaa + bbb;
|
| +''');
|
| + });
|
| + }
|
| +
|
| + test_names() {
|
| + addTestFile('''
|
| +class TreeItem {}
|
| +TreeItem getSelectedItem() => null;
|
| +main() {
|
| + var a = getSelectedItem( );
|
| +}
|
| +''');
|
| + _setOffsetLengthForString('getSelectedItem( )');
|
| + return _computeInitialFeedback().then((feedback) {
|
| + expect(
|
| + feedback.names,
|
| + unorderedEquals(['treeItem', 'item', 'selectedItem']));
|
| + expect(feedback.returnType, 'TreeItem');
|
| + });
|
| + }
|
| +
|
| + test_offsetsLengths() {
|
| + addTestFile('''
|
| +class TreeItem {}
|
| +TreeItem getSelectedItem() => null;
|
| +main() {
|
| + var a = 1 + 2;
|
| + var b = 1 + 2;
|
| +}
|
| +''');
|
| + _setOffsetLengthForString('1 + 2');
|
| + return _computeInitialFeedback().then((feedback) {
|
| + expect(feedback.offsets, [findOffset('1 + 2'), findOffset('1 + 2')]);
|
| + expect(feedback.lengths, [5, 6]);
|
| + });
|
| + }
|
| +
|
| + test_statements() {
|
| + addTestFile('''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| +// start
|
| + print(a + b);
|
| +// end
|
| + print(a + b);
|
| +}
|
| +''');
|
| + _setOffsetLengthForStartEnd();
|
| + return assertSuccessfulRefactoring(_computeChange, '''
|
| +main() {
|
| + int a = 1;
|
| + int b = 2;
|
| +// start
|
| + res(a, b);
|
| +// end
|
| + res(a, b);
|
| +}
|
| +
|
| +void res(int a, int b) {
|
| + print(a + b);
|
| +}
|
| +''');
|
| + }
|
| +
|
| + Future<Response> _computeChange() {
|
| + return _prepareOptions().then((_) {
|
| + // send request with the options
|
| + return _sendExtractRequest();
|
| + });
|
| + }
|
| +
|
| + Future<ExtractMethodFeedback> _computeInitialFeedback() {
|
| + return waitForTasksFinished().then((_) {
|
| + return _sendExtractRequest();
|
| + }).then((Response response) {
|
| + var result = new EditGetRefactoringResult.fromResponse(response);
|
| + return new ExtractMethodFeedback.fromRefactoringResult(result);
|
| + });
|
| + }
|
| +
|
| + Future _prepareOptions() {
|
| + return getRefactoringResult(() {
|
| + // get initial feedback
|
| + return _sendExtractRequest();
|
| + }).then((result) {
|
| + assertResultProblemsOK(result);
|
| + // fill options from results
|
| + var feedback = new ExtractMethodFeedback.fromRefactoringResult(result);
|
| + options = new ExtractMethodOptions(
|
| + feedback.returnType,
|
| + false,
|
| + name,
|
| + feedback.parameters,
|
| + true);
|
| + // done
|
| + return new Future.value();
|
| + });
|
| + }
|
| +
|
| + Future<Response> _sendExtractRequest() {
|
| + RefactoringKind kind = RefactoringKind.EXTRACT_METHOD;
|
| + return sendRequest(kind, offset, length, options, false);
|
| + }
|
| +
|
| + void _setOffsetLengthForStartEnd() {
|
| + offset = findOffset('// start') + '// start\n'.length;
|
| + length = findOffset('// end') - offset;
|
| + }
|
| +
|
| + void _setOffsetLengthForString(String search) {
|
| + offset = findOffset(search);
|
| + length = search.length;
|
| + }
|
| +}
|
| +
|
| +
|
| +@ReflectiveTestCase()
|
| class GetAvailableRefactoringsTest extends AbstractAnalysisTest {
|
| /**
|
| * Tests that there is a RENAME refactoring available at the [search] offset.
|
| @@ -698,14 +892,15 @@ class _AbstractGetRefactoring_Test extends AbstractAnalysisTest {
|
| }
|
|
|
| Future<Response> sendRequest(RefactoringKind kind, int offset, int length,
|
| - Map options, [bool validateOnly = false]) {
|
| + HasToJson options, [bool validateOnly = false]) {
|
| + Map optionsJson = options != null ? options.toJson() : null;
|
| Request request = new EditGetRefactoringParams(
|
| kind,
|
| testFile,
|
| offset,
|
| length,
|
| validateOnly,
|
| - options: options).toRequest('0');
|
| + options: optionsJson).toRequest('0');
|
| return serverChannel.sendRequest(request);
|
| }
|
|
|
|
|