| 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.toplevel; | 5 library test.services.completion.toplevel; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' as protocol | 7 import 'package:analysis_server/src/protocol.dart' as protocol |
| 8 show Element, ElementKind; | 8 show Element, ElementKind; |
| 9 import 'package:analysis_server/src/protocol.dart' hide Element, ElementKind; | 9 import 'package:analysis_server/src/protocol.dart' hide Element, ElementKind; |
| 10 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 10 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 !isCached(cache.otherImportedSuggestions, completion)) { | 36 !isCached(cache.otherImportedSuggestions, completion)) { |
| 37 fail('expected $completion to be cached'); | 37 fail('expected $completion to be cached'); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Assert that the ImportedComputer uses cached results to produce identical | 42 * Assert that the ImportedComputer uses cached results to produce identical |
| 43 * suggestions to the original set of suggestions. | 43 * suggestions to the original set of suggestions. |
| 44 */ | 44 */ |
| 45 @override | 45 @override |
| 46 void assertCachedCompute(_) { | 46 assertCachedCompute(_) { |
| 47 if (!(computer as ImportedComputer).shouldWaitForLowPrioritySuggestions) { | 47 if (!(computer as ImportedComputer).shouldWaitForLowPrioritySuggestions) { |
| 48 return; | 48 return null; |
| 49 } | 49 } |
| 50 expect(request.unit.element, isNotNull); | 50 expect(request.unit.element, isNotNull); |
| 51 List<CompletionSuggestion> oldSuggestions = request.suggestions; | 51 List<CompletionSuggestion> oldSuggestions = request.suggestions; |
| 52 /* | 52 /* |
| 53 * Simulate a source change to flush the cached compilation unit | 53 * Simulate a source change to flush the cached compilation unit |
| 54 */ | 54 */ |
| 55 ChangeSet changes = new ChangeSet(); | 55 ChangeSet changes = new ChangeSet(); |
| 56 changes.addedSource(testSource); | 56 changes.addedSource(testSource); |
| 57 context.applyChanges(changes); | 57 context.applyChanges(changes); |
| 58 /* | 58 /* |
| 59 * Calculate a new completion at the same location | 59 * Calculate a new completion at the same location |
| 60 */ | 60 */ |
| 61 setUpComputer(); | 61 setUpComputer(); |
| 62 int replacementOffset = request.replacementOffset; | 62 int replacementOffset = request.replacementOffset; |
| 63 int replacementLength = request.replacementLength; | 63 int replacementLength = request.replacementLength; |
| 64 request = new DartCompletionRequest(context, searchEngine, testSource, | 64 request = new DartCompletionRequest(context, searchEngine, testSource, |
| 65 completionOffset, cache, new CompletionPerformance()); | 65 completionOffset, cache, new CompletionPerformance()); |
| 66 request.replacementOffset = replacementOffset; | 66 request.replacementOffset = replacementOffset; |
| 67 request.replacementLength = replacementLength; | 67 request.replacementLength = replacementLength; |
| 68 expect(computeFast(), isTrue); | 68 |
| 69 expect(request.unit.element, isNull); | 69 void assertResultsFromCache(List<CompletionSuggestion> oldSuggestions) { |
| 70 List<CompletionSuggestion> newSuggestions = request.suggestions; | 70 List<CompletionSuggestion> newSuggestions = request.suggestions; |
| 71 if (newSuggestions.length == oldSuggestions.length) { | 71 if (newSuggestions.length == oldSuggestions.length) { |
| 72 if (!oldSuggestions | 72 if (!oldSuggestions |
| 73 .any((CompletionSuggestion s) => !newSuggestions.contains(s))) { | 73 .any((CompletionSuggestion s) => !newSuggestions.contains(s))) { |
| 74 return; | 74 return; |
| 75 } |
| 75 } | 76 } |
| 77 StringBuffer sb = new StringBuffer( |
| 78 'suggestions based upon cached results do not match expectations'); |
| 79 sb.write('\n Expected:'); |
| 80 oldSuggestions.toList() |
| 81 ..sort(suggestionComparator) |
| 82 ..forEach((CompletionSuggestion suggestion) { |
| 83 sb.write('\n ${suggestion.completion} -> $suggestion'); |
| 84 }); |
| 85 sb.write('\n Actual:'); |
| 86 newSuggestions.toList() |
| 87 ..sort(suggestionComparator) |
| 88 ..forEach((CompletionSuggestion suggestion) { |
| 89 sb.write('\n ${suggestion.completion} -> $suggestion'); |
| 90 }); |
| 91 fail(sb.toString()); |
| 76 } | 92 } |
| 77 StringBuffer sb = new StringBuffer( | 93 |
| 78 'suggestions based upon cached results do not match expectations'); | 94 if (computeFast()) { |
| 79 sb.write('\n Expected:'); | 95 expect(request.unit.element, isNull); |
| 80 oldSuggestions.toList() | 96 assertResultsFromCache(oldSuggestions); |
| 81 ..sort(suggestionComparator) | 97 } else { |
| 82 ..forEach((CompletionSuggestion suggestion) { | 98 // Results from cache might need to be adjusted |
| 83 sb.write('\n ${suggestion.completion} -> $suggestion'); | 99 // if target is a function argument in an argument list |
| 100 resolve(false); |
| 101 return computer.computeFull(request).then((bool result) { |
| 102 expect(result, isTrue); |
| 103 expect(request.unit.element, isNotNull); |
| 104 assertResultsFromCache(oldSuggestions); |
| 84 }); | 105 }); |
| 85 sb.write('\n Actual:'); | 106 } |
| 86 newSuggestions.toList() | |
| 87 ..sort(suggestionComparator) | |
| 88 ..forEach((CompletionSuggestion suggestion) { | |
| 89 sb.write('\n ${suggestion.completion} -> $suggestion'); | |
| 90 }); | |
| 91 fail(sb.toString()); | |
| 92 } | 107 } |
| 93 | 108 |
| 94 void assertNotCached(String completion) { | 109 void assertNotCached(String completion) { |
| 95 DartCompletionCache cache = request.cache; | 110 DartCompletionCache cache = request.cache; |
| 96 if (isCached(cache.importedTypeSuggestions, completion) || | 111 if (isCached(cache.importedTypeSuggestions, completion) || |
| 97 isCached(cache.importedVoidReturnSuggestions, completion) || | 112 isCached(cache.importedVoidReturnSuggestions, completion) || |
| 98 isCached(cache.libraryPrefixSuggestions, completion) || | 113 isCached(cache.libraryPrefixSuggestions, completion) || |
| 99 isCached(cache.otherImportedSuggestions, completion)) { | 114 isCached(cache.otherImportedSuggestions, completion)) { |
| 100 fail('expected $completion NOT to be cached'); | 115 fail('expected $completion NOT to be cached'); |
| 101 } | 116 } |
| 102 } | 117 } |
| 103 | 118 |
| 104 @override | 119 @override |
| 105 CompletionSuggestion assertSuggestImportedConstructor(String name) { | 120 CompletionSuggestion assertSuggestImportedConstructor(String name) { |
| 106 return assertSuggestConstructor(name); | 121 return assertSuggestConstructor(name); |
| 107 } | 122 } |
| 108 | 123 |
| 109 @override | 124 @override |
| 110 CompletionSuggestion assertSuggestImportedField(String name, String type, | 125 CompletionSuggestion assertSuggestImportedField(String name, String type, |
| 111 {int relevance: DART_RELEVANCE_INHERITED_FIELD}) { | 126 {int relevance: DART_RELEVANCE_INHERITED_FIELD}) { |
| 112 return assertSuggestField(name, type, relevance: relevance); | 127 return assertSuggestField(name, type, relevance: relevance); |
| 113 } | 128 } |
| 114 | 129 |
| 130 @override |
| 131 CompletionSuggestion assertSuggestImportedFunction( |
| 132 String name, String returnType, |
| 133 {CompletionSuggestionKind kind: CompletionSuggestionKind.INVOCATION, |
| 134 bool deprecated: false, int relevance: DART_RELEVANCE_DEFAULT}) { |
| 135 return assertSuggestFunction(name, returnType, |
| 136 kind: kind, deprecated: deprecated, relevance: relevance); |
| 137 } |
| 138 |
| 115 CompletionSuggestion assertSuggestImportedGetter( | 139 CompletionSuggestion assertSuggestImportedGetter( |
| 116 String name, String returnType, | 140 String name, String returnType, |
| 117 {int relevance: DART_RELEVANCE_INHERITED_ACCESSOR}) { | 141 {int relevance: DART_RELEVANCE_INHERITED_ACCESSOR}) { |
| 118 return assertSuggestGetter(name, returnType, relevance: relevance); | 142 return assertSuggestGetter(name, returnType, relevance: relevance); |
| 119 } | 143 } |
| 120 | 144 |
| 121 CompletionSuggestion assertSuggestImportedMethod( | 145 CompletionSuggestion assertSuggestImportedMethod( |
| 122 String name, String declaringType, String returnType, | 146 String name, String declaringType, String returnType, |
| 123 {int relevance: DART_RELEVANCE_INHERITED_METHOD}) { | 147 {int relevance: DART_RELEVANCE_INHERITED_METHOD}) { |
| 124 return assertSuggestMethod(name, declaringType, returnType, | 148 return assertSuggestMethod(name, declaringType, returnType, |
| (...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 } | 726 } |
| 703 | 727 |
| 704 @override | 728 @override |
| 705 test_partFile_TypeName2() { | 729 test_partFile_TypeName2() { |
| 706 return super.test_partFile_TypeName2().then((_) { | 730 return super.test_partFile_TypeName2().then((_) { |
| 707 expect(request.cache.importKey, | 731 expect(request.cache.importKey, |
| 708 'library libA;import "/testB.dart";part "/testA.dart";'); | 732 'library libA;import "/testB.dart";part "/testA.dart";'); |
| 709 }); | 733 }); |
| 710 } | 734 } |
| 711 } | 735 } |
| OLD | NEW |