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.services.completion.util; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analysis_services/completion/completion_suggestion.dart'; | |
10 import 'package:analysis_services/index/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'; | |
14 import 'package:analysis_testing/abstract_context.dart'; | |
15 import 'package:analysis_testing/mock_sdk.dart'; | |
16 import 'package:analyzer/src/generated/ast.dart'; | |
17 import 'package:analyzer/src/generated/element.dart'; | |
18 import 'package:analyzer/src/generated/engine.dart'; | |
19 import 'package:analyzer/src/generated/source.dart'; | |
20 import 'package:unittest/unittest.dart'; | |
21 | |
22 class AbstractCompletionTest extends AbstractContextTest { | |
23 Index index; | |
24 SearchEngineImpl searchEngine; | |
25 DartCompletionComputer computer; | |
26 String testFile = '/completionTest.dart'; | |
27 Source testSource; | |
28 int completionOffset; | |
29 bool _computeFastCalled = false; | |
30 DartCompletionRequest request; | |
31 | |
32 void addResolvedUnit(String file, String code) { | |
33 Source source = addSource(file, code); | |
34 CompilationUnit unit = resolveLibraryUnit(source); | |
35 index.indexUnit(context, unit); | |
36 } | |
37 | |
38 void addTestSource(String content) { | |
39 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); | |
40 completionOffset = content.indexOf('^'); | |
41 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | |
42 int nextOffset = content.indexOf('^', completionOffset + 1); | |
43 expect(nextOffset, equals(-1), reason: 'too many ^'); | |
44 content = content.substring(0, completionOffset) + | |
45 content.substring(completionOffset + 1); | |
46 testSource = addSource(testFile, content); | |
47 request = new DartCompletionRequest( | |
48 context, | |
49 searchEngine, | |
50 testSource, | |
51 completionOffset); | |
52 } | |
53 | |
54 void assertNotSuggested(String completion) { | |
55 if (request.suggestions.any((cs) => cs.completion == completion)) { | |
56 fail('did not expect completion: $completion'); | |
57 } | |
58 } | |
59 | |
60 void assertSuggest(CompletionSuggestionKind kind, String completion, | |
61 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, bool isDepre
cated | |
62 = false, bool isPotential = false]) { | |
63 CompletionSuggestion cs; | |
64 request.suggestions.forEach((s) { | |
65 if (s.completion == completion) { | |
66 if (cs == null) { | |
67 cs = s; | |
68 } else { | |
69 List<CompletionSuggestion> matchSuggestions = | |
70 request.suggestions.where((s) => s.completion == completion).toLis
t(); | |
71 fail( | |
72 'expected exactly one $completion but found > 1\n $matchSuggestion
s'); | |
73 } | |
74 } | |
75 }); | |
76 if (cs == null) { | |
77 List<CompletionSuggestion> completions = | |
78 request.suggestions.map((s) => s.completion).toList(); | |
79 fail('expected "$completion" but found\n $completions'); | |
80 } | |
81 expect(cs.kind, equals(kind)); | |
82 expect(cs.relevance, equals(relevance)); | |
83 expect(cs.selectionOffset, equals(completion.length)); | |
84 expect(cs.selectionLength, equals(0)); | |
85 expect(cs.isDeprecated, equals(isDeprecated)); | |
86 expect(cs.isPotential, equals(isPotential)); | |
87 } | |
88 | |
89 void assertSuggestClass(String className, [CompletionRelevance relevance = | |
90 CompletionRelevance.DEFAULT]) { | |
91 assertSuggest(CompletionSuggestionKind.CLASS, className, relevance); | |
92 } | |
93 | |
94 void assertSuggestField(String completion, [CompletionRelevance relevance = | |
95 CompletionRelevance.DEFAULT]) { | |
96 assertSuggest(CompletionSuggestionKind.FIELD, completion, relevance); | |
97 } | |
98 | |
99 void assertSuggestFunction(String completion, [CompletionRelevance relevance = | |
100 CompletionRelevance.DEFAULT]) { | |
101 assertSuggest(CompletionSuggestionKind.FUNCTION, completion, relevance); | |
102 } | |
103 | |
104 void assertSuggestGetter(String className, [CompletionRelevance relevance = | |
105 CompletionRelevance.DEFAULT]) { | |
106 assertSuggest(CompletionSuggestionKind.GETTER, className, relevance); | |
107 } | |
108 | |
109 void assertSuggestLibraryPrefix(String completion, | |
110 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | |
111 assertSuggest( | |
112 CompletionSuggestionKind.LIBRARY_PREFIX, | |
113 completion, | |
114 relevance); | |
115 } | |
116 | |
117 void assertSuggestLocalVariable(String completion, | |
118 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | |
119 assertSuggest( | |
120 CompletionSuggestionKind.LOCAL_VARIABLE, | |
121 completion, | |
122 relevance); | |
123 } | |
124 | |
125 void assertSuggestMethod(String className, [CompletionRelevance relevance = | |
126 CompletionRelevance.DEFAULT]) { | |
127 assertSuggest(CompletionSuggestionKind.METHOD, className, relevance); | |
128 } | |
129 | |
130 void assertSuggestMethodName(String completion, [CompletionRelevance relevance | |
131 = CompletionRelevance.DEFAULT]) { | |
132 assertSuggest(CompletionSuggestionKind.METHOD_NAME, completion, relevance); | |
133 } | |
134 | |
135 void assertSuggestParameter(String completion, [CompletionRelevance relevance | |
136 = CompletionRelevance.DEFAULT]) { | |
137 assertSuggest(CompletionSuggestionKind.PARAMETER, completion, relevance); | |
138 } | |
139 | |
140 void assertSuggestSetter(String className, [CompletionRelevance relevance = | |
141 CompletionRelevance.DEFAULT]) { | |
142 assertSuggest(CompletionSuggestionKind.SETTER, className, relevance); | |
143 } | |
144 | |
145 void assertSuggestTopLevelVar(String completion, | |
146 [CompletionRelevance relevance = CompletionRelevance.DEFAULT]) { | |
147 assertSuggest( | |
148 CompletionSuggestionKind.TOP_LEVEL_VARIABLE, | |
149 completion, | |
150 relevance); | |
151 } | |
152 | |
153 bool computeFast() { | |
154 _computeFastCalled = true; | |
155 CompilationUnit unit = context.parseCompilationUnit(testSource); | |
156 request.unit = unit; | |
157 request.node = new NodeLocator.con1(completionOffset).searchWithin(unit); | |
158 return computer.computeFast(request); | |
159 } | |
160 | |
161 Future<bool> computeFull([bool fullAnalysis = false]) { | |
162 if (!_computeFastCalled) { | |
163 expect(computeFast(), isFalse); | |
164 } | |
165 var result = context.performAnalysisTask(); | |
166 bool resolved = false; | |
167 while (result.hasMoreWork) { | |
168 | |
169 // Update the index | |
170 result.changeNotices.forEach((ChangeNotice notice) { | |
171 CompilationUnit unit = notice.compilationUnit; | |
172 if (unit != null) { | |
173 index.indexUnit(context, unit); | |
174 } | |
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 | |
193 result = context.performAnalysisTask(); | |
194 } | |
195 if (!resolved) { | |
196 fail('expected unit to be resolved'); | |
197 } | |
198 return computer.computeFull(request); | |
199 } | |
200 | |
201 @override | |
202 void setUp() { | |
203 super.setUp(); | |
204 index = createLocalMemoryIndex(); | |
205 searchEngine = new SearchEngineImpl(index); | |
206 addResolvedUnit(MockSdk.LIB_CORE.path, MockSdk.LIB_CORE.content); | |
207 } | |
208 } | |
OLD | NEW |