| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.engine.src.index.split_store; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_testing/mocks.dart'; | |
| 10 import 'package:analysis_testing/reflective_tests.dart'; | |
| 11 import 'package:analyzer/index/index.dart'; | |
| 12 import 'package:analyzer/src/generated/element.dart'; | |
| 13 import 'package:analyzer/src/generated/engine.dart'; | |
| 14 import 'package:analyzer/src/generated/source.dart'; | |
| 15 import 'package:analyzer/src/index/store/codec.dart'; | |
| 16 import 'package:analyzer/src/index/store/memory_node_manager.dart'; | |
| 17 import 'package:analyzer/src/index/store/split_store.dart'; | |
| 18 import 'package:typed_mock/typed_mock.dart'; | |
| 19 import 'package:unittest/unittest.dart'; | |
| 20 | |
| 21 import 'mocks.dart'; | |
| 22 import 'single_source_container.dart'; | |
| 23 | |
| 24 | |
| 25 main() { | |
| 26 groupSep = ' | '; | |
| 27 group('FileNodeManager', () { | |
| 28 runReflectiveTests(_FileNodeManagerTest); | |
| 29 }); | |
| 30 group('IndexNode', () { | |
| 31 runReflectiveTests(_IndexNodeTest); | |
| 32 }); | |
| 33 group('LocationData', () { | |
| 34 runReflectiveTests(_LocationDataTest); | |
| 35 }); | |
| 36 group('RelationKeyData', () { | |
| 37 runReflectiveTests(_RelationKeyDataTest); | |
| 38 }); | |
| 39 group('SplitIndexStore', () { | |
| 40 runReflectiveTests(_SplitIndexStoreTest); | |
| 41 }); | |
| 42 } | |
| 43 | |
| 44 | |
| 45 void _assertHasLocation(List<Location> locations, Element element, int offset, | |
| 46 int length) { | |
| 47 for (Location location in locations) { | |
| 48 if ((element == null || location.element == element) && location.offset == | |
| 49 offset && location.length == length) { | |
| 50 return; | |
| 51 } | |
| 52 } | |
| 53 fail('Expected to find Location' | |
| 54 '(element=$element, offset=$offset, length=$length)'); | |
| 55 } | |
| 56 | |
| 57 | |
| 58 @ReflectiveTestCase() | |
| 59 class _FileNodeManagerTest { | |
| 60 MockLogger logger = new MockLogger(); | |
| 61 StringCodec stringCodec = new StringCodec(); | |
| 62 RelationshipCodec relationshipCodec; | |
| 63 | |
| 64 AnalysisContext context = new MockAnalysisContext('context'); | |
| 65 ContextCodec contextCodec = new MockContextCodec(); | |
| 66 int contextId = 13; | |
| 67 | |
| 68 ElementCodec elementCodec = new MockElementCodec(); | |
| 69 int nextElementId = 0; | |
| 70 | |
| 71 FileNodeManager nodeManager; | |
| 72 FileManager fileManager = new _MockFileManager(); | |
| 73 | |
| 74 void setUp() { | |
| 75 relationshipCodec = new RelationshipCodec(stringCodec); | |
| 76 nodeManager = new FileNodeManager(fileManager, logger, stringCodec, | |
| 77 contextCodec, elementCodec, relationshipCodec); | |
| 78 when(contextCodec.encode(context)).thenReturn(contextId); | |
| 79 when(contextCodec.decode(contextId)).thenReturn(context); | |
| 80 } | |
| 81 | |
| 82 void test_clear() { | |
| 83 nodeManager.clear(); | |
| 84 verify(fileManager.clear()).once(); | |
| 85 } | |
| 86 | |
| 87 void test_getLocationCount_empty() { | |
| 88 expect(nodeManager.locationCount, 0); | |
| 89 } | |
| 90 | |
| 91 void test_getNode_contextNull() { | |
| 92 String name = '42.index'; | |
| 93 // record bytes | |
| 94 List<int> bytes; | |
| 95 when(fileManager.write(name, anyObject)).thenInvoke((name, bs) { | |
| 96 bytes = bs; | |
| 97 }); | |
| 98 // put Node | |
| 99 Future putFuture; | |
| 100 { | |
| 101 IndexNode node = new IndexNode(context, elementCodec, relationshipCodec); | |
| 102 putFuture = nodeManager.putNode(name, node); | |
| 103 } | |
| 104 // do in the "put" Future | |
| 105 putFuture.then((_) { | |
| 106 // force "null" context | |
| 107 when(contextCodec.decode(contextId)).thenReturn(null); | |
| 108 // prepare input bytes | |
| 109 when(fileManager.read(name)).thenReturn(new Future.value(bytes)); | |
| 110 // get Node | |
| 111 return nodeManager.getNode(name).then((IndexNode node) { | |
| 112 expect(node, isNull); | |
| 113 // no exceptions | |
| 114 verifyZeroInteractions(logger); | |
| 115 }); | |
| 116 }); | |
| 117 } | |
| 118 | |
| 119 test_getNode_invalidVersion() { | |
| 120 String name = '42.index'; | |
| 121 // prepare a stream with an invalid version | |
| 122 when(fileManager.read(name)).thenReturn(new Future.value([0x01, 0x02, 0x03, | |
| 123 0x04])); | |
| 124 // do in the Future | |
| 125 return nodeManager.getNode(name).then((IndexNode node) { | |
| 126 // no IndexNode | |
| 127 expect(node, isNull); | |
| 128 // failed | |
| 129 verify(logger.logError2(anyObject, anyObject)).once(); | |
| 130 }); | |
| 131 } | |
| 132 | |
| 133 test_getNode_streamException() { | |
| 134 String name = '42.index'; | |
| 135 when(fileManager.read(name)).thenReturn(new Future(() { | |
| 136 return throw new Exception(); | |
| 137 })); | |
| 138 // do in the Future | |
| 139 return nodeManager.getNode(name).then((IndexNode node) { | |
| 140 expect(node, isNull); | |
| 141 // failed | |
| 142 verify(logger.logError2(anyString, anyObject)).once(); | |
| 143 }); | |
| 144 } | |
| 145 | |
| 146 test_getNode_streamNull() { | |
| 147 String name = '42.index'; | |
| 148 when(fileManager.read(name)).thenReturn(new Future.value(null)); | |
| 149 // do in the Future | |
| 150 return nodeManager.getNode(name).then((IndexNode node) { | |
| 151 expect(node, isNull); | |
| 152 // OK | |
| 153 verifyZeroInteractions(logger); | |
| 154 }); | |
| 155 } | |
| 156 | |
| 157 void test_newNode() { | |
| 158 IndexNode node = nodeManager.newNode(context); | |
| 159 expect(node.context, context); | |
| 160 expect(node.locationCount, 0); | |
| 161 } | |
| 162 | |
| 163 test_putNode_getNode() { | |
| 164 String name = '42.index'; | |
| 165 // record bytes | |
| 166 List<int> bytes; | |
| 167 when(fileManager.write(name, anyObject)).thenInvoke((name, bs) { | |
| 168 bytes = bs; | |
| 169 }); | |
| 170 // prepare elements | |
| 171 Element elementA = _mockElement(); | |
| 172 Element elementB = _mockElement(); | |
| 173 Element elementC = _mockElement(); | |
| 174 Relationship relationship = Relationship.getRelationship('my-relationship'); | |
| 175 // put Node | |
| 176 Future putFuture; | |
| 177 { | |
| 178 // prepare relations | |
| 179 int elementIdA = 0; | |
| 180 int elementIdB = 1; | |
| 181 int elementIdC = 2; | |
| 182 int relationshipId = relationshipCodec.encode(relationship); | |
| 183 RelationKeyData key = new RelationKeyData.forData(elementIdA, | |
| 184 relationshipId); | |
| 185 List<LocationData> locations = [new LocationData.forData(elementIdB, 1, | |
| 186 10), new LocationData.forData(elementIdC, 2, 20)]; | |
| 187 Map<RelationKeyData, List<LocationData>> relations = { | |
| 188 key: locations | |
| 189 }; | |
| 190 // prepare Node | |
| 191 IndexNode node = new _MockIndexNode(); | |
| 192 when(node.context).thenReturn(context); | |
| 193 when(node.relations).thenReturn(relations); | |
| 194 when(node.locationCount).thenReturn(2); | |
| 195 // put Node | |
| 196 putFuture = nodeManager.putNode(name, node); | |
| 197 } | |
| 198 // do in the Future | |
| 199 putFuture.then((_) { | |
| 200 // has locations | |
| 201 expect(nodeManager.locationCount, 2); | |
| 202 // prepare input bytes | |
| 203 when(fileManager.read(name)).thenReturn(new Future.value(bytes)); | |
| 204 // get Node | |
| 205 return nodeManager.getNode(name).then((IndexNode node) { | |
| 206 expect(2, node.locationCount); | |
| 207 { | |
| 208 List<Location> locations = node.getRelationships(elementA, | |
| 209 relationship); | |
| 210 expect(locations, hasLength(2)); | |
| 211 _assertHasLocation(locations, elementB, 1, 10); | |
| 212 _assertHasLocation(locations, elementC, 2, 20); | |
| 213 } | |
| 214 }); | |
| 215 }); | |
| 216 } | |
| 217 | |
| 218 test_putNode_streamException() { | |
| 219 String name = '42.index'; | |
| 220 Exception exception = new Exception(); | |
| 221 when(fileManager.write(name, anyObject)).thenReturn(new Future(() { | |
| 222 return throw exception; | |
| 223 })); | |
| 224 // prepare IndexNode | |
| 225 IndexNode node = new _MockIndexNode(); | |
| 226 when(node.context).thenReturn(context); | |
| 227 when(node.locationCount).thenReturn(0); | |
| 228 when(node.relations).thenReturn({}); | |
| 229 // try to put | |
| 230 return nodeManager.putNode(name, node).then((_) { | |
| 231 // failed | |
| 232 verify(logger.logError2(anyString, anyObject)).once(); | |
| 233 }); | |
| 234 } | |
| 235 | |
| 236 void test_removeNode() { | |
| 237 String name = '42.index'; | |
| 238 nodeManager.removeNode(name); | |
| 239 verify(fileManager.delete(name)).once(); | |
| 240 } | |
| 241 | |
| 242 Element _mockElement() { | |
| 243 int elementId = nextElementId++; | |
| 244 Element element = new MockElement(); | |
| 245 when(elementCodec.encode(element)).thenReturn(elementId); | |
| 246 when(elementCodec.decode(context, elementId)).thenReturn(element); | |
| 247 return element; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 | |
| 252 @ReflectiveTestCase() | |
| 253 class _IndexNodeTest { | |
| 254 AnalysisContext context = new MockAnalysisContext('context'); | |
| 255 ElementCodec elementCodec = new MockElementCodec(); | |
| 256 int nextElementId = 0; | |
| 257 IndexNode node; | |
| 258 RelationshipCodec relationshipCodec; | |
| 259 StringCodec stringCodec = new StringCodec(); | |
| 260 | |
| 261 void setUp() { | |
| 262 relationshipCodec = new RelationshipCodec(stringCodec); | |
| 263 node = new IndexNode(context, elementCodec, relationshipCodec); | |
| 264 } | |
| 265 | |
| 266 void test_getContext() { | |
| 267 expect(node.context, context); | |
| 268 } | |
| 269 | |
| 270 void test_recordRelationship() { | |
| 271 Element elementA = _mockElement(); | |
| 272 Element elementB = _mockElement(); | |
| 273 Element elementC = _mockElement(); | |
| 274 Relationship relationship = Relationship.getRelationship('my-relationship'); | |
| 275 Location locationA = new Location(elementB, 1, 2); | |
| 276 Location locationB = new Location(elementC, 10, 20); | |
| 277 // empty initially | |
| 278 expect(node.locationCount, 0); | |
| 279 // record | |
| 280 node.recordRelationship(elementA, relationship, locationA); | |
| 281 expect(node.locationCount, 1); | |
| 282 node.recordRelationship(elementA, relationship, locationB); | |
| 283 expect(node.locationCount, 2); | |
| 284 // get relations | |
| 285 expect(node.getRelationships(elementB, relationship), isEmpty); | |
| 286 { | |
| 287 List<Location> locations = node.getRelationships(elementA, relationship); | |
| 288 expect(locations, hasLength(2)); | |
| 289 _assertHasLocation(locations, null, 1, 2); | |
| 290 _assertHasLocation(locations, null, 10, 20); | |
| 291 } | |
| 292 // verify relations map | |
| 293 { | |
| 294 Map<RelationKeyData, List<LocationData>> relations = node.relations; | |
| 295 expect(relations, hasLength(1)); | |
| 296 List<LocationData> locations = relations.values.first; | |
| 297 expect(locations, hasLength(2)); | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 void test_setRelations() { | |
| 302 Element elementA = _mockElement(); | |
| 303 Element elementB = _mockElement(); | |
| 304 Element elementC = _mockElement(); | |
| 305 Relationship relationship = Relationship.getRelationship('my-relationship'); | |
| 306 // record | |
| 307 { | |
| 308 int elementIdA = 0; | |
| 309 int elementIdB = 1; | |
| 310 int elementIdC = 2; | |
| 311 int relationshipId = relationshipCodec.encode(relationship); | |
| 312 RelationKeyData key = new RelationKeyData.forData(elementIdA, | |
| 313 relationshipId); | |
| 314 List<LocationData> locations = [new LocationData.forData(elementIdB, 1, | |
| 315 10), new LocationData.forData(elementIdC, 2, 20)]; | |
| 316 node.relations = { | |
| 317 key: locations | |
| 318 }; | |
| 319 } | |
| 320 // request | |
| 321 List<Location> locations = node.getRelationships(elementA, relationship); | |
| 322 expect(locations, hasLength(2)); | |
| 323 _assertHasLocation(locations, elementB, 1, 10); | |
| 324 _assertHasLocation(locations, elementC, 2, 20); | |
| 325 } | |
| 326 | |
| 327 Element _mockElement() { | |
| 328 int elementId = nextElementId++; | |
| 329 Element element = new MockElement(); | |
| 330 when(elementCodec.encode(element)).thenReturn(elementId); | |
| 331 when(elementCodec.decode(context, elementId)).thenReturn(element); | |
| 332 return element; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 | |
| 337 @ReflectiveTestCase() | |
| 338 class _LocationDataTest { | |
| 339 AnalysisContext context = new MockAnalysisContext('context'); | |
| 340 ElementCodec elementCodec = new MockElementCodec(); | |
| 341 StringCodec stringCodec = new StringCodec(); | |
| 342 | |
| 343 void test_newForData() { | |
| 344 Element element = new MockElement(); | |
| 345 when(elementCodec.decode(context, 0)).thenReturn(element); | |
| 346 LocationData locationData = new LocationData.forData(0, 1, 2); | |
| 347 Location location = locationData.getLocation(context, elementCodec); | |
| 348 expect(location.element, element); | |
| 349 expect(location.offset, 1); | |
| 350 expect(location.length, 2); | |
| 351 } | |
| 352 | |
| 353 void test_newForObject() { | |
| 354 // prepare Element | |
| 355 Element element = new MockElement(); | |
| 356 when(elementCodec.encode(element)).thenReturn(42); | |
| 357 when(elementCodec.decode(context, 42)).thenReturn(element); | |
| 358 // create | |
| 359 Location location = new Location(element, 1, 2); | |
| 360 LocationData locationData = new LocationData.forObject(elementCodec, | |
| 361 location); | |
| 362 // touch 'hashCode' | |
| 363 locationData.hashCode; | |
| 364 // == | |
| 365 expect(locationData == new LocationData.forData(42, 1, 2), isTrue); | |
| 366 // getLocation() | |
| 367 { | |
| 368 Location newLocation = locationData.getLocation(context, elementCodec); | |
| 369 expect(location.element, element); | |
| 370 expect(location.offset, 1); | |
| 371 expect(location.length, 2); | |
| 372 } | |
| 373 // no Element - no Location | |
| 374 { | |
| 375 when(elementCodec.decode(context, 42)).thenReturn(null); | |
| 376 Location newLocation = locationData.getLocation(context, elementCodec); | |
| 377 expect(newLocation, isNull); | |
| 378 } | |
| 379 } | |
| 380 } | |
| 381 | |
| 382 | |
| 383 /** | |
| 384 * [Location] has no [==] and [hashCode], so to compare locations by value we | |
| 385 * need to wrap them into such object. | |
| 386 */ | |
| 387 class _LocationEqualsWrapper { | |
| 388 final Location location; | |
| 389 | |
| 390 _LocationEqualsWrapper(this.location); | |
| 391 | |
| 392 @override | |
| 393 int get hashCode { | |
| 394 return 31 * (31 * location.element.hashCode + location.offset) + | |
| 395 location.length; | |
| 396 } | |
| 397 | |
| 398 @override | |
| 399 bool operator ==(Object other) { | |
| 400 if (other is _LocationEqualsWrapper) { | |
| 401 return other.location.offset == location.offset && other.location.length | |
| 402 == location.length && other.location.element == location.element; | |
| 403 } | |
| 404 return false; | |
| 405 } | |
| 406 } | |
| 407 | |
| 408 | |
| 409 class _MockFileManager extends TypedMock implements FileManager { | |
| 410 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 411 } | |
| 412 | |
| 413 | |
| 414 class _MockIndexNode extends TypedMock implements IndexNode { | |
| 415 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 416 } | |
| 417 | |
| 418 | |
| 419 @ReflectiveTestCase() | |
| 420 class _RelationKeyDataTest { | |
| 421 AnalysisContext context = new MockAnalysisContext('context'); | |
| 422 ElementCodec elementCodec = new MockElementCodec(); | |
| 423 RelationshipCodec relationshipCodec = new MockRelationshipCodec(); | |
| 424 StringCodec stringCodec = new StringCodec(); | |
| 425 | |
| 426 void test_newFromData() { | |
| 427 RelationKeyData keyData = new RelationKeyData.forData(1, 2); | |
| 428 // equals | |
| 429 expect(keyData == this, isFalse); | |
| 430 expect(keyData == new RelationKeyData.forData(10, 20), isFalse); | |
| 431 expect(keyData == keyData, isTrue); | |
| 432 expect(keyData == new RelationKeyData.forData(1, 2), isTrue); | |
| 433 } | |
| 434 | |
| 435 void test_newFromObjects() { | |
| 436 // prepare Element | |
| 437 Element element; | |
| 438 int elementId = 2; | |
| 439 { | |
| 440 element = new MockElement(); | |
| 441 ElementLocation location = new ElementLocationImpl.con3(['foo', 'bar']); | |
| 442 when(element.location).thenReturn(location); | |
| 443 when(context.getElement(location)).thenReturn(element); | |
| 444 when(elementCodec.encode(element)).thenReturn(elementId); | |
| 445 } | |
| 446 // prepare relationship | |
| 447 Relationship relationship = Relationship.getRelationship('my-relationship'); | |
| 448 int relationshipId = 1; | |
| 449 when(relationshipCodec.encode(relationship)).thenReturn(relationshipId); | |
| 450 // create RelationKeyData | |
| 451 RelationKeyData keyData = new RelationKeyData.forObject(elementCodec, | |
| 452 relationshipCodec, element, relationship); | |
| 453 // touch | |
| 454 keyData.hashCode; | |
| 455 // equals | |
| 456 expect(keyData == this, isFalse); | |
| 457 expect(keyData == new RelationKeyData.forData(10, 20), isFalse); | |
| 458 expect(keyData == keyData, isTrue); | |
| 459 expect(keyData == new RelationKeyData.forData(elementId, relationshipId), | |
| 460 isTrue); | |
| 461 } | |
| 462 } | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 @ReflectiveTestCase() | |
| 468 class _SplitIndexStoreTest { | |
| 469 AnalysisContext contextA = new MockAnalysisContext('contextA'); | |
| 470 | |
| 471 AnalysisContext contextB = new MockAnalysisContext('contextB'); | |
| 472 | |
| 473 AnalysisContext contextC = new MockAnalysisContext('contextC'); | |
| 474 | |
| 475 Element elementA = new MockElement('elementA'); | |
| 476 Element elementB = new MockElement('elementB'); | |
| 477 | |
| 478 Element elementC = new MockElement('elementC'); | |
| 479 Element elementD = new MockElement('elementD'); | |
| 480 ElementLocation elementLocationA = new ElementLocationImpl.con3( | |
| 481 ['/home/user/sourceA.dart', 'ClassA']); | |
| 482 ElementLocation elementLocationB = new ElementLocationImpl.con3( | |
| 483 ['/home/user/sourceB.dart', 'ClassB']); | |
| 484 ElementLocation elementLocationC = new ElementLocationImpl.con3( | |
| 485 ['/home/user/sourceC.dart', 'ClassC']); | |
| 486 ElementLocation elementLocationD = new ElementLocationImpl.con3( | |
| 487 ['/home/user/sourceD.dart', 'ClassD']); | |
| 488 HtmlElement htmlElementA = new MockHtmlElement(); | |
| 489 HtmlElement htmlElementB = new MockHtmlElement(); | |
| 490 LibraryElement libraryElement = new MockLibraryElement(); | |
| 491 Source librarySource = new MockSource('librarySource'); | |
| 492 CompilationUnitElement libraryUnitElement = new MockCompilationUnitElement(); | |
| 493 MemoryNodeManager nodeManager = new MemoryNodeManager(); | |
| 494 Relationship relationship = Relationship.getRelationship('test-relationship'); | |
| 495 Source sourceA = new MockSource('sourceA'); | |
| 496 Source sourceB = new MockSource('sourceB'); | |
| 497 Source sourceC = new MockSource('sourceC'); | |
| 498 Source sourceD = new MockSource('sourceD'); | |
| 499 SplitIndexStore store; | |
| 500 CompilationUnitElement unitElementA = new MockCompilationUnitElement(); | |
| 501 CompilationUnitElement unitElementB = new MockCompilationUnitElement(); | |
| 502 CompilationUnitElement unitElementC = new MockCompilationUnitElement(); | |
| 503 CompilationUnitElement unitElementD = new MockCompilationUnitElement(); | |
| 504 void setUp() { | |
| 505 store = new SplitIndexStore(nodeManager); | |
| 506 when(contextA.isDisposed).thenReturn(false); | |
| 507 when(contextB.isDisposed).thenReturn(false); | |
| 508 when(contextC.isDisposed).thenReturn(false); | |
| 509 when(contextA.getElement(elementLocationA)).thenReturn(elementA); | |
| 510 when(contextA.getElement(elementLocationB)).thenReturn(elementB); | |
| 511 when(contextA.getElement(elementLocationC)).thenReturn(elementC); | |
| 512 when(contextA.getElement(elementLocationD)).thenReturn(elementD); | |
| 513 when(sourceA.fullName).thenReturn('/home/user/sourceA.dart'); | |
| 514 when(sourceB.fullName).thenReturn('/home/user/sourceB.dart'); | |
| 515 when(sourceC.fullName).thenReturn('/home/user/sourceC.dart'); | |
| 516 when(sourceD.fullName).thenReturn('/home/user/sourceD.dart'); | |
| 517 when(elementA.context).thenReturn(contextA); | |
| 518 when(elementB.context).thenReturn(contextA); | |
| 519 when(elementC.context).thenReturn(contextA); | |
| 520 when(elementD.context).thenReturn(contextA); | |
| 521 when(elementA.location).thenReturn(elementLocationA); | |
| 522 when(elementB.location).thenReturn(elementLocationB); | |
| 523 when(elementC.location).thenReturn(elementLocationC); | |
| 524 when(elementD.location).thenReturn(elementLocationD); | |
| 525 when(elementA.enclosingElement).thenReturn(unitElementA); | |
| 526 when(elementB.enclosingElement).thenReturn(unitElementB); | |
| 527 when(elementC.enclosingElement).thenReturn(unitElementC); | |
| 528 when(elementD.enclosingElement).thenReturn(unitElementD); | |
| 529 when(elementA.source).thenReturn(sourceA); | |
| 530 when(elementB.source).thenReturn(sourceB); | |
| 531 when(elementC.source).thenReturn(sourceC); | |
| 532 when(elementD.source).thenReturn(sourceD); | |
| 533 when(elementA.library).thenReturn(libraryElement); | |
| 534 when(elementB.library).thenReturn(libraryElement); | |
| 535 when(elementC.library).thenReturn(libraryElement); | |
| 536 when(elementD.library).thenReturn(libraryElement); | |
| 537 when(unitElementA.source).thenReturn(sourceA); | |
| 538 when(unitElementB.source).thenReturn(sourceB); | |
| 539 when(unitElementC.source).thenReturn(sourceC); | |
| 540 when(unitElementD.source).thenReturn(sourceD); | |
| 541 when(unitElementA.library).thenReturn(libraryElement); | |
| 542 when(unitElementB.library).thenReturn(libraryElement); | |
| 543 when(unitElementC.library).thenReturn(libraryElement); | |
| 544 when(unitElementD.library).thenReturn(libraryElement); | |
| 545 when(htmlElementA.source).thenReturn(sourceA); | |
| 546 when(htmlElementB.source).thenReturn(sourceB); | |
| 547 // library | |
| 548 when(libraryUnitElement.library).thenReturn(libraryElement); | |
| 549 when(libraryUnitElement.source).thenReturn(librarySource); | |
| 550 when(libraryElement.source).thenReturn(librarySource); | |
| 551 when(libraryElement.definingCompilationUnit).thenReturn(libraryUnitElement); | |
| 552 } | |
| 553 | |
| 554 void test_aboutToIndexDart_disposedContext() { | |
| 555 when(contextA.isDisposed).thenReturn(true); | |
| 556 expect(store.aboutToIndexDart(contextA, unitElementA), isFalse); | |
| 557 } | |
| 558 | |
| 559 void test_aboutToIndexDart_disposedContext_wrapped() { | |
| 560 when(contextA.isDisposed).thenReturn(true); | |
| 561 InstrumentedAnalysisContextImpl instrumentedContext = | |
| 562 new MockInstrumentedAnalysisContextImpl(); | |
| 563 when(instrumentedContext.basis).thenReturn(contextA); | |
| 564 expect(store.aboutToIndexDart(instrumentedContext, unitElementA), isFalse); | |
| 565 } | |
| 566 | |
| 567 Future test_aboutToIndexDart_library_first() { | |
| 568 when(libraryElement.parts).thenReturn(<CompilationUnitElement>[unitElementA, | |
| 569 unitElementB]); | |
| 570 { | |
| 571 store.aboutToIndexDart(contextA, libraryUnitElement); | |
| 572 store.doneIndex(); | |
| 573 } | |
| 574 return store.getRelationships(elementA, relationship).then( | |
| 575 (List<Location> locations) { | |
| 576 assertLocations(locations, []); | |
| 577 }); | |
| 578 } | |
| 579 | |
| 580 test_aboutToIndexDart_library_secondWithoutOneUnit() { | |
| 581 Location locationA = mockLocation(elementA); | |
| 582 Location locationB = mockLocation(elementB); | |
| 583 { | |
| 584 store.aboutToIndexDart(contextA, unitElementA); | |
| 585 store.recordRelationship(elementA, relationship, locationA); | |
| 586 store.doneIndex(); | |
| 587 } | |
| 588 { | |
| 589 store.aboutToIndexDart(contextA, unitElementB); | |
| 590 store.recordRelationship(elementA, relationship, locationB); | |
| 591 store.doneIndex(); | |
| 592 } | |
| 593 // "A" and "B" locations | |
| 594 return store.getRelationships(elementA, relationship).then( | |
| 595 (List<Location> locations) { | |
| 596 assertLocations(locations, [locationA, locationB]); | |
| 597 // apply "libraryUnitElement", only with "B" | |
| 598 when(libraryElement.parts).thenReturn([unitElementB]); | |
| 599 { | |
| 600 store.aboutToIndexDart(contextA, libraryUnitElement); | |
| 601 store.doneIndex(); | |
| 602 } | |
| 603 }).then((_) { | |
| 604 return store.getRelationships(elementA, relationship).then( | |
| 605 (List<Location> locations) { | |
| 606 assertLocations(locations, [locationB]); | |
| 607 }); | |
| 608 }); | |
| 609 } | |
| 610 | |
| 611 void test_aboutToIndexDart_nullLibraryElement() { | |
| 612 when(unitElementA.library).thenReturn(null); | |
| 613 expect(store.aboutToIndexDart(contextA, unitElementA), isFalse); | |
| 614 } | |
| 615 | |
| 616 void test_aboutToIndexDart_nullLibraryUnitElement() { | |
| 617 when(libraryElement.definingCompilationUnit).thenReturn(null); | |
| 618 expect(store.aboutToIndexDart(contextA, unitElementA), isFalse); | |
| 619 } | |
| 620 | |
| 621 void test_aboutToIndexDart_nullUnitElement() { | |
| 622 expect(store.aboutToIndexDart(contextA, null), isFalse); | |
| 623 } | |
| 624 | |
| 625 test_aboutToIndexHtml_() { | |
| 626 Location locationA = mockLocation(elementA); | |
| 627 Location locationB = mockLocation(elementB); | |
| 628 { | |
| 629 store.aboutToIndexHtml(contextA, htmlElementA); | |
| 630 store.recordRelationship(elementA, relationship, locationA); | |
| 631 store.doneIndex(); | |
| 632 } | |
| 633 { | |
| 634 store.aboutToIndexHtml(contextA, htmlElementB); | |
| 635 store.recordRelationship(elementA, relationship, locationB); | |
| 636 store.doneIndex(); | |
| 637 } | |
| 638 // "A" and "B" locations | |
| 639 return store.getRelationships(elementA, relationship).then( | |
| 640 (List<Location> locations) { | |
| 641 assertLocations(locations, [locationA, locationB]); | |
| 642 }); | |
| 643 } | |
| 644 | |
| 645 void test_aboutToIndexHtml_disposedContext() { | |
| 646 when(contextA.isDisposed).thenReturn(true); | |
| 647 expect(store.aboutToIndexHtml(contextA, htmlElementA), isFalse); | |
| 648 } | |
| 649 | |
| 650 void test_clear() { | |
| 651 Location locationA = mockLocation(elementA); | |
| 652 store.aboutToIndexDart(contextA, unitElementA); | |
| 653 store.recordRelationship(elementA, relationship, locationA); | |
| 654 store.doneIndex(); | |
| 655 expect(nodeManager.isEmpty(), isFalse); | |
| 656 // clear | |
| 657 store.clear(); | |
| 658 expect(nodeManager.isEmpty(), isTrue); | |
| 659 } | |
| 660 | |
| 661 test_getRelationships_empty() { | |
| 662 return store.getRelationships(elementA, relationship).then( | |
| 663 (List<Location> locations) { | |
| 664 expect(locations, isEmpty); | |
| 665 }); | |
| 666 } | |
| 667 | |
| 668 void test_getStatistics() { | |
| 669 // empty initially | |
| 670 { | |
| 671 String statistics = store.statistics; | |
| 672 expect(statistics, contains('0 locations')); | |
| 673 expect(statistics, contains('0 sources')); | |
| 674 } | |
| 675 // add 2 locations | |
| 676 Location locationA = mockLocation(elementA); | |
| 677 Location locationB = mockLocation(elementB); | |
| 678 { | |
| 679 store.aboutToIndexDart(contextA, unitElementA); | |
| 680 store.recordRelationship(elementA, relationship, locationA); | |
| 681 store.doneIndex(); | |
| 682 } | |
| 683 { | |
| 684 store.aboutToIndexDart(contextA, unitElementB); | |
| 685 store.recordRelationship(elementA, relationship, locationB); | |
| 686 store.doneIndex(); | |
| 687 } | |
| 688 { | |
| 689 String statistics = store.statistics; | |
| 690 expect(statistics, contains('2 locations')); | |
| 691 expect(statistics, contains('3 sources')); | |
| 692 } | |
| 693 } | |
| 694 | |
| 695 void test_recordRelationship_nullElement() { | |
| 696 Location locationA = mockLocation(elementA); | |
| 697 store.recordRelationship(null, relationship, locationA); | |
| 698 store.doneIndex(); | |
| 699 expect(nodeManager.isEmpty(), isTrue); | |
| 700 } | |
| 701 | |
| 702 void test_recordRelationship_nullLocation() { | |
| 703 store.recordRelationship(elementA, relationship, null); | |
| 704 store.doneIndex(); | |
| 705 expect(nodeManager.isEmpty(), isTrue); | |
| 706 } | |
| 707 | |
| 708 test_recordRelationship_oneElement_twoNodes() { | |
| 709 Location locationA = mockLocation(elementA); | |
| 710 Location locationB = mockLocation(elementB); | |
| 711 { | |
| 712 store.aboutToIndexDart(contextA, unitElementA); | |
| 713 store.recordRelationship(elementA, relationship, locationA); | |
| 714 store.doneIndex(); | |
| 715 } | |
| 716 { | |
| 717 store.aboutToIndexDart(contextA, unitElementB); | |
| 718 store.recordRelationship(elementA, relationship, locationB); | |
| 719 store.doneIndex(); | |
| 720 } | |
| 721 return store.getRelationships(elementA, relationship).then( | |
| 722 (List<Location> locations) { | |
| 723 assertLocations(locations, [locationA, locationB]); | |
| 724 }); | |
| 725 } | |
| 726 | |
| 727 test_recordRelationship_oneLocation() { | |
| 728 Location locationA = mockLocation(elementA); | |
| 729 store.aboutToIndexDart(contextA, unitElementA); | |
| 730 store.recordRelationship(elementA, relationship, locationA); | |
| 731 store.doneIndex(); | |
| 732 return store.getRelationships(elementA, relationship).then( | |
| 733 (List<Location> locations) { | |
| 734 assertLocations(locations, [locationA]); | |
| 735 }); | |
| 736 } | |
| 737 | |
| 738 test_recordRelationship_twoLocations() { | |
| 739 Location locationA = mockLocation(elementA); | |
| 740 Location locationB = mockLocation(elementA); | |
| 741 store.aboutToIndexDart(contextA, unitElementA); | |
| 742 store.recordRelationship(elementA, relationship, locationA); | |
| 743 store.recordRelationship(elementA, relationship, locationB); | |
| 744 store.doneIndex(); | |
| 745 return store.getRelationships(elementA, relationship).then( | |
| 746 (List<Location> locations) { | |
| 747 assertLocations(locations, [locationA, locationB]); | |
| 748 }); | |
| 749 } | |
| 750 | |
| 751 test_removeContext() { | |
| 752 Location locationA = mockLocation(elementA); | |
| 753 Location locationB = mockLocation(elementB); | |
| 754 { | |
| 755 store.aboutToIndexDart(contextA, unitElementA); | |
| 756 store.recordRelationship(elementA, relationship, locationA); | |
| 757 store.doneIndex(); | |
| 758 } | |
| 759 { | |
| 760 store.aboutToIndexDart(contextA, unitElementB); | |
| 761 store.recordRelationship(elementA, relationship, locationB); | |
| 762 store.doneIndex(); | |
| 763 } | |
| 764 // "A" and "B" locations | |
| 765 return store.getRelationships(elementA, relationship).then( | |
| 766 (List<Location> locations) { | |
| 767 assertLocations(locations, [locationA, locationB]); | |
| 768 // remove "A" context | |
| 769 store.removeContext(contextA); | |
| 770 }).then((_) { | |
| 771 return store.getRelationships(elementA, relationship).then( | |
| 772 (List<Location> locations) { | |
| 773 assertLocations(locations, []); | |
| 774 }); | |
| 775 }); | |
| 776 } | |
| 777 | |
| 778 void test_removeContext_nullContext() { | |
| 779 store.removeContext(null); | |
| 780 } | |
| 781 | |
| 782 test_removeSource_library() { | |
| 783 Location locationA = mockLocation(elementA); | |
| 784 Location locationB = mockLocation(elementB); | |
| 785 Location locationC = mockLocation(elementC); | |
| 786 { | |
| 787 store.aboutToIndexDart(contextA, unitElementA); | |
| 788 store.recordRelationship(elementA, relationship, locationA); | |
| 789 store.doneIndex(); | |
| 790 } | |
| 791 { | |
| 792 store.aboutToIndexDart(contextA, unitElementB); | |
| 793 store.recordRelationship(elementA, relationship, locationB); | |
| 794 store.doneIndex(); | |
| 795 } | |
| 796 { | |
| 797 store.aboutToIndexDart(contextA, unitElementC); | |
| 798 store.recordRelationship(elementA, relationship, locationC); | |
| 799 store.doneIndex(); | |
| 800 } | |
| 801 // "A", "B" and "C" locations | |
| 802 return store.getRelationships(elementA, relationship).then( | |
| 803 (List<Location> locations) { | |
| 804 assertLocations(locations, [locationA, locationB, locationC]); | |
| 805 }).then((_) { | |
| 806 // remove "librarySource" | |
| 807 store.removeSource(contextA, librarySource); | |
| 808 return store.getRelationships(elementA, relationship).then( | |
| 809 (List<Location> locations) { | |
| 810 assertLocations(locations, []); | |
| 811 }); | |
| 812 }); | |
| 813 } | |
| 814 | |
| 815 void test_removeSource_nullContext() { | |
| 816 store.removeSource(null, sourceA); | |
| 817 } | |
| 818 | |
| 819 test_removeSource_unit() { | |
| 820 Location locationA = mockLocation(elementA); | |
| 821 Location locationB = mockLocation(elementB); | |
| 822 Location locationC = mockLocation(elementC); | |
| 823 { | |
| 824 store.aboutToIndexDart(contextA, unitElementA); | |
| 825 store.recordRelationship(elementA, relationship, locationA); | |
| 826 store.doneIndex(); | |
| 827 } | |
| 828 { | |
| 829 store.aboutToIndexDart(contextA, unitElementB); | |
| 830 store.recordRelationship(elementA, relationship, locationB); | |
| 831 store.doneIndex(); | |
| 832 } | |
| 833 { | |
| 834 store.aboutToIndexDart(contextA, unitElementC); | |
| 835 store.recordRelationship(elementA, relationship, locationC); | |
| 836 store.doneIndex(); | |
| 837 } | |
| 838 // "A", "B" and "C" locations | |
| 839 return store.getRelationships(elementA, relationship).then( | |
| 840 (List<Location> locations) { | |
| 841 assertLocations(locations, [locationA, locationB, locationC]); | |
| 842 }).then((_) { | |
| 843 // remove "A" source | |
| 844 store.removeSource(contextA, sourceA); | |
| 845 return store.getRelationships(elementA, relationship).then( | |
| 846 (List<Location> locations) { | |
| 847 assertLocations(locations, [locationB, locationC]); | |
| 848 }); | |
| 849 }); | |
| 850 } | |
| 851 | |
| 852 test_removeSources_library() { | |
| 853 Location locationA = mockLocation(elementA); | |
| 854 Location locationB = mockLocation(elementB); | |
| 855 { | |
| 856 store.aboutToIndexDart(contextA, unitElementA); | |
| 857 store.recordRelationship(elementA, relationship, locationA); | |
| 858 store.doneIndex(); | |
| 859 } | |
| 860 { | |
| 861 store.aboutToIndexDart(contextA, unitElementB); | |
| 862 store.recordRelationship(elementA, relationship, locationB); | |
| 863 store.doneIndex(); | |
| 864 } | |
| 865 // "A" and "B" locations | |
| 866 return store.getRelationships(elementA, relationship).then( | |
| 867 (List<Location> locations) { | |
| 868 assertLocations(locations, [locationA, locationB]); | |
| 869 }).then((_) { | |
| 870 // remove "librarySource" | |
| 871 store.removeSources(contextA, new SingleSourceContainer(librarySource)); | |
| 872 return store.getRelationships(elementA, relationship).then( | |
| 873 (List<Location> locations) { | |
| 874 assertLocations(locations, []); | |
| 875 }); | |
| 876 }); | |
| 877 } | |
| 878 | |
| 879 void test_removeSources_nullContext() { | |
| 880 store.removeSources(null, null); | |
| 881 } | |
| 882 | |
| 883 test_removeSources_unit() { | |
| 884 Location locationA = mockLocation(elementA); | |
| 885 Location locationB = mockLocation(elementB); | |
| 886 Location locationC = mockLocation(elementC); | |
| 887 { | |
| 888 store.aboutToIndexDart(contextA, unitElementA); | |
| 889 store.recordRelationship(elementA, relationship, locationA); | |
| 890 store.doneIndex(); | |
| 891 } | |
| 892 { | |
| 893 store.aboutToIndexDart(contextA, unitElementB); | |
| 894 store.recordRelationship(elementA, relationship, locationB); | |
| 895 store.doneIndex(); | |
| 896 } | |
| 897 { | |
| 898 store.aboutToIndexDart(contextA, unitElementC); | |
| 899 store.recordRelationship(elementA, relationship, locationC); | |
| 900 store.doneIndex(); | |
| 901 } | |
| 902 // "A", "B" and "C" locations | |
| 903 return store.getRelationships(elementA, relationship).then( | |
| 904 (List<Location> locations) { | |
| 905 assertLocations(locations, [locationA, locationB, locationC]); | |
| 906 }).then((_) { | |
| 907 // remove "A" source | |
| 908 store.removeSources(contextA, new SingleSourceContainer(sourceA)); | |
| 909 store.removeSource(contextA, sourceA); | |
| 910 return store.getRelationships(elementA, relationship).then( | |
| 911 (List<Location> locations) { | |
| 912 assertLocations(locations, [locationB, locationC]); | |
| 913 }); | |
| 914 }); | |
| 915 } | |
| 916 | |
| 917 test_universe_aboutToIndex() { | |
| 918 when(contextA.getElement(elementLocationA)).thenReturn(elementA); | |
| 919 when(contextB.getElement(elementLocationB)).thenReturn(elementB); | |
| 920 Location locationA = mockLocation(elementA); | |
| 921 Location locationB = mockLocation(elementB); | |
| 922 { | |
| 923 store.aboutToIndexDart(contextA, unitElementA); | |
| 924 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 925 locationA); | |
| 926 store.doneIndex(); | |
| 927 } | |
| 928 { | |
| 929 store.aboutToIndexDart(contextB, unitElementB); | |
| 930 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 931 locationB); | |
| 932 store.doneIndex(); | |
| 933 } | |
| 934 // get relationships | |
| 935 return store.getRelationships(UniverseElement.INSTANCE, relationship).then( | |
| 936 (List<Location> locations) { | |
| 937 assertLocations(locations, [locationA, locationB]); | |
| 938 }).then((_) { | |
| 939 // re-index "unitElementA" | |
| 940 store.aboutToIndexDart(contextA, unitElementA); | |
| 941 store.doneIndex(); | |
| 942 return store.getRelationships(UniverseElement.INSTANCE, | |
| 943 relationship).then((List<Location> locations) { | |
| 944 assertLocations(locations, [locationB]); | |
| 945 }); | |
| 946 }); | |
| 947 } | |
| 948 | |
| 949 test_universe_clear() { | |
| 950 when(contextA.getElement(elementLocationA)).thenReturn(elementA); | |
| 951 when(contextB.getElement(elementLocationB)).thenReturn(elementB); | |
| 952 Location locationA = mockLocation(elementA); | |
| 953 Location locationB = mockLocation(elementB); | |
| 954 { | |
| 955 store.aboutToIndexDart(contextA, unitElementA); | |
| 956 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 957 locationA); | |
| 958 store.doneIndex(); | |
| 959 } | |
| 960 { | |
| 961 store.aboutToIndexDart(contextA, unitElementB); | |
| 962 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 963 locationB); | |
| 964 store.doneIndex(); | |
| 965 } | |
| 966 return store.getRelationships(UniverseElement.INSTANCE, relationship).then( | |
| 967 (List<Location> locations) { | |
| 968 assertLocations(locations, [locationA, locationB]); | |
| 969 }).then((_) { | |
| 970 // clear | |
| 971 store.clear(); | |
| 972 return store.getRelationships(UniverseElement.INSTANCE, | |
| 973 relationship).then((List<Location> locations) { | |
| 974 expect(locations, isEmpty); | |
| 975 }); | |
| 976 }); | |
| 977 } | |
| 978 | |
| 979 test_universe_removeContext() { | |
| 980 when(contextA.getElement(elementLocationA)).thenReturn(elementA); | |
| 981 when(contextB.getElement(elementLocationB)).thenReturn(elementB); | |
| 982 Location locationA = mockLocation(elementA); | |
| 983 Location locationB = mockLocation(elementB); | |
| 984 { | |
| 985 store.aboutToIndexDart(contextA, unitElementA); | |
| 986 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 987 locationA); | |
| 988 store.doneIndex(); | |
| 989 } | |
| 990 { | |
| 991 store.aboutToIndexDart(contextB, unitElementB); | |
| 992 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 993 locationB); | |
| 994 store.doneIndex(); | |
| 995 } | |
| 996 return store.getRelationships(UniverseElement.INSTANCE, relationship).then( | |
| 997 (List<Location> locations) { | |
| 998 assertLocations(locations, [locationA, locationB]); | |
| 999 }).then((_) { | |
| 1000 // remove "contextA" | |
| 1001 store.removeContext(contextA); | |
| 1002 return store.getRelationships(UniverseElement.INSTANCE, | |
| 1003 relationship).then((List<Location> locations) { | |
| 1004 assertLocations(locations, [locationB]); | |
| 1005 }); | |
| 1006 }); | |
| 1007 } | |
| 1008 | |
| 1009 test_universe_removeSource() { | |
| 1010 when(contextA.getElement(elementLocationA)).thenReturn(elementA); | |
| 1011 when(contextB.getElement(elementLocationB)).thenReturn(elementB); | |
| 1012 Location locationA = mockLocation(elementA); | |
| 1013 Location locationB = mockLocation(elementB); | |
| 1014 { | |
| 1015 store.aboutToIndexDart(contextA, unitElementA); | |
| 1016 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 1017 locationA); | |
| 1018 store.doneIndex(); | |
| 1019 } | |
| 1020 { | |
| 1021 store.aboutToIndexDart(contextA, unitElementB); | |
| 1022 store.recordRelationship(UniverseElement.INSTANCE, relationship, | |
| 1023 locationB); | |
| 1024 store.doneIndex(); | |
| 1025 } | |
| 1026 return store.getRelationships(UniverseElement.INSTANCE, relationship).then( | |
| 1027 (List<Location> locations) { | |
| 1028 assertLocations(locations, [locationA, locationB]); | |
| 1029 }).then((_) { | |
| 1030 // remove "sourceA" | |
| 1031 store.removeSource(contextA, sourceA); | |
| 1032 return store.getRelationships(UniverseElement.INSTANCE, | |
| 1033 relationship).then((List<Location> locations) { | |
| 1034 assertLocations(locations, [locationB]); | |
| 1035 }); | |
| 1036 }); | |
| 1037 } | |
| 1038 | |
| 1039 /** | |
| 1040 * Asserts that the [actual] locations have all the [expected] locations and | |
| 1041 * only them. | |
| 1042 */ | |
| 1043 static void assertLocations(List<Location> actual, List<Location> expected) { | |
| 1044 List<_LocationEqualsWrapper> actualWrappers = wrapLocations(actual); | |
| 1045 List<_LocationEqualsWrapper> expectedWrappers = wrapLocations(expected); | |
| 1046 expect(actualWrappers, unorderedEquals(expectedWrappers)); | |
| 1047 } | |
| 1048 | |
| 1049 /** | |
| 1050 * @return the new [Location] mock. | |
| 1051 */ | |
| 1052 static Location mockLocation(Element element) { | |
| 1053 Location location = new MockLocation(); | |
| 1054 when(location.element).thenReturn(element); | |
| 1055 when(location.offset).thenReturn(0); | |
| 1056 when(location.length).thenReturn(0); | |
| 1057 return location; | |
| 1058 } | |
| 1059 | |
| 1060 /** | |
| 1061 * Wraps the given locations into [LocationEqualsWrapper]. | |
| 1062 */ | |
| 1063 static List<_LocationEqualsWrapper> wrapLocations(List<Location> locations) { | |
| 1064 List<_LocationEqualsWrapper> wrappers = <_LocationEqualsWrapper>[]; | |
| 1065 for (Location location in locations) { | |
| 1066 wrappers.add(new _LocationEqualsWrapper(location)); | |
| 1067 } | |
| 1068 return wrappers; | |
| 1069 } | |
| 1070 } | |
| OLD | NEW |