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:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/source/package_map_resolver.dart'; | 10 import 'package:analyzer/source/package_map_resolver.dart'; |
11 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
12 import 'package:analyzer/src/generated/parser.dart'; | 12 import 'package:analyzer/src/generated/parser.dart'; |
13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
15 | 15 |
16 import '../../abstract_context.dart'; | 16 import '../../abstract_context.dart'; |
17 import '../../abstract_single_unit.dart'; | 17 import '../../abstract_single_unit.dart'; |
18 import '../../reflective_tests.dart'; | 18 import '../../reflective_tests.dart'; |
19 | 19 |
20 | |
21 main() { | 20 main() { |
22 groupSep = ' | '; | 21 groupSep = ' | '; |
23 runReflectiveTests(FixProcessorTest); | 22 runReflectiveTests(FixProcessorTest); |
24 } | 23 } |
25 | 24 |
26 | |
27 typedef bool AnalysisErrorFilter(AnalysisError error); | 25 typedef bool AnalysisErrorFilter(AnalysisError error); |
28 | 26 |
29 | |
30 @reflectiveTest | 27 @reflectiveTest |
31 class FixProcessorTest extends AbstractSingleUnitTest { | 28 class FixProcessorTest extends AbstractSingleUnitTest { |
32 AnalysisErrorFilter errorFilter = null; | 29 AnalysisErrorFilter errorFilter = null; |
33 | 30 |
34 Fix fix; | 31 Fix fix; |
35 SourceChange change; | 32 SourceChange change; |
36 String resultCode; | 33 String resultCode; |
37 | 34 |
38 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { | 35 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { |
39 resolveTestUnit(''' | 36 resolveTestUnit(''' |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 78 } |
82 | 79 |
83 List<Position> expectedPositions(List<String> patterns) { | 80 List<Position> expectedPositions(List<String> patterns) { |
84 List<Position> positions = <Position>[]; | 81 List<Position> positions = <Position>[]; |
85 patterns.forEach((String search) { | 82 patterns.forEach((String search) { |
86 positions.add(expectedPosition(search)); | 83 positions.add(expectedPosition(search)); |
87 }); | 84 }); |
88 return positions; | 85 return positions; |
89 } | 86 } |
90 | 87 |
91 List<LinkedEditSuggestion> expectedSuggestions(LinkedEditSuggestionKind kind, | 88 List<LinkedEditSuggestion> expectedSuggestions( |
92 List<String> values) { | 89 LinkedEditSuggestionKind kind, List<String> values) { |
93 return values.map((value) { | 90 return values.map((value) { |
94 return new LinkedEditSuggestion(value, kind); | 91 return new LinkedEditSuggestion(value, kind); |
95 }).toList(); | 92 }).toList(); |
96 } | 93 } |
97 | 94 |
98 void setUp() { | 95 void setUp() { |
99 super.setUp(); | 96 super.setUp(); |
100 verifyNoTestUnitErrors = false; | 97 verifyNoTestUnitErrors = false; |
101 } | 98 } |
102 | 99 |
(...skipping 1568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1671 useFunction(a.test); | 1668 useFunction(a.test); |
1672 } | 1669 } |
1673 class A { | 1670 class A { |
1674 int test(double a, String b) { | 1671 int test(double a, String b) { |
1675 } | 1672 } |
1676 } | 1673 } |
1677 useFunction(int g(double a, String b)) {} | 1674 useFunction(int g(double a, String b)) {} |
1678 '''); | 1675 '''); |
1679 } | 1676 } |
1680 | 1677 |
1681 void | 1678 void test_creationFunction_forFunctionType_method_targetClass_hasOtherMember()
{ |
1682 test_creationFunction_forFunctionType_method_targetClass_hasOtherMember()
{ | |
1683 resolveTestUnit(''' | 1679 resolveTestUnit(''' |
1684 main(A a) { | 1680 main(A a) { |
1685 useFunction(a.test); | 1681 useFunction(a.test); |
1686 } | 1682 } |
1687 class A { | 1683 class A { |
1688 m() {} | 1684 m() {} |
1689 } | 1685 } |
1690 useFunction(int g(double a, String b)) {} | 1686 useFunction(int g(double a, String b)) {} |
1691 '''); | 1687 '''); |
1692 assertHasFix(FixKind.CREATE_METHOD, ''' | 1688 assertHasFix(FixKind.CREATE_METHOD, ''' |
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2830 myUndefinedMethod(0, 1.0, '3'); | 2826 myUndefinedMethod(0, 1.0, '3'); |
2831 } | 2827 } |
2832 | 2828 |
2833 void myUndefinedMethod(int i, double d, String s) { | 2829 void myUndefinedMethod(int i, double d, String s) { |
2834 } | 2830 } |
2835 } | 2831 } |
2836 '''); | 2832 '''); |
2837 // linked positions | 2833 // linked positions |
2838 int index = 0; | 2834 int index = 0; |
2839 _assertLinkedGroup( | 2835 _assertLinkedGroup( |
2840 change.linkedEditGroups[index++], | 2836 change.linkedEditGroups[index++], ['void myUndefinedMethod(']); |
2841 ['void myUndefinedMethod(']); | 2837 _assertLinkedGroup(change.linkedEditGroups[index++], |
2842 _assertLinkedGroup( | |
2843 change.linkedEditGroups[index++], | |
2844 ['myUndefinedMethod(0', 'myUndefinedMethod(int']); | 2838 ['myUndefinedMethod(0', 'myUndefinedMethod(int']); |
2845 _assertLinkedGroup( | 2839 _assertLinkedGroup(change.linkedEditGroups[index++], [ |
2846 change.linkedEditGroups[index++], | 2840 'int i' |
2847 ['int i'], | 2841 ], expectedSuggestions( |
2848 expectedSuggestions( | 2842 LinkedEditSuggestionKind.TYPE, ['int', 'num', 'Object', 'Comparable'])); |
2849 LinkedEditSuggestionKind.TYPE, | |
2850 ['int', 'num', 'Object', 'Comparable'])); | |
2851 _assertLinkedGroup(change.linkedEditGroups[index++], ['i,']); | 2843 _assertLinkedGroup(change.linkedEditGroups[index++], ['i,']); |
2852 _assertLinkedGroup( | 2844 _assertLinkedGroup(change.linkedEditGroups[index++], [ |
2853 change.linkedEditGroups[index++], | 2845 'double d' |
2854 ['double d'], | 2846 ], expectedSuggestions(LinkedEditSuggestionKind.TYPE, [ |
2855 expectedSuggestions( | 2847 'double', |
2856 LinkedEditSuggestionKind.TYPE, | 2848 'num', |
2857 ['double', 'num', 'Object', 'Comparable'])); | 2849 'Object', |
| 2850 'Comparable' |
| 2851 ])); |
2858 _assertLinkedGroup(change.linkedEditGroups[index++], ['d,']); | 2852 _assertLinkedGroup(change.linkedEditGroups[index++], ['d,']); |
2859 _assertLinkedGroup( | 2853 _assertLinkedGroup(change.linkedEditGroups[index++], [ |
2860 change.linkedEditGroups[index++], | 2854 'String s' |
2861 ['String s'], | 2855 ], expectedSuggestions( |
2862 expectedSuggestions( | 2856 LinkedEditSuggestionKind.TYPE, ['String', 'Object', 'Comparable'])); |
2863 LinkedEditSuggestionKind.TYPE, | |
2864 ['String', 'Object', 'Comparable'])); | |
2865 _assertLinkedGroup(change.linkedEditGroups[index++], ['s)']); | 2857 _assertLinkedGroup(change.linkedEditGroups[index++], ['s)']); |
2866 } | 2858 } |
2867 | 2859 |
2868 void test_undefinedMethod_createUnqualified_returnType() { | 2860 void test_undefinedMethod_createUnqualified_returnType() { |
2869 resolveTestUnit(''' | 2861 resolveTestUnit(''' |
2870 class A { | 2862 class A { |
2871 main() { | 2863 main() { |
2872 int v = myUndefinedMethod(); | 2864 int v = myUndefinedMethod(); |
2873 } | 2865 } |
2874 } | 2866 } |
2875 '''); | 2867 '''); |
2876 assertHasFix(FixKind.CREATE_METHOD, ''' | 2868 assertHasFix(FixKind.CREATE_METHOD, ''' |
2877 class A { | 2869 class A { |
2878 main() { | 2870 main() { |
2879 int v = myUndefinedMethod(); | 2871 int v = myUndefinedMethod(); |
2880 } | 2872 } |
2881 | 2873 |
2882 int myUndefinedMethod() { | 2874 int myUndefinedMethod() { |
2883 } | 2875 } |
2884 } | 2876 } |
2885 '''); | 2877 '''); |
2886 // linked positions | 2878 // linked positions |
2887 _assertLinkedGroup(change.linkedEditGroups[0], ['int myUndefinedMethod(']); | 2879 _assertLinkedGroup(change.linkedEditGroups[0], ['int myUndefinedMethod(']); |
2888 _assertLinkedGroup( | 2880 _assertLinkedGroup(change.linkedEditGroups[1], |
2889 change.linkedEditGroups[1], | |
2890 ['myUndefinedMethod();', 'myUndefinedMethod() {']); | 2881 ['myUndefinedMethod();', 'myUndefinedMethod() {']); |
2891 } | 2882 } |
2892 | 2883 |
2893 void test_undefinedMethod_createUnqualified_staticFromField() { | 2884 void test_undefinedMethod_createUnqualified_staticFromField() { |
2894 resolveTestUnit(''' | 2885 resolveTestUnit(''' |
2895 class A { | 2886 class A { |
2896 static var f = myUndefinedMethod(); | 2887 static var f = myUndefinedMethod(); |
2897 } | 2888 } |
2898 '''); | 2889 '''); |
2899 assertHasFix(FixKind.CREATE_METHOD, ''' | 2890 assertHasFix(FixKind.CREATE_METHOD, ''' |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3059 } | 3050 } |
3060 | 3051 |
3061 /** | 3052 /** |
3062 * Configures the [SourceFactory] to have the `my_pkg` package in | 3053 * Configures the [SourceFactory] to have the `my_pkg` package in |
3063 * `/packages/my_pkg/lib` folder. | 3054 * `/packages/my_pkg/lib` folder. |
3064 */ | 3055 */ |
3065 void _configureMyPkg(String myLibCode) { | 3056 void _configureMyPkg(String myLibCode) { |
3066 provider.newFile('/packages/my_pkg/lib/my_lib.dart', myLibCode); | 3057 provider.newFile('/packages/my_pkg/lib/my_lib.dart', myLibCode); |
3067 // configure SourceFactory | 3058 // configure SourceFactory |
3068 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib'); | 3059 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib'); |
3069 UriResolver pkgResolver = new PackageMapUriResolver(provider, { | 3060 UriResolver pkgResolver = |
3070 'my_pkg': [myPkgFolder] | 3061 new PackageMapUriResolver(provider, {'my_pkg': [myPkgFolder]}); |
3071 }); | |
3072 context.sourceFactory = new SourceFactory( | 3062 context.sourceFactory = new SourceFactory( |
3073 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]); | 3063 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]); |
3074 // force 'my_pkg' resolution | 3064 // force 'my_pkg' resolution |
3075 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); | 3065 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); |
3076 } | 3066 } |
3077 | 3067 |
3078 AnalysisError _findErrorToFix() { | 3068 AnalysisError _findErrorToFix() { |
3079 List<AnalysisError> errors = context.computeErrors(testSource); | 3069 List<AnalysisError> errors = context.computeErrors(testSource); |
3080 errors.removeWhere((error) { | 3070 errors.removeWhere((error) { |
3081 return error.errorCode == HintCode.UNUSED_ELEMENT || | 3071 return error.errorCode == HintCode.UNUSED_ELEMENT || |
3082 error.errorCode == HintCode.UNUSED_FIELD || | 3072 error.errorCode == HintCode.UNUSED_FIELD || |
3083 error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE; | 3073 error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE; |
3084 }); | 3074 }); |
3085 if (errorFilter != null) { | 3075 if (errorFilter != null) { |
3086 errors = errors.where(errorFilter).toList(); | 3076 errors = errors.where(errorFilter).toList(); |
3087 } | 3077 } |
3088 expect(errors, hasLength(1)); | 3078 expect(errors, hasLength(1)); |
3089 return errors[0]; | 3079 return errors[0]; |
3090 } | 3080 } |
3091 | 3081 |
3092 List<Position> _findResultPositions(List<String> searchStrings) { | 3082 List<Position> _findResultPositions(List<String> searchStrings) { |
3093 List<Position> positions = <Position>[]; | 3083 List<Position> positions = <Position>[]; |
3094 for (String search in searchStrings) { | 3084 for (String search in searchStrings) { |
3095 int offset = resultCode.indexOf(search); | 3085 int offset = resultCode.indexOf(search); |
3096 positions.add(new Position(testFile, offset)); | 3086 positions.add(new Position(testFile, offset)); |
3097 } | 3087 } |
3098 return positions; | 3088 return positions; |
3099 } | 3089 } |
3100 } | 3090 } |
OLD | NEW |