OLD | NEW |
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/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
9 import 'package:analyzer/src/dart/analysis/driver.dart'; | 9 import 'package:analyzer/src/dart/analysis/driver.dart'; |
10 import 'package:analyzer/src/dart/analysis/search.dart'; | 10 import 'package:analyzer/src/dart/analysis/search.dart'; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 buffer.write(isQualified); | 74 buffer.write(isQualified); |
75 buffer.write(")"); | 75 buffer.write(")"); |
76 return buffer.toString(); | 76 return buffer.toString(); |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 @reflectiveTest | 80 @reflectiveTest |
81 class SearchTest extends BaseAnalysisDriverTest { | 81 class SearchTest extends BaseAnalysisDriverTest { |
82 static const testUri = 'package:test/test.dart'; | 82 static const testUri = 'package:test/test.dart'; |
83 | 83 |
84 test_searchReferences_Label() async { | 84 test_searchReferences_FunctionElement_local() async { |
| 85 addTestFile(''' |
| 86 main() { |
| 87 test() {} |
| 88 test(); |
| 89 print(test); |
| 90 } |
| 91 '''); |
| 92 FunctionElement element = await _findElement('test'); |
| 93 List<String> main = [testUri, 'main']; |
| 94 var expected = [ |
| 95 _expectId(main, SearchResultKind.INVOCATION, 'test();'), |
| 96 _expectId(main, SearchResultKind.REFERENCE, 'test);') |
| 97 ]; |
| 98 await _verifyReferences(element, expected); |
| 99 } |
| 100 |
| 101 test_searchReferences_LabelElement() async { |
85 addTestFile(''' | 102 addTestFile(''' |
86 main() { | 103 main() { |
87 label: | 104 label: |
88 while (true) { | 105 while (true) { |
89 if (true) { | 106 if (true) { |
90 break label; // 1 | 107 break label; // 1 |
91 } | 108 } |
92 break label; // 2 | 109 break label; // 2 |
93 } | 110 } |
94 } | 111 } |
95 '''); | 112 '''); |
96 Element element = await _findElementAtString('label:'); | 113 Element element = await _findElement('label'); |
97 List<String> main = [testUri, 'main']; | 114 List<String> main = [testUri, 'main']; |
98 var expected = [ | 115 var expected = [ |
99 _expectId(main, SearchResultKind.REFERENCE, 'label; // 1'), | 116 _expectId(main, SearchResultKind.REFERENCE, 'label; // 1'), |
100 _expectId(main, SearchResultKind.REFERENCE, 'label; // 2') | 117 _expectId(main, SearchResultKind.REFERENCE, 'label; // 2') |
101 ]; | 118 ]; |
102 await _verifyReferences(element, expected); | 119 await _verifyReferences(element, expected); |
103 } | 120 } |
104 | 121 |
105 test_searchReferences_localVariable() async { | 122 test_searchReferences_LocalVariableElement() async { |
106 addTestFile(r''' | 123 addTestFile(r''' |
107 main() { | 124 main() { |
108 var v; | 125 var v; |
109 v = 1; | 126 v = 1; |
110 v += 2; | 127 v += 2; |
111 print(v); | 128 print(v); |
112 v(); | 129 v(); |
113 } | 130 } |
114 '''); | 131 '''); |
115 Element element = await _findElementAtString('v;'); | 132 Element element = await _findElement('v'); |
116 List<String> main = [testUri, 'main']; | 133 List<String> main = [testUri, 'main']; |
117 var expected = [ | 134 var expected = [ |
118 _expectId(main, SearchResultKind.WRITE, 'v = 1;'), | 135 _expectId(main, SearchResultKind.WRITE, 'v = 1;'), |
119 _expectId(main, SearchResultKind.READ_WRITE, 'v += 2;'), | 136 _expectId(main, SearchResultKind.READ_WRITE, 'v += 2;'), |
120 _expectId(main, SearchResultKind.READ, 'v);'), | 137 _expectId(main, SearchResultKind.READ, 'v);'), |
121 _expectId(main, SearchResultKind.INVOCATION, 'v();') | 138 _expectId(main, SearchResultKind.INVOCATION, 'v();') |
122 ]; | 139 ]; |
123 await _verifyReferences(element, expected); | 140 await _verifyReferences(element, expected); |
124 } | 141 } |
125 | 142 |
126 test_searchReferences_localVariable_inForEachLoop() async { | 143 test_searchReferences_localVariableElement_inForEachLoop() async { |
127 addTestFile(''' | 144 addTestFile(''' |
128 main() { | 145 main() { |
129 for (var v in []) { | 146 for (var v in []) { |
130 v = 1; | 147 v = 1; |
131 v += 2; | 148 v += 2; |
132 print(v); | 149 print(v); |
133 v(); | 150 v(); |
134 } | 151 } |
135 } | 152 } |
136 '''); | 153 '''); |
137 Element element = await _findElementAtString('v in []'); | 154 Element element = await _findElementAtString('v in []'); |
138 List<String> main = [testUri, 'main']; | 155 List<String> main = [testUri, 'main']; |
139 var expected = [ | 156 var expected = [ |
140 _expectId(main, SearchResultKind.WRITE, 'v = 1;'), | 157 _expectId(main, SearchResultKind.WRITE, 'v = 1;'), |
141 _expectId(main, SearchResultKind.READ_WRITE, 'v += 2;'), | 158 _expectId(main, SearchResultKind.READ_WRITE, 'v += 2;'), |
142 _expectId(main, SearchResultKind.READ, 'v);'), | 159 _expectId(main, SearchResultKind.READ, 'v);'), |
143 _expectId(main, SearchResultKind.INVOCATION, 'v();') | 160 _expectId(main, SearchResultKind.INVOCATION, 'v();') |
144 ]; | 161 ]; |
145 await _verifyReferences(element, expected); | 162 await _verifyReferences(element, expected); |
146 } | 163 } |
147 | 164 |
| 165 test_searchReferences_TypeParameterElement_ofClass() async { |
| 166 addTestFile(''' |
| 167 class A<T> { |
| 168 foo(T a) {} |
| 169 bar(T b) {} |
| 170 } |
| 171 '''); |
| 172 TypeParameterElement element = await _findElement('T'); |
| 173 var expected = [ |
| 174 _expectId([testUri, 'A', 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'), |
| 175 _expectId([testUri, 'A', 'bar', 'b'], SearchResultKind.REFERENCE, 'T b'), |
| 176 ]; |
| 177 await _verifyReferences(element, expected); |
| 178 } |
| 179 |
| 180 test_searchReferences_TypeParameterElement_ofLocalFunction() async { |
| 181 addTestFile(''' |
| 182 main() { |
| 183 void foo<T>(T a) { |
| 184 void bar(T b) {} |
| 185 } |
| 186 } |
| 187 '''); |
| 188 TypeParameterElement element = await _findElement('T'); |
| 189 var expected = [ |
| 190 _expectId( |
| 191 [testUri, 'main', 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'), |
| 192 _expectId([testUri, 'main', 'foo', 'bar', 'b'], |
| 193 SearchResultKind.REFERENCE, 'T b'), |
| 194 ]; |
| 195 await _verifyReferences(element, expected); |
| 196 } |
| 197 |
| 198 test_searchReferences_TypeParameterElement_ofMethod() async { |
| 199 addTestFile(''' |
| 200 class A { |
| 201 foo<T>(T p) {} |
| 202 } |
| 203 '''); |
| 204 TypeParameterElement element = await _findElement('T'); |
| 205 var expected = [ |
| 206 _expectId([testUri, 'A', 'foo', 'p'], SearchResultKind.REFERENCE, 'T p'), |
| 207 ]; |
| 208 await _verifyReferences(element, expected); |
| 209 } |
| 210 |
| 211 test_searchReferences_TypeParameterElement_ofTopLevelFunction() async { |
| 212 addTestFile(''' |
| 213 foo<T>(T a) { |
| 214 bar(T b) {} |
| 215 } |
| 216 '''); |
| 217 TypeParameterElement element = await _findElement('T'); |
| 218 var expected = [ |
| 219 _expectId([testUri, 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'), |
| 220 _expectId( |
| 221 [testUri, 'foo', 'bar', 'b'], SearchResultKind.REFERENCE, 'T b'), |
| 222 ]; |
| 223 await _verifyReferences(element, expected); |
| 224 } |
| 225 |
148 ExpectedResult _expectId( | 226 ExpectedResult _expectId( |
149 List<String> enclosingComponents, SearchResultKind kind, String search, | 227 List<String> enclosingComponents, SearchResultKind kind, String search, |
150 {int length, bool isResolved: true, bool isQualified: false}) { | 228 {int length, bool isResolved: true, bool isQualified: false}) { |
151 int offset = findOffset(search); | 229 int offset = findOffset(search); |
152 if (length == null) { | 230 if (length == null) { |
153 length = getLeadingIdentifierLength(search); | 231 length = getLeadingIdentifierLength(search); |
154 } | 232 } |
155 return new ExpectedResult(enclosingComponents, kind, offset, length, | 233 return new ExpectedResult(enclosingComponents, kind, offset, length, |
156 isResolved: isResolved, isQualified: isQualified); | 234 isResolved: isResolved, isQualified: isQualified); |
157 } | 235 } |
158 | 236 |
| 237 Future<Element> _findElement(String name) async { |
| 238 AnalysisResult result = await driver.getResult(testFile); |
| 239 return findChildElement(result.unit.element, name); |
| 240 } |
| 241 |
159 Future<Element> _findElementAtString(String search) async { | 242 Future<Element> _findElementAtString(String search) async { |
160 AnalysisResult result = await driver.getResult(testFile); | 243 AnalysisResult result = await driver.getResult(testFile); |
161 int offset = findOffset(search); | 244 int offset = findOffset(search); |
162 AstNode node = new NodeLocator(offset).searchWithin(result.unit); | 245 AstNode node = new NodeLocator(offset).searchWithin(result.unit); |
163 return ElementLocator.locate(node); | 246 return ElementLocator.locate(node); |
164 } | 247 } |
165 | 248 |
166 Future _verifyReferences( | 249 Future _verifyReferences( |
167 Element element, List<ExpectedResult> expectedMatches) async { | 250 Element element, List<ExpectedResult> expectedMatches) async { |
168 List<SearchResult> results = await driver.search.references(element); | 251 List<SearchResult> results = await driver.search.references(element); |
169 _assertResults(results, expectedMatches); | 252 _assertResults(results, expectedMatches); |
170 expect(results, hasLength(expectedMatches.length)); | 253 expect(results, hasLength(expectedMatches.length)); |
171 } | 254 } |
172 | 255 |
173 static void _assertResults( | 256 static void _assertResults( |
174 List<SearchResult> matches, List<ExpectedResult> expectedMatches) { | 257 List<SearchResult> matches, List<ExpectedResult> expectedMatches) { |
175 expect(matches, unorderedEquals(expectedMatches)); | 258 expect(matches, unorderedEquals(expectedMatches)); |
176 } | 259 } |
177 } | 260 } |
OLD | NEW |