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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/test/services/completion/common_usage_computer_test.dart
diff --git a/pkg/analysis_server/test/services/completion/common_usage_computer_test.dart b/pkg/analysis_server/test/services/completion/common_usage_computer_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5c64e7038a3632fc9650b330fdeec407bb5d4f5a
--- /dev/null
+++ b/pkg/analysis_server/test/services/completion/common_usage_computer_test.dart
@@ -0,0 +1,204 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library test.services.completion.computer.dart.relevance;
+
+import 'dart:async';
+
+import 'package:analysis_server/src/constants.dart';
+import 'package:analysis_server/src/domain_completion.dart';
+import 'package:analysis_server/src/protocol.dart';
+import 'package:analysis_server/src/services/completion/common_usage_computer.dart';
+import 'package:analysis_server/src/services/completion/dart_completion_cache.dart';
+import 'package:analysis_server/src/services/completion/dart_completion_manager.dart';
+import 'package:analysis_server/src/services/index/index.dart';
+import 'package:analysis_server/src/services/index/local_memory_index.dart';
+import 'package:analyzer/src/generated/engine.dart';
+import 'package:analyzer/src/generated/source.dart';
+import 'package:unittest/unittest.dart';
+
+import '../../analysis_abstract.dart';
+import '../../mocks.dart';
+import '../../reflective_tests.dart';
+
+/**
+ * A map of <library>.<classname> to an ordered list of method names,
+ * field names, getter names, and named constructors for testing.
+ * The names are ordered from most relevant to least relevant.
+ * Names not listed are considered equally less relevant than those listed.
+ */
+const Map<String, List<String>> testSelectorRelevance = const {
+ // Sample implementation which updates the relevance of the following
+ // new Random().nextInt(...)
+ // new Random().nextDouble(...)
+ // new Random().nextBool() - not commonly used thus omitted from list
+ 'dart.math.Random': const ['nextInt', 'nextDouble'],
+ 'dart.async.Future': const ['value', 'wait'],
+};
+
+main() {
+ groupSep = ' | ';
+ runReflectiveTests(CommonUsageComputerTest);
+}
+
+@reflectiveTest
+class CommonUsageComputerTest extends AbstractAnalysisTest {
+ String completionId;
+ int completionOffset;
+ int replacementOffset;
+ int replacementLength;
+ List<CompletionSuggestion> suggestions = [];
+ bool suggestionsDone = false;
+
+ String addTestFile(String content) {
+ completionOffset = content.indexOf('^');
+ expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
+ int nextOffset = content.indexOf('^', completionOffset + 1);
+ expect(nextOffset, equals(-1), reason: 'too many ^');
+ return super.addTestFile(
+ content.substring(0, completionOffset) +
+ content.substring(completionOffset + 1));
+ }
+
+ void assertHasResult(CompletionSuggestionKind kind, String completion,
+ [int relevance = DART_RELEVANCE_DEFAULT, bool isDeprecated = false,
+ bool isPotential = false]) {
+ var cs;
+ suggestions.forEach((s) {
+ if (s.completion == completion) {
+ if (cs == null) {
+ cs = s;
+ } else {
+ fail('expected exactly one $completion but found > 1');
+ }
+ }
+ });
+ if (cs == null) {
+ var completions = suggestions.map((s) => s.completion).toList();
+ fail('expected "$completion" but found\n $completions');
+ }
+ expect(cs.kind, equals(kind));
+ expect(cs.relevance, equals(relevance));
+ expect(cs.selectionOffset, equals(completion.length));
+ expect(cs.selectionLength, equals(0));
+ expect(cs.isDeprecated, equals(isDeprecated));
+ expect(cs.isPotential, equals(isPotential));
+ }
+
+ void assertNoResult(String completion) {
+ if (suggestions.any((cs) => cs.completion == completion)) {
+ fail('did not expect completion: $completion');
+ }
+ }
+
+ void assertValidId(String id) {
+ expect(id, isNotNull);
+ expect(id.isNotEmpty, isTrue);
+ }
+
+ @override
+ Index createIndex() {
+ return createLocalMemoryIndex();
+ }
+
+ Future getSuggestions() async {
+ await waitForTasksFinished();
+ CompletionGetSuggestionsParams params =
+ new CompletionGetSuggestionsParams(testFile, completionOffset);
+ Request request = params.toRequest('0');
+ CompletionDomainHandler domainHandler = new CompletionDomainHandler(server);
+ handler = domainHandler;
+
+ AnalysisContext context = server.getAnalysisContext(params.file);
+ Source source = server.getSource(params.file);
+ DartCompletionManager completionManager = new DartCompletionManager(
+ context,
+ server.searchEngine,
+ source,
+ new DartCompletionCache(context, source),
+ null,
+ new CommonUsageComputer(testSelectorRelevance));
+
+ Response response =
+ domainHandler.processRequest(request, completionManager);
+ expect(response, isResponseSuccess('0'));
+ completionId = response.id;
+ assertValidId(completionId);
+ await pumpEventQueue();
+ expect(suggestionsDone, isTrue);
+ }
+
+ void processNotification(Notification notification) {
+ if (notification.event == COMPLETION_RESULTS) {
+ var params = new CompletionResultsParams.fromNotification(notification);
+ String id = params.id;
+ assertValidId(id);
+ if (id == completionId) {
+ expect(suggestionsDone, isFalse);
+ replacementOffset = params.replacementOffset;
+ replacementLength = params.replacementLength;
+ suggestionsDone = params.isLast;
+ expect(suggestionsDone, isNotNull);
+ suggestions = params.results;
+ }
+ }
+ }
+
+ @override
+ void setUp() {
+ super.setUp();
+ createProject();
+ }
+
+ test_ConstructorName() async {
+ // SimpleIdentifier ConstructorName InstanceCreationExpression
+ addTestFile('import "dart:async"; class A {x() {new Future.^}}');
+ await getSuggestions();
+ expect(replacementOffset, equals(completionOffset));
+ expect(replacementLength, equals(0));
+ assertHasResult(CompletionSuggestionKind.INVOCATION, 'delayed');
+ assertHasResult(
+ CompletionSuggestionKind.INVOCATION,
+ 'value',
+ DART_RELEVANCE_COMMON_USAGE);
+ assertNoResult('Future');
+ assertNoResult('Object');
+ assertNoResult('A');
+ }
+
+ test_PrefixedIdentifier() async {
+ // SimpleIdentifier PrefixedIdentifeir ExpressionStatement
+ addTestFile('import "dart:async"; class A {x() {Future.^}}');
+ await getSuggestions();
+ expect(replacementOffset, equals(completionOffset));
+ expect(replacementLength, equals(0));
+ assertHasResult(
+ CompletionSuggestionKind.INVOCATION,
+ 'wait',
+ DART_RELEVANCE_COMMON_USAGE - 1);
+ assertNoResult('Future');
+ assertNoResult('Object');
+ assertNoResult('A');
+ }
+
+ test_PropertyAccess() async {
+ // SimpleIdentifier PropertyAccess ExpressionStatement
+ addTestFile('import "dart:math"; class A {x() {new Random().^}}');
+ await getSuggestions();
+ expect(replacementOffset, equals(completionOffset));
+ expect(replacementLength, equals(0));
+ assertHasResult(CompletionSuggestionKind.INVOCATION, 'nextBool');
+ assertHasResult(
+ CompletionSuggestionKind.INVOCATION,
+ 'nextDouble',
+ DART_RELEVANCE_COMMON_USAGE - 1);
+ assertHasResult(
+ CompletionSuggestionKind.INVOCATION,
+ 'nextInt',
+ DART_RELEVANCE_COMMON_USAGE);
+ assertNoResult('Random');
+ assertNoResult('Object');
+ assertNoResult('A');
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698