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

Side by Side Diff: pkg/analyzer/test/src/dart/analysis/search_test.dart

Issue 2653493006: Issue 28386. Implement search for potential usages with the new analysis driver. (Closed)
Patch Set: Created 3 years, 11 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:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/dart/analysis/driver.dart'; 10 import 'package:analyzer/src/dart/analysis/driver.dart';
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 92 }
93 93
94 test_classMembers_importNotDart() async { 94 test_classMembers_importNotDart() async {
95 await _resolveTestUnit(''' 95 await _resolveTestUnit('''
96 import 'not-dart.txt'; 96 import 'not-dart.txt';
97 '''); 97 ''');
98 RegExp regExp = new RegExp(r'^test$'); 98 RegExp regExp = new RegExp(r'^test$');
99 expect(await driver.search.classMembers(regExp), isEmpty); 99 expect(await driver.search.classMembers(regExp), isEmpty);
100 } 100 }
101 101
102 test_searchMemberReferences_qualified_resolved() async {
103 await _resolveTestUnit('''
104 class C {
105 var test;
106 }
107 main(C c) {
108 print(c.test);
109 c.test = 1;
110 c.test += 2;
111 c.test();
112 }
113 ''');
114 await _verifyNameReferences('test', []);
115 }
116
117 test_searchMemberReferences_qualified_unresolved() async {
118 await _resolveTestUnit('''
119 main(p) {
120 print(p.test);
121 p.test = 1;
122 p.test += 2;
123 p.test();
124 }
125 ''');
126 Element main = _findElement('main');
127 await _verifyNameReferences('test', <ExpectedResult>[
128 _expectIdQU(main, SearchResultKind.READ, 'test);'),
129 _expectIdQU(main, SearchResultKind.WRITE, 'test = 1;'),
130 _expectIdQU(main, SearchResultKind.READ_WRITE, 'test += 2;'),
131 _expectIdQU(main, SearchResultKind.INVOCATION, 'test();'),
132 ]);
133 }
134
135 test_searchMemberReferences_unqualified_resolved() async {
136 await _resolveTestUnit('''
137 class C {
138 var test;
139 main() {
140 print(test);
141 test = 1;
142 test += 2;
143 test();
144 }
145 }
146 ''');
147 await _verifyNameReferences('test', []);
148 }
149
150 test_searchMemberReferences_unqualified_unresolved() async {
151 await _resolveTestUnit('''
152 class C {
153 main() {
154 print(test);
155 test = 1;
156 test += 2;
157 test();
158 }
159 }
160 ''');
161 Element main = _findElement('main');
162 await _verifyNameReferences('test', <ExpectedResult>[
163 _expectIdU(main, SearchResultKind.READ, 'test);'),
164 _expectIdU(main, SearchResultKind.WRITE, 'test = 1;'),
165 _expectIdU(main, SearchResultKind.READ_WRITE, 'test += 2;'),
166 _expectIdU(main, SearchResultKind.INVOCATION, 'test();'),
167 ]);
168 }
169
102 test_searchReferences_ClassElement_definedInside() async { 170 test_searchReferences_ClassElement_definedInside() async {
103 await _resolveTestUnit(''' 171 await _resolveTestUnit('''
104 class A {}; 172 class A {};
105 main(A p) { 173 main(A p) {
106 A v; 174 A v;
107 } 175 }
108 class B1 extends A {} // extends 176 class B1 extends A {} // extends
109 class B2 implements A {} // implements 177 class B2 implements A {} // implements
110 class B3 extends Object with A {} // with 178 class B3 extends Object with A {} // with
111 List<A> v2 = null; 179 List<A> v2 = null;
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 1074
1007 /** 1075 /**
1008 * Create [ExpectedResult] for a qualified and resolved match. 1076 * Create [ExpectedResult] for a qualified and resolved match.
1009 */ 1077 */
1010 ExpectedResult _expectIdQ( 1078 ExpectedResult _expectIdQ(
1011 Element element, SearchResultKind kind, String search, 1079 Element element, SearchResultKind kind, String search,
1012 {int length, bool isResolved: true}) { 1080 {int length, bool isResolved: true}) {
1013 return _expectId(element, kind, search, isQualified: true, length: length); 1081 return _expectId(element, kind, search, isQualified: true, length: length);
1014 } 1082 }
1015 1083
1084 /**
1085 * Create [ExpectedResult] for a qualified and unresolved match.
1086 */
1087 ExpectedResult _expectIdQU(
1088 Element element, SearchResultKind kind, String search,
1089 {int length}) {
1090 return _expectId(element, kind, search,
1091 isQualified: true, isResolved: false, length: length);
1092 }
1093
1094 /**
1095 * Create [ExpectedResult] for a unqualified and unresolved match.
1096 */
1097 ExpectedResult _expectIdU(
1098 Element element, SearchResultKind kind, String search,
1099 {int length}) {
1100 return _expectId(element, kind, search,
1101 isQualified: false, isResolved: false, length: length);
1102 }
1103
1016 Element _findElement(String name, [ElementKind kind]) { 1104 Element _findElement(String name, [ElementKind kind]) {
1017 return findChildElement(testUnit.element, name, kind); 1105 return findChildElement(testUnit.element, name, kind);
1018 } 1106 }
1019 1107
1020 Element _findElementAtString(String search) { 1108 Element _findElementAtString(String search) {
1021 int offset = findOffset(search); 1109 int offset = findOffset(search);
1022 AstNode node = new NodeLocator(offset).searchWithin(testUnit); 1110 AstNode node = new NodeLocator(offset).searchWithin(testUnit);
1023 return ElementLocator.locate(node); 1111 return ElementLocator.locate(node);
1024 } 1112 }
1025 1113
1026 String _p(String path) => provider.convertPath(path); 1114 String _p(String path) => provider.convertPath(path);
1027 1115
1028 Future<Null> _resolveTestUnit(String code) async { 1116 Future<Null> _resolveTestUnit(String code) async {
1029 addTestFile(code); 1117 addTestFile(code);
1030 if (testUnit == null) { 1118 if (testUnit == null) {
1031 AnalysisResult result = await driver.getResult(testFile); 1119 AnalysisResult result = await driver.getResult(testFile);
1032 testUnit = result.unit; 1120 testUnit = result.unit;
1033 testUnitElement = testUnit.element; 1121 testUnitElement = testUnit.element;
1034 testLibraryElement = testUnitElement.library; 1122 testLibraryElement = testUnitElement.library;
1035 } 1123 }
1036 } 1124 }
1037 1125
1126 Future<Null> _verifyNameReferences(
1127 String name, List<ExpectedResult> expectedMatches) async {
1128 List<SearchResult> results =
1129 await driver.search.unresolvedMemberReferences(name);
1130 _assertResults(results, expectedMatches);
1131 expect(results, hasLength(expectedMatches.length));
1132 }
1133
1038 Future _verifyReferences( 1134 Future _verifyReferences(
1039 Element element, List<ExpectedResult> expectedMatches) async { 1135 Element element, List<ExpectedResult> expectedMatches) async {
1040 List<SearchResult> results = await driver.search.references(element); 1136 List<SearchResult> results = await driver.search.references(element);
1041 _assertResults(results, expectedMatches); 1137 _assertResults(results, expectedMatches);
1042 expect(results, hasLength(expectedMatches.length)); 1138 expect(results, hasLength(expectedMatches.length));
1043 } 1139 }
1044 1140
1045 static void _assertResults( 1141 static void _assertResults(
1046 List<SearchResult> matches, List<ExpectedResult> expectedMatches) { 1142 List<SearchResult> matches, List<ExpectedResult> expectedMatches) {
1047 expect(matches, unorderedEquals(expectedMatches)); 1143 expect(matches, unorderedEquals(expectedMatches));
1048 } 1144 }
1049 } 1145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698