OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.domain.completion; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/domain_completion.dart'; |
| 12 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
| 13 import 'package:analysis_server/src/services/index/index.dart' show Index; |
| 14 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
| 15 import 'package:unittest/unittest.dart'; |
| 16 |
| 17 import 'analysis_abstract.dart'; |
| 18 import 'mocks.dart'; |
| 19 |
| 20 class AbstractCompletionDomainTest extends AbstractAnalysisTest { |
| 21 String completionId; |
| 22 int completionOffset; |
| 23 int replacementOffset; |
| 24 int replacementLength; |
| 25 List<CompletionSuggestion> suggestions = []; |
| 26 bool suggestionsDone = false; |
| 27 |
| 28 String addTestFile(String content, {int offset}) { |
| 29 completionOffset = content.indexOf('^'); |
| 30 if (offset != null) { |
| 31 expect(completionOffset, -1, reason: 'cannot supply offset and ^'); |
| 32 completionOffset = offset; |
| 33 return super.addTestFile(content); |
| 34 } |
| 35 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 36 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 37 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 38 return super.addTestFile(content.substring(0, completionOffset) + |
| 39 content.substring(completionOffset + 1)); |
| 40 } |
| 41 |
| 42 void assertHasResult(CompletionSuggestionKind kind, String completion, |
| 43 {int relevance: DART_RELEVANCE_DEFAULT, |
| 44 bool isDeprecated: false, |
| 45 bool isPotential: false, |
| 46 int selectionOffset}) { |
| 47 var cs; |
| 48 suggestions.forEach((s) { |
| 49 if (s.completion == completion) { |
| 50 if (cs == null) { |
| 51 cs = s; |
| 52 } else { |
| 53 fail('expected exactly one $completion but found > 1'); |
| 54 } |
| 55 } |
| 56 }); |
| 57 if (cs == null) { |
| 58 var completions = suggestions.map((s) => s.completion).toList(); |
| 59 fail('expected "$completion" but found\n $completions'); |
| 60 } |
| 61 expect(cs.kind, equals(kind)); |
| 62 expect(cs.relevance, equals(relevance)); |
| 63 expect(cs.selectionOffset, selectionOffset ?? completion.length); |
| 64 expect(cs.selectionLength, equals(0)); |
| 65 expect(cs.isDeprecated, equals(isDeprecated)); |
| 66 expect(cs.isPotential, equals(isPotential)); |
| 67 } |
| 68 |
| 69 void assertNoResult(String completion) { |
| 70 if (suggestions.any((cs) => cs.completion == completion)) { |
| 71 fail('did not expect completion: $completion'); |
| 72 } |
| 73 } |
| 74 |
| 75 void assertValidId(String id) { |
| 76 expect(id, isNotNull); |
| 77 expect(id.isNotEmpty, isTrue); |
| 78 } |
| 79 |
| 80 @override |
| 81 Index createIndex() { |
| 82 return createLocalMemoryIndex(); |
| 83 } |
| 84 |
| 85 Future getSuggestions() { |
| 86 return waitForTasksFinished().then((_) { |
| 87 Request request = |
| 88 new CompletionGetSuggestionsParams(testFile, completionOffset) |
| 89 .toRequest('0'); |
| 90 Response response = handleSuccessfulRequest(request); |
| 91 completionId = response.id; |
| 92 assertValidId(completionId); |
| 93 return pumpEventQueue().then((_) { |
| 94 expect(suggestionsDone, isTrue); |
| 95 }); |
| 96 }); |
| 97 } |
| 98 |
| 99 void processNotification(Notification notification) { |
| 100 if (notification.event == COMPLETION_RESULTS) { |
| 101 var params = new CompletionResultsParams.fromNotification(notification); |
| 102 String id = params.id; |
| 103 assertValidId(id); |
| 104 if (id == completionId) { |
| 105 expect(suggestionsDone, isFalse); |
| 106 replacementOffset = params.replacementOffset; |
| 107 replacementLength = params.replacementLength; |
| 108 suggestionsDone = params.isLast; |
| 109 expect(suggestionsDone, isNotNull); |
| 110 suggestions = params.results; |
| 111 } |
| 112 } else if (notification.event == SERVER_ERROR) { |
| 113 fail('server error: ${notification.toJson()}'); |
| 114 } |
| 115 } |
| 116 |
| 117 @override |
| 118 void setUp() { |
| 119 super.setUp(); |
| 120 createProject(); |
| 121 handler = new CompletionDomainHandler(server); |
| 122 } |
| 123 } |
OLD | NEW |