OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 'package:analyzer/src/context/cache.dart'; |
| 6 import 'package:analyzer/src/generated/engine.dart'; |
| 7 import 'package:analyzer/src/generated/source.dart'; |
| 8 import 'package:analyzer/src/summary/idl.dart'; |
| 9 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 10 import 'package:analyzer/src/task/dart.dart'; |
| 11 import 'package:analyzer/src/util/fast_uri.dart'; |
| 12 import 'package:analyzer/task/dart.dart'; |
| 13 import 'package:typed_mock/typed_mock.dart'; |
| 14 import 'package:unittest/unittest.dart'; |
| 15 |
| 16 import '../../reflective_tests.dart'; |
| 17 |
| 18 main() { |
| 19 groupSep = ' | '; |
| 20 runReflectiveTests(ResynthesizerResultProviderTest); |
| 21 runReflectiveTests(SummaryDataStoreTest); |
| 22 } |
| 23 |
| 24 UnlinkedPublicNamespace _namespaceWithParts(List<String> parts) { |
| 25 UnlinkedPublicNamespace namespace = new _UnlinkedPublicNamespaceMock(); |
| 26 when(namespace.parts).thenReturn(parts); |
| 27 return namespace; |
| 28 } |
| 29 |
| 30 @reflectiveTest |
| 31 class ResynthesizerResultProviderTest { |
| 32 SourceFactory sourceFactory = new _SourceFactoryMock(); |
| 33 InternalAnalysisContext context = new _InternalAnalysisContextMock(); |
| 34 UniversalCachePartition cachePartition; |
| 35 |
| 36 Source source1 = new _SourceMock('package:p1/u1.dart', '/p1/lib/u1.dart'); |
| 37 Source source2 = new _SourceMock('package:p1/u2.dart', '/p1/lib/u2.dart'); |
| 38 Source source3 = new _SourceMock('package:p2/u1.dart', '/p2/lib/u1.dart'); |
| 39 CacheEntry entry1; |
| 40 CacheEntry entry2; |
| 41 CacheEntry entry3; |
| 42 |
| 43 PackageBundle bundle = new _PackageBundleMock(); |
| 44 UnlinkedUnit unlinkedUnit1 = new _UnlinkedUnitMock(); |
| 45 UnlinkedUnit unlinkedUnit2 = new _UnlinkedUnitMock(); |
| 46 LinkedLibrary linkedLibrary = new _LinkedLibraryMock(); |
| 47 |
| 48 SummaryDataStore dataStore = new SummaryDataStore(<String>[]); |
| 49 _TestResynthesizerResultProvider provider; |
| 50 |
| 51 void setUp() { |
| 52 cachePartition = new UniversalCachePartition(context); |
| 53 entry1 = new CacheEntry(source1); |
| 54 entry2 = new CacheEntry(source2); |
| 55 entry3 = new CacheEntry(source3); |
| 56 cachePartition.put(entry1); |
| 57 cachePartition.put(entry2); |
| 58 cachePartition.put(entry3); |
| 59 |
| 60 when(sourceFactory.resolveUri(anyObject, 'package:p1/u1.dart')) |
| 61 .thenReturn(source1); |
| 62 when(sourceFactory.resolveUri(anyObject, 'package:p1/u2.dart')) |
| 63 .thenReturn(source2); |
| 64 when(context.sourceFactory).thenReturn(sourceFactory); |
| 65 |
| 66 when(bundle.unlinkedUnitUris) |
| 67 .thenReturn(<String>['package:p1/u1.dart', 'package:p1/u2.dart']); |
| 68 when(bundle.unlinkedUnits) |
| 69 .thenReturn(<UnlinkedUnit>[unlinkedUnit1, unlinkedUnit2]); |
| 70 when(bundle.linkedLibraryUris).thenReturn(<String>['package:p1/u1.dart']); |
| 71 when(bundle.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary]); |
| 72 dataStore.addBundle('/p1.ds', bundle); |
| 73 |
| 74 when(unlinkedUnit1.publicNamespace) |
| 75 .thenReturn(_namespaceWithParts(['package:p1/u2.dart'])); |
| 76 when(unlinkedUnit2.publicNamespace).thenReturn(_namespaceWithParts([])); |
| 77 |
| 78 provider = new _TestResynthesizerResultProvider(context, dataStore); |
| 79 provider.sourcesWithResults.add(source1); |
| 80 provider.sourcesWithResults.add(source2); |
| 81 } |
| 82 |
| 83 test_compute_CONTAINING_LIBRARIES_librarySource() { |
| 84 bool success = provider.compute(entry1, CONTAINING_LIBRARIES); |
| 85 expect(success, isTrue); |
| 86 expect(entry1.getValue(CONTAINING_LIBRARIES), unorderedEquals([source1])); |
| 87 } |
| 88 |
| 89 test_compute_CONTAINING_LIBRARIES_partSource() { |
| 90 bool success = provider.compute(entry2, CONTAINING_LIBRARIES); |
| 91 expect(success, isTrue); |
| 92 expect(entry2.getValue(CONTAINING_LIBRARIES), unorderedEquals([source1])); |
| 93 } |
| 94 |
| 95 test_compute_SOURCE_KIND_librarySource() { |
| 96 bool success = provider.compute(entry1, SOURCE_KIND); |
| 97 expect(success, isTrue); |
| 98 expect(entry1.getValue(SOURCE_KIND), SourceKind.LIBRARY); |
| 99 } |
| 100 |
| 101 test_compute_SOURCE_KIND_noResults() { |
| 102 bool success = provider.compute(entry3, SOURCE_KIND); |
| 103 expect(success, isFalse); |
| 104 expect(entry3.getState(SOURCE_KIND), CacheState.INVALID); |
| 105 } |
| 106 |
| 107 test_compute_SOURCE_KIND_partSource() { |
| 108 bool success = provider.compute(entry2, SOURCE_KIND); |
| 109 expect(success, isTrue); |
| 110 expect(entry2.getValue(SOURCE_KIND), SourceKind.PART); |
| 111 } |
| 112 } |
| 113 |
| 114 @reflectiveTest |
| 115 class SummaryDataStoreTest { |
| 116 SummaryDataStore dataStore = new SummaryDataStore(<String>[]); |
| 117 |
| 118 PackageBundle bundle1 = new _PackageBundleMock(); |
| 119 PackageBundle bundle2 = new _PackageBundleMock(); |
| 120 UnlinkedUnit unlinkedUnit11 = new _UnlinkedUnitMock(); |
| 121 UnlinkedUnit unlinkedUnit12 = new _UnlinkedUnitMock(); |
| 122 UnlinkedUnit unlinkedUnit21 = new _UnlinkedUnitMock(); |
| 123 LinkedLibrary linkedLibrary1 = new _LinkedLibraryMock(); |
| 124 LinkedLibrary linkedLibrary2 = new _LinkedLibraryMock(); |
| 125 |
| 126 void setUp() { |
| 127 // bundle1 |
| 128 when(unlinkedUnit11.publicNamespace) |
| 129 .thenReturn(_namespaceWithParts(['package:p1/u2.dart'])); |
| 130 when(unlinkedUnit12.publicNamespace).thenReturn(_namespaceWithParts([])); |
| 131 when(bundle1.unlinkedUnitUris) |
| 132 .thenReturn(<String>['package:p1/u1.dart', 'package:p1/u2.dart']); |
| 133 when(bundle1.unlinkedUnits) |
| 134 .thenReturn(<UnlinkedUnit>[unlinkedUnit11, unlinkedUnit12]); |
| 135 when(bundle1.linkedLibraryUris).thenReturn(<String>['package:p1/u1.dart']); |
| 136 when(bundle1.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary1]); |
| 137 dataStore.addBundle('/p1.ds', bundle1); |
| 138 // bundle2 |
| 139 when(unlinkedUnit21.publicNamespace).thenReturn(_namespaceWithParts([])); |
| 140 when(bundle2.unlinkedUnitUris).thenReturn(<String>['package:p2/u1.dart']); |
| 141 when(bundle2.unlinkedUnits).thenReturn(<UnlinkedUnit>[unlinkedUnit21]); |
| 142 when(bundle2.linkedLibraryUris).thenReturn(<String>['package:p2/u1.dart']); |
| 143 when(bundle2.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary2]); |
| 144 dataStore.addBundle('/p2.ds', bundle2); |
| 145 } |
| 146 |
| 147 test_addBundle() { |
| 148 expect(dataStore.bundles, unorderedEquals([bundle1, bundle2])); |
| 149 expect(dataStore.uriToSummaryPath, |
| 150 containsPair('package:p1/u1.dart', '/p1.ds')); |
| 151 // unlinkedMap |
| 152 expect(dataStore.unlinkedMap, hasLength(3)); |
| 153 expect(dataStore.unlinkedMap, |
| 154 containsPair('package:p1/u1.dart', unlinkedUnit11)); |
| 155 expect(dataStore.unlinkedMap, |
| 156 containsPair('package:p1/u2.dart', unlinkedUnit12)); |
| 157 expect(dataStore.unlinkedMap, |
| 158 containsPair('package:p2/u1.dart', unlinkedUnit21)); |
| 159 // linkedMap |
| 160 expect(dataStore.linkedMap, hasLength(2)); |
| 161 expect(dataStore.linkedMap, |
| 162 containsPair('package:p1/u1.dart', linkedLibrary1)); |
| 163 expect(dataStore.linkedMap, |
| 164 containsPair('package:p2/u1.dart', linkedLibrary2)); |
| 165 } |
| 166 |
| 167 test_getContainingLibraryUris_libraryUri() { |
| 168 String partUri = 'package:p1/u1.dart'; |
| 169 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 170 expect(uris, unorderedEquals([partUri])); |
| 171 } |
| 172 |
| 173 test_getContainingLibraryUris_partUri() { |
| 174 String partUri = 'package:p1/u2.dart'; |
| 175 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 176 expect(uris, unorderedEquals(['package:p1/u1.dart'])); |
| 177 } |
| 178 |
| 179 test_getContainingLibraryUris_unknownUri() { |
| 180 String partUri = 'package:notInStore/foo.dart'; |
| 181 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 182 expect(uris, isNull); |
| 183 } |
| 184 } |
| 185 |
| 186 class _InternalAnalysisContextMock extends TypedMock |
| 187 implements InternalAnalysisContext {} |
| 188 |
| 189 class _LinkedLibraryMock extends TypedMock implements LinkedLibrary {} |
| 190 |
| 191 class _PackageBundleMock extends TypedMock implements PackageBundle {} |
| 192 |
| 193 class _SourceFactoryMock extends TypedMock implements SourceFactory {} |
| 194 |
| 195 class _SourceMock implements Source { |
| 196 final Uri uri; |
| 197 final String fullName; |
| 198 |
| 199 _SourceMock(String uriStr, this.fullName) : uri = FastUri.parse(uriStr); |
| 200 |
| 201 @override |
| 202 Source get librarySource => null; |
| 203 |
| 204 @override |
| 205 Source get source => this; |
| 206 |
| 207 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 208 |
| 209 @override |
| 210 String toString() => '$uri ($fullName)'; |
| 211 } |
| 212 |
| 213 class _TestResynthesizerResultProvider extends ResynthesizerResultProvider { |
| 214 final Set<Source> sourcesWithResults = new Set<Source>(); |
| 215 |
| 216 _TestResynthesizerResultProvider( |
| 217 InternalAnalysisContext context, SummaryDataStore dataStore) |
| 218 : super(context, dataStore); |
| 219 |
| 220 @override |
| 221 bool hasResultsForSource(Source source) { |
| 222 return sourcesWithResults.contains(source); |
| 223 } |
| 224 } |
| 225 |
| 226 class _UnlinkedPublicNamespaceMock extends TypedMock |
| 227 implements UnlinkedPublicNamespace {} |
| 228 |
| 229 class _UnlinkedUnitMock extends TypedMock implements UnlinkedUnit {} |
OLD | NEW |