| 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_server/src/protocol.dart' as protocol show Element, Ele
mentKind; | 9 import 'package:analysis_server/src/protocol.dart' as protocol show Element, |
| 10 ElementKind; |
| 10 import 'package:analysis_server/src/protocol.dart' hide Element; | 11 import 'package:analysis_server/src/protocol.dart' hide Element; |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 12 import 'package:analysis_server/src/services/index/index.dart'; | 13 import 'package:analysis_server/src/services/index/index.dart'; |
| 13 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | 14 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
| 14 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; | 15 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; |
| 15 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 19 import 'package:unittest/unittest.dart'; | 20 import 'package:unittest/unittest.dart'; |
| 20 | 21 |
| 21 import '../../abstract_context.dart'; | 22 import '../../abstract_context.dart'; |
| 22 | 23 |
| 23 class AbstractCompletionTest extends AbstractContextTest { | 24 class AbstractCompletionTest extends AbstractContextTest { |
| 24 Index index; | 25 Index index; |
| 25 SearchEngineImpl searchEngine; | 26 SearchEngineImpl searchEngine; |
| 26 DartCompletionComputer computer; | 27 DartCompletionComputer computer; |
| 27 String testFile = '/completionTest.dart'; | 28 String testFile = '/completionTest.dart'; |
| 28 Source testSource; | 29 Source testSource; |
| 30 CompilationUnit testUnit; |
| 29 int completionOffset; | 31 int completionOffset; |
| 32 AstNode completionNode; |
| 30 bool _computeFastCalled = false; | 33 bool _computeFastCalled = false; |
| 31 DartCompletionRequest request; | 34 DartCompletionRequest request; |
| 32 | 35 |
| 33 void addResolvedUnit(String file, String code) { | 36 void addResolvedUnit(String file, String code) { |
| 34 Source source = addSource(file, code); | 37 Source source = addSource(file, code); |
| 35 CompilationUnit unit = resolveLibraryUnit(source); | 38 CompilationUnit unit = resolveLibraryUnit(source); |
| 36 index.indexUnit(context, unit); | 39 index.indexUnit(context, unit); |
| 37 } | 40 } |
| 38 | 41 |
| 39 void addTestSource(String content) { | 42 void addTestSource(String content) { |
| 40 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); | 43 expect(completionOffset, isNull, reason: 'Call addTestUnit exactly once'); |
| 41 completionOffset = content.indexOf('^'); | 44 completionOffset = content.indexOf('^'); |
| 42 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | 45 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 43 int nextOffset = content.indexOf('^', completionOffset + 1); | 46 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 44 expect(nextOffset, equals(-1), reason: 'too many ^'); | 47 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 45 content = content.substring(0, completionOffset) + | 48 content = content.substring(0, completionOffset) + |
| 46 content.substring(completionOffset + 1); | 49 content.substring(completionOffset + 1); |
| 47 testSource = addSource(testFile, content); | 50 testSource = addSource(testFile, content); |
| 48 request = | 51 request = |
| 49 new DartCompletionRequest(context, searchEngine, testSource, completionO
ffset); | 52 new DartCompletionRequest(context, searchEngine, testSource, completionO
ffset); |
| 50 } | 53 } |
| 51 | 54 |
| 52 void assertNotSuggested(String completion) { | 55 void assertNotSuggested(String completion) { |
| 53 if (request.suggestions.any((cs) => cs.completion == completion)) { | 56 if (request.suggestions.any((cs) => cs.completion == completion)) { |
| 54 fail('did not expect completion: $completion'); | 57 _failWithNodeInfo('did not expect completion: $completion'); |
| 55 } | 58 } |
| 56 } | 59 } |
| 57 | 60 |
| 58 CompletionSuggestion assertSuggest(CompletionSuggestionKind kind, | 61 CompletionSuggestion assertSuggest(CompletionSuggestionKind kind, |
| 59 String completion, [CompletionRelevance relevance = CompletionRelevance.DE
FAULT, | 62 String completion, [CompletionRelevance relevance = CompletionRelevance.DE
FAULT, |
| 60 bool isDeprecated = false, bool isPotential = false]) { | 63 bool isDeprecated = false, bool isPotential = false]) { |
| 61 CompletionSuggestion cs; | 64 CompletionSuggestion cs; |
| 62 request.suggestions.forEach((s) { | 65 request.suggestions.forEach((s) { |
| 63 if (s.completion == completion) { | 66 if (s.completion == completion) { |
| 64 if (cs == null) { | 67 if (cs == null) { |
| 65 cs = s; | 68 cs = s; |
| 66 } else { | 69 } else { |
| 67 List<CompletionSuggestion> matchSuggestions = | 70 List<CompletionSuggestion> completions = |
| 68 request.suggestions.where((s) => s.completion == completion).toLis
t(); | 71 request.suggestions.where((s) => s.completion == completion).toLis
t(); |
| 69 fail( | 72 _failWithNodeInfo( |
| 70 'expected exactly one $completion but found > 1\n $matchSuggestion
s'); | 73 'expected exactly one $completion but found > 1\n$completions'); |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 }); | 76 }); |
| 74 if (cs == null) { | 77 if (cs == null) { |
| 75 List<CompletionSuggestion> completions = | 78 List<CompletionSuggestion> completions = |
| 76 request.suggestions.map((s) => s.completion).toList(); | 79 request.suggestions.map((s) => s.completion).toList(); |
| 77 fail('expected "$completion" but found\n $completions'); | 80 _failWithNodeInfo('expected "$completion" but found\n $completions'); |
| 78 } | 81 } |
| 79 expect(cs.kind, equals(kind)); | 82 expect(cs.kind, equals(kind)); |
| 80 expect(cs.relevance, equals(relevance)); | 83 expect(cs.relevance, equals(relevance)); |
| 81 expect(cs.selectionOffset, equals(completion.length)); | 84 expect(cs.selectionOffset, equals(completion.length)); |
| 82 expect(cs.selectionLength, equals(0)); | 85 expect(cs.selectionLength, equals(0)); |
| 83 expect(cs.isDeprecated, equals(isDeprecated)); | 86 expect(cs.isDeprecated, equals(isDeprecated)); |
| 84 expect(cs.isPotential, equals(isPotential)); | 87 expect(cs.isPotential, equals(isPotential)); |
| 85 return cs; | 88 return cs; |
| 86 } | 89 } |
| 87 | 90 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 expect(element.name, equals(name)); | 218 expect(element.name, equals(name)); |
| 216 //TODO (danrubel) return type level variable 'type' but not as 'returnType' | 219 //TODO (danrubel) return type level variable 'type' but not as 'returnType' |
| 217 // expect( | 220 // expect( |
| 218 // element.returnType, | 221 // element.returnType, |
| 219 // equals(returnType != null ? returnType : 'dynamic')); | 222 // equals(returnType != null ? returnType : 'dynamic')); |
| 220 return cs; | 223 return cs; |
| 221 } | 224 } |
| 222 | 225 |
| 223 bool computeFast() { | 226 bool computeFast() { |
| 224 _computeFastCalled = true; | 227 _computeFastCalled = true; |
| 225 CompilationUnit unit = context.parseCompilationUnit(testSource); | 228 testUnit = context.parseCompilationUnit(testSource); |
| 226 request.unit = unit; | 229 completionNode = |
| 227 request.node = new NodeLocator.con1(completionOffset).searchWithin(unit); | 230 new NodeLocator.con1(completionOffset).searchWithin(testUnit); |
| 231 request.unit = testUnit; |
| 232 request.node = completionNode; |
| 228 return computer.computeFast(request); | 233 return computer.computeFast(request); |
| 229 } | 234 } |
| 230 | 235 |
| 231 Future<bool> computeFull([bool fullAnalysis = false]) { | 236 Future<bool> computeFull([bool fullAnalysis = false]) { |
| 232 if (!_computeFastCalled) { | 237 if (!_computeFastCalled) { |
| 233 expect(computeFast(), isFalse); | 238 expect(computeFast(), isFalse); |
| 234 } | 239 } |
| 235 | 240 |
| 236 // Index SDK | 241 // Index SDK |
| 237 for (Source librarySource in context.librarySources) { | 242 for (Source librarySource in context.librarySources) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 } | 282 } |
| 278 return computer.computeFull(request); | 283 return computer.computeFull(request); |
| 279 } | 284 } |
| 280 | 285 |
| 281 @override | 286 @override |
| 282 void setUp() { | 287 void setUp() { |
| 283 super.setUp(); | 288 super.setUp(); |
| 284 index = createLocalMemoryIndex(); | 289 index = createLocalMemoryIndex(); |
| 285 searchEngine = new SearchEngineImpl(index); | 290 searchEngine = new SearchEngineImpl(index); |
| 286 } | 291 } |
| 292 |
| 293 void _failWithNodeInfo(String message) { |
| 294 StringBuffer sb = new StringBuffer(message); |
| 295 if (completionNode != null) { |
| 296 sb.write('\n in'); |
| 297 AstNode node = completionNode; |
| 298 while (node != null) { |
| 299 sb.write('\n ${node.runtimeType}'); |
| 300 node = node.parent; |
| 301 } |
| 302 } |
| 303 fail(sb.toString()); |
| 304 } |
| 287 } | 305 } |
| OLD | NEW |