| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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.util; | 5 library test.services.completion.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_services/completion/completion_computer.dart'; | 9 import 'package:analysis_services/completion/completion_computer.dart'; |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 10 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 11 import 'package:analysis_services/index/index.dart'; | 11 import 'package:analysis_services/index/index.dart'; |
| 12 import 'package:analysis_services/index/local_memory_index.dart'; | 12 import 'package:analysis_services/index/local_memory_index.dart'; |
| 13 import 'package:analysis_services/src/search/search_engine.dart'; | 13 import 'package:analysis_services/src/search/search_engine.dart'; |
| 14 import 'package:analysis_testing/abstract_single_unit.dart'; | 14 import 'package:analysis_testing/abstract_single_unit.dart'; |
| 15 import 'package:analysis_testing/mock_sdk.dart'; |
| 16 import 'package:analyzer/src/generated/ast.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:unittest/unittest.dart'; | 18 import 'package:unittest/unittest.dart'; |
| 16 | 19 |
| 17 class AbstractCompletionTest extends AbstractSingleUnitTest { | 20 class AbstractCompletionTest extends AbstractSingleUnitTest { |
| 18 Index index; | 21 Index index; |
| 19 SearchEngineImpl searchEngine; | 22 SearchEngineImpl searchEngine; |
| 20 CompletionComputer computer; | 23 CompletionComputer computer; |
| 24 int completionOffset; |
| 21 List<CompletionSuggestion> suggestions; | 25 List<CompletionSuggestion> suggestions; |
| 22 | 26 |
| 23 void addTestUnit(String code) { | 27 void addTestUnit(String content) { |
| 24 resolveTestUnit(code); | 28 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); |
| 29 completionOffset = content.indexOf('^'); |
| 30 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 31 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 32 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 33 content = content.substring(0, completionOffset) + |
| 34 content.substring(completionOffset + 1); |
| 35 resolveTestUnit(content); |
| 25 index.indexUnit(context, testUnit); | 36 index.indexUnit(context, testUnit); |
| 26 } | 37 } |
| 27 | 38 |
| 39 void addUnit(String file, String code) { |
| 40 Source source = addSource(file, code); |
| 41 CompilationUnit unit = resolveLibraryUnit(source); |
| 42 assertNoErrorsInSource(source); |
| 43 index.indexUnit(context, unit); |
| 44 } |
| 45 |
| 28 void assertHasResult(CompletionSuggestionKind kind, String completion, | 46 void assertHasResult(CompletionSuggestionKind kind, String completion, |
| 29 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, | 47 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, bool isDepre
cated |
| 30 bool isDeprecated = false, bool isPotential = false]) { | 48 = false, bool isPotential = false]) { |
| 31 var cs = suggestions.firstWhere((cs) => cs.completion == completion, orElse:
() { | 49 var cs = |
| 50 suggestions.firstWhere((cs) => cs.completion == completion, orElse: () { |
| 32 var completions = suggestions.map((s) => s.completion).toList(); | 51 var completions = suggestions.map((s) => s.completion).toList(); |
| 33 fail('expected "$completion" but found\n $completions'); | 52 fail('expected "$completion" but found\n $completions'); |
| 34 }); | 53 }); |
| 35 expect(cs.kind, equals(kind)); | 54 expect(cs.kind, equals(kind)); |
| 36 expect(cs.relevance, equals(relevance)); | 55 expect(cs.relevance, equals(relevance)); |
| 37 expect(cs.selectionOffset, equals(completion.length)); | 56 expect(cs.selectionOffset, equals(completion.length)); |
| 38 expect(cs.selectionLength, equals(0)); | 57 expect(cs.selectionLength, equals(0)); |
| 39 expect(cs.isDeprecated, equals(isDeprecated)); | 58 expect(cs.isDeprecated, equals(isDeprecated)); |
| 40 expect(cs.isPotential, equals(isPotential)); | 59 expect(cs.isPotential, equals(isPotential)); |
| 41 } | 60 } |
| 42 | 61 |
| 43 void assertNoResult(String completion) { | 62 void assertNoResult(String completion) { |
| 44 if (suggestions.any((cs) => cs.completion == completion)) { | 63 if (suggestions.any((cs) => cs.completion == completion)) { |
| 45 fail('did not expect completion: $completion'); | 64 fail('did not expect completion: $completion'); |
| 46 } | 65 } |
| 47 } | 66 } |
| 48 | 67 |
| 49 Future compute() { | 68 Future compute() { |
| 50 return computer.compute().then((List<CompletionSuggestion> results) { | 69 return computer.compute().then((List<CompletionSuggestion> results) { |
| 51 this.suggestions = results; | 70 this.suggestions = results; |
| 52 }); | 71 }); |
| 53 } | 72 } |
| 54 | 73 |
| 55 @override | 74 @override |
| 56 void setUp() { | 75 void setUp() { |
| 57 super.setUp(); | 76 super.setUp(); |
| 58 index = createLocalMemoryIndex(); | 77 index = createLocalMemoryIndex(); |
| 59 searchEngine = new SearchEngineImpl(index); | 78 searchEngine = new SearchEngineImpl(index); |
| 60 verifyNoTestUnitErrors = false; | 79 verifyNoTestUnitErrors = false; |
| 80 addUnit(MockSdk.LIB_CORE.path, MockSdk.LIB_CORE.content); |
| 61 } | 81 } |
| 62 } | 82 } |
| OLD | NEW |