OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | |
6 | |
7 import 'package:analyzer/file_system/file_system.dart'; | 5 import 'package:analyzer/file_system/file_system.dart'; |
8 import 'package:analyzer/file_system/memory_file_system.dart'; | 6 import 'package:analyzer/file_system/memory_file_system.dart'; |
9 import 'package:analyzer/src/dart/analysis/driver.dart'; | 7 import 'package:analyzer/src/dart/analysis/driver.dart'; |
10 import 'package:analyzer_plugin/plugin/plugin.dart'; | 8 import 'package:analyzer_plugin/protocol/protocol.dart'; |
11 import 'package:analyzer_plugin/protocol/protocol_common.dart'; | 9 import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
12 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; | 10 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
13 import 'package:path/src/context.dart'; | 11 import 'package:path/src/context.dart'; |
14 import 'package:test/test.dart'; | 12 import 'package:test/test.dart'; |
15 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
16 | 14 |
| 15 import 'mocks.dart'; |
| 16 |
17 void main() { | 17 void main() { |
18 defineReflectiveTests(ServerPluginTest); | 18 defineReflectiveTests(ServerPluginTest); |
19 } | 19 } |
20 | 20 |
21 class MockAnalysisDriver extends AnalysisDriverGeneric { | |
22 /** | |
23 * The files that have been added to this driver. | |
24 */ | |
25 List<String> addedFiles = <String>[]; | |
26 | |
27 @override | |
28 bool get hasFilesToAnalyze => false; | |
29 | |
30 @override | |
31 set priorityFiles(List<String> priorityPaths) {} | |
32 | |
33 @override | |
34 AnalysisDriverPriority get workPriority => AnalysisDriverPriority.nothing; | |
35 | |
36 @override | |
37 void addFile(String path) { | |
38 addedFiles.add(path); | |
39 } | |
40 | |
41 @override | |
42 void dispose() {} | |
43 | |
44 @override | |
45 Future<Null> performWork() => new Future.value(null); | |
46 } | |
47 | |
48 @reflectiveTest | 21 @reflectiveTest |
49 class ServerPluginTest { | 22 class ServerPluginTest { |
50 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); | 23 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
51 | 24 |
52 TestServerPlugin plugin; | 25 MockChannel channel; |
| 26 _TestServerPlugin plugin; |
53 | 27 |
54 String packagePath1; | 28 String packagePath1; |
55 String filePath1; | 29 String filePath1; |
56 ContextRoot contextRoot1; | 30 ContextRoot contextRoot1; |
57 | 31 |
58 String packagePath2; | 32 String packagePath2; |
59 String filePath2; | 33 String filePath2; |
60 ContextRoot contextRoot2; | 34 ContextRoot contextRoot2; |
61 | 35 |
62 void setUp() { | 36 void setUp() { |
63 Context pathContext = resourceProvider.pathContext; | 37 Context pathContext = resourceProvider.pathContext; |
64 | 38 |
65 packagePath1 = resourceProvider.convertPath('/package1'); | 39 packagePath1 = resourceProvider.convertPath('/package1'); |
66 filePath1 = pathContext.join(packagePath1, 'lib', 'test.dart'); | 40 filePath1 = pathContext.join(packagePath1, 'lib', 'test.dart'); |
67 resourceProvider.newFile(filePath1, ''); | 41 resourceProvider.newFile(filePath1, ''); |
68 contextRoot1 = new ContextRoot(packagePath1, <String>[]); | 42 contextRoot1 = new ContextRoot(packagePath1, <String>[]); |
69 | 43 |
70 packagePath2 = resourceProvider.convertPath('/package2'); | 44 packagePath2 = resourceProvider.convertPath('/package2'); |
71 filePath2 = pathContext.join(packagePath2, 'lib', 'test.dart'); | 45 filePath2 = pathContext.join(packagePath2, 'lib', 'test.dart'); |
72 resourceProvider.newFile(filePath2, ''); | 46 resourceProvider.newFile(filePath2, ''); |
73 contextRoot2 = new ContextRoot(packagePath2, <String>[]); | 47 contextRoot2 = new ContextRoot(packagePath2, <String>[]); |
74 | 48 |
75 plugin = new TestServerPlugin(resourceProvider); | 49 channel = new MockChannel(); |
| 50 plugin = new _TestServerPlugin(resourceProvider); |
| 51 plugin.start(channel); |
76 } | 52 } |
77 | 53 |
78 test_contextRootContaining_insideRoot() async { | 54 test_contextRootContaining_insideRoot() async { |
79 await plugin.handleAnalysisSetContextRoots( | 55 await plugin.handleAnalysisSetContextRoots( |
80 new AnalysisSetContextRootsParams([contextRoot1])); | 56 new AnalysisSetContextRootsParams([contextRoot1])); |
81 | 57 |
82 expect(plugin.contextRootContaining(filePath1), isNotNull); | 58 expect(plugin.contextRootContaining(filePath1), isNotNull); |
83 } | 59 } |
84 | 60 |
85 void test_contextRootContaining_noRoots() { | 61 void test_contextRootContaining_noRoots() { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 var result = await plugin.handleAnalysisSetContextBuilderOptions( | 105 var result = await plugin.handleAnalysisSetContextBuilderOptions( |
130 new AnalysisSetContextBuilderOptionsParams( | 106 new AnalysisSetContextBuilderOptionsParams( |
131 new ContextBuilderOptions())); | 107 new ContextBuilderOptions())); |
132 expect(result, isNotNull); | 108 expect(result, isNotNull); |
133 } | 109 } |
134 | 110 |
135 test_handleAnalysisSetContextRoots() async { | 111 test_handleAnalysisSetContextRoots() async { |
136 var result = await plugin.handleAnalysisSetContextRoots( | 112 var result = await plugin.handleAnalysisSetContextRoots( |
137 new AnalysisSetContextRootsParams([contextRoot1])); | 113 new AnalysisSetContextRootsParams([contextRoot1])); |
138 expect(result, isNotNull); | 114 expect(result, isNotNull); |
139 AnalysisDriverGeneric driver = plugin.driverMap[contextRoot1]; | 115 AnalysisDriverGeneric driver = _getDriver(contextRoot1); |
140 expect(driver, isNotNull); | 116 expect(driver, isNotNull); |
141 expect((driver as MockAnalysisDriver).addedFiles, hasLength(1)); | 117 expect((driver as MockAnalysisDriver).addedFiles, hasLength(1)); |
142 } | 118 } |
143 | 119 |
144 test_handleAnalysisSetPriorityFiles() async { | 120 test_handleAnalysisSetPriorityFiles() async { |
145 await plugin.handleAnalysisSetContextRoots( | 121 await plugin.handleAnalysisSetContextRoots( |
146 new AnalysisSetContextRootsParams([contextRoot1])); | 122 new AnalysisSetContextRootsParams([contextRoot1])); |
147 | 123 |
148 var result = await plugin.handleAnalysisSetPriorityFiles( | 124 var result = await plugin.handleAnalysisSetPriorityFiles( |
149 new AnalysisSetPriorityFilesParams([filePath1])); | 125 new AnalysisSetPriorityFilesParams([filePath1])); |
150 expect(result, isNotNull); | 126 expect(result, isNotNull); |
151 } | 127 } |
152 | 128 |
153 test_handleAnalysisSetSubscriptions() async { | 129 test_handleAnalysisSetSubscriptions() async { |
154 await plugin.handleAnalysisSetContextRoots( | 130 await plugin.handleAnalysisSetContextRoots( |
155 new AnalysisSetContextRootsParams([contextRoot1])); | 131 new AnalysisSetContextRootsParams([contextRoot1])); |
156 expect(plugin.subscriptionManager.servicesForFile(filePath1), isEmpty); | 132 expect(plugin.subscriptionManager.servicesForFile(filePath1), isEmpty); |
157 | 133 |
158 AnalysisSetSubscriptionsResult result = await plugin | 134 AnalysisSetSubscriptionsResult result = await plugin |
159 .handleAnalysisSetSubscriptions(new AnalysisSetSubscriptionsParams({ | 135 .handleAnalysisSetSubscriptions(new AnalysisSetSubscriptionsParams({ |
160 AnalysisService.OUTLINE: [filePath1] | 136 AnalysisService.OUTLINE: [filePath1] |
161 })); | 137 })); |
162 expect(result, isNotNull); | 138 expect(result, isNotNull); |
163 expect(plugin.subscriptionManager.servicesForFile(filePath1), | 139 expect(plugin.subscriptionManager.servicesForFile(filePath1), |
164 [AnalysisService.OUTLINE]); | 140 [AnalysisService.OUTLINE]); |
165 } | 141 } |
166 | 142 |
167 test_handleAnalysisUpdateContent() async { | 143 test_handleAnalysisUpdateContent_addChangeRemove() async { |
168 await plugin.handleAnalysisSetContextRoots( | 144 await plugin.handleAnalysisSetContextRoots( |
169 new AnalysisSetContextRootsParams([contextRoot1])); | 145 new AnalysisSetContextRootsParams([contextRoot1])); |
170 var addResult = await plugin.handleAnalysisUpdateContent( | 146 var addResult = await plugin.handleAnalysisUpdateContent( |
171 new AnalysisUpdateContentParams( | 147 new AnalysisUpdateContentParams( |
172 {filePath1: new AddContentOverlay('class C {}')})); | 148 {filePath1: new AddContentOverlay('class C {}')})); |
173 expect(addResult, isNotNull); | 149 expect(addResult, isNotNull); |
174 var changeResult = await plugin | 150 var changeResult = await plugin |
175 .handleAnalysisUpdateContent(new AnalysisUpdateContentParams({ | 151 .handleAnalysisUpdateContent(new AnalysisUpdateContentParams({ |
176 filePath1: | 152 filePath1: |
177 new ChangeContentOverlay([new SourceEdit(7, 0, ' extends Object')]) | 153 new ChangeContentOverlay([new SourceEdit(7, 0, ' extends Object')]) |
178 })); | 154 })); |
179 expect(changeResult, isNotNull); | 155 expect(changeResult, isNotNull); |
180 var removeResult = await plugin.handleAnalysisUpdateContent( | 156 var removeResult = await plugin.handleAnalysisUpdateContent( |
181 new AnalysisUpdateContentParams( | 157 new AnalysisUpdateContentParams( |
182 {filePath1: new RemoveContentOverlay()})); | 158 {filePath1: new RemoveContentOverlay()})); |
183 expect(removeResult, isNotNull); | 159 expect(removeResult, isNotNull); |
184 } | 160 } |
185 | 161 |
| 162 test_handleAnalysisUpdateContent_changeNoAdd() async { |
| 163 await plugin.handleAnalysisSetContextRoots( |
| 164 new AnalysisSetContextRootsParams([contextRoot1])); |
| 165 try { |
| 166 await plugin.handleAnalysisUpdateContent(new AnalysisUpdateContentParams({ |
| 167 filePath1: |
| 168 new ChangeContentOverlay([new SourceEdit(7, 0, ' extends Object')]) |
| 169 })); |
| 170 fail('Expected RequestFailure'); |
| 171 } on RequestFailure { |
| 172 // Expected |
| 173 } |
| 174 } |
| 175 |
| 176 test_handleAnalysisUpdateContent_invalidChange() async { |
| 177 await plugin.handleAnalysisSetContextRoots( |
| 178 new AnalysisSetContextRootsParams([contextRoot1])); |
| 179 await plugin.handleAnalysisUpdateContent(new AnalysisUpdateContentParams( |
| 180 {filePath1: new AddContentOverlay('class C {}')})); |
| 181 try { |
| 182 await plugin.handleAnalysisUpdateContent(new AnalysisUpdateContentParams({ |
| 183 filePath1: |
| 184 new ChangeContentOverlay([new SourceEdit(20, 5, 'class D {}')]) |
| 185 })); |
| 186 fail('Expected RequestFailure'); |
| 187 } on RequestFailure { |
| 188 // Expected |
| 189 } |
| 190 } |
| 191 |
186 test_handleCompletionGetSuggestions() async { | 192 test_handleCompletionGetSuggestions() async { |
187 await plugin.handleAnalysisSetContextRoots( | 193 await plugin.handleAnalysisSetContextRoots( |
188 new AnalysisSetContextRootsParams([contextRoot1])); | 194 new AnalysisSetContextRootsParams([contextRoot1])); |
189 | 195 |
190 CompletionGetSuggestionsResult result = | 196 CompletionGetSuggestionsResult result = |
191 await plugin.handleCompletionGetSuggestions( | 197 await plugin.handleCompletionGetSuggestions( |
192 new CompletionGetSuggestionsParams(filePath1, 12)); | 198 new CompletionGetSuggestionsParams(filePath1, 12)); |
193 expect(result, isNotNull); | 199 expect(result, isNotNull); |
194 } | 200 } |
195 | 201 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 expect(result.isCompatible, isTrue); | 251 expect(result.isCompatible, isTrue); |
246 expect(result.name, 'Test Plugin'); | 252 expect(result.name, 'Test Plugin'); |
247 expect(result.version, '0.1.0'); | 253 expect(result.version, '0.1.0'); |
248 } | 254 } |
249 | 255 |
250 @failingTest | 256 @failingTest |
251 void test_isCompatibleWith() { | 257 void test_isCompatibleWith() { |
252 fail('Not yet implemented.'); | 258 fail('Not yet implemented.'); |
253 } | 259 } |
254 | 260 |
255 @failingTest | |
256 void test_onDone() { | 261 void test_onDone() { |
257 fail('Not yet implemented.'); | 262 channel.sendDone(); |
258 } | 263 } |
259 | 264 |
260 @failingTest | |
261 void test_onError() { | 265 void test_onError() { |
262 fail('Not yet implemented.'); | 266 channel.sendError(new ArgumentError(), new StackTrace.fromString('')); |
263 } | 267 } |
264 | 268 |
265 @failingTest | 269 test_onRequest_analysisGetNavigation() async { |
266 void test_start() { | 270 var result = |
267 fail('Not yet implemented.'); | 271 await channel.sendRequest(new AnalysisGetNavigationParams('', 1, 2)); |
| 272 expect(result, isNotNull); |
| 273 } |
| 274 |
| 275 test_onRequest_analysisHandleWatchEvents() async { |
| 276 var result = |
| 277 await channel.sendRequest(new AnalysisHandleWatchEventsParams([])); |
| 278 expect(result, isNotNull); |
| 279 } |
| 280 |
| 281 test_onRequest_analysisReanalyze_all() async { |
| 282 await channel |
| 283 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 284 var result = await channel.sendRequest(new AnalysisReanalyzeParams()); |
| 285 expect(result, isNotNull); |
| 286 } |
| 287 |
| 288 test_onRequest_analysisReanalyze_subset() async { |
| 289 await channel |
| 290 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 291 await channel |
| 292 .sendRequest(new AnalysisSetContextRootsParams([contextRoot2])); |
| 293 var result = await channel |
| 294 .sendRequest(new AnalysisReanalyzeParams(roots: [packagePath2])); |
| 295 expect(result, isNotNull); |
| 296 } |
| 297 |
| 298 test_onRequest_analysisSetContextBuilderOptions() async { |
| 299 var result = await channel.sendRequest( |
| 300 new AnalysisSetContextBuilderOptionsParams( |
| 301 new ContextBuilderOptions())); |
| 302 expect(result, isNotNull); |
| 303 } |
| 304 |
| 305 test_onRequest_analysisSetContextRoots() async { |
| 306 var result = await channel |
| 307 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 308 expect(result, isNotNull); |
| 309 AnalysisDriverGeneric driver = _getDriver(contextRoot1); |
| 310 expect(driver, isNotNull); |
| 311 expect((driver as MockAnalysisDriver).addedFiles, hasLength(1)); |
| 312 } |
| 313 |
| 314 test_onRequest_analysisSetPriorityFiles() async { |
| 315 await channel |
| 316 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 317 |
| 318 var result = await channel |
| 319 .sendRequest(new AnalysisSetPriorityFilesParams([filePath1])); |
| 320 expect(result, isNotNull); |
| 321 } |
| 322 |
| 323 test_onRequest_analysisSetSubscriptions() async { |
| 324 await channel |
| 325 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 326 expect(plugin.subscriptionManager.servicesForFile(filePath1), isEmpty); |
| 327 |
| 328 var result = await channel.sendRequest(new AnalysisSetSubscriptionsParams({ |
| 329 AnalysisService.OUTLINE: [filePath1] |
| 330 })); |
| 331 expect(result, isNotNull); |
| 332 expect(plugin.subscriptionManager.servicesForFile(filePath1), |
| 333 [AnalysisService.OUTLINE]); |
| 334 } |
| 335 |
| 336 test_onRequest_analysisUpdateContent_addChangeRemove() async { |
| 337 await channel |
| 338 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 339 var addResult = await channel.sendRequest(new AnalysisUpdateContentParams( |
| 340 {filePath1: new AddContentOverlay('class C {}')})); |
| 341 expect(addResult, isNotNull); |
| 342 var changeResult = |
| 343 await channel.sendRequest(new AnalysisUpdateContentParams({ |
| 344 filePath1: |
| 345 new ChangeContentOverlay([new SourceEdit(7, 0, ' extends Object')]) |
| 346 })); |
| 347 expect(changeResult, isNotNull); |
| 348 var removeResult = await channel.sendRequest( |
| 349 new AnalysisUpdateContentParams( |
| 350 {filePath1: new RemoveContentOverlay()})); |
| 351 expect(removeResult, isNotNull); |
| 352 } |
| 353 |
| 354 test_onRequest_completionGetSuggestions() async { |
| 355 await channel |
| 356 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 357 |
| 358 var result = await channel |
| 359 .sendRequest(new CompletionGetSuggestionsParams(filePath1, 12)); |
| 360 expect(result, isNotNull); |
| 361 } |
| 362 |
| 363 test_onRequest_editGetAssists() async { |
| 364 await channel |
| 365 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 366 |
| 367 var result = |
| 368 await channel.sendRequest(new EditGetAssistsParams(filePath1, 10, 0)); |
| 369 expect(result, isNotNull); |
| 370 } |
| 371 |
| 372 test_onRequest_editGetAvailableRefactorings() async { |
| 373 await channel |
| 374 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 375 |
| 376 var result = await channel |
| 377 .sendRequest(new EditGetAvailableRefactoringsParams(filePath1, 10, 0)); |
| 378 expect(result, isNotNull); |
| 379 } |
| 380 |
| 381 test_onRequest_editGetFixes() async { |
| 382 await channel |
| 383 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 384 |
| 385 var result = |
| 386 await channel.sendRequest(new EditGetFixesParams(filePath1, 13)); |
| 387 expect(result, isNotNull); |
| 388 } |
| 389 |
| 390 test_onRequest_editGetRefactoring() async { |
| 391 await channel |
| 392 .sendRequest(new AnalysisSetContextRootsParams([contextRoot1])); |
| 393 |
| 394 var result = await channel.sendRequest(new EditGetRefactoringParams( |
| 395 RefactoringKind.RENAME, filePath1, 7, 0, false)); |
| 396 expect(result, isNotNull); |
| 397 } |
| 398 |
| 399 test_onRequest_pluginShutdown() async { |
| 400 var result = await channel.sendRequest(new PluginShutdownParams()); |
| 401 expect(result, isNotNull); |
| 402 } |
| 403 |
| 404 test_onRequest_pluginVersionCheck() async { |
| 405 var response = (await channel.sendRequest( |
| 406 new PluginVersionCheckParams('byteStorePath', 'sdkPath', '0.1.0'))); |
| 407 PluginVersionCheckResult result = |
| 408 new PluginVersionCheckResult.fromResponse(response); |
| 409 expect(result, isNotNull); |
| 410 expect(result.interestingFiles, ['*.dart']); |
| 411 expect(result.isCompatible, isTrue); |
| 412 expect(result.name, 'Test Plugin'); |
| 413 expect(result.version, '0.1.0'); |
| 414 } |
| 415 |
| 416 AnalysisDriverGeneric _getDriver(ContextRoot targetRoot) { |
| 417 for (ContextRoot root in plugin.driverMap.keys) { |
| 418 if (root.root == targetRoot.root) { |
| 419 return plugin.driverMap[root]; |
| 420 } |
| 421 } |
| 422 return null; |
268 } | 423 } |
269 } | 424 } |
270 | 425 |
271 /** | 426 class _TestServerPlugin extends MockServerPlugin { |
272 * A concrete implementation of a server plugin that is suitable for testing. | |
273 */ | |
274 class TestServerPlugin extends ServerPlugin { | |
275 Map<String, List<AnalysisService>> latestSubscriptions; | 427 Map<String, List<AnalysisService>> latestSubscriptions; |
276 | 428 |
277 TestServerPlugin(ResourceProvider resourceProvider) : super(resourceProvider); | 429 _TestServerPlugin(ResourceProvider resourceProvider) |
278 | 430 : super(resourceProvider); |
279 @override | |
280 List<String> get fileGlobsToAnalyze => <String>['*.dart']; | |
281 | |
282 @override | |
283 String get name => 'Test Plugin'; | |
284 | |
285 @override | |
286 String get version => '0.1.0'; | |
287 | |
288 @override | |
289 AnalysisDriverGeneric createAnalysisDriver(ContextRoot contextRoot) { | |
290 return new MockAnalysisDriver(); | |
291 } | |
292 | 431 |
293 @override | 432 @override |
294 void sendNotificationsForSubscriptions( | 433 void sendNotificationsForSubscriptions( |
295 Map<String, List<AnalysisService>> subscriptions) { | 434 Map<String, List<AnalysisService>> subscriptions) { |
296 latestSubscriptions = subscriptions; | 435 latestSubscriptions = subscriptions; |
297 } | 436 } |
298 } | 437 } |
OLD | NEW |