Chromium Code Reviews| 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.services.src.index.dart_index_contributor; | |
| 6 | |
| 7 import 'package:analysis_services/index/index.dart'; | |
| 8 import 'package:analysis_services/index/index_store.dart'; | |
| 9 import 'package:analysis_services/src/index/index_contributor.dart'; | |
| 10 import 'package:analysis_testing/abstract_context.dart'; | |
| 11 import 'package:analysis_testing/reflective_tests.dart'; | |
| 12 import 'package:analyzer/src/generated/ast.dart'; | |
| 13 import 'package:analyzer/src/generated/element.dart'; | |
| 14 import 'package:analyzer/src/generated/error.dart'; | |
| 15 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 16 import 'package:analyzer/src/generated/source.dart'; | |
| 17 import 'package:typed_mock/typed_mock.dart'; | |
| 18 import 'package:unittest/unittest.dart'; | |
| 19 | |
| 20 | |
| 21 main() { | |
| 22 groupSep = ' | '; | |
| 23 group('DartUnitContributor', () { | |
| 24 runReflectiveTests(DartUnitContributorTest); | |
| 25 }); | |
| 26 } | |
| 27 | |
| 28 | |
| 29 /** | |
| 30 * Returns `true` if [actual] location the same properties as [expected]. | |
|
Brian Wilkerson
2014/07/08 20:07:58
"if [actual] location the" --> "if the [actual] lo
scheglov
2014/07/08 20:54:52
Done.
| |
| 31 */ | |
| 32 bool _equalsLocation(Location actual, ExpectedLocation expected) { | |
| 33 return _equalsLocationProperties(actual, expected.element, expected.offset, | |
| 34 expected.length); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 /** | |
| 39 * Returns `true` if [actual] location the expected properties. | |
|
Brian Wilkerson
2014/07/08 20:07:58
"if [actual] location the" --> "if the [actual] lo
scheglov
2014/07/08 20:54:52
Done.
| |
| 40 */ | |
| 41 bool _equalsLocationProperties(Location actual, Element expectedElement, | |
| 42 int expectedOffset, int expectedLength) { | |
| 43 return expectedElement == actual.element && expectedOffset == actual.offset && | |
| 44 expectedLength == actual.length; | |
| 45 } | |
| 46 | |
| 47 | |
| 48 bool _equalsRecordedRelation(RecordedRelation recordedRelation, | |
| 49 Element expectedElement, Relationship expectedRelationship, | |
| 50 ExpectedLocation expectedLocation) { | |
| 51 return expectedElement == recordedRelation.element && expectedRelationship == | |
| 52 recordedRelation.relationship && (expectedLocation == null || _equalsLocat ion( | |
| 53 recordedRelation.location, expectedLocation)); | |
| 54 } | |
| 55 | |
| 56 | |
| 57 int _getLeadingIdentifierLength(String search) { | |
| 58 int length = 0; | |
| 59 while (length < search.length) { | |
| 60 int c = search.codeUnitAt(length); | |
| 61 if (c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0)) { | |
| 62 length++; | |
| 63 continue; | |
| 64 } | |
| 65 if (c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0)) { | |
| 66 length++; | |
| 67 continue; | |
| 68 } | |
| 69 if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) { | |
| 70 length++; | |
| 71 continue; | |
| 72 } | |
| 73 break; | |
| 74 } | |
| 75 return length; | |
| 76 } | |
| 77 | |
| 78 @ReflectiveTestCase() | |
| 79 class DartUnitContributorTest extends AbstractContextTest { | |
| 80 IndexStore store = new MockIndexStore(); | |
| 81 List<RecordedRelation> recordedRelations = <RecordedRelation>[]; | |
| 82 | |
| 83 bool verifyNoTestUnitErrors = true; | |
| 84 | |
| 85 String testCode; | |
| 86 Source testSource; | |
| 87 CompilationUnit testUnit; | |
| 88 CompilationUnitElement testUnitElement; | |
| 89 LibraryElement testLibraryElement; | |
| 90 | |
| 91 void setUp() { | |
| 92 super.setUp(); | |
| 93 when(store.aboutToIndexDart(context, anyObject)).thenReturn(true); | |
| 94 when(store.recordRelationship(anyObject, anyObject, anyObject)).thenInvoke( | |
| 95 (Element element, Relationship relationship, Location location) { | |
| 96 recordedRelations.add(new RecordedRelation(element, relationship, | |
| 97 location)); | |
| 98 }); | |
| 99 } | |
| 100 | |
| 101 void test_FieldElement_noAssignedType_notLHS() { | |
| 102 _indexTestUnit(''' | |
| 103 class A { | |
| 104 var myField; | |
| 105 main() { | |
| 106 print(myField); | |
| 107 } | |
| 108 }'''); | |
| 109 // prepare elements | |
| 110 Element mainElement = _findElement("main"); | |
| 111 FieldElement fieldElement = _findElement("myField"); | |
| 112 PropertyAccessorElement getterElement = fieldElement.getter; | |
| 113 // verify | |
| 114 _assertRecordedRelation(getterElement, | |
| 115 IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, _expectedLocation(mainEleme nt, | |
| 116 'myField);')); | |
| 117 } | |
| 118 | |
| 119 void test_definesClass() { | |
| 120 _indexTestUnit('class A {}'); | |
| 121 // prepare elements | |
| 122 ClassElement classElement = _findElement("A"); | |
| 123 // verify | |
| 124 _assertDefinesTopLevelElement(IndexConstants.DEFINES_CLASS, | |
| 125 _expectedLocation(classElement, 'A {}')); | |
| 126 } | |
| 127 | |
| 128 void test_definesClassAlias() { | |
| 129 _indexTestUnit(''' | |
| 130 class Mix {} | |
| 131 class MyClass = Object with Mix;'''); | |
| 132 // prepare elements | |
| 133 Element classElement = _findElement("MyClass"); | |
| 134 // verify | |
| 135 _assertDefinesTopLevelElement(IndexConstants.DEFINES_CLASS_ALIAS, | |
| 136 _expectedLocation(classElement, 'MyClass =')); | |
| 137 } | |
| 138 | |
| 139 void test_definesFunction() { | |
| 140 _indexTestUnit('myFunction() {}'); | |
| 141 // prepare elements | |
| 142 FunctionElement functionElement = _findElement("myFunction"); | |
| 143 // verify | |
| 144 _assertDefinesTopLevelElement(IndexConstants.DEFINES_FUNCTION, | |
| 145 _expectedLocation(functionElement, 'myFunction() {}')); | |
| 146 } | |
| 147 | |
| 148 void test_definesFunctionType() { | |
| 149 _indexTestUnit('typedef MyFunction(int p);'); | |
| 150 // prepare elements | |
| 151 FunctionTypeAliasElement typeAliasElement = _findElement("MyFunction"); | |
| 152 // verify | |
| 153 _assertDefinesTopLevelElement(IndexConstants.DEFINES_FUNCTION_TYPE, | |
| 154 _expectedLocation(typeAliasElement, 'MyFunction(int p);')); | |
| 155 } | |
| 156 | |
| 157 | |
| 158 void test_definesVariable() { | |
| 159 _indexTestUnit('var myVar = 42;'); | |
| 160 // prepare elements | |
| 161 VariableElement varElement = _findElement("myVar"); | |
| 162 // verify | |
| 163 _assertDefinesTopLevelElement(IndexConstants.DEFINES_VARIABLE, | |
| 164 _expectedLocation(varElement, 'myVar = 42;')); | |
| 165 } | |
| 166 | |
| 167 void test_forIn() { | |
| 168 _indexTestUnit(''' | |
| 169 main() { | |
| 170 for (var v in []) { | |
| 171 } | |
| 172 }'''); | |
| 173 // prepare elements | |
| 174 Element mainElement = _findElement("main"); | |
| 175 VariableElement variableElement = _findElement("v"); | |
| 176 // verify | |
| 177 _assertNoRecordedRelation(variableElement, IndexConstants.IS_READ_BY, | |
| 178 _expectedLocation(mainElement, 'v in []')); | |
| 179 } | |
| 180 | |
| 181 void test_isDefinedBy_ConstructorElement() { | |
| 182 _indexTestUnit(''' | |
| 183 class A { | |
| 184 A() {} | |
| 185 A.foo() {} | |
| 186 }'''); | |
| 187 // prepare elements | |
| 188 ClassElement classA = _findElement("A"); | |
| 189 ConstructorElement consA = _findNodeElementAtString("A()", (node) => node is | |
| 190 ConstructorDeclaration); | |
| 191 ConstructorElement consA_foo = _findNodeElementAtString("A.foo()", (node) => | |
| 192 node is ConstructorDeclaration); | |
| 193 // verify | |
| 194 _assertRecordedRelation(consA, IndexConstants.IS_DEFINED_BY, | |
| 195 _expectedLocation(classA, '() {}')); | |
| 196 _assertRecordedRelation(consA_foo, IndexConstants.IS_DEFINED_BY, | |
| 197 _expectedLocation(classA, '.foo() {}', '.foo'.length)); | |
| 198 } | |
| 199 | |
| 200 // void test_createElementLocation() { | |
| 201 // ElementLocation elementLocation = mock(ElementLocation); | |
| 202 // Element element = IndexContributorHelper.mockElement(Element, | |
| 203 // elementLocation, 42, "myName"); | |
| 204 // Location location = IndexContributor.createLocation(element); | |
| 205 // IndexContributorHelper.assertLocation(location, element, 42, "myName"); | |
| 206 // } | |
| 207 // | |
| 208 // void test_createElementLocation_null() { | |
| 209 // JUnitTestCase.assertSame(null, IndexContributor.createLocation(null)); | |
| 210 // } | |
| 211 | |
| 212 void test_isDefinedBy_NameElement_method() { | |
| 213 _indexTestUnit(''' | |
| 214 class A { | |
| 215 m() {} | |
| 216 }'''); | |
| 217 // prepare elements | |
| 218 Element methodElement = _findElement("m"); | |
| 219 Element nameElement = new NameElement("m"); | |
| 220 // verify | |
| 221 _assertRecordedRelation(nameElement, IndexConstants.IS_DEFINED_BY, | |
| 222 _expectedLocation(methodElement, 'm() {}')); | |
| 223 } | |
| 224 | |
| 225 | |
| 226 void test_isDefinedBy_NameElement_operator() { | |
| 227 _indexTestUnit(''' | |
| 228 class A { | |
| 229 operator +(o) {} | |
| 230 }'''); | |
| 231 // prepare elements | |
| 232 Element methodElement = _findElement("+"); | |
| 233 Element nameElement = new NameElement("+"); | |
| 234 // verify | |
| 235 _assertRecordedRelation(nameElement, IndexConstants.IS_DEFINED_BY, | |
| 236 _expectedLocation(methodElement, '+(o) {}', 1)); | |
| 237 } | |
| 238 | |
| 239 void test_isExtendedBy_ClassDeclaration() { | |
| 240 _indexTestUnit(''' | |
| 241 class A {} // 1 | |
| 242 class B extends A {} // 2 | |
| 243 '''); | |
| 244 // prepare elements | |
| 245 ClassElement classElementA = _findElement("A"); | |
| 246 ClassElement classElementB = _findElement("B"); | |
| 247 // verify | |
| 248 _assertRecordedRelation(classElementA, IndexConstants.IS_EXTENDED_BY, | |
| 249 _expectedLocation(classElementB, 'A {} // 2')); | |
| 250 } | |
| 251 | |
| 252 void test_isExtendedBy_ClassDeclaration_Object() { | |
| 253 _indexTestUnit(''' | |
| 254 class A {} // 1 | |
| 255 '''); | |
| 256 // prepare elements | |
| 257 ClassElement classElementA = _findElement("A"); | |
| 258 ClassElement classElementObject = classElementA.supertype.element; | |
| 259 // verify | |
| 260 _assertRecordedRelation(classElementObject, IndexConstants.IS_EXTENDED_BY, | |
| 261 _expectedLocation(classElementA, 'A {}', 0)); | |
| 262 } | |
| 263 | |
| 264 void test_isExtendedBy_ClassTypeAlias() { | |
| 265 _indexTestUnit(''' | |
| 266 class A {} // 1 | |
| 267 class B {} // 2 | |
| 268 class C = A with B; // 3 | |
| 269 '''); | |
| 270 // prepare elements | |
| 271 ClassElement classElementA = _findElement("A"); | |
| 272 ClassElement classElementC = _findElement("C"); | |
| 273 // verify | |
| 274 _assertRecordedRelation(classElementA, IndexConstants.IS_EXTENDED_BY, | |
| 275 _expectedLocation(classElementC, 'A with')); | |
| 276 } | |
| 277 | |
| 278 void test_isImplementedBy_ClassDeclaration() { | |
| 279 _indexTestUnit(''' | |
| 280 class A {} // 1 | |
| 281 class B implements A {} // 2 | |
| 282 '''); | |
| 283 // prepare elements | |
| 284 ClassElement classElementA = _findElement("A"); | |
| 285 ClassElement classElementB = _findElement("B"); | |
| 286 // verify | |
| 287 _assertRecordedRelation(classElementA, IndexConstants.IS_IMPLEMENTED_BY, | |
| 288 _expectedLocation(classElementB, 'A {} // 2')); | |
| 289 } | |
| 290 | |
| 291 void test_isImplementedBy_ClassTypeAlias() { | |
| 292 _indexTestUnit(''' | |
| 293 class A {} // 1 | |
| 294 class B {} // 2 | |
| 295 class C = Object with A implements B; // 3 | |
| 296 '''); | |
| 297 // prepare elements | |
| 298 ClassElement classElementB = _findElement("B"); | |
| 299 ClassElement classElementC = _findElement("C"); | |
| 300 // verify | |
| 301 _assertRecordedRelation(classElementB, IndexConstants.IS_IMPLEMENTED_BY, | |
| 302 _expectedLocation(classElementC, 'B; // 3')); | |
| 303 } | |
| 304 | |
| 305 | |
| 306 void test_isInvokedByQualified_FieldElement() { | |
| 307 _indexTestUnit(''' | |
| 308 class A { | |
| 309 var field; | |
| 310 main() { | |
| 311 this.field(); | |
| 312 } | |
| 313 }'''); | |
| 314 // prepare elements | |
| 315 Element mainElement = _findElement("main"); | |
| 316 FieldElement fieldElement = _findElement("field"); | |
| 317 PropertyAccessorElement getterElement = fieldElement.getter; | |
| 318 // verify | |
| 319 _assertRecordedRelation(getterElement, | |
| 320 IndexConstants.IS_INVOKED_BY_QUALIFIED, _expectedLocation(mainElement, | |
| 321 'field();')); | |
| 322 } | |
| 323 | |
| 324 | |
| 325 void test_isInvokedByQualified_MethodElement() { | |
| 326 _indexTestUnit(''' | |
| 327 class A { | |
| 328 foo() {} | |
| 329 main() { | |
| 330 this.foo(); | |
| 331 } | |
| 332 }'''); | |
| 333 // prepare elements | |
| 334 Element mainElement = _findElement("main"); | |
| 335 Element methodElement = _findElement("foo"); | |
| 336 // verify | |
| 337 var location = _expectedLocation(mainElement, 'foo();'); | |
| 338 _assertRecordedRelation(methodElement, | |
| 339 IndexConstants.IS_INVOKED_BY_QUALIFIED, location); | |
| 340 _assertNoRecordedRelation(methodElement, | |
| 341 IndexConstants.IS_REFERENCED_BY_QUALIFIED, location); | |
| 342 } | |
| 343 | |
| 344 void test_isInvokedByQualified_MethodElement_propagatedType() { | |
| 345 _indexTestUnit(''' | |
| 346 class A { | |
| 347 foo() {} | |
| 348 } | |
| 349 main() { | |
| 350 var a = new A(); | |
| 351 a.foo(); | |
| 352 } | |
| 353 '''); | |
| 354 // prepare elements | |
| 355 Element mainElement = _findElement("main"); | |
| 356 Element methodElement = _findElement("foo"); | |
| 357 // verify | |
| 358 _assertRecordedRelation(methodElement, | |
| 359 IndexConstants.IS_INVOKED_BY_QUALIFIED, _expectedLocation(mainElement, | |
| 360 'foo();')); | |
| 361 } | |
| 362 | |
| 363 void test_isInvokedByUnqualified_MethodElement() { | |
| 364 _indexTestUnit(''' | |
| 365 class A { | |
| 366 foo() {} | |
| 367 main() { | |
| 368 foo(); | |
| 369 } | |
| 370 }'''); | |
| 371 // prepare elements | |
| 372 Element mainElement = _findElement("main"); | |
| 373 Element methodElement = _findElement("foo"); | |
| 374 // verify | |
| 375 _assertRecordedRelation(methodElement, | |
| 376 IndexConstants.IS_INVOKED_BY_UNQUALIFIED, _expectedLocation(mainElement, | |
| 377 'foo();')); | |
| 378 } | |
| 379 | |
| 380 void test_isInvokedBy_FunctionElement() { | |
| 381 _indexTestUnit(''' | |
| 382 foo() {} | |
| 383 main() { | |
| 384 foo(); | |
| 385 }'''); | |
| 386 // prepare elements | |
| 387 Element mainElement = _findElement("main"); | |
| 388 FunctionElement functionElement = _findElement("foo"); | |
| 389 // verify | |
| 390 _assertRecordedRelation(functionElement, IndexConstants.IS_INVOKED_BY, | |
| 391 _expectedLocation(mainElement, 'foo();')); | |
| 392 } | |
| 393 | |
| 394 void test_isInvokedBy_LocalVariableElement() { | |
| 395 _indexTestUnit(''' | |
| 396 main() { | |
| 397 var v; | |
| 398 v(); | |
| 399 }'''); | |
| 400 // prepare elements | |
| 401 Element mainElement = _findElement("main"); | |
| 402 Element element = _findElement("v"); | |
| 403 // verify | |
| 404 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY, | |
| 405 _expectedLocation(mainElement, 'v();')); | |
| 406 } | |
| 407 | |
| 408 void test_isInvokedBy_ParameterElement() { | |
| 409 _indexTestUnit(''' | |
| 410 main(p()) { | |
| 411 p(); | |
| 412 }'''); | |
| 413 // prepare elements | |
| 414 Element mainElement = _findElement("main"); | |
| 415 Element element = _findElement("p"); | |
| 416 // verify | |
| 417 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY, | |
| 418 _expectedLocation(mainElement, 'p();')); | |
| 419 } | |
| 420 | |
| 421 void test_isMixedInBy_ClassDeclaration() { | |
| 422 _indexTestUnit(''' | |
| 423 class A {} // 1 | |
| 424 class B extends Object with A {} // 2 | |
| 425 '''); | |
| 426 // prepare elements | |
| 427 ClassElement classElementA = _findElement("A"); | |
| 428 ClassElement classElementB = _findElement("B"); | |
| 429 // verify | |
| 430 _assertRecordedRelation(classElementA, IndexConstants.IS_MIXED_IN_BY, | |
| 431 _expectedLocation(classElementB, 'A {} // 2')); | |
| 432 } | |
| 433 | |
| 434 void test_isMixedInBy_ClassTypeAlias() { | |
| 435 _indexTestUnit(''' | |
| 436 class A {} // 1 | |
| 437 class B = Object with A; // 2 | |
| 438 '''); | |
| 439 // prepare elements | |
| 440 ClassElement classElementA = _findElement("A"); | |
| 441 ClassElement classElementB = _findElement("B"); | |
| 442 // verify | |
| 443 _assertRecordedRelation(classElementA, IndexConstants.IS_MIXED_IN_BY, | |
| 444 _expectedLocation(classElementB, 'A; // 2')); | |
| 445 } | |
| 446 | |
| 447 void test_isReadBy_ParameterElement() { | |
| 448 _indexTestUnit(''' | |
| 449 main(var p) { | |
| 450 print(p); | |
| 451 } | |
| 452 '''); | |
| 453 // prepare elements | |
| 454 Element mainElement = _findElement("main"); | |
| 455 Element parameterElement = _findElement("p"); | |
| 456 // verify | |
| 457 _assertRecordedRelation(parameterElement, IndexConstants.IS_READ_BY, | |
| 458 _expectedLocation(mainElement, 'p);')); | |
| 459 } | |
| 460 | |
| 461 void test_isReadBy_VariableElement() { | |
| 462 _indexTestUnit(''' | |
| 463 main() { | |
| 464 var v = 0; | |
| 465 print(v); | |
| 466 } | |
| 467 '''); | |
| 468 // prepare elements | |
| 469 Element mainElement = _findElement("main"); | |
| 470 Element variableElement = _findElement("v"); | |
| 471 // verify | |
| 472 _assertRecordedRelation(variableElement, IndexConstants.IS_READ_BY, | |
| 473 _expectedLocation(mainElement, 'v);')); | |
| 474 } | |
| 475 | |
| 476 void test_isReadWrittenBy_ParameterElement() { | |
| 477 _indexTestUnit(''' | |
| 478 main(int p) { | |
| 479 p += 1; | |
| 480 } | |
| 481 '''); | |
| 482 // prepare elements | |
| 483 Element mainElement = _findElement("main"); | |
| 484 Element parameterElement = _findElement("p"); | |
| 485 // verify | |
| 486 _assertRecordedRelation(parameterElement, IndexConstants.IS_READ_WRITTEN_BY, | |
| 487 _expectedLocation(mainElement, 'p += 1')); | |
| 488 } | |
| 489 | |
| 490 void test_isReadWrittenBy_VariableElement() { | |
| 491 _indexTestUnit(''' | |
| 492 main() { | |
| 493 var v = 0; | |
| 494 v += 1; | |
| 495 } | |
| 496 '''); | |
| 497 // prepare elements | |
| 498 Element mainElement = _findElement("main"); | |
| 499 Element variableElement = _findElement("v"); | |
| 500 // verify | |
| 501 _assertRecordedRelation(variableElement, IndexConstants.IS_READ_WRITTEN_BY, | |
| 502 _expectedLocation(mainElement, 'v += 1')); | |
| 503 } | |
| 504 | |
| 505 void test_isReferencedByQualifiedResolved_NameElement_field() { | |
| 506 _indexTestUnit(''' | |
| 507 class A { | |
| 508 int field; | |
| 509 } | |
| 510 main(A a) { | |
| 511 print(a.field); | |
| 512 } | |
| 513 '''); | |
| 514 // prepare elements | |
| 515 Element mainElement = _findElement('main'); | |
| 516 Element nameElement = new NameElement('field'); | |
| 517 // verify | |
| 518 _assertRecordedRelation(nameElement, | |
| 519 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 520 mainElement, 'field);')); | |
| 521 } | |
| 522 | |
| 523 void test_isReferencedByQualifiedResolved_NameElement_method() { | |
| 524 _indexTestUnit(''' | |
| 525 class A { | |
| 526 method() {} | |
| 527 } | |
| 528 main(A a) { | |
| 529 a.method(); | |
| 530 } | |
| 531 '''); | |
| 532 // prepare elements | |
| 533 Element mainElement = _findElement('main'); | |
| 534 Element nameElement = new NameElement('method'); | |
| 535 // verify | |
| 536 _assertRecordedRelation(nameElement, | |
| 537 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 538 mainElement, 'method();')); | |
| 539 } | |
| 540 | |
| 541 void test_isReferencedByQualifiedResolved_NameElement_operator() { | |
| 542 _indexTestUnit(''' | |
| 543 class A { | |
| 544 operator +(o) {} | |
| 545 operator -(o) {} | |
| 546 operator ~() {} | |
| 547 operator ==(o) {} | |
| 548 } | |
| 549 main(A a) { | |
| 550 a + 5; | |
| 551 a += 5; | |
| 552 a == 5; | |
| 553 ++a; | |
| 554 --a; | |
| 555 ~a; | |
| 556 a++; | |
| 557 a--; | |
| 558 } | |
| 559 '''); | |
| 560 // prepare elements | |
| 561 Element mainElement = _findElement('main'); | |
| 562 // binary | |
| 563 _assertRecordedRelation(new NameElement('+'), | |
| 564 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 565 mainElement, '+ 5', '+'.length)); | |
| 566 _assertRecordedRelation(new NameElement('+'), | |
| 567 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 568 mainElement, '+= 5', '+='.length)); | |
| 569 _assertRecordedRelation(new NameElement('=='), | |
| 570 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 571 mainElement, '== 5', '=='.length)); | |
| 572 // prefix | |
| 573 _assertRecordedRelation(new NameElement('+'), | |
| 574 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 575 mainElement, '++a', '++'.length)); | |
| 576 _assertRecordedRelation(new NameElement('-'), | |
| 577 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 578 mainElement, '--a', '--'.length)); | |
| 579 _assertRecordedRelation(new NameElement('~'), | |
| 580 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 581 mainElement, '~a', '~'.length)); | |
| 582 // postfix | |
| 583 _assertRecordedRelation(new NameElement('+'), | |
| 584 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 585 mainElement, '++;', '++'.length)); | |
| 586 _assertRecordedRelation(new NameElement('-'), | |
| 587 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED, _expectedLocation( | |
| 588 mainElement, '--;', '--'.length)); | |
| 589 } | |
| 590 | |
| 591 void test_isReferencedByQualifiedUnresolved_NameElement_field() { | |
| 592 _indexTestUnit(''' | |
| 593 class A { | |
| 594 int field; | |
| 595 } | |
| 596 main(var a) { | |
| 597 print(a.field); | |
| 598 } | |
| 599 '''); | |
| 600 // prepare elements | |
| 601 Element mainElement = _findElement('main'); | |
| 602 FieldElement fieldElement = _findElement('field'); | |
| 603 Element nameElement = new NameElement('field'); | |
| 604 // verify | |
| 605 _assertRecordedRelation(nameElement, IndexConstants.IS_DEFINED_BY, | |
| 606 _expectedLocation(fieldElement, 'field;')); | |
| 607 _assertRecordedRelation(nameElement, | |
| 608 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 609 mainElement, 'field);')); | |
| 610 } | |
| 611 | |
| 612 void test_isReferencedByQualifiedUnresolved_NameElement_method() { | |
| 613 _indexTestUnit(''' | |
| 614 class A { | |
| 615 method() {} | |
| 616 } | |
| 617 main(var a) { | |
| 618 a.method(); | |
| 619 } | |
| 620 '''); | |
| 621 // prepare elements | |
| 622 Element mainElement = _findElement('main'); | |
| 623 MethodElement methodElement = _findElement('method'); | |
| 624 Element nameElement = new NameElement('method'); | |
| 625 // verify | |
| 626 _assertRecordedRelation(nameElement, IndexConstants.IS_DEFINED_BY, | |
| 627 _expectedLocation(methodElement, 'method() {}')); | |
| 628 _assertRecordedRelation(nameElement, | |
| 629 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 630 mainElement, 'method();')); | |
| 631 } | |
| 632 | |
| 633 void test_isReferencedByQualifiedUnresolved_NameElement_operator() { | |
| 634 _indexTestUnit(''' | |
| 635 class A { | |
| 636 operator +(o) {} | |
| 637 operator -(o) {} | |
| 638 operator ~() {} | |
| 639 operator ==(o) {} | |
| 640 } | |
| 641 main(a) { | |
| 642 a + 5; | |
| 643 a += 5; | |
| 644 a == 5; | |
| 645 ++a; | |
| 646 --a; | |
| 647 ~a; | |
| 648 a++; | |
| 649 a--; | |
| 650 } | |
| 651 '''); | |
| 652 // prepare elements | |
| 653 Element mainElement = _findElement('main'); | |
| 654 // binary | |
| 655 _assertRecordedRelation(new NameElement('+'), | |
| 656 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 657 mainElement, '+ 5', '+'.length)); | |
| 658 _assertRecordedRelation(new NameElement('+'), | |
| 659 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 660 mainElement, '+= 5', '+='.length)); | |
| 661 _assertRecordedRelation(new NameElement('=='), | |
| 662 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 663 mainElement, '== 5', '=='.length)); | |
| 664 // prefix | |
| 665 _assertRecordedRelation(new NameElement('+'), | |
| 666 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 667 mainElement, '++a', '++'.length)); | |
| 668 _assertRecordedRelation(new NameElement('-'), | |
| 669 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 670 mainElement, '--a', '--'.length)); | |
| 671 _assertRecordedRelation(new NameElement('~'), | |
| 672 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 673 mainElement, '~a', '~'.length)); | |
| 674 // postfix | |
| 675 _assertRecordedRelation(new NameElement('+'), | |
| 676 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 677 mainElement, '++;', '++'.length)); | |
| 678 _assertRecordedRelation(new NameElement('-'), | |
| 679 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED, _expectedLocation( | |
| 680 mainElement, '--;', '--'.length)); | |
| 681 } | |
| 682 | |
| 683 void test_isReferencedByQualified_ConstructorElement() { | |
| 684 _indexTestUnit(''' | |
| 685 class A implements B { | |
| 686 A() {} | |
| 687 A.foo() {} | |
| 688 } | |
| 689 class B extends A { | |
| 690 B() : super(); // marker-1 | |
| 691 B.foo() : super.foo(); // marker-2 | |
| 692 factory B.bar() = A.foo; // marker-3 | |
| 693 } | |
| 694 main() { | |
| 695 new A(); // marker-main-1 | |
| 696 new A.foo(); // marker-main-2 | |
| 697 } | |
| 698 '''); | |
| 699 // prepare elements | |
| 700 Element mainElement = _findElement('main'); | |
| 701 var isConstructor = (node) => node is ConstructorDeclaration; | |
| 702 ConstructorElement consA = _findNodeElementAtString("A()", isConstructor); | |
| 703 ConstructorElement consA_foo = _findNodeElementAtString("A.foo()", | |
| 704 isConstructor); | |
| 705 ConstructorElement consB = _findNodeElementAtString("B()", isConstructor); | |
| 706 ConstructorElement consB_foo = _findNodeElementAtString("B.foo()", | |
| 707 isConstructor); | |
| 708 ConstructorElement consB_bar = _findNodeElementAtString("B.bar()", | |
| 709 isConstructor); | |
| 710 // A() | |
| 711 _assertRecordedRelation(consA, IndexConstants.IS_REFERENCED_BY, | |
| 712 _expectedLocation(consB, '(); // marker-1', 0)); | |
| 713 _assertRecordedRelation(consA, IndexConstants.IS_REFERENCED_BY, | |
| 714 _expectedLocation(mainElement, '(); // marker-main-1', 0)); | |
| 715 // A.foo() | |
| 716 _assertRecordedRelation(consA_foo, IndexConstants.IS_REFERENCED_BY, | |
| 717 _expectedLocation(consB_foo, '.foo(); // marker-2', '.foo'.length)); | |
| 718 _assertRecordedRelation(consA_foo, IndexConstants.IS_REFERENCED_BY, | |
| 719 _expectedLocation(consB_bar, '.foo; // marker-3', '.foo'.length)); | |
| 720 _assertRecordedRelation(consA_foo, IndexConstants.IS_REFERENCED_BY, | |
| 721 _expectedLocation(mainElement, '.foo(); // marker-main-2', '.foo'.length )); | |
| 722 } | |
| 723 | |
| 724 void test_isReferencedByQualified_ConstructorElement_classTypeAlias() { | |
| 725 _indexTestUnit(''' | |
| 726 class M {} | |
| 727 class A implements B { | |
| 728 A() {} | |
| 729 A.named() {} | |
| 730 } | |
| 731 class B = A with M; | |
| 732 main() { | |
| 733 new B(); // marker-main-1 | |
| 734 new B.named(); // marker-main-2 | |
| 735 } | |
| 736 '''); | |
| 737 // prepare elements | |
| 738 Element mainElement = _findElement('main'); | |
| 739 var isConstructor = (node) => node is ConstructorDeclaration; | |
| 740 ConstructorElement consA = _findNodeElementAtString("A()", isConstructor); | |
| 741 ConstructorElement consA_named = _findNodeElementAtString("A.named()", | |
| 742 isConstructor); | |
| 743 // verify | |
| 744 _assertRecordedRelation(consA, IndexConstants.IS_REFERENCED_BY, | |
| 745 _expectedLocation(mainElement, '(); // marker-main-1', 0)); | |
| 746 _assertRecordedRelation(consA_named, IndexConstants.IS_REFERENCED_BY, | |
| 747 _expectedLocation(mainElement, '.named(); // marker-main-2', '.named'.le ngth)); | |
| 748 } | |
| 749 | |
| 750 void test_isReferencedByQualified_FieldElement() { | |
| 751 _indexTestUnit(''' | |
| 752 class A { | |
| 753 static var field; | |
| 754 } | |
| 755 main() { | |
| 756 A.field = 1; | |
| 757 } | |
| 758 '''); | |
| 759 // prepare elements | |
| 760 Element mainElement = _findElement('main'); | |
| 761 FieldElement fieldElement = _findElement('field'); | |
| 762 PropertyAccessorElement setterElement = fieldElement.setter; | |
| 763 // verify | |
| 764 _assertRecordedRelation(setterElement, | |
| 765 IndexConstants.IS_REFERENCED_BY_QUALIFIED, _expectedLocation(mainElement , | |
| 766 'field = 1')); | |
| 767 } | |
| 768 | |
| 769 void test_isReferencedByQualified_MethodElement() { | |
| 770 _indexTestUnit(''' | |
| 771 class A { | |
| 772 foo() {} | |
| 773 main() { | |
| 774 print(this.foo); | |
| 775 } | |
| 776 } | |
| 777 '''); | |
| 778 // prepare elements | |
| 779 Element fooElement = _findElement('foo'); | |
| 780 Element mainElement = _findElement('main'); | |
| 781 // verify | |
| 782 _assertRecordedRelation(fooElement, | |
| 783 IndexConstants.IS_REFERENCED_BY_QUALIFIED, _expectedLocation(mainElement , | |
| 784 'foo);')); | |
| 785 } | |
| 786 | |
| 787 void test_isReferencedByQualified_MethodElement_operator_binary() { | |
| 788 _indexTestUnit(''' | |
| 789 class A { | |
| 790 operator +(other) => this; | |
| 791 } | |
| 792 main(A a) { | |
| 793 print(a + 1); | |
| 794 a += 2; | |
| 795 ++a; | |
| 796 a++; | |
| 797 } | |
| 798 '''); | |
| 799 // prepare elements | |
| 800 Element element = _findElement('+'); | |
| 801 Element mainElement = _findElement('main'); | |
| 802 // verify | |
| 803 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 804 _expectedLocation(mainElement, '+ 1', "+".length)); | |
| 805 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 806 _expectedLocation(mainElement, '+= 2', "+=".length)); | |
| 807 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 808 _expectedLocation(mainElement, '++a;', "++".length)); | |
| 809 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 810 _expectedLocation(mainElement, '++;', "++".length)); | |
| 811 } | |
| 812 | |
| 813 void test_isReferencedByQualified_MethodElement_operator_index() { | |
| 814 _indexTestUnit(''' | |
| 815 class A { | |
| 816 operator [](i) => null; | |
| 817 operator []=(i, v) {} | |
| 818 } | |
| 819 main(A a) { | |
| 820 print(a[0]); | |
| 821 a[1] = 42; | |
| 822 } | |
| 823 '''); | |
| 824 // prepare elements | |
| 825 MethodElement readElement = _findElement("[]"); | |
| 826 MethodElement writeElement = _findElement("[]="); | |
| 827 Element mainElement = _findElement('main'); | |
| 828 // verify | |
| 829 _assertRecordedRelation(readElement, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 830 _expectedLocation(mainElement, '[0]', "[".length)); | |
| 831 _assertRecordedRelation(writeElement, | |
| 832 IndexConstants.IS_INVOKED_BY_QUALIFIED, _expectedLocation(mainElement, ' [1] =', | |
| 833 "[".length)); | |
| 834 } | |
| 835 | |
| 836 void test_isReferencedByQualified_MethodElement_operator_prefix() { | |
| 837 _indexTestUnit(''' | |
| 838 class A { | |
| 839 A operator ~() => this; | |
| 840 } | |
| 841 main(A a) { | |
| 842 print(~a); | |
| 843 } | |
| 844 '''); | |
| 845 // prepare elements | |
| 846 MethodElement element = _findElement("~"); | |
| 847 Element mainElement = _findElement('main'); | |
| 848 // verify | |
| 849 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY_QUALIFIED, | |
| 850 _expectedLocation(mainElement, '~a', "~".length)); | |
| 851 } | |
| 852 | |
| 853 void test_isReferencedByQualified_PropertyAccessorElement_method_getter() { | |
| 854 _indexTestUnit(''' | |
| 855 class A { | |
| 856 get foo => 42; | |
| 857 main() { | |
| 858 print(this.foo); | |
| 859 } | |
| 860 } | |
| 861 '''); | |
| 862 // prepare elements | |
| 863 PropertyAccessorElement element = _findNodeElementAtString('foo =>'); | |
| 864 Element mainElement = _findElement('main'); | |
| 865 // verify | |
| 866 _assertRecordedRelation(element, IndexConstants.IS_REFERENCED_BY_QUALIFIED, | |
| 867 _expectedLocation(mainElement, 'foo);')); | |
| 868 _assertNoRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, null); | |
| 869 } | |
| 870 | |
| 871 void test_isReferencedByQualified_PropertyAccessorElement_method_setter() { | |
| 872 _indexTestUnit(''' | |
| 873 class A { | |
| 874 set foo(x) {} | |
| 875 main() { | |
| 876 this.foo = 42; | |
| 877 } | |
| 878 } | |
| 879 '''); | |
| 880 // prepare elements | |
| 881 PropertyAccessorElement element = _findNodeElementAtString('foo(x)'); | |
| 882 Element mainElement = _findElement('main'); | |
| 883 // verify | |
| 884 _assertRecordedRelation(element, IndexConstants.IS_REFERENCED_BY_QUALIFIED, | |
| 885 _expectedLocation(mainElement, 'foo = 42;')); | |
| 886 _assertNoRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, null); | |
| 887 } | |
| 888 | |
| 889 void test_isReferencedByQualified_PropertyAccessorElement_topLevelField() { | |
| 890 addSource('/lib.dart', ''' | |
| 891 library lib; | |
| 892 var myVar; | |
| 893 '''); | |
| 894 _indexTestUnit(''' | |
| 895 import 'lib.dart' as pref; | |
| 896 main() { | |
| 897 pref.myVar = 1; | |
| 898 print(pref.myVar); | |
| 899 } | |
| 900 '''); | |
| 901 // prepare elements | |
| 902 Element mainElement = _findElement('main'); | |
| 903 ImportElement importElement = testLibraryElement.imports[0]; | |
| 904 CompilationUnitElement impUnit = | |
| 905 importElement.importedLibrary.definingCompilationUnit; | |
| 906 TopLevelVariableElement myVar = impUnit.topLevelVariables[0]; | |
| 907 PropertyAccessorElement getter = myVar.getter; | |
| 908 PropertyAccessorElement setter = myVar.setter; | |
| 909 // verify | |
| 910 _assertRecordedRelation(setter, IndexConstants.IS_REFERENCED_BY_QUALIFIED, | |
| 911 _expectedLocation(mainElement, 'myVar = 1')); | |
| 912 _assertRecordedRelation(getter, IndexConstants.IS_REFERENCED_BY_QUALIFIED, | |
| 913 _expectedLocation(mainElement, 'myVar);')); | |
| 914 } | |
| 915 | |
| 916 void test_isReferencedByUnqualified_FieldElement() { | |
| 917 _indexTestUnit(''' | |
| 918 class A { | |
| 919 var field; | |
| 920 main() { | |
| 921 field = 5; | |
| 922 print(field); | |
| 923 } | |
| 924 }'''); | |
| 925 // prepare elements | |
| 926 Element mainElement = _findElement("main"); | |
| 927 FieldElement fieldElement = _findElement("field"); | |
| 928 PropertyAccessorElement getter = fieldElement.getter; | |
| 929 PropertyAccessorElement setter = fieldElement.setter; | |
| 930 // verify | |
| 931 _assertRecordedRelation(setter, IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, | |
| 932 _expectedLocation(mainElement, 'field = 5')); | |
| 933 _assertRecordedRelation(getter, IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, | |
| 934 _expectedLocation(mainElement, 'field);')); | |
| 935 } | |
| 936 | |
| 937 void test_isReferencedByUnqualified_MethodElement() { | |
| 938 _indexTestUnit(''' | |
| 939 class A { | |
| 940 method() {} | |
| 941 main() { | |
| 942 print(method); | |
| 943 } | |
| 944 }'''); | |
| 945 // prepare elements | |
| 946 Element mainElement = _findElement("main"); | |
| 947 MethodElement methodElement = _findElement("method"); | |
| 948 // verify | |
| 949 _assertRecordedRelation(methodElement, | |
| 950 IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, _expectedLocation(mainEleme nt, | |
| 951 'method);')); | |
| 952 _assertNoRecordedRelation(methodElement, IndexConstants.IS_REFERENCED_BY, | |
| 953 null); | |
| 954 } | |
| 955 | |
| 956 void test_isReferencedByUnqualified_TopLevelVariableElement() { | |
| 957 _indexTestUnit(''' | |
| 958 var topVariable; | |
| 959 main() { | |
| 960 topVariable = 5; | |
| 961 print(topVariable); | |
| 962 }'''); | |
| 963 // prepare elements | |
| 964 Element mainElement = _findElement("main"); | |
| 965 TopLevelVariableElement variableElement = _findElement("topVariable"); | |
| 966 // verify | |
| 967 _assertRecordedRelation(variableElement.setter, | |
| 968 IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, _expectedLocation(mainEleme nt, | |
| 969 'topVariable = 5')); | |
| 970 _assertRecordedRelation(variableElement.getter, | |
| 971 IndexConstants.IS_REFERENCED_BY_UNQUALIFIED, _expectedLocation(mainEleme nt, | |
| 972 'topVariable);')); | |
| 973 } | |
| 974 | |
| 975 void test_isReferencedBy_ClassElement() { | |
| 976 _indexTestUnit(''' | |
| 977 class A { | |
| 978 static var field; | |
| 979 } | |
| 980 main(A p) { | |
| 981 A v; | |
| 982 new A(); // 2 | |
| 983 A.field = 1; | |
| 984 print(A.field); // 3 | |
| 985 } | |
| 986 '''); | |
| 987 // prepare elements | |
| 988 ClassElement aElement = _findElement("A"); | |
| 989 Element mainElement = _findElement("main"); | |
| 990 ParameterElement pElement = _findElement("p"); | |
| 991 VariableElement vElement = _findElement("v"); | |
| 992 // verify | |
| 993 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 994 _expectedLocation(pElement, 'A p) {')); | |
| 995 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 996 _expectedLocation(vElement, 'A v;')); | |
| 997 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 998 _expectedLocation(mainElement, 'A(); // 2')); | |
| 999 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 1000 _expectedLocation(mainElement, 'A.field = 1;')); | |
| 1001 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 1002 _expectedLocation(mainElement, 'A.field); // 3')); | |
| 1003 } | |
| 1004 | |
| 1005 void test_isReferencedBy_ClassTypeAlias() { | |
| 1006 _indexTestUnit(''' | |
| 1007 class A {} | |
| 1008 class B = Object with A; | |
| 1009 main(B p) { | |
| 1010 B v; | |
| 1011 } | |
| 1012 '''); | |
| 1013 // prepare elements | |
| 1014 ClassElement bElement = _findElement("B"); | |
| 1015 ParameterElement pElement = _findElement("p"); | |
| 1016 VariableElement vElement = _findElement("v"); | |
| 1017 // verify | |
| 1018 _assertRecordedRelation(bElement, IndexConstants.IS_REFERENCED_BY, | |
| 1019 _expectedLocation(pElement, 'B p) {')); | |
| 1020 _assertRecordedRelation(bElement, IndexConstants.IS_REFERENCED_BY, | |
| 1021 _expectedLocation(vElement, 'B v;')); | |
| 1022 } | |
| 1023 | |
| 1024 void test_isReferencedBy_CompilationUnitElement() { | |
| 1025 addSource('/my_unit.dart', 'part of my_lib;'); | |
| 1026 _indexTestUnit(''' | |
| 1027 library my_lib; | |
| 1028 part 'my_unit.dart'; | |
| 1029 '''); | |
| 1030 // prepare elements | |
| 1031 CompilationUnitElement myUnitElement = testLibraryElement.parts[0]; | |
| 1032 // verify | |
| 1033 _assertRecordedRelation(myUnitElement, IndexConstants.IS_REFERENCED_BY, | |
| 1034 _expectedLocation(testUnitElement, "'my_unit.dart';", "'my_unit.dart'".l ength)); | |
| 1035 } | |
| 1036 | |
| 1037 void test_isReferencedBy_ConstructorFieldInitializer() { | |
| 1038 _indexTestUnit(''' | |
| 1039 class A { | |
| 1040 int field; | |
| 1041 A() : field = 5; | |
| 1042 } | |
| 1043 '''); | |
| 1044 // prepare elements | |
| 1045 ConstructorElement constructorElement = _findNodeElementAtString("A()", | |
| 1046 (node) => node is ConstructorDeclaration); | |
| 1047 FieldElement fieldElement = _findElement("field"); | |
| 1048 // verify | |
| 1049 _assertRecordedRelation(fieldElement, IndexConstants.IS_REFERENCED_BY, | |
| 1050 _expectedLocation(constructorElement, 'field = 5')); | |
| 1051 } | |
| 1052 | |
| 1053 void test_isReferencedBy_FieldFormalParameterElement() { | |
| 1054 _indexTestUnit(''' | |
| 1055 class A { | |
| 1056 int field; | |
| 1057 A(this.field); | |
| 1058 } | |
| 1059 '''); | |
| 1060 // prepare elements | |
| 1061 FieldElement fieldElement = _findElement("field"); | |
| 1062 Element fieldParameterElement = _findNodeElementAtString("field);"); | |
| 1063 // verify | |
| 1064 _assertRecordedRelation(fieldElement, | |
| 1065 IndexConstants.IS_REFERENCED_BY_QUALIFIED, _expectedLocation( | |
| 1066 fieldParameterElement, 'field);')); | |
| 1067 } | |
| 1068 | |
| 1069 void test_isReferencedBy_FunctionElement() { | |
| 1070 _indexTestUnit(''' | |
| 1071 foo() {} | |
| 1072 main() { | |
| 1073 print(foo); | |
| 1074 print(foo()); | |
| 1075 } | |
| 1076 '''); | |
| 1077 // prepare elements | |
| 1078 FunctionElement element = _findElement("foo"); | |
| 1079 Element mainElement = _findElement("main"); | |
| 1080 // "referenced" here | |
| 1081 _assertRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, | |
| 1082 _expectedLocation(mainElement, 'foo);')); | |
| 1083 // only "invoked", but not "referenced" | |
| 1084 { | |
| 1085 _assertRecordedRelation(element, IndexConstants.IS_INVOKED_BY, | |
| 1086 _expectedLocation(mainElement, 'foo());')); | |
| 1087 _assertNoRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, | |
| 1088 _expectedLocation(mainElement, 'foo());')); | |
| 1089 } | |
| 1090 } | |
| 1091 | |
| 1092 void test_isReferencedBy_FunctionTypeAliasElement() { | |
| 1093 _indexTestUnit(''' | |
| 1094 typedef A(); | |
| 1095 main(A p) { | |
| 1096 } | |
| 1097 '''); | |
| 1098 // prepare elements | |
| 1099 Element aElement = _findElement('A'); | |
| 1100 Element pElement = _findElement('p'); | |
| 1101 // verify | |
| 1102 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 1103 _expectedLocation(pElement, 'A p) {')); | |
| 1104 } | |
| 1105 | |
| 1106 void test_isReferencedBy_ImportElement_noPrefix() { | |
| 1107 addSource('/lib.dart', ''' | |
| 1108 library lib; | |
| 1109 var myVar; | |
| 1110 myFunction() {} | |
| 1111 myToHide() {} | |
| 1112 '''); | |
| 1113 _indexTestUnit(''' | |
| 1114 import 'lib.dart' show myVar, myFunction hide myToHide; | |
| 1115 main() { | |
| 1116 myVar = 1; | |
| 1117 myFunction(); | |
| 1118 print(0); | |
| 1119 } | |
| 1120 '''); | |
| 1121 // prepare elements | |
| 1122 ImportElement importElement = testLibraryElement.imports[0]; | |
| 1123 Element mainElement = _findElement('main'); | |
| 1124 // verify | |
| 1125 _assertRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1126 _expectedLocation(mainElement, 'myVar = 1;', 0)); | |
| 1127 _assertRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1128 _expectedLocation(mainElement, 'myFunction();', 0)); | |
| 1129 _assertNoRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1130 _expectedLocation(mainElement, 'print(0);', 0)); | |
| 1131 // no references from import combinators | |
| 1132 _assertNoRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1133 _expectedLocation(testUnitElement, 'myVar, ', 0)); | |
| 1134 _assertNoRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1135 _expectedLocation(testUnitElement, 'myFunction hide', 0)); | |
| 1136 _assertNoRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1137 _expectedLocation(testUnitElement, 'myToHide;', 0)); | |
| 1138 } | |
| 1139 | |
| 1140 void test_isReferencedBy_ImportElement_withPrefix() { | |
| 1141 addSource('/libA.dart', ''' | |
| 1142 library libA; | |
| 1143 var myVar; | |
| 1144 '''); | |
| 1145 addSource('/libB.dart', ''' | |
| 1146 library libB; | |
| 1147 class MyClass {} | |
| 1148 '''); | |
| 1149 _indexTestUnit(''' | |
| 1150 import 'libA.dart' as pref; | |
| 1151 import 'libB.dart' as pref; | |
| 1152 main() { | |
| 1153 pref.myVar = 1; | |
| 1154 new pref.MyClass(); | |
| 1155 } | |
| 1156 '''); | |
| 1157 // prepare elements | |
| 1158 ImportElement importElementA = testLibraryElement.imports[0]; | |
| 1159 ImportElement importElementB = testLibraryElement.imports[1]; | |
| 1160 Element mainElement = _findElement('main'); | |
| 1161 // verify | |
| 1162 _assertRecordedRelation(importElementA, IndexConstants.IS_REFERENCED_BY, | |
| 1163 _expectedLocation(mainElement, 'pref.myVar = 1;', 'pref.'.length)); | |
| 1164 _assertRecordedRelation(importElementB, IndexConstants.IS_REFERENCED_BY, | |
| 1165 _expectedLocation(mainElement, 'pref.MyClass();', 'pref.'.length)); | |
| 1166 } | |
| 1167 | |
| 1168 void test_isReferencedBy_ImportElement_withPrefix_combinators() { | |
| 1169 addSource('/lib.dart', ''' | |
| 1170 library lib; | |
| 1171 class A {} | |
| 1172 class B {} | |
| 1173 '''); | |
| 1174 _indexTestUnit(''' | |
| 1175 import 'lib.dart' as pref show A; | |
| 1176 import 'lib.dart' as pref show B; | |
| 1177 import 'lib.dart'; | |
| 1178 import 'lib.dart' as otherPrefix; | |
| 1179 main() { | |
| 1180 new pref.A(); | |
| 1181 new pref.B(); | |
| 1182 } | |
| 1183 '''); | |
| 1184 // prepare elements | |
| 1185 ImportElement importElementA = testLibraryElement.imports[0]; | |
| 1186 ImportElement importElementB = testLibraryElement.imports[1]; | |
| 1187 Element mainElement = _findElement('main'); | |
| 1188 // verify | |
| 1189 _assertRecordedRelation(importElementA, IndexConstants.IS_REFERENCED_BY, | |
| 1190 _expectedLocation(mainElement, 'pref.A();', 'pref.'.length)); | |
| 1191 _assertRecordedRelation(importElementB, IndexConstants.IS_REFERENCED_BY, | |
| 1192 _expectedLocation(mainElement, 'pref.B();', 'pref.'.length)); | |
| 1193 } | |
| 1194 | |
| 1195 void test_isReferencedBy_ImportElement_withPrefix_invocation() { | |
| 1196 addSource('/lib.dart', ''' | |
| 1197 library lib; | |
| 1198 myFunc() {} | |
| 1199 '''); | |
| 1200 _indexTestUnit(''' | |
| 1201 import 'lib.dart' as pref; | |
| 1202 main() { | |
| 1203 pref.myFunc(); | |
| 1204 } | |
| 1205 '''); | |
| 1206 // prepare elements | |
| 1207 ImportElement importElement = testLibraryElement.imports[0]; | |
| 1208 Element mainElement = _findElement('main'); | |
| 1209 // verify | |
| 1210 _assertRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1211 _expectedLocation(mainElement, 'pref.myFunc();', 'pref.'.length)); | |
| 1212 } | |
| 1213 | |
| 1214 void test_isReferencedBy_ImportElement_withPrefix_oneCandidate() { | |
| 1215 addSource('/lib.dart', ''' | |
| 1216 library lib; | |
| 1217 class A {} | |
| 1218 class B {} | |
| 1219 '''); | |
| 1220 _indexTestUnit(''' | |
| 1221 import 'lib.dart' as pref show A; | |
| 1222 main() { | |
| 1223 new pref.A(); | |
| 1224 } | |
| 1225 '''); | |
| 1226 // prepare elements | |
| 1227 ImportElement importElement = testLibraryElement.imports[0]; | |
| 1228 Element mainElement = _findElement('main'); | |
| 1229 // verify | |
| 1230 _assertRecordedRelation(importElement, IndexConstants.IS_REFERENCED_BY, | |
| 1231 _expectedLocation(mainElement, 'pref.A();', 'pref.'.length)); | |
| 1232 } | |
| 1233 | |
| 1234 void test_isReferencedBy_ImportElement_withPrefix_unresolvedElement() { | |
| 1235 verifyNoTestUnitErrors = false; | |
| 1236 addSource('/lib.dart', ''' | |
| 1237 library lib; | |
| 1238 '''); | |
| 1239 _indexTestUnit(''' | |
| 1240 import 'lib.dart' as pref; | |
| 1241 main() { | |
| 1242 pref.myVar = 1; | |
| 1243 } | |
| 1244 '''); | |
| 1245 } | |
| 1246 | |
| 1247 void test_isReferencedBy_ImportElement_withPrefix_wrongInvocation() { | |
| 1248 verifyNoTestUnitErrors = false; | |
| 1249 _indexTestUnit(''' | |
| 1250 import 'dart:math' as m; | |
| 1251 main() { | |
| 1252 m(); | |
| 1253 }'''); | |
| 1254 } | |
| 1255 | |
| 1256 void test_isReferencedBy_ImportElement_withPrefix_wrongPrefixedIdentifier() { | |
| 1257 verifyNoTestUnitErrors = false; | |
| 1258 _indexTestUnit(''' | |
| 1259 import 'dart:math' as m; | |
| 1260 main() { | |
| 1261 x.m; | |
| 1262 } | |
| 1263 '''); | |
| 1264 } | |
| 1265 | |
| 1266 void test_isReferencedBy_LabelElement() { | |
| 1267 _indexTestUnit(''' | |
| 1268 main() { | |
| 1269 L: while (true) { | |
| 1270 break L; | |
| 1271 } | |
| 1272 } | |
| 1273 '''); | |
| 1274 // prepare elements | |
| 1275 Element mainElement = _findElement('main'); | |
| 1276 Element element = _findElement('L'); | |
| 1277 // verify | |
| 1278 _assertRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, | |
| 1279 _expectedLocation(mainElement, 'L;')); | |
| 1280 } | |
| 1281 | |
| 1282 void test_isReferencedBy_LibraryElement_export() { | |
| 1283 addSource('/lib.dart', ''' | |
| 1284 library lib; | |
| 1285 '''); | |
| 1286 _indexTestUnit(''' | |
| 1287 export 'lib.dart'; | |
| 1288 '''); | |
| 1289 // prepare elements | |
| 1290 LibraryElement libElement = testLibraryElement.exportedLibraries[0]; | |
| 1291 CompilationUnitElement libUnitElement = libElement.definingCompilationUnit; | |
| 1292 // verify | |
| 1293 _assertRecordedRelation(libUnitElement, IndexConstants.IS_REFERENCED_BY, | |
| 1294 _expectedLocation(testUnitElement, "'lib.dart'", "'lib.dart'".length)); | |
| 1295 } | |
| 1296 | |
| 1297 void test_isReferencedBy_LibraryElement_import() { | |
| 1298 addSource('/lib.dart', ''' | |
| 1299 library lib; | |
| 1300 '''); | |
| 1301 _indexTestUnit(''' | |
| 1302 import 'lib.dart'; | |
| 1303 '''); | |
| 1304 // prepare elements | |
| 1305 LibraryElement libElement = testLibraryElement.imports[0].importedLibrary; | |
| 1306 CompilationUnitElement libUnitElement = libElement.definingCompilationUnit; | |
| 1307 // verify | |
| 1308 _assertRecordedRelation(libUnitElement, IndexConstants.IS_REFERENCED_BY, | |
| 1309 _expectedLocation(testUnitElement, "'lib.dart'", "'lib.dart'".length)); | |
| 1310 } | |
| 1311 | |
| 1312 void test_isReferencedBy_ParameterElement() { | |
| 1313 _indexTestUnit(''' | |
| 1314 foo({var p}) {} | |
| 1315 main() { | |
| 1316 foo(p: 1); | |
| 1317 } | |
| 1318 '''); | |
| 1319 // prepare elements | |
| 1320 Element mainElement = _findElement('main'); | |
| 1321 Element element = _findElement('p'); | |
| 1322 // verify | |
| 1323 _assertRecordedRelation(element, IndexConstants.IS_REFERENCED_BY, | |
| 1324 _expectedLocation(mainElement, 'p: 1')); | |
| 1325 } | |
| 1326 | |
| 1327 void test_isReferencedBy_TypeParameterElement() { | |
| 1328 _indexTestUnit(''' | |
| 1329 class A<T> { | |
| 1330 T f; | |
| 1331 foo(T p) { | |
| 1332 T v; | |
| 1333 } | |
| 1334 } | |
| 1335 '''); | |
| 1336 // prepare elements | |
| 1337 Element typeParameterElement = _findElement('T'); | |
| 1338 Element fieldElement = _findElement('f'); | |
| 1339 Element parameterElement = _findElement('p'); | |
| 1340 Element variableElement = _findElement('v'); | |
| 1341 // verify | |
| 1342 _assertRecordedRelation(typeParameterElement, | |
| 1343 IndexConstants.IS_REFERENCED_BY, _expectedLocation(fieldElement, 'T f')) ; | |
| 1344 _assertRecordedRelation(typeParameterElement, | |
| 1345 IndexConstants.IS_REFERENCED_BY, _expectedLocation(parameterElement, 'T p')); | |
| 1346 _assertRecordedRelation(typeParameterElement, | |
| 1347 IndexConstants.IS_REFERENCED_BY, _expectedLocation(variableElement, 'T v ')); | |
| 1348 } | |
| 1349 | |
| 1350 /** | |
| 1351 * There was a bug in the AST structure, when single [Comment] was cloned and | |
| 1352 * assigned to both [FieldDeclaration] and [VariableDeclaration]. | |
| 1353 * | |
| 1354 * This caused duplicate indexing. | |
| 1355 * Here we test that the problem is fixed one way or another. | |
| 1356 */ | |
| 1357 void test_isReferencedBy_identifierInComment() { | |
| 1358 _indexTestUnit(''' | |
| 1359 class A {} | |
| 1360 /// [A] text | |
| 1361 var myVariable = null; | |
| 1362 '''); | |
| 1363 // prepare elements | |
| 1364 Element aElement = _findElement('A'); | |
| 1365 Element variableElement = _findElement('myVariable'); | |
| 1366 // verify | |
| 1367 _assertRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 1368 _expectedLocation(testUnitElement, 'A] text')); | |
| 1369 _assertNoRecordedRelation(aElement, IndexConstants.IS_REFERENCED_BY, | |
| 1370 _expectedLocation(variableElement, 'A] text')); | |
| 1371 } | |
| 1372 | |
| 1373 void test_isReferencedBy_libraryName() { | |
| 1374 Source libSource = addSource('/lib.dart', ''' | |
| 1375 library lib; | |
| 1376 part 'test.dart'; | |
| 1377 '''); | |
| 1378 testCode = 'part of lib;'; | |
| 1379 testSource = addSource('/test.dart', testCode); | |
| 1380 testUnit = resolveDartUnit(testSource, libSource); | |
| 1381 testUnitElement = testUnit.element; | |
| 1382 testLibraryElement = testUnitElement.library; | |
| 1383 indexDartUnit(store, context, testUnit); | |
| 1384 // verify | |
| 1385 _assertRecordedRelation(testLibraryElement, IndexConstants.IS_REFERENCED_BY, | |
| 1386 _expectedLocation(testUnitElement, "lib;")); | |
| 1387 } | |
| 1388 | |
| 1389 void test_isReferencedBy_typeInVariableList() { | |
| 1390 _indexTestUnit(''' | |
| 1391 class A {} | |
| 1392 A myVariable = null; | |
| 1393 '''); | |
| 1394 // prepare elements | |
| 1395 Element classElementA = _findElement('A'); | |
| 1396 Element variableElement = _findElement('myVariable'); | |
| 1397 // verify | |
| 1398 _assertRecordedRelation(classElementA, IndexConstants.IS_REFERENCED_BY, | |
| 1399 _expectedLocation(variableElement, 'A myVariable')); | |
| 1400 } | |
| 1401 | |
| 1402 void test_isWrittenBy_ParameterElement() { | |
| 1403 _indexTestUnit(''' | |
| 1404 main(var p) { | |
| 1405 p = 1; | |
| 1406 }'''); | |
| 1407 // prepare elements | |
| 1408 Element mainElement = _findElement("main"); | |
| 1409 ParameterElement pElement = _findElement("p"); | |
| 1410 // verify | |
| 1411 _assertRecordedRelation(pElement, IndexConstants.IS_WRITTEN_BY, | |
| 1412 _expectedLocation(mainElement, 'p = 1')); | |
| 1413 } | |
| 1414 | |
| 1415 void test_isWrittenBy_VariableElement() { | |
| 1416 _indexTestUnit(''' | |
| 1417 main() { | |
| 1418 var v = 0; | |
| 1419 v = 1; | |
| 1420 }'''); | |
| 1421 // prepare elements | |
| 1422 Element mainElement = _findElement("main"); | |
| 1423 LocalVariableElement vElement = _findElement("v"); | |
| 1424 // verify | |
| 1425 _assertRecordedRelation(vElement, IndexConstants.IS_WRITTEN_BY, | |
| 1426 _expectedLocation(mainElement, 'v = 1')); | |
| 1427 } | |
| 1428 | |
| 1429 void test_nameIsInvokedBy() { | |
| 1430 _indexTestUnit(''' | |
| 1431 class A { | |
| 1432 test(x) {} | |
| 1433 } | |
| 1434 main(A a, p) { | |
| 1435 a.test(1); | |
| 1436 p.test(2); | |
| 1437 }'''); | |
| 1438 // prepare elements | |
| 1439 Element mainElement = _findElement("main"); | |
| 1440 Element nameElement = new NameElement('test'); | |
| 1441 // verify | |
| 1442 _assertRecordedRelation(nameElement, | |
| 1443 IndexConstants.NAME_IS_INVOKED_BY_RESOLVED, _expectedLocation(mainElemen t, | |
| 1444 'test(1)')); | |
| 1445 _assertRecordedRelation(nameElement, | |
| 1446 IndexConstants.NAME_IS_INVOKED_BY_UNRESOLVED, _expectedLocation(mainElem ent, | |
| 1447 'test(2)')); | |
| 1448 _assertNoRecordedRelation(nameElement, | |
| 1449 IndexConstants.NAME_IS_READ_BY_UNRESOLVED, _expectedLocation(mainElement , | |
| 1450 'test(2)')); | |
| 1451 } | |
| 1452 | |
| 1453 void test_nameIsReadBy() { | |
| 1454 _indexTestUnit(''' | |
| 1455 class A { | |
| 1456 var test; | |
| 1457 } | |
| 1458 main(A a, p) { | |
| 1459 print(a.test); // a | |
| 1460 print(p.test); // p | |
| 1461 }'''); | |
| 1462 // prepare elements | |
| 1463 Element mainElement = _findElement("main"); | |
| 1464 Element nameElement = new NameElement('test'); | |
| 1465 // verify | |
| 1466 _assertRecordedRelation(nameElement, | |
| 1467 IndexConstants.NAME_IS_READ_BY_RESOLVED, _expectedLocation(mainElement, | |
| 1468 'test); // a')); | |
| 1469 _assertRecordedRelation(nameElement, | |
| 1470 IndexConstants.NAME_IS_READ_BY_UNRESOLVED, _expectedLocation(mainElement , | |
| 1471 'test); // p')); | |
| 1472 } | |
| 1473 | |
| 1474 void test_nameIsReadWrittenBy() { | |
| 1475 _indexTestUnit(''' | |
| 1476 class A { | |
| 1477 var test; | |
| 1478 } | |
| 1479 main(A a, p) { | |
| 1480 a.test += 1; | |
| 1481 p.test += 2; | |
| 1482 }'''); | |
| 1483 // prepare elements | |
| 1484 Element mainElement = _findElement("main"); | |
| 1485 Element nameElement = new NameElement('test'); | |
| 1486 // verify | |
| 1487 _assertRecordedRelation(nameElement, | |
| 1488 IndexConstants.NAME_IS_READ_WRITTEN_BY_RESOLVED, _expectedLocation(mainE lement, | |
| 1489 'test += 1')); | |
| 1490 _assertRecordedRelation(nameElement, | |
| 1491 IndexConstants.NAME_IS_READ_WRITTEN_BY_UNRESOLVED, _expectedLocation( | |
| 1492 mainElement, 'test += 2')); | |
| 1493 } | |
| 1494 | |
| 1495 void test_nameIsWrittenBy() { | |
| 1496 _indexTestUnit(''' | |
| 1497 class A { | |
| 1498 var test; | |
| 1499 } | |
| 1500 main(A a, p) { | |
| 1501 a.test = 1; | |
| 1502 p.test = 2; | |
| 1503 }'''); | |
| 1504 // prepare elements | |
| 1505 Element mainElement = _findElement("main"); | |
| 1506 Element nameElement = new NameElement('test'); | |
| 1507 // verify | |
| 1508 _assertRecordedRelation(nameElement, | |
| 1509 IndexConstants.NAME_IS_WRITTEN_BY_RESOLVED, _expectedLocation(mainElemen t, | |
| 1510 'test = 1')); | |
| 1511 _assertRecordedRelation(nameElement, | |
| 1512 IndexConstants.NAME_IS_WRITTEN_BY_UNRESOLVED, _expectedLocation(mainElem ent, | |
| 1513 'test = 2')); | |
| 1514 } | |
| 1515 | |
| 1516 void test_nullUnit() { | |
| 1517 indexDartUnit(store, context, null); | |
| 1518 } | |
| 1519 | |
| 1520 void test_nullUnitElement() { | |
| 1521 CompilationUnit unit = new CompilationUnit(null, null, [], [], null); | |
| 1522 indexDartUnit(store, context, unit); | |
| 1523 } | |
| 1524 | |
| 1525 void _assertDefinesTopLevelElement(Relationship relationship, | |
| 1526 ExpectedLocation expectedLocation) { | |
| 1527 _assertRecordedRelation(testLibraryElement, relationship, expectedLocation); | |
| 1528 _assertRecordedRelation(UniverseElement.INSTANCE, relationship, | |
| 1529 expectedLocation); | |
| 1530 } | |
| 1531 | |
| 1532 void _assertNoErrorsInSource() { | |
| 1533 List<AnalysisError> errors = context.getErrors(testSource).errors; | |
| 1534 expect(errors, isEmpty); | |
| 1535 } | |
| 1536 | |
| 1537 /** | |
| 1538 * Asserts that [recordedRelations] has no item with the specified properties. | |
| 1539 */ | |
| 1540 void _assertNoRecordedRelation(Element element, Relationship relationship, | |
| 1541 ExpectedLocation location) { | |
| 1542 for (RecordedRelation recordedRelation in recordedRelations) { | |
| 1543 if (_equalsRecordedRelation(recordedRelation, element, relationship, | |
| 1544 location)) { | |
| 1545 fail('not expected: ${recordedRelation} in\n' + recordedRelations.join( | |
| 1546 '\n')); | |
| 1547 } | |
| 1548 } | |
| 1549 } | |
| 1550 | |
| 1551 /** | |
| 1552 * Asserts that [recordedRelations] has an item with the expected properties. | |
| 1553 */ | |
| 1554 Location _assertRecordedRelation(Element expectedElement, | |
| 1555 Relationship expectedRelationship, ExpectedLocation expectedLocation) { | |
| 1556 for (RecordedRelation recordedRelation in recordedRelations) { | |
| 1557 if (_equalsRecordedRelation(recordedRelation, expectedElement, | |
| 1558 expectedRelationship, expectedLocation)) { | |
| 1559 return recordedRelation.location; | |
| 1560 } | |
| 1561 } | |
| 1562 fail("not found\n$expectedElement $expectedRelationship " | |
| 1563 "in $expectedLocation in\n" + recordedRelations.join('\n')); | |
| 1564 return null; | |
| 1565 } | |
| 1566 | |
| 1567 ExpectedLocation _expectedLocation(Element element, String search, [int length | |
| 1568 = -1]) { | |
| 1569 int offset = _findOffset(search); | |
| 1570 if (length == -1) { | |
| 1571 length = _getLeadingIdentifierLength(search); | |
| 1572 } | |
| 1573 return new ExpectedLocation(element, offset, length); | |
| 1574 } | |
| 1575 | |
| 1576 Element _findElement(String name, [ElementKind kind]) { | |
| 1577 return findChildElement(testUnitElement, name, kind); | |
| 1578 } | |
| 1579 | |
| 1580 AstNode _findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { | |
| 1581 AstNode result = new NodeLocator.con1(offset).searchWithin(testUnit); | |
| 1582 if (result != null && predicate != null) { | |
| 1583 result = result.getAncestor(predicate); | |
| 1584 } | |
| 1585 return result; | |
| 1586 } | |
| 1587 | |
| 1588 AstNode _findNodeAtString(String search, [Predicate<AstNode> predicate]) { | |
| 1589 int offset = _findOffset(search); | |
| 1590 return _findNodeAtOffset(offset, predicate); | |
| 1591 } | |
| 1592 | |
| 1593 Element _findNodeElementAtString(String search, | |
| 1594 [Predicate<AstNode> predicate]) { | |
| 1595 AstNode node = _findNodeAtString(search, predicate); | |
| 1596 if (node == null) { | |
| 1597 return null; | |
| 1598 } | |
| 1599 return ElementLocator.locate(node); | |
| 1600 } | |
| 1601 | |
| 1602 int _findOffset(String search) { | |
| 1603 int offset = testCode.indexOf(search); | |
| 1604 expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode"); | |
| 1605 return offset; | |
| 1606 } | |
| 1607 | |
| 1608 void _indexTestUnit(String code) { | |
| 1609 testCode = code; | |
| 1610 testSource = addSource('/test.dart', code); | |
| 1611 testUnit = resolveLibraryUnit(testSource); | |
| 1612 if (verifyNoTestUnitErrors) { | |
| 1613 _assertNoErrorsInSource(); | |
| 1614 } | |
| 1615 testUnitElement = testUnit.element; | |
| 1616 testLibraryElement = testUnitElement.library; | |
| 1617 indexDartUnit(store, context, testUnit); | |
| 1618 } | |
| 1619 } | |
| 1620 | |
| 1621 class ExpectedLocation { | |
| 1622 Element element; | |
| 1623 int offset; | |
| 1624 int length; | |
| 1625 | |
| 1626 ExpectedLocation(this.element, this.offset, this.length); | |
| 1627 | |
| 1628 @override | |
| 1629 String toString() { | |
| 1630 return 'ExpectedLocation(element=$element; offset=$offset; length=$length)'; | |
| 1631 } | |
| 1632 } | |
| 1633 | |
| 1634 class MockIndexStore extends TypedMock implements IndexStore { | |
| 1635 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 1636 } | |
| 1637 | |
| 1638 | |
| 1639 /** | |
| 1640 * Information about a relation recorded into {@link IndexStore}. | |
| 1641 */ | |
| 1642 class RecordedRelation { | |
| 1643 final Element element; | |
| 1644 final Relationship relationship; | |
| 1645 final Location location; | |
| 1646 | |
| 1647 RecordedRelation(this.element, this.relationship, this.location); | |
| 1648 | |
| 1649 @override | |
| 1650 String toString() { | |
| 1651 return 'RecordedRelation(element=$element; relationship=$relationship; ' | |
| 1652 'location=$location)'; | |
| 1653 } | |
| 1654 } | |
| OLD | NEW |