Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(677)

Side by Side Diff: pkg/analysis_services/test/index/dart_index_contributor_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698