| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 7 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/src/summary/format.dart'; | 9 import 'package:analyzer/src/summary/format.dart'; |
| 9 import 'package:analyzer/src/summary/idl.dart'; | 10 import 'package:analyzer/src/summary/idl.dart'; |
| 10 import 'package:analyzer/src/summary/index_unit.dart'; | 11 import 'package:analyzer/src/summary/index_unit.dart'; |
| 11 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 12 | 13 |
| 13 import '../../reflective_tests.dart'; | 14 import '../../reflective_tests.dart'; |
| 14 import '../abstract_single_unit.dart'; | 15 import '../abstract_single_unit.dart'; |
| 15 | 16 |
| 16 main() { | 17 main() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 33 return '(unit=$unitElement; offset=$offset; length=$length;' | 34 return '(unit=$unitElement; offset=$offset; length=$length;' |
| 34 ' isQualified=$isQualified isResolved=$isResolved)'; | 35 ' isQualified=$isQualified isResolved=$isResolved)'; |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 @reflectiveTest | 39 @reflectiveTest |
| 39 class PackageIndexAssemblerTest extends AbstractSingleUnitTest { | 40 class PackageIndexAssemblerTest extends AbstractSingleUnitTest { |
| 40 PackageIndex packageIndex; | 41 PackageIndex packageIndex; |
| 41 UnitIndex unitIndex; | 42 UnitIndex unitIndex; |
| 42 | 43 |
| 44 _ElementIndexAssert assertThat(Element element) { |
| 45 return new _ElementIndexAssert(this, element); |
| 46 } |
| 47 |
| 48 CompilationUnitElement importedUnit({int index: 0}) { |
| 49 List<ImportElement> imports = testLibraryElement.imports; |
| 50 return imports[index].importedLibrary.definingCompilationUnit; |
| 51 } |
| 52 |
| 43 void test_isExtendedBy_ClassDeclaration() { | 53 void test_isExtendedBy_ClassDeclaration() { |
| 44 _indexTestUnit(''' | 54 _indexTestUnit(''' |
| 45 class A {} // 1 | 55 class A {} // 1 |
| 46 class B extends A {} // 2 | 56 class B extends A {} // 2 |
| 47 '''); | 57 '''); |
| 48 ClassElement classElementA = findElement('A'); | 58 ClassElement elementA = findElement('A'); |
| 49 // verify | 59 assertThat(elementA).isExtendedAt('A {} // 2'); |
| 50 _assertHasRelation(classElementA, IndexRelationKind.IS_EXTENDED_BY, | 60 } |
| 51 _expectedLocation('A {} // 2')); | 61 |
| 62 void test_isExtendedBy_ClassDeclaration_Object() { |
| 63 _indexTestUnit(''' |
| 64 class A {} |
| 65 '''); |
| 66 ClassElement elementA = findElement('A'); |
| 67 ClassElement elementObject = elementA.supertype.element; |
| 68 assertThat(elementObject).isExtendedAt('A {}', length: 0); |
| 69 } |
| 70 |
| 71 void test_isExtendedBy_ClassTypeAlias() { |
| 72 _indexTestUnit(''' |
| 73 class A {} |
| 74 class B {} |
| 75 class C = A with B; |
| 76 '''); |
| 77 ClassElement elementA = findElement('A'); |
| 78 assertThat(elementA).isExtendedAt('A with'); |
| 79 } |
| 80 |
| 81 void test_isImplementedBy_ClassDeclaration() { |
| 82 _indexTestUnit(''' |
| 83 class A {} // 1 |
| 84 class B implements A {} // 2 |
| 85 '''); |
| 86 ClassElement elementA = findElement('A'); |
| 87 assertThat(elementA).isImplementedAt('A {} // 2'); |
| 88 } |
| 89 |
| 90 void test_isImplementedBy_ClassTypeAlias() { |
| 91 _indexTestUnit(''' |
| 92 class A {} // 1 |
| 93 class B {} // 2 |
| 94 class C = Object with A implements B; // 3 |
| 95 '''); |
| 96 ClassElement elementB = findElement('B'); |
| 97 assertThat(elementB).isImplementedAt('B; // 3'); |
| 98 } |
| 99 |
| 100 void test_isInvokedBy_FieldElement() { |
| 101 _indexTestUnit(''' |
| 102 class A { |
| 103 var field; |
| 104 main() { |
| 105 this.field(); // q |
| 106 field(); // nq |
| 107 } |
| 108 }'''); |
| 109 FieldElement field = findElement('field'); |
| 110 assertThat(field.getter) |
| 111 ..isInvokedAt('field(); // q', qualified: true) |
| 112 ..isInvokedAt('field(); // nq'); |
| 113 } |
| 114 |
| 115 void test_isInvokedBy_FunctionElement() { |
| 116 addSource( |
| 117 '/lib.dart', |
| 118 ''' |
| 119 library lib; |
| 120 foo() {} |
| 121 '''); |
| 122 _indexTestUnit(''' |
| 123 import 'lib.dart'; |
| 124 import 'lib.dart' as pref; |
| 125 main() { |
| 126 pref.foo(); // q |
| 127 foo(); // nq |
| 128 }'''); |
| 129 FunctionElement element = importedUnit().functions[0]; |
| 130 assertThat(element) |
| 131 ..isInvokedAt('foo(); // q', qualified: true) |
| 132 ..isInvokedAt('foo(); // nq'); |
| 133 } |
| 134 |
| 135 void test_isInvokedBy_MethodElement() { |
| 136 _indexTestUnit(''' |
| 137 class A { |
| 138 foo() {} |
| 139 main() { |
| 140 this.foo(); // q |
| 141 foo(); // nq |
| 142 } |
| 143 }'''); |
| 144 Element element = findElement('foo'); |
| 145 assertThat(element) |
| 146 ..isInvokedAt('foo(); // q', qualified: true) |
| 147 ..isInvokedAt('foo(); // nq'); |
| 148 } |
| 149 |
| 150 void test_isInvokedBy_MethodElement_propagatedType() { |
| 151 _indexTestUnit(''' |
| 152 class A { |
| 153 foo() {} |
| 154 } |
| 155 main() { |
| 156 var a = new A(); |
| 157 a.foo(); |
| 158 } |
| 159 '''); |
| 160 Element element = findElement('foo'); |
| 161 assertThat(element).isInvokedAt('foo();', qualified: true); |
| 162 } |
| 163 |
| 164 void test_isInvokedBy_operator_binary() { |
| 165 _indexTestUnit(''' |
| 166 class A { |
| 167 operator +(other) => this; |
| 168 } |
| 169 main(A a) { |
| 170 print(a + 1); |
| 171 a += 2; |
| 172 ++a; |
| 173 a++; |
| 174 } |
| 175 '''); |
| 176 MethodElement element = findElement('+'); |
| 177 assertThat(element) |
| 178 ..isInvokedAt('+ 1', length: 1) |
| 179 ..isInvokedAt('+= 2', length: 2) |
| 180 ..isInvokedAt('++a', length: 2) |
| 181 ..isInvokedAt('++;', length: 2); |
| 182 } |
| 183 |
| 184 void test_isInvokedBy_operator_index() { |
| 185 _indexTestUnit(''' |
| 186 class A { |
| 187 operator [](i) => null; |
| 188 operator []=(i, v) {} |
| 189 } |
| 190 main(A a) { |
| 191 print(a[0]); |
| 192 a[1] = 42; |
| 193 } |
| 194 '''); |
| 195 MethodElement readElement = findElement('[]'); |
| 196 MethodElement writeElement = findElement('[]='); |
| 197 assertThat(readElement).isInvokedAt('[0]', length: 1); |
| 198 assertThat(writeElement).isInvokedAt('[1]', length: 1); |
| 199 } |
| 200 |
| 201 void test_isInvokedBy_operator_prefix() { |
| 202 _indexTestUnit(''' |
| 203 class A { |
| 204 A operator ~() => this; |
| 205 } |
| 206 main(A a) { |
| 207 print(~a); |
| 208 } |
| 209 '''); |
| 210 MethodElement element = findElement('~'); |
| 211 assertThat(element).isInvokedAt('~a', length: 1); |
| 212 } |
| 213 |
| 214 void test_isMixedInBy_ClassDeclaration() { |
| 215 _indexTestUnit(''' |
| 216 class A {} // 1 |
| 217 class B extends Object with A {} // 2 |
| 218 '''); |
| 219 ClassElement elementA = findElement('A'); |
| 220 assertThat(elementA).isMixedInAt('A {} // 2'); |
| 221 } |
| 222 |
| 223 void test_isMixedInBy_ClassTypeAlias() { |
| 224 _indexTestUnit(''' |
| 225 class A {} // 1 |
| 226 class B = Object with A; // 2 |
| 227 '''); |
| 228 ClassElement elementA = findElement('A'); |
| 229 assertThat(elementA).isMixedInAt('A; // 2'); |
| 52 } | 230 } |
| 53 | 231 |
| 54 void test_isReferencedBy_ClassElement() { | 232 void test_isReferencedBy_ClassElement() { |
| 55 _indexTestUnit(''' | 233 _indexTestUnit(''' |
| 56 class A { | 234 class A { |
| 57 static var field; | 235 static var field; |
| 58 } | 236 } |
| 59 main(A p) { | 237 main(A p) { |
| 60 A v; | 238 A v; |
| 61 new A(); // 2 | 239 new A(); // 2 |
| 62 A.field = 1; | 240 A.field = 1; |
| 63 print(A.field); // 3 | 241 print(A.field); // 3 |
| 64 } | 242 } |
| 65 '''); | 243 '''); |
| 66 ClassElement element = findElement("A"); | 244 ClassElement element = findElement('A'); |
| 67 // verify | 245 assertThat(element) |
| 68 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | 246 ..isReferencedAt('A p) {') |
| 69 _expectedLocation('A p) {')); | 247 ..isReferencedAt('A v;') |
| 70 _assertHasRelation( | 248 ..isReferencedAt('A(); // 2') |
| 71 element, IndexRelationKind.IS_REFERENCED_BY, _expectedLocation('A v;')); | 249 ..isReferencedAt('A.field = 1;') |
| 72 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | 250 ..isReferencedAt('A.field); // 3'); |
| 73 _expectedLocation('A(); // 2')); | 251 } |
| 74 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | 252 |
| 75 _expectedLocation('A.field = 1;')); | 253 void test_isReferencedBy_ClassElement_invocation() { |
| 76 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | 254 verifyNoTestUnitErrors = false; |
| 77 _expectedLocation('A.field); // 3')); | 255 _indexTestUnit(''' |
| 256 class A {} |
| 257 main() { |
| 258 A(); // invalid code, but still a reference |
| 259 }'''); |
| 260 Element element = findElement('A'); |
| 261 assertThat(element).isReferencedAt('A();'); |
| 262 } |
| 263 |
| 264 void test_isReferencedBy_ClassTypeAlias() { |
| 265 _indexTestUnit(''' |
| 266 class A {} |
| 267 class B = Object with A; |
| 268 main(B p) { |
| 269 B v; |
| 270 } |
| 271 '''); |
| 272 ClassElement element = findElement('B'); |
| 273 assertThat(element)..isReferencedAt('B p) {')..isReferencedAt('B v;'); |
| 274 } |
| 275 |
| 276 void test_isReferencedBy_CompilationUnitElement_export() { |
| 277 addSource( |
| 278 '/lib.dart', |
| 279 ''' |
| 280 library lib; |
| 281 '''); |
| 282 _indexTestUnit(''' |
| 283 export 'lib.dart'; |
| 284 '''); |
| 285 LibraryElement element = testLibraryElement.exports[0].exportedLibrary; |
| 286 assertThat(element)..isReferencedAt("'lib.dart'", length: 10); |
| 287 } |
| 288 |
| 289 void test_isReferencedBy_CompilationUnitElement_import() { |
| 290 addSource( |
| 291 '/lib.dart', |
| 292 ''' |
| 293 library lib; |
| 294 '''); |
| 295 _indexTestUnit(''' |
| 296 import 'lib.dart'; |
| 297 '''); |
| 298 LibraryElement element = testLibraryElement.imports[0].importedLibrary; |
| 299 assertThat(element)..isReferencedAt("'lib.dart'", length: 10); |
| 300 } |
| 301 |
| 302 void test_isReferencedBy_CompilationUnitElement_part() { |
| 303 addSource('/my_unit.dart', 'part of my_lib;'); |
| 304 _indexTestUnit(''' |
| 305 library my_lib; |
| 306 part 'my_unit.dart'; |
| 307 '''); |
| 308 CompilationUnitElement element = testLibraryElement.parts[0]; |
| 309 assertThat(element)..isReferencedAt("'my_unit.dart';", length: 14); |
| 310 } |
| 311 |
| 312 void test_isReferencedBy_ConstructorElement() { |
| 313 _indexTestUnit(''' |
| 314 class A implements B { |
| 315 A() {} |
| 316 A.foo() {} |
| 317 } |
| 318 class B extends A { |
| 319 B() : super(); // 1 |
| 320 B.foo() : super.foo(); // 2 |
| 321 factory B.bar() = A.foo; // 3 |
| 322 } |
| 323 main() { |
| 324 new A(); // 4 |
| 325 new A.foo(); // 5 |
| 326 } |
| 327 '''); |
| 328 ClassElement classA = findElement('A'); |
| 329 ConstructorElement constA = classA.constructors[0]; |
| 330 ConstructorElement constA_foo = classA.constructors[1]; |
| 331 // A() |
| 332 assertThat(constA) |
| 333 ..isReferencedAt('(); // 1', length: 0) |
| 334 ..isReferencedAt('(); // 4', length: 0); |
| 335 // A.foo() |
| 336 assertThat(constA_foo) |
| 337 ..isReferencedAt('.foo(); // 2', length: 4) |
| 338 ..isReferencedAt('.foo; // 3', length: 4) |
| 339 ..isReferencedAt('.foo(); // 5', length: 4); |
| 340 } |
| 341 |
| 342 void test_isReferencedBy_ConstructorElement_classTypeAlias() { |
| 343 _indexTestUnit(''' |
| 344 class M {} |
| 345 class A implements B { |
| 346 A() {} |
| 347 A.named() {} |
| 348 } |
| 349 class B = A with M; |
| 350 main() { |
| 351 new B(); // 1 |
| 352 new B.named(); // 2 |
| 353 } |
| 354 '''); |
| 355 ClassElement classA = findElement('A'); |
| 356 ConstructorElement constA = classA.constructors[0]; |
| 357 ConstructorElement constA_named = classA.constructors[1]; |
| 358 assertThat(constA).isReferencedAt('(); // 1', length: 0); |
| 359 assertThat(constA_named).isReferencedAt('.named(); // 2', length: 6); |
| 360 } |
| 361 |
| 362 void test_isReferencedBy_ConstructorElement_redirection() { |
| 363 _indexTestUnit(''' |
| 364 class A { |
| 365 A() : this.bar(); // 1 |
| 366 A.foo() : this(); // 2 |
| 367 A.bar(); |
| 368 } |
| 369 '''); |
| 370 ClassElement classA = findElement('A'); |
| 371 ConstructorElement constA = classA.constructors[0]; |
| 372 ConstructorElement constA_bar = classA.constructors[2]; |
| 373 assertThat(constA).isReferencedAt('(); // 2', length: 0); |
| 374 assertThat(constA_bar).isReferencedAt('.bar(); // 1', length: 4); |
| 375 } |
| 376 |
| 377 void test_isReferencedBy_ConstructorFieldInitializer() { |
| 378 _indexTestUnit(''' |
| 379 class A { |
| 380 int field; |
| 381 A() : field = 5; |
| 382 } |
| 383 '''); |
| 384 FieldElement element = findElement('field'); |
| 385 assertThat(element).isReferencedAt('field = 5'); |
| 386 } |
| 387 |
| 388 void test_isReferencedBy_FieldElement() { |
| 389 _indexTestUnit(''' |
| 390 class A { |
| 391 var field; |
| 392 A({this.field}); |
| 393 m() { |
| 394 field = 1; // nq |
| 395 print(field); // nq |
| 396 } |
| 397 } |
| 398 main(A a) { |
| 399 a.field = 2; // q |
| 400 print(a.field); // q |
| 401 new A(field: 3); |
| 402 } |
| 403 '''); |
| 404 FieldElement field = findElement('field'); |
| 405 PropertyAccessorElement getter = field.getter; |
| 406 PropertyAccessorElement setter = field.setter; |
| 407 // A() |
| 408 assertThat(field)..isReferencedAt('field});'); |
| 409 // m() |
| 410 assertThat(setter)..isReferencedAt('field = 1; // nq'); |
| 411 assertThat(getter)..isReferencedAt('field); // nq'); |
| 412 // main() |
| 413 assertThat(setter)..isReferencedAt('field = 2; // q', qualified: true); |
| 414 assertThat(getter)..isReferencedAt('field); // q', qualified: true); |
| 415 assertThat(field)..isReferencedAt('field: 3'); |
| 416 } |
| 417 |
| 418 void test_isReferencedBy_FunctionElement() { |
| 419 _indexTestUnit(''' |
| 420 foo() {} |
| 421 main() { |
| 422 print(foo); |
| 423 print(foo()); |
| 424 } |
| 425 '''); |
| 426 FunctionElement element = findElement('foo'); |
| 427 assertThat(element) |
| 428 ..isReferencedAt('foo);') |
| 429 ..isInvokedAt('foo());'); |
| 430 } |
| 431 |
| 432 void test_isReferencedBy_FunctionTypeAliasElement() { |
| 433 _indexTestUnit(''' |
| 434 typedef A(); |
| 435 main(A p) { |
| 436 } |
| 437 '''); |
| 438 Element element = findElement('A'); |
| 439 assertThat(element)..isReferencedAt('A p) {'); |
| 78 } | 440 } |
| 79 | 441 |
| 80 /** | 442 /** |
| 443 * There was a bug in the AST structure, when single [Comment] was cloned and |
| 444 * assigned to both [FieldDeclaration] and [VariableDeclaration]. |
| 445 * |
| 446 * This caused duplicate indexing. |
| 447 * Here we test that the problem is fixed one way or another. |
| 448 */ |
| 449 void test_isReferencedBy_identifierInComment() { |
| 450 _indexTestUnit(''' |
| 451 class A {} |
| 452 /// [A] text |
| 453 var myVariable = null; |
| 454 '''); |
| 455 Element element = findElement('A'); |
| 456 assertThat(element)..isReferencedAt('A] text'); |
| 457 } |
| 458 |
| 459 void test_isReferencedBy_MethodElement() { |
| 460 _indexTestUnit(''' |
| 461 class A { |
| 462 method() {} |
| 463 main() { |
| 464 print(this.method); // q |
| 465 print(method); // nq |
| 466 } |
| 467 }'''); |
| 468 MethodElement element = findElement('method'); |
| 469 assertThat(element) |
| 470 ..isReferencedAt('method); // q', qualified: true) |
| 471 ..isReferencedAt('method); // nq'); |
| 472 } |
| 473 |
| 474 void test_isReferencedBy_ParameterElement() { |
| 475 _indexTestUnit(''' |
| 476 foo({var p}) {} |
| 477 main() { |
| 478 foo(p: 1); |
| 479 } |
| 480 '''); |
| 481 Element element = findElement('p'); |
| 482 assertThat(element)..isReferencedAt('p: 1'); |
| 483 } |
| 484 |
| 485 void test_isReferencedBy_TopLevelVariableElement() { |
| 486 addSource( |
| 487 '/lib.dart', |
| 488 ''' |
| 489 library lib; |
| 490 var V; |
| 491 '''); |
| 492 _indexTestUnit(''' |
| 493 import 'lib.dart' show V; // imp |
| 494 import 'lib.dart' as pref; |
| 495 main() { |
| 496 pref.V = 5; // q |
| 497 print(pref.V); // q |
| 498 V = 5; // nq |
| 499 print(V); // nq |
| 500 }'''); |
| 501 TopLevelVariableElement variable = importedUnit().topLevelVariables[0]; |
| 502 assertThat(variable)..isReferencedAt('V; // imp'); |
| 503 assertThat(variable.getter) |
| 504 ..isReferencedAt('V); // q', qualified: true) |
| 505 ..isReferencedAt('V); // nq'); |
| 506 assertThat(variable.setter) |
| 507 ..isReferencedAt('V = 5; // q', qualified: true) |
| 508 ..isReferencedAt('V = 5; // nq'); |
| 509 } |
| 510 |
| 511 void test_isReferencedBy_typeInVariableList() { |
| 512 _indexTestUnit(''' |
| 513 class A {} |
| 514 A myVariable = null; |
| 515 '''); |
| 516 Element element = findElement('A'); |
| 517 assertThat(element).isReferencedAt('A myVariable'); |
| 518 } |
| 519 |
| 520 /** |
| 81 * Asserts that [unitIndex] has an item with the expected properties. | 521 * Asserts that [unitIndex] has an item with the expected properties. |
| 82 */ | 522 */ |
| 83 void _assertHasRelation( | 523 void _assertHasRelation( |
| 84 Element element, | 524 Element element, |
| 85 IndexRelationKind expectedRelationship, | 525 IndexRelationKind expectedRelationship, |
| 86 ExpectedLocation expectedLocation) { | 526 ExpectedLocation expectedLocation) { |
| 87 int elementId = _findElementId(element); | 527 int elementId = _findElementId(element); |
| 88 for (int i = 0; i < unitIndex.locationOffsets.length; i++) { | 528 for (int i = 0; i < unitIndex.locationOffsets.length; i++) { |
| 89 if (unitIndex.elements[i] == elementId && | 529 if (unitIndex.elements[i] == elementId && |
| 90 unitIndex.locationOffsets[i] == expectedLocation.offset && | 530 unitIndex.locationOffsets[i] == expectedLocation.offset && |
| 91 unitIndex.locationLengths[i] == expectedLocation.length) { | 531 unitIndex.locationLengths[i] == expectedLocation.length) { |
| 532 // TODO(scheglov) continue looking to check that only one usage at one |
| 533 // location, e.g. no both 'reference' and 'invocation'. |
| 92 return; | 534 return; |
| 93 } | 535 } |
| 94 } | 536 } |
| 95 _failWithIndexDump( | 537 _failWithIndexDump( |
| 96 'not found\n$element $expectedRelationship at $expectedLocation'); | 538 'not found\n$element $expectedRelationship at $expectedLocation'); |
| 97 } | 539 } |
| 98 | 540 |
| 99 ExpectedLocation _expectedLocation(String search, | 541 ExpectedLocation _expectedLocation(String search, |
| 100 {int length: -1, bool isQualified: false, bool isResolved: true}) { | 542 {int length, bool isQualified: false, bool isResolved: true}) { |
| 101 int offset = findOffset(search); | 543 int offset = findOffset(search); |
| 102 if (length == -1) { | 544 if (length == null) { |
| 103 length = getLeadingIdentifierLength(search); | 545 length = getLeadingIdentifierLength(search); |
| 104 } | 546 } |
| 105 return new ExpectedLocation( | 547 return new ExpectedLocation( |
| 106 testUnitElement, offset, length, isQualified, isResolved); | 548 testUnitElement, offset, length, isQualified, isResolved); |
| 107 } | 549 } |
| 108 | 550 |
| 109 void _failWithIndexDump(String msg) { | 551 void _failWithIndexDump(String msg) { |
| 110 String packageIndexJsonString = | 552 String packageIndexJsonString = |
| 111 new JsonEncoder.withIndent(' ').convert(packageIndex.toJson()); | 553 new JsonEncoder.withIndent(' ').convert(packageIndex.toJson()); |
| 112 fail('$msg in\n' + packageIndexJsonString); | 554 fail('$msg in\n' + packageIndexJsonString); |
| 113 } | 555 } |
| 114 | 556 |
| 115 /** | 557 /** |
| 116 * Return the [element] identifier in [packageIndex] or fail. | 558 * Return the [element] identifier in [packageIndex] or fail. |
| 117 */ | 559 */ |
| 118 int _findElementId(Element element) { | 560 int _findElementId(Element element) { |
| 119 int elementUnitId = _getElementUnitId(element); | 561 int elementUnitId = _getElementUnitId(element); |
| 562 int offset = element.nameOffset; |
| 563 if (element is LibraryElement || element is CompilationUnitElement) { |
| 564 offset = 0; |
| 565 } |
| 566 IndexElementKind kind = PackageIndexAssembler.getIndexElementKind(element); |
| 120 for (int elementId = 0; | 567 for (int elementId = 0; |
| 121 elementId < packageIndex.elementUnits.length; | 568 elementId < packageIndex.elementUnits.length; |
| 122 elementId++) { | 569 elementId++) { |
| 123 if (packageIndex.elementUnits[elementId] == elementUnitId && | 570 if (packageIndex.elementUnits[elementId] == elementUnitId && |
| 124 packageIndex.elementOffsets[elementId] == element.nameOffset) { | 571 packageIndex.elementOffsets[elementId] == offset && |
| 572 packageIndex.elementKinds[elementId] == kind) { |
| 125 return elementId; | 573 return elementId; |
| 126 } | 574 } |
| 127 } | 575 } |
| 128 _failWithIndexDump('Element $element is not referenced'); | 576 _failWithIndexDump('Element $element is not referenced'); |
| 129 return 0; | 577 return 0; |
| 130 } | 578 } |
| 131 | 579 |
| 132 int _getElementUnitId(Element element) { | 580 int _getElementUnitId(Element element) { |
| 133 CompilationUnitElement unitElement = | 581 CompilationUnitElement unitElement = |
| 134 PackageIndexAssembler.getUnitElement(element); | 582 PackageIndexAssembler.getUnitElement(element); |
| 135 int libraryUriId = _getUriId(unitElement.library.source.uri); | 583 int libraryUriId = _getUriId(unitElement.library.source.uri); |
| 136 int unitUriId = _getUriId(unitElement.library.source.uri); | 584 int unitUriId = _getUriId(unitElement.source.uri); |
| 137 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) { | 585 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) { |
| 138 if (packageIndex.elementLibraryUris[i] == libraryUriId && | 586 if (packageIndex.elementLibraryUris[i] == libraryUriId && |
| 139 packageIndex.elementUnitUris[i] == unitUriId) { | 587 packageIndex.elementUnitUris[i] == unitUriId) { |
| 140 return i; | 588 return i; |
| 141 } | 589 } |
| 142 } | 590 } |
| 143 fail('Unit $unitElement of $element is not referenced in the index.'); | 591 _failWithIndexDump('Unit $unitElement of $element is not referenced'); |
| 144 return -1; | 592 return -1; |
| 145 } | 593 } |
| 146 | 594 |
| 147 int _getUriId(Uri uri) { | 595 int _getUriId(Uri uri) { |
| 148 String str = uri.toString(); | 596 String str = uri.toString(); |
| 149 int id = packageIndex.uris.indexOf(str); | 597 int id = packageIndex.uris.indexOf(str); |
| 150 expect(id, isNonNegative); | 598 expect(id, isNonNegative); |
| 151 return id; | 599 return id; |
| 152 } | 600 } |
| 153 | 601 |
| 154 void _indexTestUnit(String code) { | 602 void _indexTestUnit(String code) { |
| 155 resolveTestUnit(code); | 603 resolveTestUnit(code); |
| 156 PackageIndexAssembler assembler = new PackageIndexAssembler(); | 604 PackageIndexAssembler assembler = new PackageIndexAssembler(); |
| 157 assembler.index(testUnit); | 605 assembler.index(testUnit); |
| 158 // assemble, write and read | 606 // assemble, write and read |
| 159 PackageIndexBuilder indexBuilder = assembler.assemble(); | 607 PackageIndexBuilder indexBuilder = assembler.assemble(); |
| 160 List<int> indexBytes = indexBuilder.toBuffer(); | 608 List<int> indexBytes = indexBuilder.toBuffer(); |
| 161 packageIndex = new PackageIndex.fromBuffer(indexBytes); | 609 packageIndex = new PackageIndex.fromBuffer(indexBytes); |
| 162 // prepare the only unit index | 610 // prepare the only unit index |
| 163 expect(packageIndex.units, hasLength(1)); | 611 expect(packageIndex.units, hasLength(1)); |
| 164 unitIndex = packageIndex.units[0]; | 612 unitIndex = packageIndex.units[0]; |
| 165 } | 613 } |
| 166 } | 614 } |
| 615 |
| 616 class _ElementIndexAssert { |
| 617 final PackageIndexAssemblerTest test; |
| 618 final Element element; |
| 619 |
| 620 _ElementIndexAssert(this.test, this.element); |
| 621 |
| 622 void isExtendedAt(String search, {int length}) { |
| 623 test._assertHasRelation(element, IndexRelationKind.IS_EXTENDED_BY, |
| 624 test._expectedLocation(search, length: length)); |
| 625 } |
| 626 |
| 627 void isImplementedAt(String search, {int length}) { |
| 628 test._assertHasRelation(element, IndexRelationKind.IS_IMPLEMENTED_BY, |
| 629 test._expectedLocation(search, length: length)); |
| 630 } |
| 631 |
| 632 void isInvokedAt(String search, {int length, bool qualified: false}) { |
| 633 // TODO(scheglov) use 'qualified' |
| 634 test._assertHasRelation(element, IndexRelationKind.IS_INVOKED_BY, |
| 635 test._expectedLocation(search, length: length)); |
| 636 } |
| 637 |
| 638 void isMixedInAt(String search, {int length}) { |
| 639 test._assertHasRelation(element, IndexRelationKind.IS_MIXED_IN_BY, |
| 640 test._expectedLocation(search, length: length)); |
| 641 } |
| 642 |
| 643 void isReferencedAt(String search, {int length, bool qualified: false}) { |
| 644 // TODO(scheglov) use 'qualified' |
| 645 test._assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, |
| 646 test._expectedLocation(search, length: length)); |
| 647 } |
| 648 } |
| OLD | NEW |