| 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'; |
| 8 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; |
| 9 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 7 import 'package:analysis_server/src/services/completion/imported_computer.dart'; | 10 import 'package:analysis_server/src/services/completion/imported_computer.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 8 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 9 | 13 |
| 10 import '../../reflective_tests.dart'; | 14 import '../../reflective_tests.dart'; |
| 11 import 'completion_test_util.dart'; | 15 import 'completion_test_util.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; | |
| 13 import 'package:analyzer/src/generated/engine.dart'; | |
| 14 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 15 | 16 |
| 16 main() { | 17 main() { |
| 17 groupSep = ' | '; | 18 groupSep = ' | '; |
| 18 runReflectiveTests(ImportedTypeComputerTest); | 19 runReflectiveTests(ImportedComputerTest); |
| 19 } | 20 } |
| 20 | 21 |
| 21 @ReflectiveTestCase() | 22 @ReflectiveTestCase() |
| 22 class ImportedTypeComputerTest extends AbstractSelectorSuggestionTest { | 23 class ImportedComputerTest extends AbstractSelectorSuggestionTest { |
| 23 | 24 |
| 24 @override | 25 void assertCached(String completion) { |
| 25 void setUpComputer() { | 26 DartCompletionCache cache = request.cache; |
| 26 computer = new ImportedComputer(); | 27 if (!isCached(cache.importedTypeSuggestions, completion) && |
| 28 !isCached(cache.importedVoidReturnSuggestions, completion) && |
| 29 !isCached(cache.libraryPrefixSuggestions, completion) && |
| 30 !isCached(cache.otherImportedSuggestions, completion)) { |
| 31 fail('expected $completion to be cached'); |
| 32 } |
| 27 } | 33 } |
| 28 | 34 |
| 29 /** | 35 /** |
| 30 * Assert that the ImportedComputer uses cached results to produce identical | 36 * Assert that the ImportedComputer uses cached results to produce identical |
| 31 * suggestions to the original set of suggestions. | 37 * suggestions to the original set of suggestions. |
| 32 */ | 38 */ |
| 39 @override |
| 33 void assertCachedCompute(_) { | 40 void assertCachedCompute(_) { |
| 34 expect(request.unit.element, isNotNull); | 41 expect(request.unit.element, isNotNull); |
| 35 List<CompletionSuggestion> oldSuggestions = request.suggestions; | 42 List<CompletionSuggestion> oldSuggestions = request.suggestions; |
| 36 /* | 43 /* |
| 37 * Simulate a source change to flush the cached compilation unit | 44 * Simulate a source change to flush the cached compilation unit |
| 38 */ | 45 */ |
| 39 ChangeSet changes = new ChangeSet(); | 46 ChangeSet changes = new ChangeSet(); |
| 40 changes.addedSource(testSource); | 47 changes.addedSource(testSource); |
| 41 context.applyChanges(changes); | 48 context.applyChanges(changes); |
| 42 /* | 49 /* |
| 43 * Calculate a new completion at the same location | 50 * Calculate a new completion at the same location |
| 44 */ | 51 */ |
| 45 setUpComputer(); | 52 setUpComputer(); |
| 46 request = new DartCompletionRequest( | 53 request = new DartCompletionRequest( |
| 47 context, | 54 context, |
| 48 searchEngine, | 55 searchEngine, |
| 49 testSource, | 56 testSource, |
| 50 completionOffset, | 57 completionOffset, |
| 51 cache); | 58 cache); |
| 52 expect(computeFast(), isTrue); | 59 expect(computeFast(), isTrue); |
| 53 expect(request.unit.element, isNull); | 60 expect(request.unit.element, isNull); |
| 54 List<CompletionSuggestion> newSuggestions = request.suggestions; | 61 List<CompletionSuggestion> newSuggestions = request.suggestions; |
| 55 expect(newSuggestions.length, oldSuggestions.length); | 62 if (newSuggestions.length == oldSuggestions.length) { |
| 56 oldSuggestions.forEach((CompletionSuggestion s) { | 63 if (!oldSuggestions.any( |
| 57 expect(newSuggestions.contains(s), isTrue); | 64 (CompletionSuggestion s) => !newSuggestions.contains(s))) { |
| 58 }); | 65 return; |
| 66 } |
| 67 } |
| 68 StringBuffer sb = new StringBuffer( |
| 69 'suggestions based upon cached results do not match expectations'); |
| 70 sb.write('\n Expected:'); |
| 71 oldSuggestions.toList() |
| 72 ..sort(suggestionComparator) |
| 73 ..forEach((CompletionSuggestion suggestion) { |
| 74 sb.write('\n ${suggestion.completion} -> $suggestion'); |
| 75 }); |
| 76 sb.write('\n Actual:'); |
| 77 newSuggestions.toList() |
| 78 ..sort(suggestionComparator) |
| 79 ..forEach((CompletionSuggestion suggestion) { |
| 80 sb.write('\n ${suggestion.completion} -> $suggestion'); |
| 81 }); |
| 82 fail(sb.toString()); |
| 83 } |
| 84 |
| 85 void assertNotCached(String completion) { |
| 86 DartCompletionCache cache = request.cache; |
| 87 if (isCached(cache.importedTypeSuggestions, completion) || |
| 88 isCached(cache.importedVoidReturnSuggestions, completion) || |
| 89 isCached(cache.libraryPrefixSuggestions, completion) || |
| 90 isCached(cache.otherImportedSuggestions, completion)) { |
| 91 fail('expected $completion NOT to be cached'); |
| 92 } |
| 93 } |
| 94 |
| 95 bool isCached(List<CompletionSuggestion> suggestions, String completion) => |
| 96 suggestions.any((CompletionSuggestion s) => s.completion == completion); |
| 97 |
| 98 @override |
| 99 void setUpComputer() { |
| 100 computer = new ImportedComputer(); |
| 59 } | 101 } |
| 60 | 102 |
| 61 @override | 103 @override |
| 62 test_ArgumentList() { | 104 test_ArgumentList() { |
| 63 return super.test_ArgumentList().then((_) { | 105 return super.test_ArgumentList().then((_) { |
| 64 expect(request.cache.importKey, "import '/libA.dart';"); | 106 expect(request.cache.importKey, "import '/libA.dart';"); |
| 65 }); | 107 }); |
| 66 } | 108 } |
| 67 | 109 |
| 68 @override | 110 @override |
| 69 test_ArgumentList_imported_function() { | 111 test_ArgumentList_imported_function() { |
| 70 return super.test_ArgumentList_imported_function().then((_) { | 112 return super.test_ArgumentList_imported_function().then((_) { |
| 71 expect(request.cache.importKey, "import '/libA.dart';"); | 113 expect(request.cache.importKey, "import '/libA.dart';"); |
| 72 }); | 114 }); |
| 73 } | 115 } |
| 74 | 116 |
| 75 @override | 117 @override |
| 76 test_AssignmentExpression_RHS() { | 118 test_AssignmentExpression_RHS() { |
| 77 return super.test_AssignmentExpression_RHS().then((_) { | 119 return super.test_AssignmentExpression_RHS().then((_) { |
| 78 expect(request.cache.importKey, ''); | 120 expect(request.cache.importKey, ''); |
| 79 }); | 121 }); |
| 80 } | 122 } |
| 81 | 123 |
| 82 @override | 124 @override |
| 83 test_Block() { | 125 test_Block() { |
| 84 return super.test_Block().then((_) { | 126 return super.test_Block().then((_) { |
| 85 expect(request.cache.importKey, 'import "/testAB.dart";import "/testCD.dar
t" hide D;import "/testEEF.dart" show EE;import "/testG.dart" as g;'); | 127 expect( |
| 128 request.cache.importKey, |
| 129 'import "/testAB.dart";import "/testCD.dart" hide D;import "/testEEF.d
art" show EE;import "/testG.dart" as g;'); |
| 130 assertCached('A'); |
| 131 assertCached('T3'); |
| 132 }); |
| 133 } |
| 134 |
| 135 @override |
| 136 test_Block_inherited_imported() { |
| 137 return super.test_Block_inherited_imported().then((_) { |
| 138 assertCached('E'); |
| 139 assertCached('F'); |
| 140 assertNotCached('e1'); |
| 141 assertNotCached('i2'); |
| 142 assertNotCached('m1'); |
| 86 }); | 143 }); |
| 87 } | 144 } |
| 88 } | 145 } |
| OLD | NEW |