OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.computer.dart.relevance; | 5 library test.services.completion.computer.dart.relevance; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/src/analysis_server.dart' | 9 import 'package:analysis_server/src/analysis_server.dart' |
10 show ContextSourcePair; | 10 show ContextSourcePair; |
11 import 'package:analysis_server/src/constants.dart'; | 11 import 'package:analysis_server/src/constants.dart'; |
12 import 'package:analysis_server/src/domain_completion.dart'; | 12 import 'package:analysis_server/src/domain_completion.dart'; |
13 import 'package:analysis_server/src/protocol.dart'; | 13 import 'package:analysis_server/src/protocol.dart'; |
14 import 'package:analysis_server/src/services/completion/common_usage_computer.da
rt'; | 14 import 'package:analysis_server/src/services/completion/common_usage_computer.da
rt'; |
15 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; | 15 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; |
16 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 16 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
17 import 'package:analysis_server/src/services/index/index.dart'; | 17 import 'package:analysis_server/src/services/index/index.dart'; |
18 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | 18 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
19 import 'package:analyzer/src/generated/engine.dart'; | 19 import 'package:analyzer/src/generated/engine.dart'; |
20 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
21 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 21 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
22 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
23 | 23 |
24 import '../../analysis_abstract.dart'; | 24 import '../../analysis_abstract.dart'; |
25 import '../../mocks.dart'; | 25 import '../../mocks.dart'; |
| 26 import '../../utils.dart'; |
26 | 27 |
27 main() { | 28 main() { |
28 groupSep = ' | '; | 29 initializeTestEnvironment(); |
29 defineReflectiveTests(CommonUsageComputerTest); | 30 defineReflectiveTests(CommonUsageComputerTest); |
30 } | 31 } |
31 | 32 |
32 @reflectiveTest | 33 @reflectiveTest |
33 class CommonUsageComputerTest extends AbstractAnalysisTest { | 34 class CommonUsageComputerTest extends AbstractAnalysisTest { |
34 String completionId; | 35 String completionId; |
35 int completionOffset; | 36 int completionOffset; |
36 int replacementOffset; | 37 int replacementOffset; |
37 int replacementLength; | 38 int replacementLength; |
38 List<CompletionSuggestion> suggestions = []; | 39 List<CompletionSuggestion> suggestions = []; |
39 bool suggestionsDone = false; | 40 bool suggestionsDone = false; |
40 | 41 |
41 String addTestFile(String content) { | 42 String addTestFile(String content) { |
42 completionOffset = content.indexOf('^'); | 43 completionOffset = content.indexOf('^'); |
43 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | 44 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
44 int nextOffset = content.indexOf('^', completionOffset + 1); | 45 int nextOffset = content.indexOf('^', completionOffset + 1); |
45 expect(nextOffset, equals(-1), reason: 'too many ^'); | 46 expect(nextOffset, equals(-1), reason: 'too many ^'); |
46 return super.addTestFile(content.substring(0, completionOffset) + | 47 return super.addTestFile(content.substring(0, completionOffset) + |
47 content.substring(completionOffset + 1)); | 48 content.substring(completionOffset + 1)); |
48 } | 49 } |
49 | 50 |
50 void assertHasResult(CompletionSuggestionKind kind, String completion, | 51 void assertHasResult(CompletionSuggestionKind kind, String completion, |
51 [int relevance = DART_RELEVANCE_DEFAULT, bool isDeprecated = false, | 52 [int relevance = DART_RELEVANCE_DEFAULT, |
| 53 bool isDeprecated = false, |
52 bool isPotential = false]) { | 54 bool isPotential = false]) { |
53 var cs; | 55 var cs; |
54 suggestions.forEach((s) { | 56 suggestions.forEach((s) { |
55 if (s.completion == completion) { | 57 if (s.completion == completion) { |
56 if (cs == null) { | 58 if (cs == null) { |
57 cs = s; | 59 cs = s; |
58 } else { | 60 } else { |
59 fail('expected exactly one $completion but found > 1'); | 61 fail('expected exactly one $completion but found > 1'); |
60 } | 62 } |
61 } | 63 } |
(...skipping 30 matching lines...) Expand all Loading... |
92 await waitForTasksFinished(); | 94 await waitForTasksFinished(); |
93 CompletionGetSuggestionsParams params = | 95 CompletionGetSuggestionsParams params = |
94 new CompletionGetSuggestionsParams(testFile, completionOffset); | 96 new CompletionGetSuggestionsParams(testFile, completionOffset); |
95 Request request = params.toRequest('0'); | 97 Request request = params.toRequest('0'); |
96 CompletionDomainHandler domainHandler = new CompletionDomainHandler(server); | 98 CompletionDomainHandler domainHandler = new CompletionDomainHandler(server); |
97 handler = domainHandler; | 99 handler = domainHandler; |
98 | 100 |
99 ContextSourcePair contextSource = server.getContextSourcePair(params.file); | 101 ContextSourcePair contextSource = server.getContextSourcePair(params.file); |
100 AnalysisContext context = contextSource.context; | 102 AnalysisContext context = contextSource.context; |
101 Source source = contextSource.source; | 103 Source source = contextSource.source; |
102 DartCompletionManager completionManager = new DartCompletionManager(context, | 104 DartCompletionManager completionManager = new DartCompletionManager( |
103 server.searchEngine, source, new DartCompletionCache(context, source), | 105 context, |
104 null, new CommonUsageComputer(selectorRelevance)); | 106 server.searchEngine, |
| 107 source, |
| 108 new DartCompletionCache(context, source), |
| 109 null, |
| 110 new CommonUsageComputer(selectorRelevance)); |
105 | 111 |
106 Response response = | 112 Response response = |
107 domainHandler.processRequest(request, completionManager); | 113 domainHandler.processRequest(request, completionManager); |
108 expect(response, isResponseSuccess('0')); | 114 expect(response, isResponseSuccess('0')); |
109 completionId = response.id; | 115 completionId = response.id; |
110 assertValidId(completionId); | 116 assertValidId(completionId); |
111 await pumpEventQueue(); | 117 await pumpEventQueue(); |
112 expect(suggestionsDone, isTrue); | 118 expect(suggestionsDone, isTrue); |
113 } | 119 } |
114 | 120 |
(...skipping 15 matching lines...) Expand all Loading... |
130 | 136 |
131 @override | 137 @override |
132 void setUp() { | 138 void setUp() { |
133 super.setUp(); | 139 super.setUp(); |
134 createProject(); | 140 createProject(); |
135 } | 141 } |
136 | 142 |
137 test_ConstructorName() async { | 143 test_ConstructorName() async { |
138 // SimpleIdentifier ConstructorName InstanceCreationExpression | 144 // SimpleIdentifier ConstructorName InstanceCreationExpression |
139 addTestFile('import "dart:async"; class A {x() {new Future.^}}'); | 145 addTestFile('import "dart:async"; class A {x() {new Future.^}}'); |
140 await getSuggestions({'dart.async.Future': ['value', 'wait']}); | 146 await getSuggestions({ |
| 147 'dart.async.Future': ['value', 'wait'] |
| 148 }); |
141 expect(replacementOffset, equals(completionOffset)); | 149 expect(replacementOffset, equals(completionOffset)); |
142 expect(replacementLength, equals(0)); | 150 expect(replacementLength, equals(0)); |
143 assertHasResult(CompletionSuggestionKind.INVOCATION, 'delayed'); | 151 assertHasResult(CompletionSuggestionKind.INVOCATION, 'delayed'); |
144 assertHasResult(CompletionSuggestionKind.INVOCATION, 'value', | 152 assertHasResult(CompletionSuggestionKind.INVOCATION, 'value', |
145 DART_RELEVANCE_COMMON_USAGE); | 153 DART_RELEVANCE_COMMON_USAGE); |
146 assertNoResult('Future'); | 154 assertNoResult('Future'); |
147 assertNoResult('Object'); | 155 assertNoResult('Object'); |
148 assertNoResult('A'); | 156 assertNoResult('A'); |
149 } | 157 } |
150 | 158 |
151 test_PrefixedIdentifier_field() async { | 159 test_PrefixedIdentifier_field() async { |
152 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement | 160 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement |
153 addTestFile('class A {static int s1; static int s2; x() {A.^}}'); | 161 addTestFile('class A {static int s1; static int s2; x() {A.^}}'); |
154 await getSuggestions({'.A': ['s2']}); | 162 await getSuggestions({ |
| 163 '.A': ['s2'] |
| 164 }); |
155 expect(replacementOffset, equals(completionOffset)); | 165 expect(replacementOffset, equals(completionOffset)); |
156 expect(replacementLength, equals(0)); | 166 expect(replacementLength, equals(0)); |
157 assertHasResult(CompletionSuggestionKind.INVOCATION, 's1'); | 167 assertHasResult(CompletionSuggestionKind.INVOCATION, 's1'); |
158 assertHasResult( | 168 assertHasResult( |
159 CompletionSuggestionKind.INVOCATION, 's2', DART_RELEVANCE_COMMON_USAGE); | 169 CompletionSuggestionKind.INVOCATION, 's2', DART_RELEVANCE_COMMON_USAGE); |
160 assertNoResult('Future'); | 170 assertNoResult('Future'); |
161 assertNoResult('Object'); | 171 assertNoResult('Object'); |
162 assertNoResult('A'); | 172 assertNoResult('A'); |
163 } | 173 } |
164 | 174 |
165 test_PrefixedIdentifier_getter() async { | 175 test_PrefixedIdentifier_getter() async { |
166 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement | 176 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement |
167 addTestFile('class A {int get g1 => 1; int get g2 => 2; x() {new A().^}}'); | 177 addTestFile('class A {int get g1 => 1; int get g2 => 2; x() {new A().^}}'); |
168 await getSuggestions({'.A': ['g2']}); | 178 await getSuggestions({ |
| 179 '.A': ['g2'] |
| 180 }); |
169 expect(replacementOffset, equals(completionOffset)); | 181 expect(replacementOffset, equals(completionOffset)); |
170 expect(replacementLength, equals(0)); | 182 expect(replacementLength, equals(0)); |
171 assertHasResult(CompletionSuggestionKind.INVOCATION, 'g1'); | 183 assertHasResult(CompletionSuggestionKind.INVOCATION, 'g1'); |
172 assertHasResult( | 184 assertHasResult( |
173 CompletionSuggestionKind.INVOCATION, 'g2', DART_RELEVANCE_COMMON_USAGE); | 185 CompletionSuggestionKind.INVOCATION, 'g2', DART_RELEVANCE_COMMON_USAGE); |
174 assertNoResult('Future'); | 186 assertNoResult('Future'); |
175 assertNoResult('Object'); | 187 assertNoResult('Object'); |
176 assertNoResult('A'); | 188 assertNoResult('A'); |
177 } | 189 } |
178 | 190 |
179 test_PrefixedIdentifier_setter() async { | 191 test_PrefixedIdentifier_setter() async { |
180 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement | 192 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement |
181 addTestFile('class A {set s1(v) {}; set s2(v) {}; x() {new A().^}}'); | 193 addTestFile('class A {set s1(v) {}; set s2(v) {}; x() {new A().^}}'); |
182 await getSuggestions({'.A': ['s2']}); | 194 await getSuggestions({ |
| 195 '.A': ['s2'] |
| 196 }); |
183 expect(replacementOffset, equals(completionOffset)); | 197 expect(replacementOffset, equals(completionOffset)); |
184 expect(replacementLength, equals(0)); | 198 expect(replacementLength, equals(0)); |
185 assertHasResult(CompletionSuggestionKind.INVOCATION, 's1'); | 199 assertHasResult(CompletionSuggestionKind.INVOCATION, 's1'); |
186 assertHasResult( | 200 assertHasResult( |
187 CompletionSuggestionKind.INVOCATION, 's2', DART_RELEVANCE_COMMON_USAGE); | 201 CompletionSuggestionKind.INVOCATION, 's2', DART_RELEVANCE_COMMON_USAGE); |
188 assertNoResult('Future'); | 202 assertNoResult('Future'); |
189 assertNoResult('Object'); | 203 assertNoResult('Object'); |
190 assertNoResult('A'); | 204 assertNoResult('A'); |
191 } | 205 } |
192 | 206 |
193 test_PrefixedIdentifier_static_method() async { | 207 test_PrefixedIdentifier_static_method() async { |
194 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement | 208 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement |
195 addTestFile('import "dart:async"; class A {x() {Future.^}}'); | 209 addTestFile('import "dart:async"; class A {x() {Future.^}}'); |
196 await getSuggestions({'dart.async.Future': ['value', 'wait']}); | 210 await getSuggestions({ |
| 211 'dart.async.Future': ['value', 'wait'] |
| 212 }); |
197 expect(replacementOffset, equals(completionOffset)); | 213 expect(replacementOffset, equals(completionOffset)); |
198 expect(replacementLength, equals(0)); | 214 expect(replacementLength, equals(0)); |
199 assertHasResult(CompletionSuggestionKind.INVOCATION, 'wait', | 215 assertHasResult(CompletionSuggestionKind.INVOCATION, 'wait', |
200 DART_RELEVANCE_COMMON_USAGE - 1); | 216 DART_RELEVANCE_COMMON_USAGE - 1); |
201 assertNoResult('Future'); | 217 assertNoResult('Future'); |
202 assertNoResult('Object'); | 218 assertNoResult('Object'); |
203 assertNoResult('A'); | 219 assertNoResult('A'); |
204 } | 220 } |
205 | 221 |
206 test_PropertyAccess() async { | 222 test_PropertyAccess() async { |
207 // SimpleIdentifier PropertyAccess ExpressionStatement | 223 // SimpleIdentifier PropertyAccess ExpressionStatement |
208 addTestFile('import "dart:math"; class A {x() {new Random().^}}'); | 224 addTestFile('import "dart:math"; class A {x() {new Random().^}}'); |
209 await getSuggestions({'dart.math.Random': ['nextInt', 'nextDouble']}); | 225 await getSuggestions({ |
| 226 'dart.math.Random': ['nextInt', 'nextDouble'] |
| 227 }); |
210 expect(replacementOffset, equals(completionOffset)); | 228 expect(replacementOffset, equals(completionOffset)); |
211 expect(replacementLength, equals(0)); | 229 expect(replacementLength, equals(0)); |
212 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextBool'); | 230 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextBool'); |
213 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextDouble', | 231 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextDouble', |
214 DART_RELEVANCE_COMMON_USAGE - 1); | 232 DART_RELEVANCE_COMMON_USAGE - 1); |
215 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextInt', | 233 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextInt', |
216 DART_RELEVANCE_COMMON_USAGE); | 234 DART_RELEVANCE_COMMON_USAGE); |
217 assertNoResult('Random'); | 235 assertNoResult('Random'); |
218 assertNoResult('Object'); | 236 assertNoResult('Object'); |
219 assertNoResult('A'); | 237 assertNoResult('A'); |
220 } | 238 } |
221 } | 239 } |
OLD | NEW |