Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: pkg/analysis_server/test/services/completion/common_usage_computer_test.dart

Issue 925723002: update code completion suggestion relevance based on common usage (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rework common usage computer to inject test relevance map Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.services.completion.computer.dart.relevance;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/src/constants.dart';
10 import 'package:analysis_server/src/domain_completion.dart';
11 import 'package:analysis_server/src/protocol.dart';
12 import 'package:analysis_server/src/services/completion/common_usage_computer.da rt';
13 import 'package:analysis_server/src/services/completion/dart_completion_cache.da rt';
14 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart';
15 import 'package:analysis_server/src/services/index/index.dart';
16 import 'package:analysis_server/src/services/index/local_memory_index.dart';
17 import 'package:analyzer/src/generated/engine.dart';
18 import 'package:analyzer/src/generated/source.dart';
19 import 'package:unittest/unittest.dart';
20
21 import '../../analysis_abstract.dart';
22 import '../../mocks.dart';
23 import '../../reflective_tests.dart';
24
25 /**
26 * A map of <library>.<classname> to an ordered list of method names,
27 * field names, getter names, and named constructors for testing.
28 * The names are ordered from most relevant to least relevant.
29 * Names not listed are considered equally less relevant than those listed.
30 */
31 const Map<String, List<String>> testSelectorRelevance = const {
32 // Sample implementation which updates the relevance of the following
33 // new Random().nextInt(...)
34 // new Random().nextDouble(...)
35 // new Random().nextBool() - not commonly used thus omitted from list
36 'dart.math.Random': const ['nextInt', 'nextDouble'],
37 'dart.async.Future': const ['value', 'wait'],
38 };
39
40 main() {
41 groupSep = ' | ';
42 runReflectiveTests(CommonUsageComputerTest);
43 }
44
45 @reflectiveTest
46 class CommonUsageComputerTest extends AbstractAnalysisTest {
47 String completionId;
48 int completionOffset;
49 int replacementOffset;
50 int replacementLength;
51 List<CompletionSuggestion> suggestions = [];
52 bool suggestionsDone = false;
53
54 String addTestFile(String content) {
55 completionOffset = content.indexOf('^');
56 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
57 int nextOffset = content.indexOf('^', completionOffset + 1);
58 expect(nextOffset, equals(-1), reason: 'too many ^');
59 return super.addTestFile(
60 content.substring(0, completionOffset) +
61 content.substring(completionOffset + 1));
62 }
63
64 void assertHasResult(CompletionSuggestionKind kind, String completion,
65 [int relevance = DART_RELEVANCE_DEFAULT, bool isDeprecated = false,
66 bool isPotential = false]) {
67 var cs;
68 suggestions.forEach((s) {
69 if (s.completion == completion) {
70 if (cs == null) {
71 cs = s;
72 } else {
73 fail('expected exactly one $completion but found > 1');
74 }
75 }
76 });
77 if (cs == null) {
78 var completions = 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 assertNoResult(String completion) {
90 if (suggestions.any((cs) => cs.completion == completion)) {
91 fail('did not expect completion: $completion');
92 }
93 }
94
95 void assertValidId(String id) {
96 expect(id, isNotNull);
97 expect(id.isNotEmpty, isTrue);
98 }
99
100 @override
101 Index createIndex() {
102 return createLocalMemoryIndex();
103 }
104
105 Future getSuggestions() async {
106 await waitForTasksFinished();
107 CompletionGetSuggestionsParams params =
108 new CompletionGetSuggestionsParams(testFile, completionOffset);
109 Request request = params.toRequest('0');
110 CompletionDomainHandler domainHandler = new CompletionDomainHandler(server);
111 handler = domainHandler;
112
113 AnalysisContext context = server.getAnalysisContext(params.file);
114 Source source = server.getSource(params.file);
115 DartCompletionManager completionManager = new DartCompletionManager(
116 context,
117 server.searchEngine,
118 source,
119 new DartCompletionCache(context, source),
120 null,
121 new CommonUsageComputer(testSelectorRelevance));
122
123 Response response =
124 domainHandler.processRequest(request, completionManager);
125 expect(response, isResponseSuccess('0'));
126 completionId = response.id;
127 assertValidId(completionId);
128 await pumpEventQueue();
129 expect(suggestionsDone, isTrue);
130 }
131
132 void processNotification(Notification notification) {
133 if (notification.event == COMPLETION_RESULTS) {
134 var params = new CompletionResultsParams.fromNotification(notification);
135 String id = params.id;
136 assertValidId(id);
137 if (id == completionId) {
138 expect(suggestionsDone, isFalse);
139 replacementOffset = params.replacementOffset;
140 replacementLength = params.replacementLength;
141 suggestionsDone = params.isLast;
142 expect(suggestionsDone, isNotNull);
143 suggestions = params.results;
144 }
145 }
146 }
147
148 @override
149 void setUp() {
150 super.setUp();
151 createProject();
152 }
153
154 test_ConstructorName() async {
155 // SimpleIdentifier ConstructorName InstanceCreationExpression
156 addTestFile('import "dart:async"; class A {x() {new Future.^}}');
157 await getSuggestions();
158 expect(replacementOffset, equals(completionOffset));
159 expect(replacementLength, equals(0));
160 assertHasResult(CompletionSuggestionKind.INVOCATION, 'delayed');
161 assertHasResult(
162 CompletionSuggestionKind.INVOCATION,
163 'value',
164 DART_RELEVANCE_COMMON_USAGE);
165 assertNoResult('Future');
166 assertNoResult('Object');
167 assertNoResult('A');
168 }
169
170 test_PrefixedIdentifier() async {
171 // SimpleIdentifier PrefixedIdentifeir ExpressionStatement
172 addTestFile('import "dart:async"; class A {x() {Future.^}}');
173 await getSuggestions();
174 expect(replacementOffset, equals(completionOffset));
175 expect(replacementLength, equals(0));
176 assertHasResult(
177 CompletionSuggestionKind.INVOCATION,
178 'wait',
179 DART_RELEVANCE_COMMON_USAGE - 1);
180 assertNoResult('Future');
181 assertNoResult('Object');
182 assertNoResult('A');
183 }
184
185 test_PropertyAccess() async {
186 // SimpleIdentifier PropertyAccess ExpressionStatement
187 addTestFile('import "dart:math"; class A {x() {new Random().^}}');
188 await getSuggestions();
189 expect(replacementOffset, equals(completionOffset));
190 expect(replacementLength, equals(0));
191 assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextBool');
192 assertHasResult(
193 CompletionSuggestionKind.INVOCATION,
194 'nextDouble',
195 DART_RELEVANCE_COMMON_USAGE - 1);
196 assertHasResult(
197 CompletionSuggestionKind.INVOCATION,
198 'nextInt',
199 DART_RELEVANCE_COMMON_USAGE);
200 assertNoResult('Random');
201 assertNoResult('Object');
202 assertNoResult('A');
203 }
204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698