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

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

Issue 1787803003: Improve used name relations and fields indexing. (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 638 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 main() { 649 main() {
650 new A(); // 1 650 new A(); // 1
651 } 651 }
652 '''); 652 ''');
653 ClassElement classA = findElement('A'); 653 ClassElement classA = findElement('A');
654 ConstructorElement constA = classA.constructors[0]; 654 ConstructorElement constA = classA.constructors[0];
655 // A() 655 // A()
656 assertThat(constA)..isReferencedAt('(); // 1', true, length: 0); 656 assertThat(constA)..isReferencedAt('(); // 1', true, length: 0);
657 } 657 }
658 658
659 void test_isReferencedBy_ConstructorFieldInitializer() {
660 _indexTestUnit('''
661 class A {
662 int field;
663 A() : field = 5;
664 }
665 ''');
666 FieldElement element = findElement('field');
667 assertThat(element).isReferencedAt('field = 5', true);
668 }
669
670 void test_isReferencedBy_FieldElement() { 659 void test_isReferencedBy_FieldElement() {
671 _indexTestUnit(''' 660 _indexTestUnit('''
672 class A { 661 class A {
673 var field; 662 var field;
674 A({this.field}); 663 A({this.field});
675 m() { 664 m() {
676 field = 1; // nq 665 field = 2; // nq
677 print(field); // nq 666 print(field); // nq
678 } 667 }
679 } 668 }
680 main(A a) { 669 main(A a) {
681 a.field = 2; // q 670 a.field = 3; // q
682 print(a.field); // q 671 print(a.field); // q
683 new A(field: 3); 672 new A(field: 4);
684 } 673 }
685 '''); 674 ''');
686 FieldElement field = findElement('field'); 675 FieldElement field = findElement('field');
687 PropertyAccessorElement getter = field.getter; 676 PropertyAccessorElement getter = field.getter;
688 PropertyAccessorElement setter = field.setter; 677 PropertyAccessorElement setter = field.setter;
689 // A() 678 // A()
690 assertThat(field)..isReferencedAt('field});', true); 679 assertThat(field)..isWrittenAt('field});', true);
691 // m() 680 // m()
692 assertThat(setter)..isReferencedAt('field = 1; // nq', false); 681 assertThat(setter)..isReferencedAt('field = 2; // nq', false);
693 assertThat(getter)..isReferencedAt('field); // nq', false); 682 assertThat(getter)..isReferencedAt('field); // nq', false);
694 // main() 683 // main()
695 assertThat(setter)..isReferencedAt('field = 2; // q', true); 684 assertThat(setter)..isReferencedAt('field = 3; // q', true);
696 assertThat(getter)..isReferencedAt('field); // q', true); 685 assertThat(getter)..isReferencedAt('field); // q', true);
697 assertThat(field)..isReferencedAt('field: 3', true); 686 assertThat(field)..isReferencedAt('field: 4', true);
698 } 687 }
699 688
700 void test_isReferencedBy_FunctionElement() { 689 void test_isReferencedBy_FunctionElement() {
701 _indexTestUnit(''' 690 _indexTestUnit('''
702 foo() {} 691 foo() {}
703 main() { 692 main() {
704 print(foo); 693 print(foo);
705 print(foo()); 694 print(foo());
706 } 695 }
707 '''); 696 ''');
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 781
793 void test_isReferencedBy_typeInVariableList() { 782 void test_isReferencedBy_typeInVariableList() {
794 _indexTestUnit(''' 783 _indexTestUnit('''
795 class A {} 784 class A {}
796 A myVariable = null; 785 A myVariable = null;
797 '''); 786 ''');
798 Element element = findElement('A'); 787 Element element = findElement('A');
799 assertThat(element).isReferencedAt('A myVariable', false); 788 assertThat(element).isReferencedAt('A myVariable', false);
800 } 789 }
801 790
802 void test_usedName_isInvokedBy() { 791 void test_isWrittenBy_FieldElement() {
792 _indexTestUnit('''
793 class A {
794 int field;
795 A.foo({this.field});
796 A.bar() : field = 5;
797 }
798 ''');
799 FieldElement element = findElement('field');
800 assertThat(element)
801 ..isWrittenAt('field})', true)
802 ..isWrittenAt('field = 5', true);
803 }
804
805 void test_usedName_qualified_resolved() {
803 verifyNoTestUnitErrors = false; 806 verifyNoTestUnitErrors = false;
804 _indexTestUnit(''' 807 _indexTestUnit('''
805 class C { 808 class C {
806 x() {} 809 var x;
807 } 810 }
808 main(C c) { 811 main(C c) {
809 x(); // nq 812 c.x;
810 c.x(); // q 813 c.x = 1;
811 y(); // nq 814 c.x += 2;
812 c.y(); // q 815 c.x();
813 } 816 }
814 '''); 817 ''');
815 assertThatName('x') 818 assertThatName('x')
816 ..isNotInvokedAt('x(); // nq') 819 ..isNotUsed('x;', IndexRelationKind.IS_READ_BY)
817 ..isNotInvokedAt('x(); // q'); 820 ..isNotUsed('x = 1;', IndexRelationKind.IS_WRITTEN_BY)
818 assertThatName('y') 821 ..isNotUsed('x += 2;', IndexRelationKind.IS_READ_WRITTEN_BY)
819 ..isNotInvokedAt('y(); // nq') 822 ..isNotUsed('x();', IndexRelationKind.IS_INVOKED_BY);
820 ..isInvokedAt('y(); // q');
821 } 823 }
822 824
823 void test_usedName_isReferencedBy() { 825 void test_usedName_qualified_unresolved() {
826 verifyNoTestUnitErrors = false;
827 _indexTestUnit('''
828 main(p) {
829 p.x;
830 p.x = 1;
831 p.x += 2;
832 p.x();
833 }
834 ''');
835 assertThatName('x')
836 ..isUsed('x;', IndexRelationKind.IS_READ_BY)
837 ..isUsed('x = 1;', IndexRelationKind.IS_WRITTEN_BY)
838 ..isUsed('x += 2;', IndexRelationKind.IS_READ_WRITTEN_BY)
839 ..isUsed('x();', IndexRelationKind.IS_INVOKED_BY);
840 }
841
842 void test_usedName_unqualified() {
824 verifyNoTestUnitErrors = false; 843 verifyNoTestUnitErrors = false;
825 _indexTestUnit(''' 844 _indexTestUnit('''
826 class C { 845 class C {
827 int x; 846 var x;
828 } 847 }
829 main(C c) { 848 main() {
830 x; // nq 849 x;
831 c.x; // q 850 x = 1;
832 y; // nq 851 x += 2;
833 c.y; // q 852 x();
834 } 853 }
835 '''); 854 ''');
836 assertThatName('x') 855 assertThatName('x')
837 ..isNotReferencedAt('x; // nq') 856 ..isNotUsed('x;', IndexRelationKind.IS_READ_BY)
838 ..isNotReferencedAt('x; // q'); 857 ..isNotUsed('x = 1;', IndexRelationKind.IS_WRITTEN_BY)
839 assertThatName('y') 858 ..isNotUsed('x += 2;', IndexRelationKind.IS_READ_WRITTEN_BY)
840 ..isNotReferencedAt('y; // nq') 859 ..isNotUsed('x();', IndexRelationKind.IS_INVOKED_BY);
841 ..isReferencedAt('y; // q');
842 } 860 }
843 861
844 void _assertDefinedName(String name, IndexNameKind kind, String search) { 862 void _assertDefinedName(String name, IndexNameKind kind, String search) {
845 int offset = findOffset(search); 863 int offset = findOffset(search);
846 int nameId = _getStringId(name); 864 int nameId = _getStringId(name);
847 for (int i = 0; i < unitIndex.definedNames.length; i++) { 865 for (int i = 0; i < unitIndex.definedNames.length; i++) {
848 if (unitIndex.definedNames[i] == nameId && 866 if (unitIndex.definedNames[i] == nameId &&
849 unitIndex.definedNameKinds[i] == kind && 867 unitIndex.definedNameKinds[i] == kind &&
850 unitIndex.definedNameOffsets[i] == offset) { 868 unitIndex.definedNameOffsets[i] == offset) {
851 return; 869 return;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1036 test._expectedLocation(search, isQualified, length: length)); 1054 test._expectedLocation(search, isQualified, length: length));
1037 } 1055 }
1038 1056
1039 void isReferencedAt(String search, bool isQualified, {int length}) { 1057 void isReferencedAt(String search, bool isQualified, {int length}) {
1040 test._assertHasRelation( 1058 test._assertHasRelation(
1041 element, 1059 element,
1042 relations, 1060 relations,
1043 IndexRelationKind.IS_REFERENCED_BY, 1061 IndexRelationKind.IS_REFERENCED_BY,
1044 test._expectedLocation(search, isQualified, length: length)); 1062 test._expectedLocation(search, isQualified, length: length));
1045 } 1063 }
1064
1065 void isWrittenAt(String search, bool isQualified, {int length}) {
1066 test._assertHasRelation(element, relations, IndexRelationKind.IS_WRITTEN_BY,
1067 test._expectedLocation(search, isQualified, length: length));
1068 }
1046 } 1069 }
1047 1070
1048 class _NameIndexAssert { 1071 class _NameIndexAssert {
1049 final PackageIndexAssemblerTest test; 1072 final PackageIndexAssemblerTest test;
1050 final String name; 1073 final String name;
1051 1074
1052 _NameIndexAssert(this.test, this.name); 1075 _NameIndexAssert(this.test, this.name);
1053 1076
1054 void isInvokedAt(String search, {int length}) { 1077 void isNotUsed(String search, IndexRelationKind kind) {
1055 test._assertUsedName(name, IndexRelationKind.IS_INVOKED_BY, 1078 test._assertUsedName(
1056 test._expectedLocation(search, true, length: length), false); 1079 name, kind, test._expectedLocation(search, true), true);
1057 } 1080 }
1058 1081
1059 void isNotInvokedAt(String search, {int length}) { 1082 void isUsed(String search, IndexRelationKind kind) {
1060 test._assertUsedName(name, IndexRelationKind.IS_INVOKED_BY, 1083 test._assertUsedName(
1061 test._expectedLocation(search, true, length: length), true); 1084 name, kind, test._expectedLocation(search, true), false);
1062 }
1063
1064 void isNotReferencedAt(String search, {int length}) {
1065 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY,
1066 test._expectedLocation(search, true, length: length), true);
1067 }
1068
1069 void isReferencedAt(String search, {int length}) {
1070 test._assertUsedName(name, IndexRelationKind.IS_REFERENCED_BY,
1071 test._expectedLocation(search, true, length: length), false);
1072 } 1085 }
1073 } 1086 }
1074 1087
1075 class _Relation { 1088 class _Relation {
1076 final IndexRelationKind kind; 1089 final IndexRelationKind kind;
1077 final int offset; 1090 final int offset;
1078 final int length; 1091 final int length;
1079 final bool isQualified; 1092 final bool isQualified;
1080 1093
1081 _Relation(this.kind, this.offset, this.length, this.isQualified); 1094 _Relation(this.kind, this.offset, this.length, this.isQualified);
1082 1095
1083 @override 1096 @override
1084 String toString() { 1097 String toString() {
1085 return '_Relation{kind: $kind, offset: $offset, length: $length, ' 1098 return '_Relation{kind: $kind, offset: $offset, length: $length, '
1086 'isQualified: $isQualified}'; 1099 'isQualified: $isQualified}';
1087 } 1100 }
1088 } 1101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698