| 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.search.element_references; | 5 library test.search.element_references; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/computer/element.dart'; | |
| 10 import 'package:analysis_server/src/constants.dart'; | 9 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analysis_server/src/search/search_domain.dart'; | |
| 13 import 'package:analysis_server/src/search/search_result.dart'; | 11 import 'package:analysis_server/src/search/search_result.dart'; |
| 14 import 'package:analysis_services/index/index.dart' show Index; | |
| 15 import 'package:analysis_services/index/local_memory_index.dart'; | |
| 16 import 'package:analysis_testing/reflective_tests.dart'; | 12 import 'package:analysis_testing/reflective_tests.dart'; |
| 17 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 18 | 14 |
| 19 import '../analysis_abstract.dart'; | 15 import 'abstract_search_domain.dart'; |
| 20 | 16 |
| 21 | 17 |
| 22 main() { | 18 main() { |
| 23 groupSep = ' | '; | 19 groupSep = ' | '; |
| 24 group('findElementReferences', () { | 20 group('findElementReferences', () { |
| 25 runReflectiveTests(ElementReferencesTest); | 21 runReflectiveTests(ElementReferencesTest); |
| 26 }); | 22 }); |
| 27 } | 23 } |
| 28 | 24 |
| 29 | 25 |
| 30 @ReflectiveTestCase() | 26 @ReflectiveTestCase() |
| 31 class ElementReferencesTest extends AbstractAnalysisTest { | 27 class ElementReferencesTest extends AbstractSearchDomainTest { |
| 32 String searchId; | |
| 33 bool searchDone = false; | |
| 34 List<SearchResult> results = <SearchResult>[]; | |
| 35 SearchResult result; | |
| 36 | |
| 37 void assertHasResult(SearchResultKind kind, String search, [int length]) { | |
| 38 int offset = findOffset(search); | |
| 39 if (length == null) { | |
| 40 length = findIdentifierLength(search); | |
| 41 } | |
| 42 findResult(kind, testFile, offset, length, true); | |
| 43 } | |
| 44 | |
| 45 void assertNoResult(SearchResultKind kind, String search, [int length]) { | |
| 46 int offset = findOffset(search); | |
| 47 if (length == null) { | |
| 48 length = findIdentifierLength(search); | |
| 49 } | |
| 50 findResult(kind, testFile, offset, length, false); | |
| 51 } | |
| 52 | |
| 53 @override | |
| 54 Index createIndex() { | |
| 55 return createLocalMemoryIndex(); | |
| 56 } | |
| 57 | |
| 58 Future findElementReferences(String search, bool includePotential) { | 28 Future findElementReferences(String search, bool includePotential) { |
| 59 int offset = findOffset(search); | 29 int offset = findOffset(search); |
| 60 return waitForTasksFinished().then((_) { | 30 return waitForTasksFinished().then((_) { |
| 61 Request request = new Request('0', SEARCH_FIND_ELEMENT_REFERENCES); | 31 Request request = new Request('0', SEARCH_FIND_ELEMENT_REFERENCES); |
| 62 request.setParameter(FILE, testFile); | 32 request.setParameter(FILE, testFile); |
| 63 request.setParameter(OFFSET, offset); | 33 request.setParameter(OFFSET, offset); |
| 64 request.setParameter(INCLUDE_POTENTIAL, includePotential); | 34 request.setParameter(INCLUDE_POTENTIAL, includePotential); |
| 65 Response response = handleSuccessfulRequest(request); | 35 Response response = handleSuccessfulRequest(request); |
| 66 searchId = response.getResult(ID); | 36 searchId = response.getResult(ID); |
| 67 results.clear(); | 37 results.clear(); |
| 68 return _waitForSearchResults(); | 38 return waitForSearchResults(); |
| 69 }); | 39 }); |
| 70 } | 40 } |
| 71 | 41 |
| 72 void findResult(SearchResultKind kind, String file, int offset, int length, | |
| 73 bool expected) { | |
| 74 for (SearchResult result in results) { | |
| 75 Location location = result.location; | |
| 76 if (result.kind == kind && | |
| 77 location.file == file && | |
| 78 location.offset == offset && | |
| 79 location.length == length) { | |
| 80 if (!expected) { | |
| 81 fail('Unexpected result $result in\n' + results.join('\n')); | |
| 82 } | |
| 83 this.result = result; | |
| 84 return; | |
| 85 } | |
| 86 } | |
| 87 if (expected) { | |
| 88 fail( | |
| 89 'Not found: "search" kind=$kind offset=$offset length=$length\nin\n' + | |
| 90 results.join('\n')); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 String getPathString(List<Element> path) { | |
| 95 return path.map((Element element) { | |
| 96 return '${element.kind} ${element.name}'; | |
| 97 }).join('\n'); | |
| 98 } | |
| 99 | |
| 100 void processNotification(Notification notification) { | |
| 101 if (notification.event == SEARCH_RESULTS) { | |
| 102 String id = notification.getParameter(ID); | |
| 103 if (id == searchId) { | |
| 104 for (Map<String, Object> json in notification.getParameter(RESULTS)) { | |
| 105 results.add(new SearchResult.fromJson(json)); | |
| 106 } | |
| 107 searchDone = notification.getParameter(LAST); | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 @override | |
| 113 void setUp() { | |
| 114 super.setUp(); | |
| 115 createProject(); | |
| 116 handler = new SearchDomainHandler(server); | |
| 117 } | |
| 118 | |
| 119 test_constructor_named() { | 42 test_constructor_named() { |
| 120 addTestFile(''' | 43 addTestFile(''' |
| 121 class A { | 44 class A { |
| 122 A.named(p); | 45 A.named(p); |
| 123 } | 46 } |
| 124 main() { | 47 main() { |
| 125 new A.named(1); | 48 new A.named(1); |
| 126 new A.named(2); | 49 new A.named(2); |
| 127 } | 50 } |
| 128 '''); | 51 '''); |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 T f; | 556 T f; |
| 634 T m() => null; | 557 T m() => null; |
| 635 } | 558 } |
| 636 '''); | 559 '''); |
| 637 return findElementReferences('T> {', false).then((_) { | 560 return findElementReferences('T> {', false).then((_) { |
| 638 expect(results, hasLength(2)); | 561 expect(results, hasLength(2)); |
| 639 assertHasResult(SearchResultKind.REFERENCE, 'T f;'); | 562 assertHasResult(SearchResultKind.REFERENCE, 'T f;'); |
| 640 assertHasResult(SearchResultKind.REFERENCE, 'T m()'); | 563 assertHasResult(SearchResultKind.REFERENCE, 'T m()'); |
| 641 }); | 564 }); |
| 642 } | 565 } |
| 643 | |
| 644 Future _waitForSearchResults() { | |
| 645 if (searchDone) { | |
| 646 return new Future.value(); | |
| 647 } | |
| 648 return new Future.delayed(Duration.ZERO, _waitForSearchResults); | |
| 649 } | |
| 650 } | 566 } |
| OLD | NEW |