OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 10 import 'package:analyzer_plugin/plugin/occurrences_mixin.dart'; |
| 11 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 12 import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
| 13 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 14 import 'package:analyzer_plugin/src/utilities/occurrences/occurrences.dart'; |
| 15 import 'package:analyzer_plugin/utilities/occurrences/occurrences.dart'; |
| 16 import 'package:path/src/context.dart'; |
| 17 import 'package:test/test.dart'; |
| 18 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 19 |
| 20 import 'mocks.dart'; |
| 21 |
| 22 void main() { |
| 23 defineReflectiveTests(OccurrencesMixinTest); |
| 24 } |
| 25 |
| 26 @reflectiveTest |
| 27 class OccurrencesMixinTest { |
| 28 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
| 29 |
| 30 String packagePath1; |
| 31 String filePath1; |
| 32 ContextRoot contextRoot1; |
| 33 |
| 34 MockChannel channel; |
| 35 _TestServerPlugin plugin; |
| 36 |
| 37 void setUp() { |
| 38 Context pathContext = resourceProvider.pathContext; |
| 39 |
| 40 packagePath1 = resourceProvider.convertPath('/package1'); |
| 41 filePath1 = pathContext.join(packagePath1, 'lib', 'test.dart'); |
| 42 resourceProvider.newFile(filePath1, ''); |
| 43 contextRoot1 = new ContextRoot(packagePath1, <String>[]); |
| 44 |
| 45 channel = new MockChannel(); |
| 46 plugin = new _TestServerPlugin(resourceProvider); |
| 47 plugin.start(channel); |
| 48 } |
| 49 |
| 50 test_sendOccurrencesNotification() async { |
| 51 await plugin.handleAnalysisSetContextRoots( |
| 52 new AnalysisSetContextRootsParams([contextRoot1])); |
| 53 |
| 54 Completer<Null> notificationReceived = new Completer<Null>(); |
| 55 channel.listen(null, onNotification: (Notification notification) { |
| 56 expect(notification, isNotNull); |
| 57 AnalysisOccurrencesParams params = |
| 58 new AnalysisOccurrencesParams.fromNotification(notification); |
| 59 expect(params.file, filePath1); |
| 60 List<Occurrences> occurrenceList = params.occurrences; |
| 61 expect(occurrenceList, hasLength(3)); |
| 62 |
| 63 void validate(String elementName, List<int> expectedOffsets) { |
| 64 for (Occurrences occurrences in occurrenceList) { |
| 65 if (occurrences.element.name == elementName) { |
| 66 expect(occurrences.offsets, expectedOffsets); |
| 67 expect(occurrences.length, elementName.length); |
| 68 return; |
| 69 } |
| 70 } |
| 71 fail('No occurence named $elementName'); |
| 72 } |
| 73 |
| 74 validate('method', [10, 30]); |
| 75 validate('C', [20, 40, 50, 60, 70]); |
| 76 validate('local', [80]); |
| 77 notificationReceived.complete(); |
| 78 }); |
| 79 await plugin.sendOccurrencesNotification(filePath1); |
| 80 await notificationReceived.future; |
| 81 } |
| 82 } |
| 83 |
| 84 class _TestOccurrencesContributor implements OccurrencesContributor { |
| 85 Map<Element, List<int>> elements; |
| 86 |
| 87 _TestOccurrencesContributor(this.elements); |
| 88 |
| 89 @override |
| 90 void computeOccurrences( |
| 91 OccurrencesRequest request, OccurrencesCollector collector) { |
| 92 elements.forEach((element, offsets) { |
| 93 for (int offset in offsets) { |
| 94 collector.addOccurrence(element, offset); |
| 95 } |
| 96 }); |
| 97 } |
| 98 } |
| 99 |
| 100 class _TestServerPlugin extends MockServerPlugin with OccurrencesMixin { |
| 101 _TestServerPlugin(ResourceProvider resourceProvider) |
| 102 : super(resourceProvider); |
| 103 |
| 104 @override |
| 105 List<OccurrencesContributor> getOccurrencesContributors(String path) { |
| 106 Element element1 = new Element(ElementKind.METHOD, 'method', 0); |
| 107 Element element2 = new Element(ElementKind.CLASS, 'C', 0); |
| 108 Element element3 = new Element(ElementKind.LOCAL_VARIABLE, 'local', 0); |
| 109 return <OccurrencesContributor>[ |
| 110 new _TestOccurrencesContributor({ |
| 111 element1: [10, 30], |
| 112 element2: [20, 40, 60] |
| 113 }), |
| 114 new _TestOccurrencesContributor({ |
| 115 element2: [50, 70], |
| 116 element3: [80] |
| 117 }) |
| 118 ]; |
| 119 } |
| 120 |
| 121 @override |
| 122 Future<OccurrencesRequest> getOccurrencesRequest(String path) async { |
| 123 AnalysisResult result = new AnalysisResult( |
| 124 null, null, path, null, null, null, null, null, null, null, null); |
| 125 return new DartOccurrencesRequestImpl(resourceProvider, result); |
| 126 } |
| 127 } |
OLD | NEW |