| 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.domain.completion; | |
| 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_services/completion/completion_suggestion.dart'; | |
| 13 import 'package:analysis_services/constants.dart'; | |
| 14 import 'package:analysis_services/index/index.dart' show Index; | |
| 15 import 'package:analysis_services/index/local_memory_index.dart'; | |
| 16 import 'package:analysis_testing/reflective_tests.dart'; | |
| 17 import 'package:unittest/unittest.dart'; | |
| 18 | |
| 19 import 'analysis_abstract.dart'; | |
| 20 | |
| 21 main() { | |
| 22 groupSep = ' | '; | |
| 23 runReflectiveTests(CompletionTest); | |
| 24 } | |
| 25 | |
| 26 @ReflectiveTestCase() | |
| 27 class CompletionTest extends AbstractAnalysisTest { | |
| 28 String completionId; | |
| 29 int completionOffset; | |
| 30 List<CompletionSuggestion> suggestions = []; | |
| 31 bool suggestionsDone = false; | |
| 32 | |
| 33 String addTestFile(String content) { | |
| 34 completionOffset = content.indexOf('^'); | |
| 35 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | |
| 36 int nextOffset = content.indexOf('^', completionOffset + 1); | |
| 37 expect(nextOffset, equals(-1), reason: 'too many ^'); | |
| 38 return super.addTestFile( | |
| 39 content.substring(0, completionOffset) | |
| 40 + content.substring(completionOffset + 1)); | |
| 41 } | |
| 42 | |
| 43 void assertHasResult(CompletionSuggestionKind kind, String completion, | |
| 44 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, | |
| 45 bool isDeprecated = false, bool isPotential = false]) { | |
| 46 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { | |
| 47 var completions = suggestions.map((s) => s.completion).toList(); | |
| 48 fail('expected "$completion" but found\n $completions'); | |
| 49 }); | |
| 50 expect(cs.kind, equals(kind)); | |
| 51 expect(cs.relevance, equals(relevance)); | |
| 52 expect(cs.selectionOffset, equals(completion.length)); | |
| 53 expect(cs.selectionLength, equals(0)); | |
| 54 expect(cs.isDeprecated, equals(isDeprecated)); | |
| 55 expect(cs.isPotential, equals(isPotential)); | |
| 56 } | |
| 57 | |
| 58 void assertNoResult(String completion) { | |
| 59 if (suggestions.any((cs) => cs.completion == completion)) { | |
| 60 fail('did not expect completion: $completion'); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void assertValidId(String id) { | |
| 65 expect(id, isNotNull); | |
| 66 expect(id.isNotEmpty, isTrue); | |
| 67 } | |
| 68 | |
| 69 @override | |
| 70 Index createIndex() { | |
| 71 return createLocalMemoryIndex(); | |
| 72 } | |
| 73 | |
| 74 Future getSuggestions() { | |
| 75 return waitForTasksFinished().then((_) { | |
| 76 Request request = new Request('0', COMPLETION_GET_SUGGESTIONS); | |
| 77 request.setParameter(FILE, testFile); | |
| 78 request.setParameter(OFFSET, completionOffset); | |
| 79 Response response = handleSuccessfulRequest(request); | |
| 80 completionId = response.getResult(ID); | |
| 81 assertValidId(completionId); | |
| 82 return waitForSuggestions(); | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 void processNotification(Notification notification) { | |
| 87 if (notification.event == COMPLETION_RESULTS) { | |
| 88 String id = notification.getParameter(ID); | |
| 89 assertValidId(id); | |
| 90 if (id == completionId) { | |
| 91 expect(suggestionsDone, isFalse); | |
| 92 suggestionsDone = notification.getParameter(LAST); | |
| 93 expect(suggestionsDone, isNotNull); | |
| 94 for (Map<String, Object> json in notification.getParameter(RESULTS)) { | |
| 95 expect(json, isNotNull); | |
| 96 suggestions.add(new CompletionSuggestion.fromJson(json)); | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 @override | |
| 103 void setUp() { | |
| 104 super.setUp(); | |
| 105 createProject(); | |
| 106 handler = new CompletionDomainHandler(server); | |
| 107 } | |
| 108 | |
| 109 Future waitForSuggestions() { | |
| 110 if (suggestionsDone) { | |
| 111 return new Future.value(); | |
| 112 } | |
| 113 return new Future.delayed(Duration.ZERO, waitForSuggestions); | |
| 114 } | |
| 115 | |
| 116 test_suggestions_importedType() { | |
| 117 addTestFile(''' | |
| 118 import 'dart:html'; | |
| 119 main() {^} | |
| 120 '''); | |
| 121 return getSuggestions().then((_) { | |
| 122 assertHasResult(CompletionSuggestionKind.CLASS, 'Object'); | |
| 123 assertHasResult(CompletionSuggestionKind.CLASS, 'HtmlElement'); | |
| 124 assertNoResult('test'); | |
| 125 }); | |
| 126 } | |
| 127 | |
| 128 test_suggestions_topLevel() { | |
| 129 addTestFile(''' | |
| 130 typedef foo(); | |
| 131 var test^ = ''; | |
| 132 main() {test.} | |
| 133 '''); | |
| 134 return getSuggestions().then((_) { | |
| 135 assertHasResult(CompletionSuggestionKind.CLASS, 'Object'); | |
| 136 assertHasResult(CompletionSuggestionKind.TOP_LEVEL_VARIABLE, 'test'); | |
| 137 assertNoResult('HtmlElement'); | |
| 138 }); | |
| 139 } | |
| 140 } | |
| OLD | NEW |