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

Side by Side Diff: pkg/analysis_server/test/services/search/search_engine2_test.dart

Issue 1763133002: New SearchEngine implementation - references only. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.services.src.search.search_engine2;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/src/services/index2/index2.dart';
10 import 'package:analysis_server/src/services/search/search_engine.dart';
11 import 'package:analysis_server/src/services/search/search_engine_internal2.dart ';
12 import 'package:analyzer/dart/element/element.dart';
13 import 'package:analyzer/src/dart/element/element.dart';
14 import 'package:analyzer/src/dart/element/member.dart';
15 import 'package:analyzer/src/generated/source.dart';
16 import 'package:test_reflective_loader/test_reflective_loader.dart';
17 import 'package:unittest/unittest.dart';
18
19 import '../../abstract_single_unit.dart';
20 import '../../utils.dart';
21
22 main() {
23 initializeTestEnvironment();
24 defineReflectiveTests(SearchEngineImpl2Test);
25 }
26
27 class ExpectedMatch {
28 final Element element;
29 final MatchKind kind;
30 SourceRange range;
31 final bool isResolved;
32 final bool isQualified;
33
34 ExpectedMatch(this.element, this.kind, int offset, int length,
35 {this.isResolved: true, this.isQualified: false}) {
36 this.range = new SourceRange(offset, length);
37 }
38
39 bool operator ==(SearchMatch match) {
40 return match.element == this.element &&
41 match.kind == this.kind &&
42 match.isResolved == this.isResolved &&
43 match.isQualified == this.isQualified &&
44 match.sourceRange == this.range;
45 }
46
47 @override
48 String toString() {
49 StringBuffer buffer = new StringBuffer();
50 buffer.write("ExpectedMatch(kind=");
51 buffer.write(kind);
52 buffer.write(", element=");
53 buffer.write(element != null ? element.displayName : 'null');
54 buffer.write(", range=");
55 buffer.write(range);
56 buffer.write(", isResolved=");
57 buffer.write(isResolved);
58 buffer.write(", isQualified=");
59 buffer.write(isQualified);
60 buffer.write(")");
61 return buffer.toString();
62 }
63 }
64
65 @reflectiveTest
66 class SearchEngineImpl2Test extends AbstractSingleUnitTest {
67 Index2 index;
68 SearchEngineImpl2 searchEngine;
69
70 void setUp() {
71 super.setUp();
72 index = createMemoryIndex2();
73 searchEngine = new SearchEngineImpl2(context, index);
74 }
75
76 // Future test_searchAllSubtypes() {
77 // _indexTestUnit('''
78 //class T {}
79 //class A extends T {}
80 //class B extends A {}
81 //class C implements B {}
82 //''');
83 // ClassElement element = findElement('T');
84 // ClassElement elementA = findElement('A');
85 // ClassElement elementB = findElement('B');
86 // ClassElement elementC = findElement('C');
87 // var expected = [
88 // _expectId(elementA, MatchKind.DECLARATION, 'A extends T'),
89 // _expectId(elementB, MatchKind.DECLARATION, 'B extends A'),
90 // _expectId(elementC, MatchKind.DECLARATION, 'C implements B')
91 // ];
92 // return searchEngine.searchAllSubtypes(element).then((matches) {
93 // _assertMatches(matches, expected);
94 // });
95 // }
96 //
97 // Future test_searchElementDeclarations() {
98 // _indexTestUnit('''
99 //class A {
100 // test() {}
101 //}
102 //class B {
103 // int test = 1;
104 // main() {
105 // int test = 2;
106 // }
107 //}
108 //''');
109 // ClassElement elementA = findElement('A');
110 // ClassElement elementB = findElement('B');
111 // Element element_test = findElement('test', ElementKind.LOCAL_VARIABLE);
112 // var expected = [
113 // _expectId(elementA.methods[0], MatchKind.DECLARATION, 'test() {}'),
114 // _expectId(elementB.fields[0], MatchKind.DECLARATION, 'test = 1;'),
115 // _expectId(element_test, MatchKind.DECLARATION, 'test = 2;'),
116 // ];
117 // return searchEngine.searchElementDeclarations('test').then((matches) {
118 // _assertMatches(matches, expected);
119 // });
120 // }
121 //
122 // Future test_searchMemberDeclarations() {
123 // _indexTestUnit('''
124 //class A {
125 // test() {}
126 //}
127 //class B {
128 // int test = 1;
129 // main() {
130 // int test = 2;
131 // }
132 //}
133 //''');
134 // ClassElement elementA = findElement('A');
135 // ClassElement elementB = findElement('B');
136 // var expected = [
137 // _expectId(elementA.methods[0], MatchKind.DECLARATION, 'test() {}'),
138 // _expectId(elementB.fields[0], MatchKind.DECLARATION, 'test = 1;')
139 // ];
140 // return searchEngine.searchMemberDeclarations('test').then((matches) {
141 // _assertMatches(matches, expected);
142 // });
143 // }
144 //
145 // Future test_searchMemberReferences() {
146 // _indexTestUnit('''
147 //class A {
148 // var test; // A
149 // mainA() {
150 // test(); // a-inv-r-nq
151 // test = 1; // a-write-r-nq
152 // test += 2; // a-read-write-r-nq
153 // print(test); // a-read-r-nq
154 // }
155 //}
156 //main(A a, p) {
157 // a.test(); // a-inv-r-q
158 // a.test = 1; // a-write-r-q
159 // a.test += 2; // a-read-write-r-q
160 // print(a.test); // a-read-r-q
161 // p.test(); // p-inv-ur-q
162 // p.test = 1; // p-write-ur-q
163 // p.test += 2; // p-read-write-ur-q
164 // print(p.test); // p-read-ur-q
165 //}
166 //''');
167 // Element mainA = findElement('mainA');
168 // Element main = findElement('main');
169 // var expected = [
170 // _expectId(mainA, MatchKind.INVOCATION, 'test(); // a-inv-r-nq'),
171 // _expectId(mainA, MatchKind.WRITE, 'test = 1; // a-write-r-nq'),
172 // _expectId(mainA, MatchKind.READ_WRITE, 'test += 2; // a-read-write-r-nq' ),
173 // _expectId(mainA, MatchKind.READ, 'test); // a-read-r-nq'),
174 // _expectIdQ(main, MatchKind.INVOCATION, 'test(); // a-inv-r-q'),
175 // _expectIdQ(main, MatchKind.WRITE, 'test = 1; // a-write-r-q'),
176 // _expectIdQ(main, MatchKind.READ_WRITE, 'test += 2; // a-read-write-r-q') ,
177 // _expectIdQ(main, MatchKind.READ, 'test); // a-read-r-q'),
178 // _expectIdU(main, MatchKind.INVOCATION, 'test(); // p-inv-ur-q'),
179 // _expectIdU(main, MatchKind.WRITE, 'test = 1; // p-write-ur-q'),
180 // _expectIdU(main, MatchKind.READ_WRITE, 'test += 2; // p-read-write-ur-q' ),
181 // _expectIdU(main, MatchKind.READ, 'test); // p-read-ur-q'),
182 // ];
183 // return searchEngine.searchMemberReferences('test').then((matches) {
184 // _assertMatches(matches, expected);
185 // });
186 // }
187
188 test_searchReferences_ClassElement() async {
189 _indexTestUnit('''
190 class A {}
191 main(A p) {
192 A v;
193 }
194 ''');
195 ClassElement element = findElement('A');
196 Element pElement = findElement('p');
197 Element vElement = findElement('v');
198 var expected = [
199 _expectId(pElement, MatchKind.REFERENCE, 'A p'),
200 _expectId(vElement, MatchKind.REFERENCE, 'A v')
201 ];
202 await _verifyReferences(element, expected);
203 }
204
205 test_searchReferences_CompilationUnitElement() async {
206 addSource(
207 '/my_part.dart',
208 '''
209 part of lib;
210 ''');
211 _indexTestUnit('''
212 library lib;
213 part 'my_part.dart';
214 ''');
215 CompilationUnitElement element = testLibraryElement.parts[0];
216 var expected = [
217 _expectIdQ(testUnitElement, MatchKind.REFERENCE, "'my_part.dart'",
218 length: "'my_part.dart'".length)
219 ];
220 await _verifyReferences(element, expected);
221 }
222
223 test_searchReferences_ConstructorElement() async {
224 _indexTestUnit('''
225 class A {
226 A.named() {}
227 }
228 main() {
229 new A.named();
230 }
231 ''');
232 ConstructorElement element = findElement('named');
233 Element mainElement = findElement('main');
234 var expected = [
235 _expectIdQ(mainElement, MatchKind.REFERENCE, '.named();', length: 6)
236 ];
237 await _verifyReferences(element, expected);
238 }
239
240 test_searchReferences_Element_unknown() async {
241 await _verifyReferences(DynamicElementImpl.instance, []);
242 }
243
244 test_searchReferences_FieldElement() async {
245 _indexTestUnit('''
246 class A {
247 var field;
248 A({this.field});
249 main() {
250 new A(field: 1);
251 // getter
252 print(field); // ref-nq
253 print(this.field); // ref-q
254 field(); // inv-nq
255 this.field(); // inv-q
256 // setter
257 field = 2; // ref-nq;
258 this.field = 3; // ref-q;
259 }
260 }
261 ''');
262 FieldElement element = findElement('field');
263 Element main = findElement('main');
264 Element fieldParameter = findElement('field', ElementKind.PARAMETER);
265 var expected = [
266 _expectIdQ(fieldParameter, MatchKind.REFERENCE, 'field}'),
267 _expectIdQ(main, MatchKind.REFERENCE, 'field: 1'),
268 _expectId(main, MatchKind.READ, 'field); // ref-nq'),
269 _expectIdQ(main, MatchKind.READ, 'field); // ref-q'),
270 _expectId(main, MatchKind.INVOCATION, 'field(); // inv-nq'),
271 _expectIdQ(main, MatchKind.INVOCATION, 'field(); // inv-q'),
272 _expectId(main, MatchKind.WRITE, 'field = 2; // ref-nq'),
273 _expectIdQ(main, MatchKind.WRITE, 'field = 3; // ref-q'),
274 ];
275 await _verifyReferences(element, expected);
276 }
277
278 test_searchReferences_FunctionElement() async {
279 _indexTestUnit('''
280 test() {}
281 main() {
282 test();
283 print(test);
284 }
285 ''');
286 FunctionElement element = findElement('test');
287 Element mainElement = findElement('main');
288 var expected = [
289 _expectId(mainElement, MatchKind.INVOCATION, 'test();'),
290 _expectId(mainElement, MatchKind.REFERENCE, 'test);')
291 ];
292 await _verifyReferences(element, expected);
293 }
294
295 test_searchReferences_FunctionTypeAliasElement() async {
296 _indexTestUnit('''
297 typedef Test();
298 main() {
299 Test a;
300 Test b;
301 }
302 ''');
303 FunctionTypeAliasElement element = findElement('Test');
304 Element aElement = findElement('a');
305 Element bElement = findElement('b');
306 var expected = [
307 _expectId(aElement, MatchKind.REFERENCE, 'Test a;'),
308 _expectId(bElement, MatchKind.REFERENCE, 'Test b;')
309 ];
310 await _verifyReferences(element, expected);
311 }
312
313 // Future test_searchReferences_ImportElement_noPrefix() {
314 // _indexTestUnit('''
315 //import 'dart:math';
316 //main() {
317 // print(E);
318 //}
319 //''');
320 // ImportElement element = testLibraryElement.imports[0];
321 // Element mainElement = findElement('main');
322 // var kind = MatchKind.REFERENCE;
323 // var expected = [_expectId(mainElement, kind, 'E);', length: 0)];
324 // return _verifyReferences(element, expected);
325 // }
326 //
327 // Future test_searchReferences_ImportElement_withPrefix() {
328 // _indexTestUnit('''
329 //import 'dart:math' as math;
330 //main() {
331 // print(math.PI);
332 //}
333 //''');
334 // ImportElement element = testLibraryElement.imports[0];
335 // Element mainElement = findElement('main');
336 // var kind = MatchKind.REFERENCE;
337 // var expected = [
338 // _expectId(mainElement, kind, 'math.PI);', length: 'math.'.length)
339 // ];
340 // return _verifyReferences(element, expected);
341 // }
342 //
343 // Future test_searchReferences_LabelElement() {
344 // _indexTestUnit('''
345 //main() {
346 //label:
347 // while (true) {
348 // if (true) {
349 // break label; // 1
350 // }
351 // break label; // 2
352 // }
353 //}
354 //''');
355 // LabelElement element = findElement('label');
356 // Element mainElement = findElement('main');
357 // var expected = [
358 // _expectId(mainElement, MatchKind.REFERENCE, 'label; // 1'),
359 // _expectId(mainElement, MatchKind.REFERENCE, 'label; // 2')
360 // ];
361 // return _verifyReferences(element, expected);
362 // }
363 //
364 // Future test_searchReferences_LibraryElement() {
365 // var codeA = 'part of lib; // A';
366 // var codeB = 'part of lib; // B';
367 // addSource('/unitA.dart', codeA);
368 // addSource('/unitB.dart', codeB);
369 // _indexTestUnit('''
370 //library lib;
371 //part 'unitA.dart';
372 //part 'unitB.dart';
373 //''');
374 // LibraryElement element = testLibraryElement;
375 // CompilationUnitElement elementA = element.parts[0];
376 // CompilationUnitElement elementB = element.parts[1];
377 // index.index(context, elementA.computeNode());
378 // index.index(context, elementB.computeNode());
379 // var expected = [
380 // new ExpectedMatch(elementA, MatchKind.REFERENCE,
381 // codeA.indexOf('lib; // A'), 'lib'.length),
382 // new ExpectedMatch(elementB, MatchKind.REFERENCE,
383 // codeB.indexOf('lib; // B'), 'lib'.length),
384 // ];
385 // return _verifyReferences(element, expected);
386 // }
387 //
388 // Future test_searchReferences_LocalVariableElement() {
389 // _indexTestUnit('''
390 //main() {
391 // var v;
392 // v = 1;
393 // v += 2;
394 // print(v);
395 // v();
396 //}
397 //''');
398 // LocalVariableElement element = findElement('v');
399 // Element mainElement = findElement('main');
400 // var expected = [
401 // _expectId(mainElement, MatchKind.WRITE, 'v = 1;'),
402 // _expectId(mainElement, MatchKind.READ_WRITE, 'v += 2;'),
403 // _expectId(mainElement, MatchKind.READ, 'v);'),
404 // _expectId(mainElement, MatchKind.INVOCATION, 'v();')
405 // ];
406 // return _verifyReferences(element, expected);
407 // }
408
409 test_searchReferences_MethodElement() async {
410 _indexTestUnit('''
411 class A {
412 m() {}
413 main() {
414 m(); // 1
415 this.m(); // 2
416 print(m); // 3
417 print(this.m); // 4
418 }
419 }
420 ''');
421 MethodElement method = findElement('m');
422 Element mainElement = findElement('main');
423 var expected = [
424 _expectId(mainElement, MatchKind.INVOCATION, 'm(); // 1'),
425 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // 2'),
426 _expectId(mainElement, MatchKind.REFERENCE, 'm); // 3'),
427 _expectIdQ(mainElement, MatchKind.REFERENCE, 'm); // 4')
428 ];
429 await _verifyReferences(method, expected);
430 }
431
432 test_searchReferences_MethodMember() async {
433 _indexTestUnit('''
434 class A<T> {
435 T m() => null;
436 }
437 main(A<int> a) {
438 a.m(); // ref
439 }
440 ''');
441 MethodMember method = findNodeElementAtString('m(); // ref');
442 Element mainElement = findElement('main');
443 var expected = [
444 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // ref')
445 ];
446 await _verifyReferences(method, expected);
447 }
448
449 // Future test_searchReferences_ParameterElement() {
450 // _indexTestUnit('''
451 //foo({p}) {
452 // p = 1;
453 // p += 2;
454 // print(p);
455 // p();
456 //}
457 //main() {
458 // foo(p: 42);
459 //}
460 //''');
461 // ParameterElement element = findElement('p');
462 // Element fooElement = findElement('foo');
463 // Element mainElement = findElement('main');
464 // var expected = [
465 // _expectId(fooElement, MatchKind.WRITE, 'p = 1;'),
466 // _expectId(fooElement, MatchKind.READ_WRITE, 'p += 2;'),
467 // _expectId(fooElement, MatchKind.READ, 'p);'),
468 // _expectId(fooElement, MatchKind.INVOCATION, 'p();'),
469 // _expectId(mainElement, MatchKind.REFERENCE, 'p: 42')
470 // ];
471 // return _verifyReferences(element, expected);
472 // }
473 //
474 // Future test_searchReferences_PrefixElement() {
475 // _indexTestUnit('''
476 //import 'dart:async' as ppp;
477 //main() {
478 // ppp.Future a;
479 // ppp.Stream b;
480 //}
481 //''');
482 // PrefixElement element = findNodeElementAtString('ppp;');
483 // Element elementA = findElement('a');
484 // Element elementB = findElement('b');
485 // var expected = [
486 // _expectId(elementA, MatchKind.REFERENCE, 'ppp.Future'),
487 // _expectId(elementB, MatchKind.REFERENCE, 'ppp.Stream')
488 // ];
489 // return _verifyReferences(element, expected);
490 // }
491
492 test_searchReferences_PropertyAccessorElement_getter() async {
493 _indexTestUnit('''
494 class A {
495 get g => null;
496 main() {
497 g; // 1
498 this.g; // 2
499 }
500 }
501 ''');
502 PropertyAccessorElement element = findElement('g', ElementKind.GETTER);
503 Element mainElement = findElement('main');
504 var expected = [
505 _expectId(mainElement, MatchKind.REFERENCE, 'g; // 1'),
506 _expectIdQ(mainElement, MatchKind.REFERENCE, 'g; // 2')
507 ];
508 await _verifyReferences(element, expected);
509 }
510
511 test_searchReferences_PropertyAccessorElement_setter() async {
512 _indexTestUnit('''
513 class A {
514 set s(x) {}
515 main() {
516 s = 1;
517 this.s = 2;
518 }
519 }
520 ''');
521 PropertyAccessorElement element = findElement('s=');
522 Element mainElement = findElement('main');
523 var expected = [
524 _expectId(mainElement, MatchKind.REFERENCE, 's = 1'),
525 _expectIdQ(mainElement, MatchKind.REFERENCE, 's = 2')
526 ];
527 await _verifyReferences(element, expected);
528 }
529
530 test_searchReferences_TopLevelVariableElement() async {
531 addSource(
532 '/lib.dart',
533 '''
534 library lib;
535 var V;
536 ''');
537 _indexTestUnit('''
538 import 'lib.dart' show V; // imp
539 import 'lib.dart' as pref;
540 main() {
541 pref.V = 1; // q
542 print(pref.V); // q
543 pref.V(); // q
544 V = 1; // nq
545 print(V); // nq
546 V(); // nq
547 }
548 ''');
549 ImportElement importElement = testLibraryElement.imports[0];
550 CompilationUnitElement impUnit =
551 importElement.importedLibrary.definingCompilationUnit;
552 TopLevelVariableElement variable = impUnit.topLevelVariables[0];
553 Element main = findElement('main');
554 var expected = [
555 _expectIdQ(testUnitElement, MatchKind.REFERENCE, 'V; // imp'),
556 _expectIdQ(main, MatchKind.WRITE, 'V = 1; // q'),
557 _expectIdQ(main, MatchKind.READ, 'V); // q'),
558 _expectIdQ(main, MatchKind.INVOCATION, 'V(); // q'),
559 _expectId(main, MatchKind.WRITE, 'V = 1; // nq'),
560 _expectId(main, MatchKind.READ, 'V); // nq'),
561 _expectId(main, MatchKind.INVOCATION, 'V(); // nq'),
562 ];
563 await _verifyReferences(variable, expected);
564 }
565
566 // Future test_searchReferences_TypeParameterElement() {
567 // _indexTestUnit('''
568 //class A<T> {
569 // main(T a, T b) {}
570 //}
571 //''');
572 // TypeParameterElement element = findElement('T');
573 // Element aElement = findElement('a');
574 // Element bElement = findElement('b');
575 // var expected = [
576 // _expectId(aElement, MatchKind.REFERENCE, 'T a'),
577 // _expectId(bElement, MatchKind.REFERENCE, 'T b')
578 // ];
579 // return _verifyReferences(element, expected);
580 // }
581
582 // Future test_searchSubtypes() {
583 // _indexTestUnit('''
584 //class T {}
585 //class A extends T {} // A
586 //class B = Object with T; // B
587 //class C implements T {} // C
588 //''');
589 // ClassElement element = findElement('T');
590 // ClassElement elementA = findElement('A');
591 // ClassElement elementB = findElement('B');
592 // ClassElement elementC = findElement('C');
593 // var expected = [
594 // _expectId(elementA, MatchKind.REFERENCE, 'T {} // A'),
595 // _expectId(elementB, MatchKind.REFERENCE, 'T; // B'),
596 // _expectId(elementC, MatchKind.REFERENCE, 'T {} // C')
597 // ];
598 // return searchEngine.searchSubtypes(element).then((matches) {
599 // _assertMatches(matches, expected);
600 // });
601 // }
602 //
603 // Future test_searchTopLevelDeclarations() {
604 // _indexTestUnit('''
605 //class A {} // A
606 //class B = Object with A;
607 //typedef C();
608 //D() {}
609 //var E = null;
610 //class NoMatchABCDE {}
611 //''');
612 // Element topA = findElement('A');
613 // Element topB = findElement('B');
614 // Element topC = findElement('C');
615 // Element topD = findElement('D');
616 // Element topE = findElement('E');
617 // var expected = [
618 // _expectId(topA, MatchKind.DECLARATION, 'A {} // A'),
619 // _expectId(topB, MatchKind.DECLARATION, 'B ='),
620 // _expectId(topC, MatchKind.DECLARATION, 'C()'),
621 // _expectId(topD, MatchKind.DECLARATION, 'D() {}'),
622 // _expectId(topE, MatchKind.DECLARATION, 'E = null')
623 // ];
624 // return _verifyTopLevelDeclarations('^[A-E]\$', expected);
625 // }
626
627 ExpectedMatch _expectId(Element element, MatchKind kind, String search,
628 {int length, bool isResolved: true, bool isQualified: false}) {
629 int offset = findOffset(search);
630 if (length == null) {
631 length = getLeadingIdentifierLength(search);
632 }
633 return new ExpectedMatch(element, kind, offset, length,
634 isResolved: isResolved, isQualified: isQualified);
635 }
636
637 ExpectedMatch _expectIdQ(Element element, MatchKind kind, String search,
638 {int length}) {
639 return _expectId(element, kind, search, isQualified: true, length: length);
640 }
641
642 // ExpectedMatch _expectIdU(Element element, MatchKind kind, String search) {
643 // return _expectId(element, kind, search,
644 // isQualified: true, isResolved: false);
645 // }
646
647 void _indexTestUnit(String code) {
648 resolveTestUnit(code);
649 index.indexUnit(testUnit);
650 }
651
652 Future _verifyReferences(
653 Element element, List<ExpectedMatch> expectedMatches) async {
654 List<SearchMatch> matches = await searchEngine.searchReferences(element);
655 _assertMatches(matches, expectedMatches);
656 }
657
658 // Future _verifyTopLevelDeclarations(
659 // String pattern, List<ExpectedMatch> expectedMatches) {
660 // return searchEngine
661 // .searchTopLevelDeclarations(pattern)
662 // .then((List<SearchMatch> matches) {
663 // _assertMatches(matches, expectedMatches);
664 // });
665 // }
666
667 static void _assertMatches(
668 List<SearchMatch> matches, List<ExpectedMatch> expectedMatches) {
669 expect(matches, unorderedEquals(expectedMatches));
670 }
671 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698