| 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:analyzer/task/general.dart'; |
| 14 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 15 import 'package:typed_mock/typed_mock.dart'; |
| 16 import 'package:unittest/unittest.dart'; |
| 17 |
| 18 main() { |
| 19 groupSep = ' | '; |
| 20 defineReflectiveTests(ResynthesizerResultProviderTest); |
| 21 defineReflectiveTests(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_LINE_INFO_emptyLineStarts() { |
| 96 when(unlinkedUnit1.lineStarts).thenReturn(<int>[]); |
| 97 bool success = provider.compute(entry1, LINE_INFO); |
| 98 expect(success, isFalse); |
| 99 } |
| 100 |
| 101 test_compute_LINE_INFO_hasLineStarts() { |
| 102 when(unlinkedUnit1.lineStarts).thenReturn(<int>[10, 20, 30]); |
| 103 bool success = provider.compute(entry1, LINE_INFO); |
| 104 expect(success, isTrue); |
| 105 expect(entry1.getValue(LINE_INFO).lineStarts, <int>[10, 20, 30]); |
| 106 } |
| 107 |
| 108 test_compute_MODIFICATION_TIME_hasResult() { |
| 109 bool success = provider.compute(entry1, MODIFICATION_TIME); |
| 110 expect(success, isTrue); |
| 111 expect(entry1.getValue(MODIFICATION_TIME), 0); |
| 112 } |
| 113 |
| 114 test_compute_MODIFICATION_TIME_noResult() { |
| 115 bool success = provider.compute(entry3, MODIFICATION_TIME); |
| 116 expect(success, isFalse); |
| 117 expect(entry3.getState(MODIFICATION_TIME), CacheState.INVALID); |
| 118 } |
| 119 |
| 120 test_compute_SOURCE_KIND_librarySource() { |
| 121 bool success = provider.compute(entry1, SOURCE_KIND); |
| 122 expect(success, isTrue); |
| 123 expect(entry1.getValue(SOURCE_KIND), SourceKind.LIBRARY); |
| 124 } |
| 125 |
| 126 test_compute_SOURCE_KIND_noResults() { |
| 127 bool success = provider.compute(entry3, SOURCE_KIND); |
| 128 expect(success, isFalse); |
| 129 expect(entry3.getState(SOURCE_KIND), CacheState.INVALID); |
| 130 } |
| 131 |
| 132 test_compute_SOURCE_KIND_partSource() { |
| 133 bool success = provider.compute(entry2, SOURCE_KIND); |
| 134 expect(success, isTrue); |
| 135 expect(entry2.getValue(SOURCE_KIND), SourceKind.PART); |
| 136 } |
| 137 } |
| 138 |
| 139 @reflectiveTest |
| 140 class SummaryDataStoreTest { |
| 141 SummaryDataStore dataStore = |
| 142 new SummaryDataStore(<String>[], recordDependencyInfo: true); |
| 143 |
| 144 PackageBundle bundle1 = new _PackageBundleMock(); |
| 145 PackageBundle bundle2 = new _PackageBundleMock(); |
| 146 UnlinkedUnit unlinkedUnit11 = new _UnlinkedUnitMock(); |
| 147 UnlinkedUnit unlinkedUnit12 = new _UnlinkedUnitMock(); |
| 148 UnlinkedUnit unlinkedUnit21 = new _UnlinkedUnitMock(); |
| 149 LinkedLibrary linkedLibrary1 = new _LinkedLibraryMock(); |
| 150 LinkedLibrary linkedLibrary2 = new _LinkedLibraryMock(); |
| 151 |
| 152 void setUp() { |
| 153 // bundle1 |
| 154 when(unlinkedUnit11.publicNamespace) |
| 155 .thenReturn(_namespaceWithParts(['package:p1/u2.dart'])); |
| 156 when(unlinkedUnit12.publicNamespace).thenReturn(_namespaceWithParts([])); |
| 157 when(bundle1.unlinkedUnitUris) |
| 158 .thenReturn(<String>['package:p1/u1.dart', 'package:p1/u2.dart']); |
| 159 when(bundle1.unlinkedUnits) |
| 160 .thenReturn(<UnlinkedUnit>[unlinkedUnit11, unlinkedUnit12]); |
| 161 when(bundle1.linkedLibraryUris).thenReturn(<String>['package:p1/u1.dart']); |
| 162 when(bundle1.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary1]); |
| 163 when(bundle1.apiSignature).thenReturn('signature1'); |
| 164 dataStore.addBundle('/p1.ds', bundle1); |
| 165 // bundle2 |
| 166 when(unlinkedUnit21.publicNamespace).thenReturn(_namespaceWithParts([])); |
| 167 when(bundle2.unlinkedUnitUris).thenReturn(<String>['package:p2/u1.dart']); |
| 168 when(bundle2.unlinkedUnits).thenReturn(<UnlinkedUnit>[unlinkedUnit21]); |
| 169 when(bundle2.linkedLibraryUris).thenReturn(<String>['package:p2/u1.dart']); |
| 170 when(bundle2.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary2]); |
| 171 when(bundle2.apiSignature).thenReturn('signature2'); |
| 172 dataStore.addBundle('/p2.ds', bundle2); |
| 173 } |
| 174 |
| 175 test_addBundle() { |
| 176 expect(dataStore.bundles, unorderedEquals([bundle1, bundle2])); |
| 177 expect(dataStore.dependencies[0].summaryPath, '/p1.ds'); |
| 178 expect(dataStore.dependencies[0].apiSignature, 'signature1'); |
| 179 expect(dataStore.dependencies[0].includedPackageNames, ['p1']); |
| 180 expect(dataStore.dependencies[0].includesFileUris, false); |
| 181 expect(dataStore.dependencies[0].includesDartUris, false); |
| 182 expect(dataStore.dependencies[1].summaryPath, '/p2.ds'); |
| 183 expect(dataStore.dependencies[1].apiSignature, 'signature2'); |
| 184 expect(dataStore.dependencies[1].includedPackageNames, ['p2']); |
| 185 expect(dataStore.dependencies[1].includesFileUris, false); |
| 186 expect(dataStore.dependencies[1].includesDartUris, false); |
| 187 expect(dataStore.uriToSummaryPath, |
| 188 containsPair('package:p1/u1.dart', '/p1.ds')); |
| 189 // unlinkedMap |
| 190 expect(dataStore.unlinkedMap, hasLength(3)); |
| 191 expect(dataStore.unlinkedMap, |
| 192 containsPair('package:p1/u1.dart', unlinkedUnit11)); |
| 193 expect(dataStore.unlinkedMap, |
| 194 containsPair('package:p1/u2.dart', unlinkedUnit12)); |
| 195 expect(dataStore.unlinkedMap, |
| 196 containsPair('package:p2/u1.dart', unlinkedUnit21)); |
| 197 // linkedMap |
| 198 expect(dataStore.linkedMap, hasLength(2)); |
| 199 expect(dataStore.linkedMap, |
| 200 containsPair('package:p1/u1.dart', linkedLibrary1)); |
| 201 expect(dataStore.linkedMap, |
| 202 containsPair('package:p2/u1.dart', linkedLibrary2)); |
| 203 } |
| 204 |
| 205 test_addBundle_dartUris() { |
| 206 PackageBundle bundle = new _PackageBundleMock(); |
| 207 when(bundle.unlinkedUnitUris).thenReturn(<String>['dart:core']); |
| 208 when(bundle.unlinkedUnits).thenReturn(<UnlinkedUnit>[unlinkedUnit11]); |
| 209 when(bundle.linkedLibraryUris).thenReturn(<String>['dart:core']); |
| 210 when(bundle.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary1]); |
| 211 when(bundle.apiSignature).thenReturn('signature'); |
| 212 dataStore.addBundle('/p3.ds', bundle); |
| 213 expect(dataStore.dependencies.last.includedPackageNames, []); |
| 214 expect(dataStore.dependencies.last.includesFileUris, false); |
| 215 expect(dataStore.dependencies.last.includesDartUris, true); |
| 216 } |
| 217 |
| 218 test_addBundle_fileUris() { |
| 219 PackageBundle bundle = new _PackageBundleMock(); |
| 220 when(bundle.unlinkedUnitUris).thenReturn(<String>['file:/foo.dart']); |
| 221 when(bundle.unlinkedUnits).thenReturn(<UnlinkedUnit>[unlinkedUnit11]); |
| 222 when(bundle.linkedLibraryUris).thenReturn(<String>['file:/foo.dart']); |
| 223 when(bundle.linkedLibraries).thenReturn(<LinkedLibrary>[linkedLibrary1]); |
| 224 when(bundle.apiSignature).thenReturn('signature'); |
| 225 dataStore.addBundle('/p3.ds', bundle); |
| 226 expect(dataStore.dependencies.last.includedPackageNames, []); |
| 227 expect(dataStore.dependencies.last.includesFileUris, true); |
| 228 expect(dataStore.dependencies.last.includesDartUris, false); |
| 229 } |
| 230 |
| 231 test_addBundle_multiProject() { |
| 232 PackageBundle bundle = new _PackageBundleMock(); |
| 233 when(bundle.unlinkedUnitUris) |
| 234 .thenReturn(<String>['package:p2/u1.dart', 'package:p1/u1.dart']); |
| 235 when(bundle.unlinkedUnits) |
| 236 .thenReturn(<UnlinkedUnit>[unlinkedUnit21, unlinkedUnit11]); |
| 237 when(bundle.linkedLibraryUris) |
| 238 .thenReturn(<String>['package:p2/u1.dart', 'package:p1/u1.dart']); |
| 239 when(bundle.linkedLibraries) |
| 240 .thenReturn(<LinkedLibrary>[linkedLibrary2, linkedLibrary1]); |
| 241 when(bundle.apiSignature).thenReturn('signature'); |
| 242 dataStore.addBundle('/p3.ds', bundle); |
| 243 expect(dataStore.dependencies.last.includedPackageNames, ['p1', 'p2']); |
| 244 } |
| 245 |
| 246 test_getContainingLibraryUris_libraryUri() { |
| 247 String partUri = 'package:p1/u1.dart'; |
| 248 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 249 expect(uris, unorderedEquals([partUri])); |
| 250 } |
| 251 |
| 252 test_getContainingLibraryUris_partUri() { |
| 253 String partUri = 'package:p1/u2.dart'; |
| 254 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 255 expect(uris, unorderedEquals(['package:p1/u1.dart'])); |
| 256 } |
| 257 |
| 258 test_getContainingLibraryUris_unknownUri() { |
| 259 String partUri = 'package:notInStore/foo.dart'; |
| 260 List<String> uris = dataStore.getContainingLibraryUris(partUri); |
| 261 expect(uris, isNull); |
| 262 } |
| 263 } |
| 264 |
| 265 class _InternalAnalysisContextMock extends TypedMock |
| 266 implements InternalAnalysisContext {} |
| 267 |
| 268 class _LinkedLibraryMock extends TypedMock implements LinkedLibrary {} |
| 269 |
| 270 class _PackageBundleMock extends TypedMock implements PackageBundle {} |
| 271 |
| 272 class _SourceFactoryMock extends TypedMock implements SourceFactory {} |
| 273 |
| 274 class _SourceMock implements Source { |
| 275 final Uri uri; |
| 276 final String fullName; |
| 277 |
| 278 _SourceMock(String uriStr, this.fullName) : uri = FastUri.parse(uriStr); |
| 279 |
| 280 @override |
| 281 Source get librarySource => null; |
| 282 |
| 283 @override |
| 284 Source get source => this; |
| 285 |
| 286 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 287 |
| 288 @override |
| 289 String toString() => '$uri ($fullName)'; |
| 290 } |
| 291 |
| 292 class _TestResynthesizerResultProvider extends ResynthesizerResultProvider { |
| 293 final Set<Source> sourcesWithResults = new Set<Source>(); |
| 294 |
| 295 _TestResynthesizerResultProvider( |
| 296 InternalAnalysisContext context, SummaryDataStore dataStore) |
| 297 : super(context, dataStore); |
| 298 |
| 299 @override |
| 300 bool hasResultsForSource(Source source) { |
| 301 return sourcesWithResults.contains(source); |
| 302 } |
| 303 } |
| 304 |
| 305 class _UnlinkedPublicNamespaceMock extends TypedMock |
| 306 implements UnlinkedPublicNamespace {} |
| 307 |
| 308 class _UnlinkedUnitMock extends TypedMock implements UnlinkedUnit {} |
| OLD | NEW |