| 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.completion.util; | 5 library test.services.completion.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 void addTestSource(String content) { | 38 void addTestSource(String content) { |
| 39 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); | 39 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); |
| 40 completionOffset = content.indexOf('^'); | 40 completionOffset = content.indexOf('^'); |
| 41 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | 41 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 42 int nextOffset = content.indexOf('^', completionOffset + 1); | 42 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 43 expect(nextOffset, equals(-1), reason: 'too many ^'); | 43 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 44 content = content.substring(0, completionOffset) + | 44 content = content.substring(0, completionOffset) + |
| 45 content.substring(completionOffset + 1); | 45 content.substring(completionOffset + 1); |
| 46 testSource = addSource(testFile, content); | 46 testSource = addSource(testFile, content); |
| 47 request = new DartCompletionRequest( | 47 request = |
| 48 context, | 48 new DartCompletionRequest(context, searchEngine, testSource, completionO
ffset); |
| 49 searchEngine, | |
| 50 testSource, | |
| 51 completionOffset); | |
| 52 } | 49 } |
| 53 | 50 |
| 54 void assertNotSuggested(String completion) { | 51 void assertNotSuggested(String completion) { |
| 55 if (request.suggestions.any((cs) => cs.completion == completion)) { | 52 if (request.suggestions.any((cs) => cs.completion == completion)) { |
| 56 fail('did not expect completion: $completion'); | 53 fail('did not expect completion: $completion'); |
| 57 } | 54 } |
| 58 } | 55 } |
| 59 | 56 |
| 60 void assertSuggest(CompletionSuggestionKind kind, String completion, | 57 CompletionSuggestion assertSuggest(CompletionSuggestionKind kind, |
| 61 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, bool isDepre
cated | 58 String completion, [CompletionRelevance relevance = CompletionRelevance.DE
FAULT, |
| 62 = false, bool isPotential = false]) { | 59 bool isDeprecated = false, bool isPotential = false]) { |
| 63 CompletionSuggestion cs; | 60 CompletionSuggestion cs; |
| 64 request.suggestions.forEach((s) { | 61 request.suggestions.forEach((s) { |
| 65 if (s.completion == completion) { | 62 if (s.completion == completion) { |
| 66 if (cs == null) { | 63 if (cs == null) { |
| 67 cs = s; | 64 cs = s; |
| 68 } else { | 65 } else { |
| 69 List<CompletionSuggestion> matchSuggestions = | 66 List<CompletionSuggestion> matchSuggestions = |
| 70 request.suggestions.where((s) => s.completion == completion).toLis
t(); | 67 request.suggestions.where((s) => s.completion == completion).toLis
t(); |
| 71 fail( | 68 fail( |
| 72 'expected exactly one $completion but found > 1\n $matchSuggestion
s'); | 69 'expected exactly one $completion but found > 1\n $matchSuggestion
s'); |
| 73 } | 70 } |
| 74 } | 71 } |
| 75 }); | 72 }); |
| 76 if (cs == null) { | 73 if (cs == null) { |
| 77 List<CompletionSuggestion> completions = | 74 List<CompletionSuggestion> completions = |
| 78 request.suggestions.map((s) => s.completion).toList(); | 75 request.suggestions.map((s) => s.completion).toList(); |
| 79 fail('expected "$completion" but found\n $completions'); | 76 fail('expected "$completion" but found\n $completions'); |
| 80 } | 77 } |
| 81 expect(cs.kind, equals(kind)); | 78 expect(cs.kind, equals(kind)); |
| 82 expect(cs.relevance, equals(relevance)); | 79 expect(cs.relevance, equals(relevance)); |
| 83 expect(cs.selectionOffset, equals(completion.length)); | 80 expect(cs.selectionOffset, equals(completion.length)); |
| 84 expect(cs.selectionLength, equals(0)); | 81 expect(cs.selectionLength, equals(0)); |
| 85 expect(cs.isDeprecated, equals(isDeprecated)); | 82 expect(cs.isDeprecated, equals(isDeprecated)); |
| 86 expect(cs.isPotential, equals(isPotential)); | 83 expect(cs.isPotential, equals(isPotential)); |
| 84 return cs; |
| 87 } | 85 } |
| 88 | 86 |
| 89 void assertSuggestClass(String className, [CompletionRelevance relevance = | 87 void assertSuggestClass(String className, [CompletionRelevance relevance = |
| 90 CompletionRelevance.DEFAULT]) { | 88 CompletionRelevance.DEFAULT]) { |
| 91 assertSuggest(CompletionSuggestionKind.CLASS, className, relevance); | 89 assertSuggest(CompletionSuggestionKind.CLASS, className, relevance); |
| 92 } | 90 } |
| 93 | 91 |
| 94 void assertSuggestField(String completion, [CompletionRelevance relevance = | 92 void assertSuggestField(String completion, String declaringType, String type, |
| 95 CompletionRelevance.DEFAULT]) { | 93 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 96 assertSuggest(CompletionSuggestionKind.FIELD, completion, relevance); | 94 CompletionSuggestion cs = |
| 95 assertSuggest(CompletionSuggestionKind.FIELD, completion, relevance); |
| 96 expect(cs.declaringType, equals(declaringType)); |
| 97 expect(cs.returnType, equals(type)); |
| 97 } | 98 } |
| 98 | 99 |
| 99 void assertSuggestFunction(String completion, [CompletionRelevance relevance = | 100 void assertSuggestFunction(String completion, String returnType, |
| 100 CompletionRelevance.DEFAULT]) { | 101 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 101 assertSuggest(CompletionSuggestionKind.FUNCTION, completion, relevance); | 102 CompletionSuggestion cs = |
| 103 assertSuggest(CompletionSuggestionKind.FUNCTION, completion, relevance); |
| 104 expect(cs.returnType, equals(returnType)); |
| 102 } | 105 } |
| 103 | 106 |
| 104 void assertSuggestGetter(String className, [CompletionRelevance relevance = | 107 void assertSuggestGetter(String className, String returnType, |
| 105 CompletionRelevance.DEFAULT]) { | 108 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 106 assertSuggest(CompletionSuggestionKind.GETTER, className, relevance); | 109 CompletionSuggestion cs = |
| 110 assertSuggest(CompletionSuggestionKind.GETTER, className, relevance); |
| 111 expect(cs.returnType, equals(returnType)); |
| 107 } | 112 } |
| 108 | 113 |
| 109 void assertSuggestLibraryPrefix(String completion, | 114 void assertSuggestLibraryPrefix(String completion, |
| 110 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | 115 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 111 assertSuggest( | 116 assertSuggest( |
| 112 CompletionSuggestionKind.LIBRARY_PREFIX, | 117 CompletionSuggestionKind.LIBRARY_PREFIX, |
| 113 completion, | 118 completion, |
| 114 relevance); | 119 relevance); |
| 115 } | 120 } |
| 116 | 121 |
| 117 void assertSuggestLocalVariable(String completion, | 122 void assertSuggestLocalVariable(String completion, String returnType, |
| 118 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | 123 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 119 assertSuggest( | 124 CompletionSuggestion cs = |
| 120 CompletionSuggestionKind.LOCAL_VARIABLE, | 125 assertSuggest(CompletionSuggestionKind.LOCAL_VARIABLE, completion, relev
ance); |
| 121 completion, | 126 expect(cs.returnType, equals(returnType)); |
| 122 relevance); | |
| 123 } | 127 } |
| 124 | 128 |
| 125 void assertSuggestMethod(String className, [CompletionRelevance relevance = | 129 void assertSuggestMethod(String className, String declaringType, |
| 130 String returnType, [CompletionRelevance relevance = |
| 126 CompletionRelevance.DEFAULT]) { | 131 CompletionRelevance.DEFAULT]) { |
| 127 assertSuggest(CompletionSuggestionKind.METHOD, className, relevance); | 132 CompletionSuggestion cs = |
| 133 assertSuggest(CompletionSuggestionKind.METHOD, className, relevance); |
| 134 expect(cs.declaringType, equals(declaringType)); |
| 135 expect(cs.returnType, equals(returnType)); |
| 128 } | 136 } |
| 129 | 137 |
| 130 void assertSuggestMethodName(String completion, [CompletionRelevance relevance | 138 void assertSuggestMethodName(String completion, String declaringType, |
| 131 = CompletionRelevance.DEFAULT]) { | 139 String returnType, [CompletionRelevance relevance = |
| 132 assertSuggest(CompletionSuggestionKind.METHOD_NAME, completion, relevance); | 140 CompletionRelevance.DEFAULT]) { |
| 141 CompletionSuggestion cs = |
| 142 assertSuggest(CompletionSuggestionKind.METHOD_NAME, completion, relevanc
e); |
| 143 expect(cs.declaringType, equals(declaringType)); |
| 144 expect(cs.returnType, equals(returnType)); |
| 133 } | 145 } |
| 134 | 146 |
| 135 void assertSuggestParameter(String completion, [CompletionRelevance relevance | 147 void assertSuggestParameter(String completion, String returnType, |
| 136 = CompletionRelevance.DEFAULT]) { | 148 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 137 assertSuggest(CompletionSuggestionKind.PARAMETER, completion, relevance); | 149 assertSuggest(CompletionSuggestionKind.PARAMETER, completion, relevance); |
| 138 } | 150 } |
| 139 | 151 |
| 140 void assertSuggestSetter(String className, [CompletionRelevance relevance = | 152 void assertSuggestSetter(String className, [CompletionRelevance relevance = |
| 141 CompletionRelevance.DEFAULT]) { | 153 CompletionRelevance.DEFAULT]) { |
| 142 assertSuggest(CompletionSuggestionKind.SETTER, className, relevance); | 154 assertSuggest(CompletionSuggestionKind.SETTER, className, relevance); |
| 143 } | 155 } |
| 144 | 156 |
| 145 void assertSuggestTopLevelVar(String completion, | 157 void assertSuggestTopLevelVar(String completion, String returnType, |
| 146 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | 158 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 147 assertSuggest( | 159 CompletionSuggestion cs = assertSuggest( |
| 148 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, | 160 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, |
| 149 completion, | 161 completion, |
| 150 relevance); | 162 relevance); |
| 163 expect(cs.returnType, equals(returnType)); |
| 151 } | 164 } |
| 152 | 165 |
| 153 bool computeFast() { | 166 bool computeFast() { |
| 154 _computeFastCalled = true; | 167 _computeFastCalled = true; |
| 155 CompilationUnit unit = context.parseCompilationUnit(testSource); | 168 CompilationUnit unit = context.parseCompilationUnit(testSource); |
| 156 request.unit = unit; | 169 request.unit = unit; |
| 157 request.node = new NodeLocator.con1(completionOffset).searchWithin(unit); | 170 request.node = new NodeLocator.con1(completionOffset).searchWithin(unit); |
| 158 return computer.computeFast(request); | 171 return computer.computeFast(request); |
| 159 } | 172 } |
| 160 | 173 |
| 161 Future<bool> computeFull([bool fullAnalysis = false]) { | 174 Future<bool> computeFull([bool fullAnalysis = false]) { |
| 162 if (!_computeFastCalled) { | 175 if (!_computeFastCalled) { |
| 163 expect(computeFast(), isFalse); | 176 expect(computeFast(), isFalse); |
| 164 } | 177 } |
| 165 | 178 |
| 166 // Index SDK | 179 // Index SDK |
| 167 for (Source librarySource in context.librarySources) { | 180 for (Source librarySource in context.librarySources) { |
| 168 CompilationUnit unit = context.getResolvedCompilationUnit2(librarySource,
librarySource); | 181 CompilationUnit unit = |
| 182 context.getResolvedCompilationUnit2(librarySource, librarySource); |
| 169 if (unit != null) { | 183 if (unit != null) { |
| 170 index.indexUnit(context, unit); | 184 index.indexUnit(context, unit); |
| 171 } | 185 } |
| 172 } | 186 } |
| 173 | 187 |
| 174 var result = context.performAnalysisTask(); | 188 var result = context.performAnalysisTask(); |
| 175 bool resolved = false; | 189 bool resolved = false; |
| 176 while (result.hasMoreWork) { | 190 while (result.hasMoreWork) { |
| 177 | 191 |
| 178 // Update the index | 192 // Update the index |
| 179 result.changeNotices.forEach((ChangeNotice notice) { | 193 result.changeNotices.forEach((ChangeNotice notice) { |
| 180 CompilationUnit unit = notice.compilationUnit; | 194 CompilationUnit unit = notice.compilationUnit; |
| 181 if (unit != null) { | 195 if (unit != null) { |
| 182 index.indexUnit(context, unit); | 196 index.indexUnit(context, unit); |
| 183 } | 197 } |
| 184 }); | 198 }); |
| 185 | 199 |
| 186 // If the unit has been resolved, then finish the completion | 200 // If the unit has been resolved, then finish the completion |
| 187 LibraryElement library = context.getLibraryElement(testSource); | 201 LibraryElement library = context.getLibraryElement(testSource); |
| 188 if (library != null) { | 202 if (library != null) { |
| 189 CompilationUnit unit = | 203 CompilationUnit unit = |
| 190 context.getResolvedCompilationUnit(testSource, library); | 204 context.getResolvedCompilationUnit(testSource, library); |
| 191 if (unit != null) { | 205 if (unit != null) { |
| 192 request.unit = unit; | 206 request.unit = unit; |
| 193 request.node = new NodeLocator.con1( | 207 request.node = |
| 194 completionOffset).searchWithin(unit); | 208 new NodeLocator.con1(completionOffset).searchWithin(unit); |
| 195 resolved = true; | 209 resolved = true; |
| 196 if (!fullAnalysis) { | 210 if (!fullAnalysis) { |
| 197 break; | 211 break; |
| 198 } | 212 } |
| 199 } | 213 } |
| 200 } | 214 } |
| 201 | 215 |
| 202 result = context.performAnalysisTask(); | 216 result = context.performAnalysisTask(); |
| 203 } | 217 } |
| 204 if (!resolved) { | 218 if (!resolved) { |
| 205 fail('expected unit to be resolved'); | 219 fail('expected unit to be resolved'); |
| 206 } | 220 } |
| 207 return computer.computeFull(request); | 221 return computer.computeFull(request); |
| 208 } | 222 } |
| 209 | 223 |
| 210 @override | 224 @override |
| 211 void setUp() { | 225 void setUp() { |
| 212 super.setUp(); | 226 super.setUp(); |
| 213 index = createLocalMemoryIndex(); | 227 index = createLocalMemoryIndex(); |
| 214 searchEngine = new SearchEngineImpl(index); | 228 searchEngine = new SearchEngineImpl(index); |
| 215 } | 229 } |
| 216 } | 230 } |
| OLD | NEW |