| 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'
; |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; | 11 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; |
| 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 13 import 'package:analysis_server/src/services/completion/imported_computer.dart'; | 13 import 'package:analysis_server/src/services/completion/imported_computer.dart'; |
| 14 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
| 15 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
| 16 import 'package:analyzer/src/generated/engine.dart'; | 16 import 'package:analyzer/src/generated/engine.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:unittest/unittest.dart'; | 18 import 'package:unittest/unittest.dart'; |
| 19 | 19 |
| 20 import '../../abstract_context.dart'; | 20 import '../../abstract_context.dart'; |
| 21 import '../../reflective_tests.dart'; | 21 import '../../reflective_tests.dart'; |
| 22 import 'completion_test_util.dart'; | 22 import 'completion_test_util.dart'; |
| 23 | 23 |
| 24 main() { | 24 main() { |
| 25 groupSep = ' | '; | 25 groupSep = ' | '; |
| 26 runReflectiveTests(ImportedComputerTest); | 26 runReflectiveTests(ImportedReferenceContributorTest); |
| 27 } | 27 } |
| 28 | 28 |
| 29 @reflectiveTest | 29 @reflectiveTest |
| 30 class ImportedComputerTest extends AbstractSelectorSuggestionTest { | 30 class ImportedReferenceContributorTest extends AbstractSelectorSuggestionTest { |
| 31 void assertCached(String completion) { | 31 void assertCached(String completion) { |
| 32 DartCompletionCache cache = request.cache; | 32 DartCompletionCache cache = request.cache; |
| 33 if (!isCached(cache.importedTypeSuggestions, completion) && | 33 if (!isCached(cache.importedTypeSuggestions, completion) && |
| 34 !isCached(cache.importedVoidReturnSuggestions, completion) && | 34 !isCached(cache.importedVoidReturnSuggestions, completion) && |
| 35 !isCached(cache.libraryPrefixSuggestions, completion) && | 35 !isCached(cache.libraryPrefixSuggestions, completion) && |
| 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 ImportedReferenceContributor uses cached results |
| 43 * suggestions to the original set of suggestions. | 43 * to produce identical suggestions to the original set of suggestions. |
| 44 */ | 44 */ |
| 45 @override | 45 @override |
| 46 assertCachedCompute(_) { | 46 assertCachedCompute(_) { |
| 47 if (!(computer as ImportedComputer).shouldWaitForLowPrioritySuggestions) { | 47 if (!(contributor as ImportedReferenceContributor).shouldWaitForLowPriorityS
uggestions) { |
| 48 return null; | 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 setUpContributor(); |
| 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 | 68 |
| 69 void assertResultsFromCache(List<CompletionSuggestion> oldSuggestions) { | 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) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 91 fail(sb.toString()); | 91 fail(sb.toString()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 if (computeFast()) { | 94 if (computeFast()) { |
| 95 expect(request.unit.element, isNull); | 95 expect(request.unit.element, isNull); |
| 96 assertResultsFromCache(oldSuggestions); | 96 assertResultsFromCache(oldSuggestions); |
| 97 } else { | 97 } else { |
| 98 // Results from cache might need to be adjusted | 98 // Results from cache might need to be adjusted |
| 99 // if target is a function argument in an argument list | 99 // if target is a function argument in an argument list |
| 100 resolve(false); | 100 resolve(false); |
| 101 return computer.computeFull(request).then((bool result) { | 101 return contributor.computeFull(request).then((bool result) { |
| 102 expect(result, isTrue); | 102 expect(result, isTrue); |
| 103 expect(request.unit.element, isNotNull); | 103 expect(request.unit.element, isNotNull); |
| 104 assertResultsFromCache(oldSuggestions); | 104 assertResultsFromCache(oldSuggestions); |
| 105 }); | 105 }); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 void assertNotCached(String completion) { | 109 void assertNotCached(String completion) { |
| 110 DartCompletionCache cache = request.cache; | 110 DartCompletionCache cache = request.cache; |
| 111 if (isCached(cache.importedTypeSuggestions, completion) || | 111 if (isCached(cache.importedTypeSuggestions, completion) || |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 expect(element.parameters, isNull); | 167 expect(element.parameters, isNull); |
| 168 expect(element.returnType, isNull); | 168 expect(element.returnType, isNull); |
| 169 assertHasNoParameterInfo(cs); | 169 assertHasNoParameterInfo(cs); |
| 170 return cs; | 170 return cs; |
| 171 } | 171 } |
| 172 | 172 |
| 173 bool isCached(List<CompletionSuggestion> suggestions, String completion) => | 173 bool isCached(List<CompletionSuggestion> suggestions, String completion) => |
| 174 suggestions.any((CompletionSuggestion s) => s.completion == completion); | 174 suggestions.any((CompletionSuggestion s) => s.completion == completion); |
| 175 | 175 |
| 176 @override | 176 @override |
| 177 void setUpComputer() { | 177 void setUpContributor() { |
| 178 computer = new ImportedComputer(shouldWaitForLowPrioritySuggestions: true); | 178 contributor = new ImportedReferenceContributor(shouldWaitForLowPrioritySugge
stions: true); |
| 179 } | 179 } |
| 180 | 180 |
| 181 @override | 181 @override |
| 182 test_ArgumentList() { | 182 test_ArgumentList() { |
| 183 return super.test_ArgumentList().then((_) { | 183 return super.test_ArgumentList().then((_) { |
| 184 expect(request.cache.importKey, "import '/libA.dart';"); | 184 expect(request.cache.importKey, "import '/libA.dart';"); |
| 185 ClassElement objClassElem1 = request.cache.importedClassMap['Object']; | 185 ClassElement objClassElem1 = request.cache.importedClassMap['Object']; |
| 186 expect(objClassElem1, isNotNull); | 186 expect(objClassElem1, isNotNull); |
| 187 ClassElement objClassElem2 = request.cache.objectClassElement; | 187 ClassElement objClassElem2 = request.cache.objectClassElement; |
| 188 expect(objClassElem1, same(objClassElem2)); | 188 expect(objClassElem1, same(objClassElem2)); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 addTestSource(''' | 248 addTestSource(''' |
| 249 import "/testAB.dart"; | 249 import "/testAB.dart"; |
| 250 import "/testCD.dart" hide D; | 250 import "/testCD.dart" hide D; |
| 251 import "/testEEF.dart" show EE; | 251 import "/testEEF.dart" show EE; |
| 252 import "/testG.dart" as g; | 252 import "/testG.dart" as g; |
| 253 int T5; | 253 int T5; |
| 254 var _T6; | 254 var _T6; |
| 255 Z D2() {int x;} | 255 Z D2() {int x;} |
| 256 class X {a() {var f; {var x;} ^ var r;} void b() { }} | 256 class X {a() {var f; {var x;} ^ var r;} void b() { }} |
| 257 class Z { }'''); | 257 class Z { }'''); |
| 258 (computer as ImportedComputer).shouldWaitForLowPrioritySuggestions = false; | 258 (contributor as ImportedReferenceContributor).shouldWaitForLowPrioritySugges
tions = false; |
| 259 computeFast(); | 259 computeFast(); |
| 260 return computeFull((bool result) { | 260 return computeFull((bool result) { |
| 261 assertSuggestImportedClass('C'); | 261 assertSuggestImportedClass('C'); |
| 262 // Assert computer does not wait for or include low priority results | 262 // Assert contributor does not wait for or include low priority results |
| 263 // from non-imported libraries unless instructed to do so. | 263 // from non-imported libraries unless instructed to do so. |
| 264 assertNotSuggested('H'); | 264 assertNotSuggested('H'); |
| 265 }); | 265 }); |
| 266 } | 266 } |
| 267 | 267 |
| 268 test_function_parameters_mixed_required_and_named() { | 268 test_function_parameters_mixed_required_and_named() { |
| 269 addSource('/libA.dart', ''' | 269 addSource('/libA.dart', ''' |
| 270 void m(x, {int y}) {} | 270 void m(x, {int y}) {} |
| 271 '''); | 271 '''); |
| 272 addTestSource(''' | 272 addTestSource(''' |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 class ClassInLocalContext {int x;}'''); | 651 class ClassInLocalContext {int x;}'''); |
| 652 testFile = '/context1/completionTest.dart'; | 652 testFile = '/context1/completionTest.dart'; |
| 653 addTestSource(''' | 653 addTestSource(''' |
| 654 import "/context1/libA.dart"; | 654 import "/context1/libA.dart"; |
| 655 import "/foo.dart"; | 655 import "/foo.dart"; |
| 656 main() {C^} | 656 main() {C^} |
| 657 '''); | 657 '''); |
| 658 computeFast(); | 658 computeFast(); |
| 659 return computeFull((bool result) { | 659 return computeFull((bool result) { |
| 660 assertSuggestImportedClass('ClassInLocalContext'); | 660 assertSuggestImportedClass('ClassInLocalContext'); |
| 661 // Assert computer does not include results from 2nd context. | 661 // Assert contributor does not include results from 2nd context. |
| 662 assertNotSuggested('ClassFromAnotherContext'); | 662 assertNotSuggested('ClassFromAnotherContext'); |
| 663 }); | 663 }); |
| 664 } | 664 } |
| 665 | 665 |
| 666 test_no_parameters_field() { | 666 test_no_parameters_field() { |
| 667 addSource('/libA.dart', ''' | 667 addSource('/libA.dart', ''' |
| 668 class A { | 668 class A { |
| 669 int x; | 669 int x; |
| 670 } | 670 } |
| 671 '''); | 671 '''); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 } | 725 } |
| 726 | 726 |
| 727 @override | 727 @override |
| 728 test_partFile_TypeName2() { | 728 test_partFile_TypeName2() { |
| 729 return super.test_partFile_TypeName2().then((_) { | 729 return super.test_partFile_TypeName2().then((_) { |
| 730 expect(request.cache.importKey, | 730 expect(request.cache.importKey, |
| 731 'library libA;import "/testB.dart";part "/testA.dart";'); | 731 'library libA;import "/testB.dart";part "/testA.dart";'); |
| 732 }); | 732 }); |
| 733 } | 733 } |
| 734 } | 734 } |
| OLD | NEW |