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.fixes; | 5 library test.edit.fixes; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
10 import 'package:analysis_server/src/domain_analysis.dart'; | 10 import 'package:analysis_server/src/domain_analysis.dart'; |
11 import 'package:analysis_server/src/edit/edit_domain.dart'; | 11 import 'package:analysis_server/src/edit/edit_domain.dart'; |
12 import 'package:plugin/manager.dart'; | 12 import 'package:plugin/manager.dart'; |
13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
14 import 'package:unittest/unittest.dart' hide ERROR; | 14 import 'package:unittest/unittest.dart' hide ERROR; |
15 | 15 |
16 import '../analysis_abstract.dart'; | 16 import '../analysis_abstract.dart'; |
17 import '../mocks.dart'; | 17 import '../mocks.dart'; |
18 import '../utils.dart'; | 18 import '../utils.dart'; |
19 | 19 |
20 main() { | 20 main() { |
21 initializeTestEnvironment(); | 21 initializeTestEnvironment(); |
22 defineReflectiveTests(FixesTest); | 22 defineReflectiveTests(FixesTest); |
23 } | 23 } |
24 | 24 |
25 @reflectiveTest | 25 @reflectiveTest |
26 class FixesTest extends AbstractAnalysisTest { | 26 class FixesTest extends AbstractAnalysisTest { |
27 @override | 27 @override |
28 void setUp() { | 28 void setUp() { |
29 super.setUp(); | 29 super.setUp(); |
30 createProject(); | |
31 ExtensionManager manager = new ExtensionManager(); | 30 ExtensionManager manager = new ExtensionManager(); |
32 manager.processPlugins([server.serverPlugin]); | 31 manager.processPlugins([server.serverPlugin]); |
33 handler = new EditDomainHandler(server); | 32 handler = new EditDomainHandler(server); |
34 } | 33 } |
35 | 34 |
36 test_fixUndefinedClass() async { | 35 test_fixUndefinedClass() async { |
| 36 createProject(); |
37 addTestFile(''' | 37 addTestFile(''' |
38 main() { | 38 main() { |
39 Future<String> x = null; | 39 Future<String> x = null; |
40 } | 40 } |
41 '''); | 41 '''); |
42 await waitForTasksFinished(); | 42 await waitForTasksFinished(); |
43 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('Future<String>'); | 43 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('Future<String>'); |
44 expect(errorFixes, hasLength(1)); | 44 expect(errorFixes, hasLength(1)); |
45 AnalysisError error = errorFixes[0].error; | 45 AnalysisError error = errorFixes[0].error; |
46 expect(error.severity, AnalysisErrorSeverity.WARNING); | 46 expect(error.severity, AnalysisErrorSeverity.WARNING); |
47 expect(error.type, AnalysisErrorType.STATIC_WARNING); | 47 expect(error.type, AnalysisErrorType.STATIC_WARNING); |
48 List<SourceChange> fixes = errorFixes[0].fixes; | 48 List<SourceChange> fixes = errorFixes[0].fixes; |
49 expect(fixes, hasLength(2)); | 49 expect(fixes, hasLength(2)); |
50 expect(fixes[0].message, matches('Import library')); | 50 expect(fixes[0].message, matches('Import library')); |
51 expect(fixes[1].message, matches('Create class')); | 51 expect(fixes[1].message, matches('Create class')); |
52 } | 52 } |
53 | 53 |
54 test_hasFixes() async { | 54 test_hasFixes() async { |
| 55 createProject(); |
55 addTestFile(''' | 56 addTestFile(''' |
56 foo() { | 57 foo() { |
57 print(1) | 58 print(1) |
58 } | 59 } |
59 bar() { | 60 bar() { |
60 print(10) print(20) | 61 print(10) print(20) |
61 } | 62 } |
62 '''); | 63 '''); |
63 await waitForTasksFinished(); | 64 await waitForTasksFinished(); |
64 // print(1) | 65 // print(1) |
65 { | 66 { |
66 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(1)'); | 67 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(1)'); |
67 expect(errorFixes, hasLength(1)); | 68 expect(errorFixes, hasLength(1)); |
68 _isSyntacticErrorWithSingleFix(errorFixes[0]); | 69 _isSyntacticErrorWithSingleFix(errorFixes[0]); |
69 } | 70 } |
70 // print(10) | 71 // print(10) |
71 { | 72 { |
72 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(10)'); | 73 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(10)'); |
73 expect(errorFixes, hasLength(2)); | 74 expect(errorFixes, hasLength(2)); |
74 _isSyntacticErrorWithSingleFix(errorFixes[0]); | 75 _isSyntacticErrorWithSingleFix(errorFixes[0]); |
75 _isSyntacticErrorWithSingleFix(errorFixes[1]); | 76 _isSyntacticErrorWithSingleFix(errorFixes[1]); |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 test_overlayOnlyFile() async { | 80 test_overlayOnlyFile() async { |
80 // add an overlay-only file | 81 createProject(); |
81 { | 82 testCode = ''' |
82 testCode = ''' | |
83 main() { | 83 main() { |
84 print(1) | 84 print(1) |
85 } | 85 } |
86 '''; | 86 '''; |
87 Request request = new AnalysisUpdateContentParams( | 87 _addOverlay(testFile, testCode); |
88 {testFile: new AddContentOverlay(testCode)}).toRequest('0'); | |
89 Response response = | |
90 new AnalysisDomainHandler(server).handleRequest(request); | |
91 expect(response, isResponseSuccess('0')); | |
92 } | |
93 // ask for fixes | 88 // ask for fixes |
94 await waitForTasksFinished(); | 89 await waitForTasksFinished(); |
95 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(1)'); | 90 List<AnalysisErrorFixes> errorFixes = await _getFixesAt('print(1)'); |
96 expect(errorFixes, hasLength(1)); | 91 expect(errorFixes, hasLength(1)); |
97 _isSyntacticErrorWithSingleFix(errorFixes[0]); | 92 _isSyntacticErrorWithSingleFix(errorFixes[0]); |
98 } | 93 } |
99 | 94 |
| 95 test_suggestImportFromDifferentAnalysisRoot() async { |
| 96 // Set up two projects. |
| 97 resourceProvider..newFolder("/project1")..newFolder("/project2"); |
| 98 handleSuccessfulRequest( |
| 99 new AnalysisSetAnalysisRootsParams(["/project1", "/project2"], []) |
| 100 .toRequest('0'), |
| 101 handler: analysisHandler); |
| 102 |
| 103 // Set up files. |
| 104 testFile = "/project1/main.dart"; |
| 105 testCode = "main() { print(new Foo()); }"; |
| 106 _addOverlay(testFile, testCode); |
| 107 // Add another file in the same project that imports the target file. |
| 108 // This ensures it will be analyzed as an implicit Source. |
| 109 _addOverlay("/project1/another.dart", 'import "../project2/target.dart";'); |
| 110 _addOverlay("/project2/target.dart", "class Foo() {}"); |
| 111 |
| 112 await waitForTasksFinished(); |
| 113 |
| 114 List<String> fixes = (await _getFixesAt('Foo()')) |
| 115 .single |
| 116 .fixes |
| 117 .map((f) => f.message) |
| 118 .toList(); |
| 119 expect(fixes, contains("Import library '../project2/target.dart'")); |
| 120 } |
| 121 |
| 122 void _addOverlay(String name, String contents) { |
| 123 Request request = |
| 124 new AnalysisUpdateContentParams({name: new AddContentOverlay(contents)}) |
| 125 .toRequest('0'); |
| 126 handleSuccessfulRequest(request, handler: analysisHandler); |
| 127 } |
| 128 |
100 Future<List<AnalysisErrorFixes>> _getFixes(int offset) async { | 129 Future<List<AnalysisErrorFixes>> _getFixes(int offset) async { |
101 Request request = new EditGetFixesParams(testFile, offset).toRequest('0'); | 130 Request request = new EditGetFixesParams(testFile, offset).toRequest('0'); |
102 Response response = await waitResponse(request); | 131 Response response = await waitResponse(request); |
103 var result = new EditGetFixesResult.fromResponse(response); | 132 var result = new EditGetFixesResult.fromResponse(response); |
104 return result.fixes; | 133 return result.fixes; |
105 } | 134 } |
106 | 135 |
107 Future<List<AnalysisErrorFixes>> _getFixesAt(String search) async { | 136 Future<List<AnalysisErrorFixes>> _getFixesAt(String search) async { |
108 int offset = findOffset(search); | 137 int offset = findOffset(search); |
109 return await _getFixes(offset); | 138 return await _getFixes(offset); |
110 } | 139 } |
111 | 140 |
112 void _isSyntacticErrorWithSingleFix(AnalysisErrorFixes fixes) { | 141 void _isSyntacticErrorWithSingleFix(AnalysisErrorFixes fixes) { |
113 AnalysisError error = fixes.error; | 142 AnalysisError error = fixes.error; |
114 expect(error.severity, AnalysisErrorSeverity.ERROR); | 143 expect(error.severity, AnalysisErrorSeverity.ERROR); |
115 expect(error.type, AnalysisErrorType.SYNTACTIC_ERROR); | 144 expect(error.type, AnalysisErrorType.SYNTACTIC_ERROR); |
116 expect(fixes.fixes, hasLength(1)); | 145 expect(fixes.fixes, hasLength(1)); |
117 } | 146 } |
118 } | 147 } |
OLD | NEW |