| 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_services/completion/completion_computer.dart'; | |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 9 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 11 import 'package:analysis_services/index/index.dart'; | 10 import 'package:analysis_services/index/index.dart'; |
| 12 import 'package:analysis_services/index/local_memory_index.dart'; | 11 import 'package:analysis_services/index/local_memory_index.dart'; |
| 12 import 'package:analysis_services/src/completion/dart_completion_manager.dart'; |
| 13 import 'package:analysis_services/src/search/search_engine.dart'; | 13 import 'package:analysis_services/src/search/search_engine.dart'; |
| 14 import 'package:analysis_testing/abstract_context.dart'; | 14 import 'package:analysis_testing/abstract_context.dart'; |
| 15 import 'package:analysis_testing/mock_sdk.dart'; | 15 import 'package:analysis_testing/mock_sdk.dart'; |
| 16 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
| 17 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:unittest/unittest.dart'; | 20 import 'package:unittest/unittest.dart'; |
| 21 | 21 |
| 22 class AbstractCompletionTest extends AbstractContextTest { | 22 class AbstractCompletionTest extends AbstractContextTest { |
| 23 Index index; | 23 Index index; |
| 24 SearchEngineImpl searchEngine; | 24 SearchEngineImpl searchEngine; |
| 25 CompletionComputer computer; | 25 DartCompletionComputer computer; |
| 26 String testFile = '/completionTest.dart'; | 26 String testFile = '/completionTest.dart'; |
| 27 Source testSource; | 27 Source testSource; |
| 28 int completionOffset; | 28 int completionOffset; |
| 29 List<CompletionSuggestion> suggestions; | |
| 30 bool _computeFastCalled = false; | 29 bool _computeFastCalled = false; |
| 30 DartCompletionRequest request; |
| 31 | 31 |
| 32 void addResolvedUnit(String file, String code) { | 32 void addResolvedUnit(String file, String code) { |
| 33 Source source = addSource(file, code); | 33 Source source = addSource(file, code); |
| 34 CompilationUnit unit = resolveLibraryUnit(source); | 34 CompilationUnit unit = resolveLibraryUnit(source); |
| 35 index.indexUnit(context, unit); | 35 index.indexUnit(context, unit); |
| 36 } | 36 } |
| 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( |
| 48 context, |
| 49 searchEngine, |
| 50 testSource, |
| 51 completionOffset); |
| 47 } | 52 } |
| 48 | 53 |
| 49 void assertNotSuggested(String completion) { | 54 void assertNotSuggested(String completion) { |
| 50 if (suggestions.any((cs) => cs.completion == completion)) { | 55 if (request.suggestions.any((cs) => cs.completion == completion)) { |
| 51 fail('did not expect completion: $completion'); | 56 fail('did not expect completion: $completion'); |
| 52 } | 57 } |
| 53 } | 58 } |
| 54 | 59 |
| 55 void assertSuggest(CompletionSuggestionKind kind, String completion, | 60 void assertSuggest(CompletionSuggestionKind kind, String completion, |
| 56 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, bool isDepre
cated | 61 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, bool isDepre
cated |
| 57 = false, bool isPotential = false]) { | 62 = false, bool isPotential = false]) { |
| 58 var cs; | 63 CompletionSuggestion cs; |
| 59 suggestions.forEach((s) { | 64 request.suggestions.forEach((s) { |
| 60 if (s.completion == completion) { | 65 if (s.completion == completion) { |
| 61 if (cs == null) { | 66 if (cs == null) { |
| 62 cs = s; | 67 cs = s; |
| 63 } else { | 68 } else { |
| 64 var match = | 69 List<CompletionSuggestion> matchSuggestions = |
| 65 suggestions.where((s) => s.completion == completion).toList(); | 70 request.suggestions.where((s) => s.completion == completion).toLis
t(); |
| 66 fail('expected exactly one $completion but found > 1\n $match'); | 71 fail( |
| 72 'expected exactly one $completion but found > 1\n $matchSuggestion
s'); |
| 67 } | 73 } |
| 68 } | 74 } |
| 69 }); | 75 }); |
| 70 if (cs == null) { | 76 if (cs == null) { |
| 71 var completions = suggestions.map((s) => s.completion).toList(); | 77 List<CompletionSuggestion> completions = |
| 78 request.suggestions.map((s) => s.completion).toList(); |
| 72 fail('expected "$completion" but found\n $completions'); | 79 fail('expected "$completion" but found\n $completions'); |
| 73 } | 80 } |
| 74 expect(cs.kind, equals(kind)); | 81 expect(cs.kind, equals(kind)); |
| 75 expect(cs.relevance, equals(relevance)); | 82 expect(cs.relevance, equals(relevance)); |
| 76 expect(cs.selectionOffset, equals(completion.length)); | 83 expect(cs.selectionOffset, equals(completion.length)); |
| 77 expect(cs.selectionLength, equals(0)); | 84 expect(cs.selectionLength, equals(0)); |
| 78 expect(cs.isDeprecated, equals(isDeprecated)); | 85 expect(cs.isDeprecated, equals(isDeprecated)); |
| 79 expect(cs.isPotential, equals(isPotential)); | 86 expect(cs.isPotential, equals(isPotential)); |
| 80 } | 87 } |
| 81 | 88 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 100 } | 107 } |
| 101 | 108 |
| 102 void assertSuggestLibraryPrefix(String completion, | 109 void assertSuggestLibraryPrefix(String completion, |
| 103 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | 110 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 104 assertSuggest( | 111 assertSuggest( |
| 105 CompletionSuggestionKind.LIBRARY_PREFIX, | 112 CompletionSuggestionKind.LIBRARY_PREFIX, |
| 106 completion, | 113 completion, |
| 107 relevance); | 114 relevance); |
| 108 } | 115 } |
| 109 | 116 |
| 117 void assertSuggestLocalVariable(String completion, |
| 118 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 119 assertSuggest( |
| 120 CompletionSuggestionKind.LOCAL_VARIABLE, |
| 121 completion, |
| 122 relevance); |
| 123 } |
| 124 |
| 110 void assertSuggestMethod(String className, [CompletionRelevance relevance = | 125 void assertSuggestMethod(String className, [CompletionRelevance relevance = |
| 111 CompletionRelevance.DEFAULT]) { | 126 CompletionRelevance.DEFAULT]) { |
| 112 assertSuggest(CompletionSuggestionKind.METHOD, className, relevance); | 127 assertSuggest(CompletionSuggestionKind.METHOD, className, relevance); |
| 113 } | 128 } |
| 114 | 129 |
| 115 void assertSuggestMethodName(String completion, [CompletionRelevance relevance | 130 void assertSuggestMethodName(String completion, [CompletionRelevance relevance |
| 116 = CompletionRelevance.DEFAULT]) { | 131 = CompletionRelevance.DEFAULT]) { |
| 117 assertSuggest(CompletionSuggestionKind.METHOD_NAME, completion, relevance); | 132 assertSuggest(CompletionSuggestionKind.METHOD_NAME, completion, relevance); |
| 118 } | 133 } |
| 119 | 134 |
| 120 void assertSuggestParameter(String completion, [CompletionRelevance relevance | 135 void assertSuggestParameter(String completion, [CompletionRelevance relevance |
| 121 = CompletionRelevance.DEFAULT]) { | 136 = CompletionRelevance.DEFAULT]) { |
| 122 assertSuggest(CompletionSuggestionKind.PARAMETER, completion, relevance); | 137 assertSuggest(CompletionSuggestionKind.PARAMETER, completion, relevance); |
| 123 } | 138 } |
| 124 | 139 |
| 125 void assertSuggestSetter(String className, [CompletionRelevance relevance = | 140 void assertSuggestSetter(String className, [CompletionRelevance relevance = |
| 126 CompletionRelevance.DEFAULT]) { | 141 CompletionRelevance.DEFAULT]) { |
| 127 assertSuggest(CompletionSuggestionKind.SETTER, className, relevance); | 142 assertSuggest(CompletionSuggestionKind.SETTER, className, relevance); |
| 128 } | 143 } |
| 129 | 144 |
| 130 void assertSuggestTopLevelVar(String completion, | 145 void assertSuggestTopLevelVar(String completion, |
| 131 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | 146 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { |
| 132 assertSuggest( | 147 assertSuggest( |
| 133 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, | 148 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, |
| 134 completion, | 149 completion, |
| 135 relevance); | 150 relevance); |
| 136 } | 151 } |
| 137 | 152 |
| 138 void assertSuggestVariable(String completion, [CompletionRelevance relevance = | |
| 139 CompletionRelevance.DEFAULT]) { | |
| 140 assertSuggest( | |
| 141 CompletionSuggestionKind.LOCAL_VARIABLE, | |
| 142 completion, | |
| 143 relevance); | |
| 144 } | |
| 145 | |
| 146 bool computeFast() { | 153 bool computeFast() { |
| 147 _computeFastCalled = true; | 154 _computeFastCalled = true; |
| 148 computer | |
| 149 ..context = context | |
| 150 ..searchEngine = searchEngine | |
| 151 ..source = testSource | |
| 152 ..offset = completionOffset; | |
| 153 suggestions = []; | |
| 154 CompilationUnit unit = context.parseCompilationUnit(testSource); | 155 CompilationUnit unit = context.parseCompilationUnit(testSource); |
| 155 AstNode node = new NodeLocator.con1(completionOffset).searchWithin(unit); | 156 request.unit = unit; |
| 156 return computer.computeFast(unit, node, suggestions); | 157 request.node = new NodeLocator.con1(completionOffset).searchWithin(unit); |
| 158 return computer.computeFast(request); |
| 157 } | 159 } |
| 158 | 160 |
| 159 Future<bool> computeFull() { | 161 Future<bool> computeFull([bool fullAnalysis = false]) { |
| 160 if (!_computeFastCalled) { | 162 if (!_computeFastCalled) { |
| 161 expect(computeFast(), isFalse); | 163 expect(computeFast(), isFalse); |
| 162 } | 164 } |
| 163 var result = context.performAnalysisTask(); | 165 var result = context.performAnalysisTask(); |
| 166 bool resolved = false; |
| 164 while (result.hasMoreWork) { | 167 while (result.hasMoreWork) { |
| 168 |
| 169 // Update the index |
| 165 result.changeNotices.forEach((ChangeNotice notice) { | 170 result.changeNotices.forEach((ChangeNotice notice) { |
| 166 CompilationUnit unit = notice.compilationUnit; | 171 CompilationUnit unit = notice.compilationUnit; |
| 167 if (unit != null) { | 172 if (unit != null) { |
| 168 index.indexUnit(context, unit); | 173 index.indexUnit(context, unit); |
| 169 } | 174 } |
| 170 }); | 175 }); |
| 176 |
| 177 // If the unit has been resolved, then finish the completion |
| 178 LibraryElement library = context.getLibraryElement(testSource); |
| 179 if (library != null) { |
| 180 CompilationUnit unit = |
| 181 context.getResolvedCompilationUnit(testSource, library); |
| 182 if (unit != null) { |
| 183 request.unit = unit; |
| 184 request.node = new NodeLocator.con1( |
| 185 completionOffset).searchWithin(unit); |
| 186 resolved = true; |
| 187 if (!fullAnalysis) { |
| 188 break; |
| 189 } |
| 190 } |
| 191 } |
| 192 |
| 171 result = context.performAnalysisTask(); | 193 result = context.performAnalysisTask(); |
| 172 } | 194 } |
| 173 LibraryElement library = context.getLibraryElement(testSource); | 195 if (!resolved) { |
| 174 expect(library, isNotNull); | 196 fail('expected unit to be resolved'); |
| 175 var unit = context.getResolvedCompilationUnit(testSource, library); | 197 } |
| 176 expect(unit, isNotNull); | 198 return computer.computeFull(request); |
| 177 AstNode node = new NodeLocator.con1(completionOffset).searchWithin(unit); | |
| 178 return computer.computeFull(unit, node, suggestions); | |
| 179 } | 199 } |
| 180 | 200 |
| 181 @override | 201 @override |
| 182 void setUp() { | 202 void setUp() { |
| 183 super.setUp(); | 203 super.setUp(); |
| 184 index = createLocalMemoryIndex(); | 204 index = createLocalMemoryIndex(); |
| 185 searchEngine = new SearchEngineImpl(index); | 205 searchEngine = new SearchEngineImpl(index); |
| 186 addResolvedUnit(MockSdk.LIB_CORE.path, MockSdk.LIB_CORE.content); | 206 addResolvedUnit(MockSdk.LIB_CORE.path, MockSdk.LIB_CORE.content); |
| 187 } | 207 } |
| 188 } | 208 } |
| OLD | NEW |