| 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.services.correction.fix; | 5 library test.services.correction.fix; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' hide AnalysisError; | 7 import 'package:analysis_server/src/protocol.dart' hide AnalysisError; |
| 8 import 'package:analysis_server/src/services/correction/fix.dart'; | 8 import 'package:analysis_server/src/services/correction/fix.dart'; |
| 9 import 'package:analysis_server/src/services/index/index.dart'; | 9 import 'package:analysis_server/src/services/index/index.dart'; |
| 10 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | 10 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
| 11 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; | 11 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/src/generated/error.dart'; | 14 import 'package:analyzer/src/generated/error.dart'; |
| 15 import 'package:analyzer/src/generated/parser.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
| 17 | 18 |
| 18 import '../../abstract_context.dart'; | 19 import '../../abstract_context.dart'; |
| 19 import '../../abstract_single_unit.dart'; | 20 import '../../abstract_single_unit.dart'; |
| 20 import '../../reflective_tests.dart'; | 21 import '../../reflective_tests.dart'; |
| 21 | 22 |
| 22 | 23 |
| 23 main() { | 24 main() { |
| 24 groupSep = ' | '; | 25 groupSep = ' | '; |
| 25 runReflectiveTests(FixProcessorTest); | 26 runReflectiveTests(FixProcessorTest); |
| 26 } | 27 } |
| 27 | 28 |
| 28 | 29 |
| 30 typedef bool AnalysisErrorFilter(AnalysisError error); |
| 31 |
| 32 |
| 29 @reflectiveTest | 33 @reflectiveTest |
| 30 class FixProcessorTest extends AbstractSingleUnitTest { | 34 class FixProcessorTest extends AbstractSingleUnitTest { |
| 31 Index index; | 35 Index index; |
| 32 SearchEngineImpl searchEngine; | 36 SearchEngineImpl searchEngine; |
| 33 | 37 |
| 34 bool checkHasSingleError = true; | 38 AnalysisErrorFilter errorFilter = null; |
| 35 | 39 |
| 36 Fix fix; | 40 Fix fix; |
| 37 SourceChange change; | 41 SourceChange change; |
| 38 String resultCode; | 42 String resultCode; |
| 39 | 43 |
| 40 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { | 44 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { |
| 41 _indexTestUnit(''' | 45 _indexTestUnit(''' |
| 42 main() { | 46 main() { |
| 43 bool b = true; | 47 bool b = true; |
| 44 $lineWithTest | 48 $lineWithTest |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 }).toList(); | 101 }).toList(); |
| 98 } | 102 } |
| 99 | 103 |
| 100 void setUp() { | 104 void setUp() { |
| 101 super.setUp(); | 105 super.setUp(); |
| 102 index = createLocalMemoryIndex(); | 106 index = createLocalMemoryIndex(); |
| 103 searchEngine = new SearchEngineImpl(index); | 107 searchEngine = new SearchEngineImpl(index); |
| 104 verifyNoTestUnitErrors = false; | 108 verifyNoTestUnitErrors = false; |
| 105 } | 109 } |
| 106 | 110 |
| 111 void test_addSync_blockFunctionBody() { |
| 112 _indexTestUnit(''' |
| 113 foo() {} |
| 114 main() { |
| 115 await foo(); |
| 116 } |
| 117 '''); |
| 118 List<AnalysisError> errors = context.computeErrors(testSource); |
| 119 expect(errors, hasLength(2)); |
| 120 // ParserError: Expected to find ';' |
| 121 { |
| 122 AnalysisError error = errors[0]; |
| 123 expect(error.message, "Expected to find ';'"); |
| 124 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); |
| 125 expect(fixes, isEmpty); |
| 126 } |
| 127 // Undefined name 'await' |
| 128 { |
| 129 AnalysisError error = errors[1]; |
| 130 expect(error.message, "Undefined name 'await'"); |
| 131 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); |
| 132 // has exactly one fix |
| 133 expect(fixes, hasLength(1)); |
| 134 Fix fix = fixes[0]; |
| 135 expect(fix.kind, FixKind.ADD_ASYNC); |
| 136 // apply to "file" |
| 137 List<SourceFileEdit> fileEdits = fix.change.edits; |
| 138 expect(fileEdits, hasLength(1)); |
| 139 resultCode = SourceEdit.applySequence(testCode, fileEdits[0].edits); |
| 140 // verify |
| 141 expect(resultCode, ''' |
| 142 foo() {} |
| 143 main() async { |
| 144 await foo(); |
| 145 } |
| 146 '''); |
| 147 } |
| 148 } |
| 149 |
| 150 void test_addSync_expressionFunctionBody() { |
| 151 errorFilter = (AnalysisError error) { |
| 152 return error.errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER; |
| 153 }; |
| 154 _indexTestUnit(''' |
| 155 foo() {} |
| 156 main() => await foo(); |
| 157 '''); |
| 158 assertHasFix(FixKind.ADD_ASYNC, ''' |
| 159 foo() {} |
| 160 main() async => await foo(); |
| 161 '''); |
| 162 } |
| 163 |
| 107 void test_boolean() { | 164 void test_boolean() { |
| 108 _indexTestUnit(''' | 165 _indexTestUnit(''' |
| 109 main() { | 166 main() { |
| 110 boolean v; | 167 boolean v; |
| 111 } | 168 } |
| 112 '''); | 169 '''); |
| 113 assertHasFix(FixKind.REPLACE_BOOLEAN_WITH_BOOL, ''' | 170 assertHasFix(FixKind.REPLACE_BOOLEAN_WITH_BOOL, ''' |
| 114 main() { | 171 main() { |
| 115 bool v; | 172 bool v; |
| 116 } | 173 } |
| (...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 } | 886 } |
| 830 | 887 |
| 831 void test_createGetter_unqualified_instance_asStatement() { | 888 void test_createGetter_unqualified_instance_asStatement() { |
| 832 _indexTestUnit(''' | 889 _indexTestUnit(''' |
| 833 class A { | 890 class A { |
| 834 main() { | 891 main() { |
| 835 test; | 892 test; |
| 836 } | 893 } |
| 837 } | 894 } |
| 838 '''); | 895 '''); |
| 839 // TODO | |
| 840 assertHasFix(FixKind.CREATE_GETTER, ''' | 896 assertHasFix(FixKind.CREATE_GETTER, ''' |
| 841 class A { | 897 class A { |
| 842 get test => null; | 898 get test => null; |
| 843 | 899 |
| 844 main() { | 900 main() { |
| 845 test; | 901 test; |
| 846 } | 902 } |
| 847 } | 903 } |
| 848 '''); | 904 '''); |
| 849 } | 905 } |
| (...skipping 1135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1985 _indexTestUnit(''' | 2041 _indexTestUnit(''' |
| 1986 import 'no/matter/my_lib.dart'; | 2042 import 'no/matter/my_lib.dart'; |
| 1987 '''); | 2043 '''); |
| 1988 performAllAnalysisTasks(); | 2044 performAllAnalysisTasks(); |
| 1989 assertHasFix(FixKind.REPLACE_IMPORT_URI, ''' | 2045 assertHasFix(FixKind.REPLACE_IMPORT_URI, ''' |
| 1990 import 'package:my_pkg/my_lib.dart'; | 2046 import 'package:my_pkg/my_lib.dart'; |
| 1991 '''); | 2047 '''); |
| 1992 } | 2048 } |
| 1993 | 2049 |
| 1994 void test_replaceVarWithDynamic() { | 2050 void test_replaceVarWithDynamic() { |
| 1995 checkHasSingleError = false; | 2051 errorFilter = (AnalysisError error) { |
| 2052 return error.errorCode == ParserErrorCode.VAR_AS_TYPE_NAME; |
| 2053 }; |
| 1996 _indexTestUnit(''' | 2054 _indexTestUnit(''' |
| 1997 class A { | 2055 class A { |
| 1998 Map<String, var> m; | 2056 Map<String, var> m; |
| 1999 } | 2057 } |
| 2000 '''); | 2058 '''); |
| 2001 assertHasFix(FixKind.REPLACE_VAR_WITH_DYNAMIC, ''' | 2059 assertHasFix(FixKind.REPLACE_VAR_WITH_DYNAMIC, ''' |
| 2002 class A { | 2060 class A { |
| 2003 Map<String, dynamic> m; | 2061 Map<String, dynamic> m; |
| 2004 } | 2062 } |
| 2005 '''); | 2063 '''); |
| (...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2707 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); | 2765 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); |
| 2708 } | 2766 } |
| 2709 | 2767 |
| 2710 AnalysisError _findErrorToFix() { | 2768 AnalysisError _findErrorToFix() { |
| 2711 List<AnalysisError> errors = context.computeErrors(testSource); | 2769 List<AnalysisError> errors = context.computeErrors(testSource); |
| 2712 errors.removeWhere((error) { | 2770 errors.removeWhere((error) { |
| 2713 return error.errorCode == HintCode.UNUSED_ELEMENT || | 2771 return error.errorCode == HintCode.UNUSED_ELEMENT || |
| 2714 error.errorCode == HintCode.UNUSED_FIELD || | 2772 error.errorCode == HintCode.UNUSED_FIELD || |
| 2715 error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE; | 2773 error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE; |
| 2716 }); | 2774 }); |
| 2717 if (checkHasSingleError) { | 2775 if (errorFilter != null) { |
| 2718 expect(errors, hasLength(1)); | 2776 errors = errors.where(errorFilter).toList(); |
| 2719 } | 2777 } |
| 2778 expect(errors, hasLength(1)); |
| 2720 return errors[0]; | 2779 return errors[0]; |
| 2721 } | 2780 } |
| 2722 | 2781 |
| 2723 List<Position> _findResultPositions(List<String> searchStrings) { | 2782 List<Position> _findResultPositions(List<String> searchStrings) { |
| 2724 List<Position> positions = <Position>[]; | 2783 List<Position> positions = <Position>[]; |
| 2725 for (String search in searchStrings) { | 2784 for (String search in searchStrings) { |
| 2726 int offset = resultCode.indexOf(search); | 2785 int offset = resultCode.indexOf(search); |
| 2727 positions.add(new Position(testFile, offset)); | 2786 positions.add(new Position(testFile, offset)); |
| 2728 } | 2787 } |
| 2729 return positions; | 2788 return positions; |
| 2730 } | 2789 } |
| 2731 | 2790 |
| 2732 void _indexTestUnit(String code) { | 2791 void _indexTestUnit(String code) { |
| 2733 resolveTestUnit(code); | 2792 resolveTestUnit(code); |
| 2734 index.indexUnit(context, testUnit); | 2793 index.indexUnit(context, testUnit); |
| 2735 } | 2794 } |
| 2736 } | 2795 } |
| OLD | NEW |