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.domain.completion; | 5 library test.domain.completion; |
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/analysis_server.dart'; | 10 import 'package:analysis_server/src/analysis_server.dart'; |
11 import 'package:analysis_server/src/channel/channel.dart'; | 11 import 'package:analysis_server/src/channel/channel.dart'; |
12 import 'package:analysis_server/src/constants.dart'; | 12 import 'package:analysis_server/src/constants.dart'; |
13 import 'package:analysis_server/src/context_manager.dart'; | 13 import 'package:analysis_server/src/context_manager.dart'; |
14 import 'package:analysis_server/src/domain_analysis.dart'; | 14 import 'package:analysis_server/src/domain_analysis.dart'; |
15 import 'package:analysis_server/src/domain_completion.dart'; | 15 import 'package:analysis_server/src/domain_completion.dart'; |
16 import 'package:analysis_server/src/plugin/server_plugin.dart'; | 16 import 'package:analysis_server/src/plugin/server_plugin.dart'; |
17 import 'package:analysis_server/src/provisional/completion/completion_core.dart' | 17 import 'package:analysis_server/src/provisional/completion/completion_core.dart' |
18 show AnalysisRequest, CompletionRequest, CompletionResult; | 18 show AnalysisRequest, CompletionRequest, CompletionResult; |
19 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; | 19 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
20 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 20 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 21 import 'package:analysis_server/src/services/completion/dart/completion_manager.
dart'; |
21 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; | 22 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; |
22 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
23 import 'package:analysis_server/src/services/index/index.dart' show Index; | 23 import 'package:analysis_server/src/services/index/index.dart' show Index; |
24 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | 24 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
25 import 'package:analysis_server/src/services/search/search_engine.dart'; | 25 import 'package:analysis_server/src/services/search/search_engine.dart'; |
26 import 'package:analyzer/file_system/file_system.dart'; | 26 import 'package:analyzer/file_system/file_system.dart'; |
27 import 'package:analyzer/instrumentation/instrumentation.dart'; | 27 import 'package:analyzer/instrumentation/instrumentation.dart'; |
28 import 'package:analyzer/source/pub_package_map_provider.dart'; | 28 import 'package:analyzer/source/pub_package_map_provider.dart'; |
29 import 'package:analyzer/src/generated/engine.dart'; | 29 import 'package:analyzer/src/generated/engine.dart'; |
30 import 'package:analyzer/src/generated/sdk.dart'; | 30 import 'package:analyzer/src/generated/sdk.dart'; |
31 import 'package:analyzer/src/generated/source.dart'; | 31 import 'package:analyzer/src/generated/source.dart'; |
32 import 'package:plugin/manager.dart'; | 32 import 'package:plugin/manager.dart'; |
33 import 'package:plugin/plugin.dart'; | 33 import 'package:plugin/plugin.dart'; |
34 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 34 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
35 import 'package:unittest/unittest.dart'; | 35 import 'package:unittest/unittest.dart'; |
36 | 36 |
37 import 'analysis_abstract.dart'; | 37 import 'analysis_abstract.dart'; |
| 38 import 'domain_completion_util.dart'; |
38 import 'mock_sdk.dart'; | 39 import 'mock_sdk.dart'; |
39 import 'mocks.dart'; | 40 import 'mocks.dart'; |
40 import 'utils.dart'; | 41 import 'utils.dart'; |
41 | 42 |
42 main() { | 43 main() { |
43 initializeTestEnvironment(); | 44 initializeTestEnvironment(); |
44 defineReflectiveTests(CompletionManagerTest); | 45 defineReflectiveTests(CompletionManagerTest); |
45 defineReflectiveTests(CompletionTest); | 46 defineReflectiveTests(CompletionTest); |
46 defineReflectiveTests(_NoSearchEngine); | 47 defineReflectiveTests(_NoSearchEngine); |
47 } | 48 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 expect(completionDomain.manager, isNotNull); | 270 expect(completionDomain.manager, isNotNull); |
270 ChangeSet changeSet = new ChangeSet(); | 271 ChangeSet changeSet = new ChangeSet(); |
271 changeSet.removedSource(completionDomain.manager.source); | 272 changeSet.removedSource(completionDomain.manager.source); |
272 completionDomain.sourcesChanged(new SourcesChangedEvent(changeSet)); | 273 completionDomain.sourcesChanged(new SourcesChangedEvent(changeSet)); |
273 expect(completionDomain.manager, isNull); | 274 expect(completionDomain.manager, isNull); |
274 }); | 275 }); |
275 } | 276 } |
276 } | 277 } |
277 | 278 |
278 @reflectiveTest | 279 @reflectiveTest |
279 class CompletionTest extends AbstractAnalysisTest { | 280 class CompletionTest extends AbstractCompletionDomainTest { |
280 String completionId; | |
281 int completionOffset; | |
282 int replacementOffset; | |
283 int replacementLength; | |
284 List<CompletionSuggestion> suggestions = []; | |
285 bool suggestionsDone = false; | |
286 | |
287 String addTestFile(String content, {int offset}) { | |
288 completionOffset = content.indexOf('^'); | |
289 if (offset != null) { | |
290 expect(completionOffset, -1, reason: 'cannot supply offset and ^'); | |
291 completionOffset = offset; | |
292 return super.addTestFile(content); | |
293 } | |
294 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | |
295 int nextOffset = content.indexOf('^', completionOffset + 1); | |
296 expect(nextOffset, equals(-1), reason: 'too many ^'); | |
297 return super.addTestFile(content.substring(0, completionOffset) + | |
298 content.substring(completionOffset + 1)); | |
299 } | |
300 | |
301 void assertHasResult(CompletionSuggestionKind kind, String completion, | |
302 {int relevance: DART_RELEVANCE_DEFAULT, | |
303 bool isDeprecated: false, | |
304 bool isPotential: false, | |
305 int selectionOffset}) { | |
306 var cs; | |
307 suggestions.forEach((s) { | |
308 if (s.completion == completion) { | |
309 if (cs == null) { | |
310 cs = s; | |
311 } else { | |
312 fail('expected exactly one $completion but found > 1'); | |
313 } | |
314 } | |
315 }); | |
316 if (cs == null) { | |
317 var completions = suggestions.map((s) => s.completion).toList(); | |
318 fail('expected "$completion" but found\n $completions'); | |
319 } | |
320 expect(cs.kind, equals(kind)); | |
321 expect(cs.relevance, equals(relevance)); | |
322 expect(cs.selectionOffset, selectionOffset ?? completion.length); | |
323 expect(cs.selectionLength, equals(0)); | |
324 expect(cs.isDeprecated, equals(isDeprecated)); | |
325 expect(cs.isPotential, equals(isPotential)); | |
326 } | |
327 | |
328 void assertNoResult(String completion) { | |
329 if (suggestions.any((cs) => cs.completion == completion)) { | |
330 fail('did not expect completion: $completion'); | |
331 } | |
332 } | |
333 | |
334 void assertValidId(String id) { | |
335 expect(id, isNotNull); | |
336 expect(id.isNotEmpty, isTrue); | |
337 } | |
338 | |
339 @override | |
340 Index createIndex() { | |
341 return createLocalMemoryIndex(); | |
342 } | |
343 | |
344 Future getSuggestions() { | |
345 return waitForTasksFinished().then((_) { | |
346 Request request = | |
347 new CompletionGetSuggestionsParams(testFile, completionOffset) | |
348 .toRequest('0'); | |
349 Response response = handleSuccessfulRequest(request); | |
350 completionId = response.id; | |
351 assertValidId(completionId); | |
352 return pumpEventQueue().then((_) { | |
353 expect(suggestionsDone, isTrue); | |
354 }); | |
355 }); | |
356 } | |
357 | |
358 void processNotification(Notification notification) { | |
359 if (notification.event == COMPLETION_RESULTS) { | |
360 var params = new CompletionResultsParams.fromNotification(notification); | |
361 String id = params.id; | |
362 assertValidId(id); | |
363 if (id == completionId) { | |
364 expect(suggestionsDone, isFalse); | |
365 replacementOffset = params.replacementOffset; | |
366 replacementLength = params.replacementLength; | |
367 suggestionsDone = params.isLast; | |
368 expect(suggestionsDone, isNotNull); | |
369 suggestions = params.results; | |
370 } | |
371 } else if (notification.event == SERVER_ERROR) { | |
372 fail('server error: ${notification.toJson()}'); | |
373 } | |
374 } | |
375 | |
376 @override | |
377 void setUp() { | |
378 super.setUp(); | |
379 createProject(); | |
380 handler = new CompletionDomainHandler(server); | |
381 } | |
382 | |
383 test_html() { | 281 test_html() { |
384 testFile = '/project/web/test.html'; | 282 testFile = '/project/web/test.html'; |
385 addTestFile(''' | 283 addTestFile(''' |
386 <html>^</html> | 284 <html>^</html> |
387 '''); | 285 '''); |
388 return getSuggestions().then((_) { | 286 return getSuggestions().then((_) { |
389 expect(replacementOffset, equals(completionOffset)); | 287 expect(replacementOffset, equals(completionOffset)); |
390 expect(replacementLength, equals(0)); | 288 expect(replacementLength, equals(0)); |
391 expect(suggestions, hasLength(0)); | 289 expect(suggestions, hasLength(0)); |
392 }); | 290 }); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 test_invocation() { | 394 test_invocation() { |
497 addTestFile('class A {b() {}} main() {A a; a.^}'); | 395 addTestFile('class A {b() {}} main() {A a; a.^}'); |
498 return getSuggestions().then((_) { | 396 return getSuggestions().then((_) { |
499 expect(replacementOffset, equals(completionOffset)); | 397 expect(replacementOffset, equals(completionOffset)); |
500 expect(replacementLength, equals(0)); | 398 expect(replacementLength, equals(0)); |
501 assertHasResult(CompletionSuggestionKind.INVOCATION, 'b'); | 399 assertHasResult(CompletionSuggestionKind.INVOCATION, 'b'); |
502 }); | 400 }); |
503 } | 401 } |
504 | 402 |
505 test_invocation_sdk_relevancy_off() { | 403 test_invocation_sdk_relevancy_off() { |
506 var originalSorter = DartCompletionManager.defaultContributionSorter; | 404 var originalSorter = DartCompletionManager.contributionSorter; |
507 var mockSorter = new MockRelevancySorter(); | 405 var mockSorter = new MockRelevancySorter(); |
508 DartCompletionManager.defaultContributionSorter = mockSorter; | 406 DartCompletionManager.contributionSorter = mockSorter; |
509 addTestFile('main() {Map m; m.^}'); | 407 addTestFile('main() {Map m; m.^}'); |
510 return getSuggestions().then((_) { | 408 return getSuggestions().then((_) { |
511 // Assert that the CommonUsageComputer has been replaced | 409 // Assert that the CommonUsageComputer has been replaced |
512 expect(suggestions.any((s) => s.relevance == DART_RELEVANCE_COMMON_USAGE), | 410 expect(suggestions.any((s) => s.relevance == DART_RELEVANCE_COMMON_USAGE), |
513 isFalse); | 411 isFalse); |
514 DartCompletionManager.defaultContributionSorter = originalSorter; | 412 DartCompletionManager.contributionSorter = originalSorter; |
515 mockSorter.enabled = false; | 413 mockSorter.enabled = false; |
516 }); | 414 }); |
517 } | 415 } |
518 | 416 |
519 test_invocation_sdk_relevancy_on() { | 417 test_invocation_sdk_relevancy_on() { |
520 addTestFile('main() {Map m; m.^}'); | 418 addTestFile('main() {Map m; m.^}'); |
521 return getSuggestions().then((_) { | 419 return getSuggestions().then((_) { |
522 // Assert that the CommonUsageComputer is working | 420 // Assert that the CommonUsageComputer is working |
523 expect(suggestions.any((s) => s.relevance == DART_RELEVANCE_COMMON_USAGE), | 421 expect(suggestions.any((s) => s.relevance == DART_RELEVANCE_COMMON_USAGE), |
524 isTrue); | 422 isTrue); |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 } | 886 } |
989 '''); | 887 '''); |
990 await waitForTasksFinished(); | 888 await waitForTasksFinished(); |
991 Request request = | 889 Request request = |
992 new CompletionGetSuggestionsParams(testFile, 0).toRequest('0'); | 890 new CompletionGetSuggestionsParams(testFile, 0).toRequest('0'); |
993 Response response = handler.handleRequest(request); | 891 Response response = handler.handleRequest(request); |
994 expect(response.error, isNotNull); | 892 expect(response.error, isNotNull); |
995 expect(response.error.code, RequestErrorCode.NO_INDEX_GENERATED); | 893 expect(response.error.code, RequestErrorCode.NO_INDEX_GENERATED); |
996 } | 894 } |
997 } | 895 } |
OLD | NEW |