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

Side by Side Diff: pkg/analyzer/test/src/summary/index_unit_test.dart

Issue 1743963002: Index defined top-level and class member names. (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
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:convert'; 5 import 'dart:convert';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
9 import 'package:analyzer/src/summary/format.dart'; 9 import 'package:analyzer/src/summary/format.dart';
10 import 'package:analyzer/src/summary/idl.dart'; 10 import 'package:analyzer/src/summary/idl.dart';
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 _ElementIndexAssert assertThat(Element element) { 44 _ElementIndexAssert assertThat(Element element) {
45 return new _ElementIndexAssert(this, element); 45 return new _ElementIndexAssert(this, element);
46 } 46 }
47 47
48 CompilationUnitElement importedUnit({int index: 0}) { 48 CompilationUnitElement importedUnit({int index: 0}) {
49 List<ImportElement> imports = testLibraryElement.imports; 49 List<ImportElement> imports = testLibraryElement.imports;
50 return imports[index].importedLibrary.definingCompilationUnit; 50 return imports[index].importedLibrary.definingCompilationUnit;
51 } 51 }
52 52
53 void test_definedName_classMember_field() {
54 _indexTestUnit('''
55 class A {
56 int f;
57 }''');
58 _assertDefinedName('f', IndexNameKind.classMember, 'f;');
59 }
60
61 void test_definedName_classMember_getter() {
62 _indexTestUnit('''
63 class A {
64 int get g => 0;
65 }''');
66 _assertDefinedName('g', IndexNameKind.classMember, 'g => 0;');
67 }
68
69 void test_definedName_classMember_method() {
70 _indexTestUnit('''
71 class A {
72 m() {}
73 }''');
74 _assertDefinedName('m', IndexNameKind.classMember, 'm() {}');
75 }
76
77 void test_definedName_classMember_operator() {
78 _indexTestUnit('''
79 class A {
80 operator +(o) {}
81 }''');
82 _assertDefinedName('+', IndexNameKind.classMember, '+(o) {}');
83 }
84
85 void test_definedName_classMember_setter() {
86 _indexTestUnit('''
87 class A {
88 int set s (_) {}
89 }''');
90 _assertDefinedName('s', IndexNameKind.classMember, 's (_) {}');
91 }
92
93 void test_definedName_topLevel_class() {
94 _indexTestUnit('class A {}');
95 _assertDefinedName('A', IndexNameKind.topLevel, 'A {}');
96 }
97
98 void test_definedName_topLevel_classAlias() {
99 _indexTestUnit('''
100 class M {}
101 class C = Object with M;''');
102 _assertDefinedName('C', IndexNameKind.topLevel, 'C =');
103 }
104
105 void test_definedName_topLevel_enum() {
106 _indexTestUnit('enum E {a, b, c}');
107 _assertDefinedName('E', IndexNameKind.topLevel, 'E {');
108 }
109
110 void test_definedName_topLevel_function() {
111 _indexTestUnit('foo() {}');
112 _assertDefinedName('foo', IndexNameKind.topLevel, 'foo() {}');
113 }
114
115 void test_definedName_topLevel_functionTypeAlias() {
116 _indexTestUnit('typedef F(int p);');
117 _assertDefinedName('F', IndexNameKind.topLevel, 'F(int p);');
118 }
119
120 void test_definedName_topLevel_getter() {
121 _indexTestUnit('''
122 int get g => 0;
123 ''');
124 _assertDefinedName('g', IndexNameKind.topLevel, 'g => 0;');
125 }
126
127 void test_definedName_topLevel_setter() {
128 _indexTestUnit('''
129 int set s (_) {}
130 ''');
131 _assertDefinedName('s', IndexNameKind.topLevel, 's (_) {}');
132 }
133
134 void test_definedName_topLevel_topLevelVariable() {
135 _indexTestUnit('var V = 42;');
136 _assertDefinedName('V', IndexNameKind.topLevel, 'V = 42;');
137 }
138
53 void test_isExtendedBy_ClassDeclaration() { 139 void test_isExtendedBy_ClassDeclaration() {
54 _indexTestUnit(''' 140 _indexTestUnit('''
55 class A {} // 1 141 class A {} // 1
56 class B extends A {} // 2 142 class B extends A {} // 2
57 '''); 143 ''');
58 ClassElement elementA = findElement('A'); 144 ClassElement elementA = findElement('A');
59 assertThat(elementA).isExtendedAt('A {} // 2'); 145 assertThat(elementA).isExtendedAt('A {} // 2');
60 } 146 }
61 147
62 void test_isExtendedBy_ClassDeclaration_Object() { 148 void test_isExtendedBy_ClassDeclaration_Object() {
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 596
511 void test_isReferencedBy_typeInVariableList() { 597 void test_isReferencedBy_typeInVariableList() {
512 _indexTestUnit(''' 598 _indexTestUnit('''
513 class A {} 599 class A {}
514 A myVariable = null; 600 A myVariable = null;
515 '''); 601 ''');
516 Element element = findElement('A'); 602 Element element = findElement('A');
517 assertThat(element).isReferencedAt('A myVariable'); 603 assertThat(element).isReferencedAt('A myVariable');
518 } 604 }
519 605
606 void _assertDefinedName(String name, IndexNameKind kind, String search) {
607 int offset = findOffset(search);
608 int nameId = _getStringId(name);
609 for (int i = 0; i < unitIndex.definedNames.length; i++) {
610 if (unitIndex.definedNames[i] == nameId &&
611 unitIndex.definedNameKinds[i] == kind &&
612 unitIndex.definedNameOffsets[i] == offset) {
613 return;
614 }
615 }
616 _failWithIndexDump('Not found $name $kind at $offset');
617 }
618
520 /** 619 /**
521 * Asserts that [unitIndex] has an item with the expected properties. 620 * Asserts that [unitIndex] has an item with the expected properties.
522 */ 621 */
523 void _assertHasRelation( 622 void _assertHasRelation(
524 Element element, 623 Element element,
525 IndexRelationKind expectedRelationKind, 624 IndexRelationKind expectedRelationKind,
526 ExpectedLocation expectedLocation) { 625 ExpectedLocation expectedLocation) {
527 int elementId = _findElementId(element); 626 int elementId = _findElementId(element);
528 int matchIndex; 627 int matchIndex;
529 for (int i = 0; i < unitIndex.locationOffsets.length; i++) { 628 for (int i = 0; i < unitIndex.locationOffsets.length; i++) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) { 693 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) {
595 if (packageIndex.elementLibraryUris[i] == libraryUriId && 694 if (packageIndex.elementLibraryUris[i] == libraryUriId &&
596 packageIndex.elementUnitUris[i] == unitUriId) { 695 packageIndex.elementUnitUris[i] == unitUriId) {
597 return i; 696 return i;
598 } 697 }
599 } 698 }
600 _failWithIndexDump('Unit $unitElement of $element is not referenced'); 699 _failWithIndexDump('Unit $unitElement of $element is not referenced');
601 return -1; 700 return -1;
602 } 701 }
603 702
703 int _getStringId(String str) {
704 int id = packageIndex.strings.indexOf(str);
705 expect(id, isNonNegative);
706 return id;
707 }
708
604 int _getUriId(Uri uri) { 709 int _getUriId(Uri uri) {
605 String str = uri.toString(); 710 String str = uri.toString();
606 int id = packageIndex.uris.indexOf(str); 711 return _getStringId(str);
607 expect(id, isNonNegative);
608 return id;
609 } 712 }
610 713
611 void _indexTestUnit(String code) { 714 void _indexTestUnit(String code) {
612 resolveTestUnit(code); 715 resolveTestUnit(code);
613 PackageIndexAssembler assembler = new PackageIndexAssembler(); 716 PackageIndexAssembler assembler = new PackageIndexAssembler();
614 assembler.index(testUnit); 717 assembler.index(testUnit);
615 // assemble, write and read 718 // assemble, write and read
616 PackageIndexBuilder indexBuilder = assembler.assemble(); 719 PackageIndexBuilder indexBuilder = assembler.assemble();
617 List<int> indexBytes = indexBuilder.toBuffer(); 720 List<int> indexBytes = indexBuilder.toBuffer();
618 packageIndex = new PackageIndex.fromBuffer(indexBytes); 721 packageIndex = new PackageIndex.fromBuffer(indexBytes);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 test._expectedLocation(search, length: length)); 761 test._expectedLocation(search, length: length));
659 } 762 }
660 763
661 void isReferencedQualifiedAt(String search, {int length}) { 764 void isReferencedQualifiedAt(String search, {int length}) {
662 test._assertHasRelation( 765 test._assertHasRelation(
663 element, 766 element,
664 IndexRelationKind.IS_REFERENCED_QUALIFIED_BY, 767 IndexRelationKind.IS_REFERENCED_QUALIFIED_BY,
665 test._expectedLocation(search, length: length)); 768 test._expectedLocation(search, length: length));
666 } 769 }
667 } 770 }
OLDNEW
« pkg/analyzer/lib/src/summary/index_unit.dart ('K') | « pkg/analyzer/lib/src/summary/index_unit.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698