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

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

Issue 1778363003: Improve search for constructors and import elements. (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 23 matching lines...) Expand all
34 ' isQualified=$isQualified)'; 34 ' isQualified=$isQualified)';
35 } 35 }
36 } 36 }
37 37
38 @reflectiveTest 38 @reflectiveTest
39 class PackageIndexAssemblerTest extends AbstractSingleUnitTest { 39 class PackageIndexAssemblerTest extends AbstractSingleUnitTest {
40 PackageIndex packageIndex; 40 PackageIndex packageIndex;
41 UnitIndex unitIndex; 41 UnitIndex unitIndex;
42 42
43 _ElementIndexAssert assertThat(Element element) { 43 _ElementIndexAssert assertThat(Element element) {
44 return new _ElementIndexAssert(this, element); 44 List<_Relation> relations = _getElementRelations(element);
45 return new _ElementIndexAssert(this, element, relations);
45 } 46 }
46 47
47 _NameIndexAssert assertThatName(String name) { 48 _NameIndexAssert assertThatName(String name) {
48 return new _NameIndexAssert(this, name); 49 return new _NameIndexAssert(this, name);
49 } 50 }
50 51
51 CompilationUnitElement importedUnit({int index: 0}) { 52 CompilationUnitElement importedUnit({int index: 0}) {
52 List<ImportElement> imports = testLibraryElement.imports; 53 List<ImportElement> imports = testLibraryElement.imports;
53 return imports[index].importedLibrary.definingCompilationUnit; 54 return imports[index].importedLibrary.definingCompilationUnit;
54 } 55 }
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 main() { 554 main() {
554 new A(); // 4 555 new A(); // 4
555 new A.foo(); // 5 556 new A.foo(); // 5
556 } 557 }
557 '''); 558 ''');
558 ClassElement classA = findElement('A'); 559 ClassElement classA = findElement('A');
559 ConstructorElement constA = classA.constructors[0]; 560 ConstructorElement constA = classA.constructors[0];
560 ConstructorElement constA_foo = classA.constructors[1]; 561 ConstructorElement constA_foo = classA.constructors[1];
561 // A() 562 // A()
562 assertThat(constA) 563 assertThat(constA)
564 ..hasRelationCount(2)
563 ..isReferencedAt('(); // 1', true, length: 0) 565 ..isReferencedAt('(); // 1', true, length: 0)
564 ..isReferencedAt('(); // 4', true, length: 0); 566 ..isReferencedAt('(); // 4', true, length: 0);
565 // A.foo() 567 // A.foo()
566 assertThat(constA_foo) 568 assertThat(constA_foo)
569 ..hasRelationCount(3)
567 ..isReferencedAt('.foo(); // 2', true, length: 4) 570 ..isReferencedAt('.foo(); // 2', true, length: 4)
568 ..isReferencedAt('.foo; // 3', true, length: 4) 571 ..isReferencedAt('.foo; // 3', true, length: 4)
569 ..isReferencedAt('.foo(); // 5', true, length: 4); 572 ..isReferencedAt('.foo(); // 5', true, length: 4);
570 } 573 }
571 574
572 void test_isReferencedBy_ConstructorElement_classTypeAlias() { 575 void test_isReferencedBy_ConstructorElement_classTypeAlias() {
573 _indexTestUnit(''' 576 _indexTestUnit('''
574 class M {} 577 class M {}
575 class A implements B { 578 class A implements B {
576 A() {} 579 A() {}
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 } 852 }
850 } 853 }
851 _failWithIndexDump('Not found $name $kind at $offset'); 854 _failWithIndexDump('Not found $name $kind at $offset');
852 } 855 }
853 856
854 /** 857 /**
855 * Asserts that [unitIndex] has an item with the expected properties. 858 * Asserts that [unitIndex] has an item with the expected properties.
856 */ 859 */
857 void _assertHasRelation( 860 void _assertHasRelation(
858 Element element, 861 Element element,
862 List<_Relation> relations,
859 IndexRelationKind expectedRelationKind, 863 IndexRelationKind expectedRelationKind,
860 ExpectedLocation expectedLocation) { 864 ExpectedLocation expectedLocation) {
861 int elementId = _findElementId(element); 865 for (var relation in relations) {
862 for (int i = 0; i < unitIndex.usedElementOffsets.length; i++) { 866 if (relation.kind == expectedRelationKind &&
863 if (unitIndex.usedElements[i] == elementId && 867 relation.offset == expectedLocation.offset &&
864 unitIndex.usedElementKinds[i] == expectedRelationKind && 868 relation.length == expectedLocation.length &&
865 unitIndex.usedElementOffsets[i] == expectedLocation.offset && 869 relation.isQualified == expectedLocation.isQualified) {
866 unitIndex.usedElementLengths[i] == expectedLocation.length &&
867 unitIndex.usedElementIsQualifiedFlags[i] ==
868 expectedLocation.isQualified) {
869 return; 870 return;
870 } 871 }
871 } 872 }
872 _failWithIndexDump( 873 _failWithIndexDump(
873 'not found\n$element $expectedRelationKind at $expectedLocation'); 874 'not found\n$element $expectedRelationKind at $expectedLocation');
874 } 875 }
875 876
876 void _assertUsedName(String name, IndexRelationKind kind, 877 void _assertUsedName(String name, IndexRelationKind kind,
877 ExpectedLocation expectedLocation, bool isNot) { 878 ExpectedLocation expectedLocation, bool isNot) {
878 int nameId = _getStringId(name); 879 int nameId = _getStringId(name);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 if (packageIndex.elementUnits[elementId] == unitId && 920 if (packageIndex.elementUnits[elementId] == unitId &&
920 packageIndex.elementOffsets[elementId] == info.offset && 921 packageIndex.elementOffsets[elementId] == info.offset &&
921 packageIndex.elementKinds[elementId] == info.kind) { 922 packageIndex.elementKinds[elementId] == info.kind) {
922 return elementId; 923 return elementId;
923 } 924 }
924 } 925 }
925 _failWithIndexDump('Element $element is not referenced'); 926 _failWithIndexDump('Element $element is not referenced');
926 return 0; 927 return 0;
927 } 928 }
928 929
930 /**
931 * Return all relations with [element] in [unitIndex].
932 */
933 List<_Relation> _getElementRelations(Element element) {
934 int elementId = _findElementId(element);
935 List<_Relation> relations = <_Relation>[];
936 for (int i = 0; i < unitIndex.usedElementOffsets.length; i++) {
937 if (unitIndex.usedElements[i] == elementId) {
938 relations.add(new _Relation(
939 unitIndex.usedElementKinds[i],
940 unitIndex.usedElementOffsets[i],
941 unitIndex.usedElementLengths[i],
942 unitIndex.usedElementIsQualifiedFlags[i]));
943 }
944 }
945 return relations;
946 }
947
929 int _getStringId(String str) { 948 int _getStringId(String str) {
930 int id = packageIndex.strings.indexOf(str); 949 int id = packageIndex.strings.indexOf(str);
931 expect(id, isNonNegative); 950 expect(id, isNonNegative);
932 return id; 951 return id;
933 } 952 }
934 953
935 int _getUnitId(Element element) { 954 int _getUnitId(Element element) {
936 CompilationUnitElement unitElement = 955 CompilationUnitElement unitElement =
937 PackageIndexAssembler.getUnitElement(element); 956 PackageIndexAssembler.getUnitElement(element);
938 int libraryUriId = _getUriId(unitElement.library.source.uri); 957 int libraryUriId = _getUriId(unitElement.library.source.uri);
(...skipping 26 matching lines...) Expand all
965 // prepare the only unit index 984 // prepare the only unit index
966 expect(packageIndex.units, hasLength(1)); 985 expect(packageIndex.units, hasLength(1));
967 unitIndex = packageIndex.units[0]; 986 unitIndex = packageIndex.units[0];
968 expect(unitIndex.unit, _getUnitId(testUnitElement)); 987 expect(unitIndex.unit, _getUnitId(testUnitElement));
969 } 988 }
970 } 989 }
971 990
972 class _ElementIndexAssert { 991 class _ElementIndexAssert {
973 final PackageIndexAssemblerTest test; 992 final PackageIndexAssemblerTest test;
974 final Element element; 993 final Element element;
994 final List<_Relation> relations;
975 995
976 _ElementIndexAssert(this.test, this.element); 996 _ElementIndexAssert(this.test, this.element, this.relations);
997
998 void hasRelationCount(int expectedCount) {
999 expect(relations, hasLength(expectedCount));
1000 }
977 1001
978 void isAncestorOf(String search, {int length}) { 1002 void isAncestorOf(String search, {int length}) {
979 test._assertHasRelation(element, IndexRelationKind.IS_ANCESTOR_OF, 1003 test._assertHasRelation(
1004 element,
1005 relations,
1006 IndexRelationKind.IS_ANCESTOR_OF,
980 test._expectedLocation(search, false, length: length)); 1007 test._expectedLocation(search, false, length: length));
981 } 1008 }
982 1009
983 void isExtendedAt(String search, bool isQualified, {int length}) { 1010 void isExtendedAt(String search, bool isQualified, {int length}) {
984 test._assertHasRelation(element, IndexRelationKind.IS_EXTENDED_BY, 1011 test._assertHasRelation(
1012 element,
1013 relations,
1014 IndexRelationKind.IS_EXTENDED_BY,
985 test._expectedLocation(search, isQualified, length: length)); 1015 test._expectedLocation(search, isQualified, length: length));
986 } 1016 }
987 1017
988 void isImplementedAt(String search, bool isQualified, {int length}) { 1018 void isImplementedAt(String search, bool isQualified, {int length}) {
989 test._assertHasRelation(element, IndexRelationKind.IS_IMPLEMENTED_BY, 1019 test._assertHasRelation(
1020 element,
1021 relations,
1022 IndexRelationKind.IS_IMPLEMENTED_BY,
990 test._expectedLocation(search, isQualified, length: length)); 1023 test._expectedLocation(search, isQualified, length: length));
991 } 1024 }
992 1025
993 void isInvokedAt(String search, bool isQualified, {int length}) { 1026 void isInvokedAt(String search, bool isQualified, {int length}) {
994 test._assertHasRelation(element, IndexRelationKind.IS_INVOKED_BY, 1027 test._assertHasRelation(element, relations, IndexRelationKind.IS_INVOKED_BY,
995 test._expectedLocation(search, isQualified, length: length)); 1028 test._expectedLocation(search, isQualified, length: length));
996 } 1029 }
997 1030
998 void isMixedInAt(String search, bool isQualified, {int length}) { 1031 void isMixedInAt(String search, bool isQualified, {int length}) {
999 test._assertHasRelation(element, IndexRelationKind.IS_MIXED_IN_BY, 1032 test._assertHasRelation(
1033 element,
1034 relations,
1035 IndexRelationKind.IS_MIXED_IN_BY,
1000 test._expectedLocation(search, isQualified, length: length)); 1036 test._expectedLocation(search, isQualified, length: length));
1001 } 1037 }
1002 1038
1003 void isReferencedAt(String search, bool isQualified, {int length}) { 1039 void isReferencedAt(String search, bool isQualified, {int length}) {
1004 test._assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, 1040 test._assertHasRelation(
1041 element,
1042 relations,
1043 IndexRelationKind.IS_REFERENCED_BY,
1005 test._expectedLocation(search, isQualified, length: length)); 1044 test._expectedLocation(search, isQualified, length: length));
1006 } 1045 }
1007 } 1046 }
1008 1047
1009 class _NameIndexAssert { 1048 class _NameIndexAssert {
1010 final PackageIndexAssemblerTest test; 1049 final PackageIndexAssemblerTest test;
1011 final String name; 1050 final String name;
1012 1051
1013 _NameIndexAssert(this.test, this.name); 1052 _NameIndexAssert(this.test, this.name);
1014 1053
(...skipping 10 matching lines...) Expand all
1025 void isNotReferencedAt(String search, {int length}) { 1064 void isNotReferencedAt(String search, {int length}) {
1026 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY, 1065 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY,
1027 test._expectedLocation(search, true, length: length), true); 1066 test._expectedLocation(search, true, length: length), true);
1028 } 1067 }
1029 1068
1030 void isReferencedAt(String search, {int length}) { 1069 void isReferencedAt(String search, {int length}) {
1031 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY, 1070 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY,
1032 test._expectedLocation(search, true, length: length), false); 1071 test._expectedLocation(search, true, length: length), false);
1033 } 1072 }
1034 } 1073 }
1074
1075 class _Relation {
1076 final IndexRelationKind kind;
1077 final int offset;
1078 final int length;
1079 final bool isQualified;
1080
1081 _Relation(this.kind, this.offset, this.length, this.isQualified);
1082
1083 @override
1084 String toString() {
1085 return '_Relation{kind: $kind, offset: $offset, length: $length, '
1086 'isQualified: $isQualified}';
1087 }
1088 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698