Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.services.src.search.search_engine; | 5 library test.services.src.search.search_engine2; |
|
Brian Wilkerson
2016/03/14 18:57:32
"analysis_server.test.services.search.search_engin
| |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/services/index/index.dart'; | 9 import 'package:analysis_server/src/services/index/index.dart'; |
| 10 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | |
| 11 import 'package:analysis_server/src/services/search/search_engine.dart'; | 10 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 12 import 'package:analysis_server/src/services/search/search_engine_internal.dart' ; | 11 import 'package:analysis_server/src/services/search/search_engine_internal.dart' ; |
| 13 import 'package:analyzer/dart/element/element.dart'; | 12 import 'package:analyzer/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/dart/element/element.dart'; | 13 import 'package:analyzer/src/dart/element/element.dart'; |
| 15 import 'package:analyzer/src/dart/element/member.dart'; | 14 import 'package:analyzer/src/dart/element/member.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 18 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
| 19 | 18 |
| 20 import '../../abstract_single_unit.dart'; | 19 import '../../abstract_single_unit.dart'; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 } | 62 } |
| 64 } | 63 } |
| 65 | 64 |
| 66 @reflectiveTest | 65 @reflectiveTest |
| 67 class SearchEngineImplTest extends AbstractSingleUnitTest { | 66 class SearchEngineImplTest extends AbstractSingleUnitTest { |
| 68 Index index; | 67 Index index; |
| 69 SearchEngineImpl searchEngine; | 68 SearchEngineImpl searchEngine; |
| 70 | 69 |
| 71 void setUp() { | 70 void setUp() { |
| 72 super.setUp(); | 71 super.setUp(); |
| 73 index = createLocalMemoryIndex(); | 72 index = createMemoryIndex(); |
| 74 searchEngine = new SearchEngineImpl(index); | 73 searchEngine = new SearchEngineImpl(index); |
| 75 } | 74 } |
| 76 | 75 |
| 77 Future test_searchAllSubtypes() { | 76 test_searchAllSubtypes() async { |
| 78 _indexTestUnit(''' | 77 _indexTestUnit(''' |
| 79 class T {} | 78 class T {} |
| 80 class A extends T {} | 79 class A extends T {} |
| 81 class B extends A {} | 80 class B extends A {} |
| 82 class C implements B {} | 81 class C implements B {} |
| 83 '''); | 82 '''); |
| 84 ClassElement element = findElement('T'); | 83 ClassElement element = findElement('T'); |
| 85 ClassElement elementA = findElement('A'); | 84 ClassElement elementA = findElement('A'); |
| 86 ClassElement elementB = findElement('B'); | 85 ClassElement elementB = findElement('B'); |
| 87 ClassElement elementC = findElement('C'); | 86 ClassElement elementC = findElement('C'); |
| 88 var expected = [ | 87 var expected = [ |
| 89 _expectId(elementA, MatchKind.DECLARATION, 'A extends T'), | 88 _expectId(elementA, MatchKind.DECLARATION, 'A extends T'), |
| 90 _expectId(elementB, MatchKind.DECLARATION, 'B extends A'), | 89 _expectId(elementB, MatchKind.DECLARATION, 'B extends A'), |
| 91 _expectId(elementC, MatchKind.DECLARATION, 'C implements B') | 90 _expectId(elementC, MatchKind.DECLARATION, 'C implements B') |
| 92 ]; | 91 ]; |
| 93 return searchEngine.searchAllSubtypes(element).then((matches) { | 92 List<SearchMatch> matches = await searchEngine.searchAllSubtypes(element); |
| 94 _assertMatches(matches, expected); | 93 _assertMatches(matches, expected); |
| 95 }); | |
| 96 } | 94 } |
| 97 | 95 |
| 98 Future test_searchMemberDeclarations() { | 96 test_searchMemberDeclarations() async { |
| 99 _indexTestUnit(''' | 97 _indexTestUnit(''' |
| 100 class A { | 98 class A { |
| 101 test() {} | 99 test() {} |
| 102 } | 100 } |
| 103 class B { | 101 class B { |
| 104 int test = 1; | 102 int test = 1; |
| 105 main() { | 103 main() { |
| 106 int test = 2; | 104 int test = 2; |
| 107 } | 105 } |
| 108 } | 106 } |
| 109 '''); | 107 '''); |
| 110 ClassElement elementA = findElement('A'); | 108 ClassElement elementA = findElement('A'); |
| 111 ClassElement elementB = findElement('B'); | 109 ClassElement elementB = findElement('B'); |
| 112 var expected = [ | 110 var expected = [ |
| 113 _expectId(elementA.methods[0], MatchKind.DECLARATION, 'test() {}'), | 111 _expectId(elementA.methods[0], MatchKind.DECLARATION, 'test() {}'), |
| 114 _expectId(elementB.fields[0], MatchKind.DECLARATION, 'test = 1;') | 112 _expectId(elementB.fields[0], MatchKind.DECLARATION, 'test = 1;') |
| 115 ]; | 113 ]; |
| 116 return searchEngine.searchMemberDeclarations('test').then((matches) { | 114 List<SearchMatch> matches = |
| 117 _assertMatches(matches, expected); | 115 await searchEngine.searchMemberDeclarations('test'); |
| 118 }); | 116 _assertMatches(matches, expected); |
| 119 } | 117 } |
| 120 | 118 |
| 121 Future test_searchMemberReferences() { | 119 test_searchMemberReferences_qualified_resolved() async { |
| 122 _indexTestUnit(''' | 120 _indexTestUnit(''' |
| 123 class A { | 121 class C { |
| 124 var test; // A | 122 var test; |
| 125 mainA() { | 123 } |
| 126 test(); // a-inv-r-nq | 124 main(C c) { |
| 127 test = 1; // a-write-r-nq | 125 print(c.test); |
| 128 test += 2; // a-read-write-r-nq | 126 c.test = 1; |
| 129 print(test); // a-read-r-nq | 127 c.test += 2; |
| 128 c.test(); | |
| 129 } | |
| 130 '''); | |
| 131 List<SearchMatch> matches = | |
| 132 await searchEngine.searchMemberReferences('test'); | |
| 133 expect(matches, isEmpty); | |
| 134 } | |
| 135 | |
| 136 test_searchMemberReferences_qualified_unresolved() async { | |
| 137 _indexTestUnit(''' | |
| 138 main(p) { | |
| 139 print(p.test); | |
| 140 p.test = 1; | |
| 141 p.test += 2; | |
| 142 p.test(); | |
| 143 } | |
| 144 '''); | |
| 145 Element main = findElement('main'); | |
| 146 var expected = [ | |
| 147 _expectIdQU(main, MatchKind.READ, 'test);'), | |
| 148 _expectIdQU(main, MatchKind.WRITE, 'test = 1;'), | |
| 149 _expectIdQU(main, MatchKind.READ_WRITE, 'test += 2;'), | |
| 150 _expectIdQU(main, MatchKind.INVOCATION, 'test();'), | |
| 151 ]; | |
| 152 List<SearchMatch> matches = | |
| 153 await searchEngine.searchMemberReferences('test'); | |
| 154 _assertMatches(matches, expected); | |
| 155 } | |
| 156 | |
| 157 test_searchMemberReferences_unqualified_resolved() async { | |
| 158 _indexTestUnit(''' | |
| 159 class C { | |
| 160 var test; | |
| 161 main() { | |
| 162 print(test); | |
| 163 test = 1; | |
| 164 test += 2; | |
| 165 test(); | |
| 130 } | 166 } |
| 131 } | 167 } |
| 132 main(A a, p) { | 168 '''); |
| 133 a.test(); // a-inv-r-q | 169 List<SearchMatch> matches = |
| 134 a.test = 1; // a-write-r-q | 170 await searchEngine.searchMemberReferences('test'); |
| 135 a.test += 2; // a-read-write-r-q | 171 expect(matches, isEmpty); |
| 136 print(a.test); // a-read-r-q | 172 } |
| 137 p.test(); // p-inv-ur-q | 173 |
| 138 p.test = 1; // p-write-ur-q | 174 test_searchMemberReferences_unqualified_unresolved() async { |
| 139 p.test += 2; // p-read-write-ur-q | 175 verifyNoTestUnitErrors = false; |
| 140 print(p.test); // p-read-ur-q | 176 _indexTestUnit(''' |
| 177 class C { | |
| 178 main() { | |
| 179 print(test); | |
| 180 test = 1; | |
| 181 test += 2; | |
| 182 test(); | |
| 183 } | |
| 141 } | 184 } |
| 142 '''); | 185 '''); |
| 143 Element mainA = findElement('mainA'); | |
| 144 Element main = findElement('main'); | 186 Element main = findElement('main'); |
| 145 var expected = [ | 187 var expected = [ |
| 146 _expectId(mainA, MatchKind.INVOCATION, 'test(); // a-inv-r-nq'), | 188 _expectIdU(main, MatchKind.READ, 'test);'), |
| 147 _expectId(mainA, MatchKind.WRITE, 'test = 1; // a-write-r-nq'), | 189 _expectIdU(main, MatchKind.WRITE, 'test = 1;'), |
| 148 _expectId(mainA, MatchKind.READ_WRITE, 'test += 2; // a-read-write-r-nq'), | 190 _expectIdU(main, MatchKind.READ_WRITE, 'test += 2;'), |
| 149 _expectId(mainA, MatchKind.READ, 'test); // a-read-r-nq'), | 191 _expectIdU(main, MatchKind.INVOCATION, 'test();'), |
| 150 _expectIdQ(main, MatchKind.INVOCATION, 'test(); // a-inv-r-q'), | |
| 151 _expectIdQ(main, MatchKind.WRITE, 'test = 1; // a-write-r-q'), | |
| 152 _expectIdQ(main, MatchKind.READ_WRITE, 'test += 2; // a-read-write-r-q'), | |
| 153 _expectIdQ(main, MatchKind.READ, 'test); // a-read-r-q'), | |
| 154 _expectIdU(main, MatchKind.INVOCATION, 'test(); // p-inv-ur-q'), | |
| 155 _expectIdU(main, MatchKind.WRITE, 'test = 1; // p-write-ur-q'), | |
| 156 _expectIdU(main, MatchKind.READ_WRITE, 'test += 2; // p-read-write-ur-q'), | |
| 157 _expectIdU(main, MatchKind.READ, 'test); // p-read-ur-q'), | |
| 158 ]; | 192 ]; |
| 159 return searchEngine.searchMemberReferences('test').then((matches) { | 193 List<SearchMatch> matches = |
| 160 _assertMatches(matches, expected); | 194 await searchEngine.searchMemberReferences('test'); |
| 161 }); | 195 _assertMatches(matches, expected); |
| 162 } | 196 } |
| 163 | 197 |
| 164 Future test_searchReferences_ClassElement() { | 198 test_searchReferences_ClassElement() async { |
| 165 _indexTestUnit(''' | 199 _indexTestUnit(''' |
| 166 class A {} | 200 class A {} |
| 167 main(A p) { | 201 main(A p) { |
| 168 A v; | 202 A v; |
| 169 } | 203 } |
| 170 '''); | 204 '''); |
| 171 ClassElement element = findElement('A'); | 205 ClassElement element = findElement('A'); |
| 172 Element pElement = findElement('p'); | 206 Element pElement = findElement('p'); |
| 173 Element vElement = findElement('v'); | 207 Element vElement = findElement('v'); |
| 174 var expected = [ | 208 var expected = [ |
| 175 _expectId(pElement, MatchKind.REFERENCE, 'A p'), | 209 _expectId(pElement, MatchKind.REFERENCE, 'A p'), |
| 176 _expectId(vElement, MatchKind.REFERENCE, 'A v') | 210 _expectId(vElement, MatchKind.REFERENCE, 'A v') |
| 177 ]; | 211 ]; |
| 178 return _verifyReferences(element, expected); | 212 await _verifyReferences(element, expected); |
| 179 } | 213 } |
| 180 | 214 |
| 181 Future test_searchReferences_CompilationUnitElement() { | 215 test_searchReferences_CompilationUnitElement() async { |
| 182 addSource( | 216 addSource( |
| 183 '/my_part.dart', | 217 '/my_part.dart', |
| 184 ''' | 218 ''' |
| 185 part of lib; | 219 part of lib; |
| 186 '''); | 220 '''); |
| 187 _indexTestUnit(''' | 221 _indexTestUnit(''' |
| 188 library lib; | 222 library lib; |
| 189 part 'my_part.dart'; | 223 part 'my_part.dart'; |
| 190 '''); | 224 '''); |
| 191 CompilationUnitElement element = testLibraryElement.parts[0]; | 225 CompilationUnitElement element = testLibraryElement.parts[0]; |
| 192 var expected = [ | 226 var expected = [ |
| 193 _expectId(testUnitElement, MatchKind.REFERENCE, "'my_part.dart'", | 227 _expectIdQ(testUnitElement, MatchKind.REFERENCE, "'my_part.dart'", |
| 194 length: "'my_part.dart'".length) | 228 length: "'my_part.dart'".length) |
| 195 ]; | 229 ]; |
| 196 return _verifyReferences(element, expected); | 230 await _verifyReferences(element, expected); |
| 197 } | 231 } |
| 198 | 232 |
| 199 Future test_searchReferences_ConstructorElement() { | 233 test_searchReferences_ConstructorElement() async { |
| 200 _indexTestUnit(''' | 234 _indexTestUnit(''' |
| 201 class A { | 235 class A { |
| 202 A.named() {} | 236 A.named() {} |
| 203 } | 237 } |
| 204 main() { | 238 main() { |
| 205 new A.named(); | 239 new A.named(); |
| 206 } | 240 } |
| 207 '''); | 241 '''); |
| 208 ConstructorElement element = findElement('named'); | 242 ConstructorElement element = findElement('named'); |
| 209 Element mainElement = findElement('main'); | 243 Element mainElement = findElement('main'); |
| 210 var expected = [ | 244 var expected = [ |
| 211 _expectId(mainElement, MatchKind.REFERENCE, '.named();', length: 6) | 245 _expectIdQ(mainElement, MatchKind.REFERENCE, '.named();', length: 6) |
| 212 ]; | 246 ]; |
| 213 return _verifyReferences(element, expected); | 247 await _verifyReferences(element, expected); |
| 214 } | 248 } |
| 215 | 249 |
| 216 Future test_searchReferences_Element_unknown() { | 250 test_searchReferences_ConstructorElement_synthetic() async { |
| 217 return _verifyReferences(DynamicElementImpl.instance, []); | 251 _indexTestUnit(''' |
| 252 class A { | |
| 253 } | |
| 254 main() { | |
| 255 new A(); | |
| 256 } | |
| 257 '''); | |
| 258 ClassElement classElement = findElement('A'); | |
| 259 ConstructorElement element = classElement.unnamedConstructor; | |
| 260 Element mainElement = findElement('main'); | |
| 261 var expected = [ | |
| 262 _expectIdQ(mainElement, MatchKind.REFERENCE, '();', length: 0) | |
| 263 ]; | |
| 264 await _verifyReferences(element, expected); | |
| 218 } | 265 } |
| 219 | 266 |
| 220 Future test_searchReferences_FieldElement() { | 267 test_searchReferences_Element_unknown() async { |
| 268 await _verifyReferences(DynamicElementImpl.instance, []); | |
| 269 } | |
| 270 | |
| 271 test_searchReferences_FieldElement() async { | |
| 221 _indexTestUnit(''' | 272 _indexTestUnit(''' |
| 222 class A { | 273 class A { |
| 223 var field; | 274 var field; |
| 224 A({this.field}); | 275 A({this.field}); |
| 225 main() { | 276 main() { |
| 226 new A(field: 1); | 277 new A(field: 1); |
| 227 // getter | 278 // getter |
| 228 print(field); // ref-nq | 279 print(field); // ref-nq |
| 229 print(this.field); // ref-q | 280 print(this.field); // ref-q |
| 230 field(); // inv-nq | 281 field(); // inv-nq |
| 231 this.field(); // inv-q | 282 this.field(); // inv-q |
| 232 // setter | 283 // setter |
| 233 field = 2; // ref-nq; | 284 field = 2; // ref-nq; |
| 234 this.field = 3; // ref-q; | 285 this.field = 3; // ref-q; |
| 235 } | 286 } |
| 236 } | 287 } |
| 237 '''); | 288 '''); |
| 238 FieldElement element = findElement('field'); | 289 FieldElement element = findElement('field'); |
| 239 Element main = findElement('main'); | 290 Element main = findElement('main'); |
| 240 Element fieldParameter = findElement('field', ElementKind.PARAMETER); | 291 Element fieldParameter = findElement('field', ElementKind.PARAMETER); |
| 241 var expected = [ | 292 var expected = [ |
| 242 _expectId(fieldParameter, MatchKind.WRITE, 'field}'), | 293 _expectIdQ(fieldParameter, MatchKind.WRITE, 'field}'), |
| 243 _expectId(main, MatchKind.REFERENCE, 'field: 1'), | 294 _expectIdQ(main, MatchKind.REFERENCE, 'field: 1'), |
| 244 _expectId(main, MatchKind.READ, 'field); // ref-nq'), | 295 _expectId(main, MatchKind.READ, 'field); // ref-nq'), |
| 245 _expectIdQ(main, MatchKind.READ, 'field); // ref-q'), | 296 _expectIdQ(main, MatchKind.READ, 'field); // ref-q'), |
| 246 _expectId(main, MatchKind.INVOCATION, 'field(); // inv-nq'), | 297 _expectId(main, MatchKind.INVOCATION, 'field(); // inv-nq'), |
| 247 _expectIdQ(main, MatchKind.INVOCATION, 'field(); // inv-q'), | 298 _expectIdQ(main, MatchKind.INVOCATION, 'field(); // inv-q'), |
| 248 _expectId(main, MatchKind.WRITE, 'field = 2; // ref-nq'), | 299 _expectId(main, MatchKind.WRITE, 'field = 2; // ref-nq'), |
| 249 _expectIdQ(main, MatchKind.WRITE, 'field = 3; // ref-q'), | 300 _expectIdQ(main, MatchKind.WRITE, 'field = 3; // ref-q'), |
| 250 ]; | 301 ]; |
| 251 return _verifyReferences(element, expected); | 302 await _verifyReferences(element, expected); |
| 252 } | 303 } |
| 253 | 304 |
| 254 Future test_searchReferences_FunctionElement() { | 305 test_searchReferences_FieldElement_ofEnum() async { |
| 306 _indexTestUnit(''' | |
| 307 enum MyEnum { | |
| 308 A, B, C | |
| 309 } | |
| 310 main() { | |
| 311 print(MyEnum.A.index); | |
| 312 print(MyEnum.values); | |
| 313 print(MyEnum.A); | |
| 314 print(MyEnum.B); | |
| 315 } | |
| 316 '''); | |
| 317 ClassElement enumElement = findElement('MyEnum'); | |
| 318 Element mainElement = findElement('main'); | |
| 319 await _verifyReferences(enumElement.getField('index'), | |
| 320 [_expectIdQ(mainElement, MatchKind.READ, 'index);')]); | |
| 321 await _verifyReferences(enumElement.getField('values'), | |
| 322 [_expectIdQ(mainElement, MatchKind.READ, 'values);')]); | |
| 323 await _verifyReferences(enumElement.getField('A'), [ | |
| 324 _expectIdQ(mainElement, MatchKind.READ, 'A.index);'), | |
| 325 _expectIdQ(mainElement, MatchKind.READ, 'A);') | |
| 326 ]); | |
| 327 await _verifyReferences(enumElement.getField('B'), | |
| 328 [_expectIdQ(mainElement, MatchKind.READ, 'B);')]); | |
| 329 } | |
| 330 | |
| 331 test_searchReferences_FieldElement_synthetic() async { | |
| 332 _indexTestUnit(''' | |
| 333 class A { | |
| 334 get field => null; | |
| 335 set field(x) {} | |
| 336 main() { | |
| 337 // getter | |
| 338 print(field); // ref-nq | |
| 339 print(this.field); // ref-q | |
| 340 field(); // inv-nq | |
| 341 this.field(); // inv-q | |
| 342 // setter | |
| 343 field = 2; // ref-nq; | |
| 344 this.field = 3; // ref-q; | |
| 345 } | |
| 346 } | |
| 347 '''); | |
| 348 FieldElement element = findElement('field', ElementKind.FIELD); | |
| 349 Element main = findElement('main'); | |
| 350 var expected = [ | |
| 351 _expectId(main, MatchKind.READ, 'field); // ref-nq'), | |
| 352 _expectIdQ(main, MatchKind.READ, 'field); // ref-q'), | |
| 353 _expectId(main, MatchKind.INVOCATION, 'field(); // inv-nq'), | |
| 354 _expectIdQ(main, MatchKind.INVOCATION, 'field(); // inv-q'), | |
| 355 _expectId(main, MatchKind.WRITE, 'field = 2; // ref-nq'), | |
| 356 _expectIdQ(main, MatchKind.WRITE, 'field = 3; // ref-q'), | |
| 357 ]; | |
| 358 await _verifyReferences(element, expected); | |
| 359 } | |
| 360 | |
| 361 test_searchReferences_FunctionElement() async { | |
| 255 _indexTestUnit(''' | 362 _indexTestUnit(''' |
| 256 test() {} | 363 test() {} |
| 257 main() { | 364 main() { |
| 258 test(); | 365 test(); |
| 259 print(test); | 366 print(test); |
| 260 } | 367 } |
| 261 '''); | 368 '''); |
| 262 FunctionElement element = findElement('test'); | 369 FunctionElement element = findElement('test'); |
| 263 Element mainElement = findElement('main'); | 370 Element mainElement = findElement('main'); |
| 264 var expected = [ | 371 var expected = [ |
| 265 _expectId(mainElement, MatchKind.INVOCATION, 'test();'), | 372 _expectId(mainElement, MatchKind.INVOCATION, 'test();'), |
| 266 _expectId(mainElement, MatchKind.REFERENCE, 'test);') | 373 _expectId(mainElement, MatchKind.REFERENCE, 'test);') |
| 267 ]; | 374 ]; |
| 268 return _verifyReferences(element, expected); | 375 await _verifyReferences(element, expected); |
| 269 } | 376 } |
| 270 | 377 |
| 271 Future test_searchReferences_FunctionTypeAliasElement() { | 378 test_searchReferences_FunctionElement_local() async { |
| 379 _indexTestUnit(''' | |
| 380 main() { | |
| 381 test() {} | |
| 382 test(); | |
| 383 print(test); | |
| 384 } | |
| 385 '''); | |
| 386 FunctionElement element = findElement('test'); | |
| 387 Element mainElement = findElement('main'); | |
| 388 var expected = [ | |
| 389 _expectId(mainElement, MatchKind.INVOCATION, 'test();'), | |
| 390 _expectId(mainElement, MatchKind.REFERENCE, 'test);') | |
| 391 ]; | |
| 392 await _verifyReferences(element, expected); | |
| 393 } | |
| 394 | |
| 395 test_searchReferences_FunctionTypeAliasElement() async { | |
| 272 _indexTestUnit(''' | 396 _indexTestUnit(''' |
| 273 typedef Test(); | 397 typedef Test(); |
| 274 main() { | 398 main() { |
| 275 Test a; | 399 Test a; |
| 276 Test b; | 400 Test b; |
| 277 } | 401 } |
| 278 '''); | 402 '''); |
| 279 FunctionTypeAliasElement element = findElement('Test'); | 403 FunctionTypeAliasElement element = findElement('Test'); |
| 280 Element aElement = findElement('a'); | 404 Element aElement = findElement('a'); |
| 281 Element bElement = findElement('b'); | 405 Element bElement = findElement('b'); |
| 282 var expected = [ | 406 var expected = [ |
| 283 _expectId(aElement, MatchKind.REFERENCE, 'Test a;'), | 407 _expectId(aElement, MatchKind.REFERENCE, 'Test a;'), |
| 284 _expectId(bElement, MatchKind.REFERENCE, 'Test b;') | 408 _expectId(bElement, MatchKind.REFERENCE, 'Test b;') |
| 285 ]; | 409 ]; |
| 286 return _verifyReferences(element, expected); | 410 await _verifyReferences(element, expected); |
| 287 } | 411 } |
| 288 | 412 |
| 289 Future test_searchReferences_ImportElement_noPrefix() { | 413 test_searchReferences_ImportElement_noPrefix() async { |
| 290 _indexTestUnit(''' | 414 _indexTestUnit(''' |
| 291 import 'dart:math'; | 415 import 'dart:math' show max, PI, Random hide min; |
| 416 export 'dart:math' show max, PI, Random hide min; | |
| 292 main() { | 417 main() { |
| 293 print(E); | 418 print(PI); |
| 419 print(new Random()); | |
| 420 print(max(1, 2)); | |
| 294 } | 421 } |
| 422 Random bar() => null; | |
| 295 '''); | 423 '''); |
| 296 ImportElement element = testLibraryElement.imports[0]; | 424 ImportElement element = testLibraryElement.imports[0]; |
| 297 Element mainElement = findElement('main'); | 425 Element mainElement = findElement('main'); |
| 426 Element barElement = findElement('bar'); | |
| 298 var kind = MatchKind.REFERENCE; | 427 var kind = MatchKind.REFERENCE; |
| 299 var expected = [_expectId(mainElement, kind, 'E);', length: 0)]; | 428 var expected = [ |
| 300 return _verifyReferences(element, expected); | 429 _expectId(mainElement, kind, 'PI);', length: 0), |
| 430 _expectId(mainElement, kind, 'Random()', length: 0), | |
| 431 _expectId(mainElement, kind, 'max(', length: 0), | |
| 432 _expectId(barElement, kind, 'Random bar()', length: 0), | |
| 433 ]; | |
| 434 await _verifyReferences(element, expected); | |
| 301 } | 435 } |
| 302 | 436 |
| 303 Future test_searchReferences_ImportElement_withPrefix() { | 437 test_searchReferences_ImportElement_withPrefix() async { |
| 304 _indexTestUnit(''' | 438 _indexTestUnit(''' |
| 305 import 'dart:math' as math; | 439 import 'dart:math' as math show max, PI, Random hide min; |
| 440 export 'dart:math' show max, PI, Random hide min; | |
| 306 main() { | 441 main() { |
| 307 print(math.PI); | 442 print(math.PI); |
| 443 print(new math.Random()); | |
| 444 print(math.max(1, 2)); | |
| 308 } | 445 } |
| 446 math.Random bar() => null; | |
| 309 '''); | 447 '''); |
| 310 ImportElement element = testLibraryElement.imports[0]; | 448 ImportElement element = testLibraryElement.imports[0]; |
| 311 Element mainElement = findElement('main'); | 449 Element mainElement = findElement('main'); |
| 450 Element barElement = findElement('bar'); | |
| 312 var kind = MatchKind.REFERENCE; | 451 var kind = MatchKind.REFERENCE; |
| 452 var length = 'math.'.length; | |
| 313 var expected = [ | 453 var expected = [ |
| 314 _expectId(mainElement, kind, 'math.PI);', length: 'math.'.length) | 454 _expectId(mainElement, kind, 'math.PI);', length: length), |
| 455 _expectId(mainElement, kind, 'math.Random()', length: length), | |
| 456 _expectId(mainElement, kind, 'math.max(', length: length), | |
| 457 _expectId(barElement, kind, 'math.Random bar()', length: length), | |
| 315 ]; | 458 ]; |
| 316 return _verifyReferences(element, expected); | 459 await _verifyReferences(element, expected); |
| 317 } | 460 } |
| 318 | 461 |
| 319 Future test_searchReferences_LabelElement() { | 462 test_searchReferences_LabelElement() async { |
| 320 _indexTestUnit(''' | 463 _indexTestUnit(''' |
| 321 main() { | 464 main() { |
| 322 label: | 465 label: |
| 323 while (true) { | 466 while (true) { |
| 324 if (true) { | 467 if (true) { |
| 325 break label; // 1 | 468 break label; // 1 |
| 326 } | 469 } |
| 327 break label; // 2 | 470 break label; // 2 |
| 328 } | 471 } |
| 329 } | 472 } |
| 330 '''); | 473 '''); |
| 331 LabelElement element = findElement('label'); | 474 LabelElement element = findElement('label'); |
| 332 Element mainElement = findElement('main'); | 475 Element mainElement = findElement('main'); |
| 333 var expected = [ | 476 var expected = [ |
| 334 _expectId(mainElement, MatchKind.REFERENCE, 'label; // 1'), | 477 _expectId(mainElement, MatchKind.REFERENCE, 'label; // 1'), |
| 335 _expectId(mainElement, MatchKind.REFERENCE, 'label; // 2') | 478 _expectId(mainElement, MatchKind.REFERENCE, 'label; // 2') |
| 336 ]; | 479 ]; |
| 337 return _verifyReferences(element, expected); | 480 await _verifyReferences(element, expected); |
| 338 } | 481 } |
| 339 | 482 |
| 340 Future test_searchReferences_LibraryElement() { | 483 test_searchReferences_LibraryElement() async { |
| 341 var codeA = 'part of lib; // A'; | 484 var codeA = 'part of lib; // A'; |
| 342 var codeB = 'part of lib; // B'; | 485 var codeB = 'part of lib; // B'; |
| 343 addSource('/unitA.dart', codeA); | 486 addSource('/unitA.dart', codeA); |
| 344 addSource('/unitB.dart', codeB); | 487 addSource('/unitB.dart', codeB); |
| 345 _indexTestUnit(''' | 488 _indexTestUnit(''' |
| 346 library lib; | 489 library lib; |
| 347 part 'unitA.dart'; | 490 part 'unitA.dart'; |
| 348 part 'unitB.dart'; | 491 part 'unitB.dart'; |
| 349 '''); | 492 '''); |
| 350 LibraryElement element = testLibraryElement; | 493 LibraryElement element = testLibraryElement; |
| 351 CompilationUnitElement elementA = element.parts[0]; | 494 CompilationUnitElement unitElementA = element.parts[0]; |
| 352 CompilationUnitElement elementB = element.parts[1]; | 495 CompilationUnitElement unitElementB = element.parts[1]; |
| 353 index.index(context, elementA.computeNode()); | 496 index.indexUnit(unitElementA.computeNode()); |
| 354 index.index(context, elementB.computeNode()); | 497 index.indexUnit(unitElementB.computeNode()); |
| 355 var expected = [ | 498 var expected = [ |
| 356 new ExpectedMatch(elementA, MatchKind.REFERENCE, | 499 new ExpectedMatch(unitElementA, MatchKind.REFERENCE, |
| 357 codeA.indexOf('lib; // A'), 'lib'.length), | 500 codeA.indexOf('lib; // A'), 'lib'.length), |
| 358 new ExpectedMatch(elementB, MatchKind.REFERENCE, | 501 new ExpectedMatch(unitElementB, MatchKind.REFERENCE, |
| 359 codeB.indexOf('lib; // B'), 'lib'.length), | 502 codeB.indexOf('lib; // B'), 'lib'.length), |
| 360 ]; | 503 ]; |
| 361 return _verifyReferences(element, expected); | 504 await _verifyReferences(element, expected); |
| 362 } | 505 } |
| 363 | 506 |
| 364 Future test_searchReferences_LocalVariableElement() { | 507 test_searchReferences_LocalVariableElement() async { |
| 365 _indexTestUnit(''' | 508 _indexTestUnit(''' |
| 366 main() { | 509 main() { |
| 367 var v; | 510 var v; |
| 368 v = 1; | 511 v = 1; |
| 369 v += 2; | 512 v += 2; |
| 370 print(v); | 513 print(v); |
| 371 v(); | 514 v(); |
| 372 } | 515 } |
| 373 '''); | 516 '''); |
| 374 LocalVariableElement element = findElement('v'); | 517 LocalVariableElement element = findElement('v'); |
| 375 Element mainElement = findElement('main'); | 518 Element mainElement = findElement('main'); |
| 376 var expected = [ | 519 var expected = [ |
| 377 _expectId(mainElement, MatchKind.WRITE, 'v = 1;'), | 520 _expectId(mainElement, MatchKind.WRITE, 'v = 1;'), |
| 378 _expectId(mainElement, MatchKind.READ_WRITE, 'v += 2;'), | 521 _expectId(mainElement, MatchKind.READ_WRITE, 'v += 2;'), |
| 379 _expectId(mainElement, MatchKind.READ, 'v);'), | 522 _expectId(mainElement, MatchKind.READ, 'v);'), |
| 380 _expectId(mainElement, MatchKind.INVOCATION, 'v();') | 523 _expectId(mainElement, MatchKind.INVOCATION, 'v();') |
| 381 ]; | 524 ]; |
| 382 return _verifyReferences(element, expected); | 525 await _verifyReferences(element, expected); |
| 383 } | 526 } |
| 384 | 527 |
| 385 Future test_searchReferences_MethodElement() { | 528 test_searchReferences_MethodElement() async { |
| 386 _indexTestUnit(''' | 529 _indexTestUnit(''' |
| 387 class A { | 530 class A { |
| 388 m() {} | 531 m() {} |
| 389 main() { | 532 main() { |
| 390 m(); // 1 | 533 m(); // 1 |
| 391 this.m(); // 2 | 534 this.m(); // 2 |
| 392 print(m); // 3 | 535 print(m); // 3 |
| 393 print(this.m); // 4 | 536 print(this.m); // 4 |
| 394 } | 537 } |
| 395 } | 538 } |
| 396 '''); | 539 '''); |
| 397 MethodElement method = findElement('m'); | 540 MethodElement method = findElement('m'); |
| 398 Element mainElement = findElement('main'); | 541 Element mainElement = findElement('main'); |
| 399 var expected = [ | 542 var expected = [ |
| 400 _expectId(mainElement, MatchKind.INVOCATION, 'm(); // 1'), | 543 _expectId(mainElement, MatchKind.INVOCATION, 'm(); // 1'), |
| 401 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // 2'), | 544 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // 2'), |
| 402 _expectId(mainElement, MatchKind.REFERENCE, 'm); // 3'), | 545 _expectId(mainElement, MatchKind.REFERENCE, 'm); // 3'), |
| 403 _expectIdQ(mainElement, MatchKind.REFERENCE, 'm); // 4') | 546 _expectIdQ(mainElement, MatchKind.REFERENCE, 'm); // 4') |
| 404 ]; | 547 ]; |
| 405 return _verifyReferences(method, expected); | 548 await _verifyReferences(method, expected); |
| 406 } | 549 } |
| 407 | 550 |
| 408 Future test_searchReferences_MethodMember() { | 551 test_searchReferences_MethodMember() async { |
| 409 _indexTestUnit(''' | 552 _indexTestUnit(''' |
| 410 class A<T> { | 553 class A<T> { |
| 411 T m() => null; | 554 T m() => null; |
| 412 } | 555 } |
| 413 main(A<int> a) { | 556 main(A<int> a) { |
| 414 a.m(); // ref | 557 a.m(); // ref |
| 415 } | 558 } |
| 416 '''); | 559 '''); |
| 417 MethodMember method = findNodeElementAtString('m(); // ref'); | 560 MethodMember method = findNodeElementAtString('m(); // ref'); |
| 418 Element mainElement = findElement('main'); | 561 Element mainElement = findElement('main'); |
| 419 var expected = [ | 562 var expected = [ |
| 420 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // ref') | 563 _expectIdQ(mainElement, MatchKind.INVOCATION, 'm(); // ref') |
| 421 ]; | 564 ]; |
| 422 return _verifyReferences(method, expected); | 565 await _verifyReferences(method, expected); |
| 423 } | 566 } |
| 424 | 567 |
| 425 Future test_searchReferences_ParameterElement() { | 568 test_searchReferences_ParameterElement_ofLocalFunction() async { |
| 569 _indexTestUnit(''' | |
| 570 main() { | |
| 571 foo({p}) { | |
| 572 p = 1; | |
| 573 p += 2; | |
| 574 print(p); | |
| 575 p(); | |
| 576 } | |
| 577 foo(p: 42); | |
| 578 } | |
| 579 '''); | |
| 580 ParameterElement element = findElement('p'); | |
| 581 Element fooElement = findElement('foo'); | |
| 582 Element mainElement = findElement('main'); | |
| 583 var expected = [ | |
| 584 _expectId(fooElement, MatchKind.WRITE, 'p = 1;'), | |
| 585 _expectId(fooElement, MatchKind.READ_WRITE, 'p += 2;'), | |
| 586 _expectId(fooElement, MatchKind.READ, 'p);'), | |
| 587 _expectId(fooElement, MatchKind.INVOCATION, 'p();'), | |
| 588 _expectIdQ(mainElement, MatchKind.REFERENCE, 'p: 42') | |
| 589 ]; | |
| 590 await _verifyReferences(element, expected); | |
| 591 } | |
| 592 | |
| 593 test_searchReferences_ParameterElement_ofMethod() async { | |
| 594 _indexTestUnit(''' | |
| 595 class C { | |
| 596 foo({p}) { | |
| 597 p = 1; | |
| 598 p += 2; | |
| 599 print(p); | |
| 600 p(); | |
| 601 } | |
| 602 } | |
| 603 main(C c) { | |
| 604 c.foo(p: 42); | |
| 605 } | |
| 606 '''); | |
| 607 ParameterElement element = findElement('p'); | |
| 608 Element fooElement = findElement('foo'); | |
| 609 Element mainElement = findElement('main'); | |
| 610 var expected = [ | |
| 611 _expectId(fooElement, MatchKind.WRITE, 'p = 1;'), | |
| 612 _expectId(fooElement, MatchKind.READ_WRITE, 'p += 2;'), | |
| 613 _expectId(fooElement, MatchKind.READ, 'p);'), | |
| 614 _expectId(fooElement, MatchKind.INVOCATION, 'p();'), | |
| 615 _expectIdQ(mainElement, MatchKind.REFERENCE, 'p: 42') | |
| 616 ]; | |
| 617 await _verifyReferences(element, expected); | |
| 618 } | |
| 619 | |
| 620 test_searchReferences_ParameterElement_ofTopLevelFunction() async { | |
| 426 _indexTestUnit(''' | 621 _indexTestUnit(''' |
| 427 foo({p}) { | 622 foo({p}) { |
| 428 p = 1; | 623 p = 1; |
| 429 p += 2; | 624 p += 2; |
| 430 print(p); | 625 print(p); |
| 431 p(); | 626 p(); |
| 432 } | 627 } |
| 433 main() { | 628 main() { |
| 434 foo(p: 42); | 629 foo(p: 42); |
| 435 } | 630 } |
| 436 '''); | 631 '''); |
| 437 ParameterElement element = findElement('p'); | 632 ParameterElement element = findElement('p'); |
| 438 Element fooElement = findElement('foo'); | 633 Element fooElement = findElement('foo'); |
| 439 Element mainElement = findElement('main'); | 634 Element mainElement = findElement('main'); |
| 440 var expected = [ | 635 var expected = [ |
| 441 _expectId(fooElement, MatchKind.WRITE, 'p = 1;'), | 636 _expectId(fooElement, MatchKind.WRITE, 'p = 1;'), |
| 442 _expectId(fooElement, MatchKind.READ_WRITE, 'p += 2;'), | 637 _expectId(fooElement, MatchKind.READ_WRITE, 'p += 2;'), |
| 443 _expectId(fooElement, MatchKind.READ, 'p);'), | 638 _expectId(fooElement, MatchKind.READ, 'p);'), |
| 444 _expectId(fooElement, MatchKind.INVOCATION, 'p();'), | 639 _expectId(fooElement, MatchKind.INVOCATION, 'p();'), |
| 445 _expectId(mainElement, MatchKind.REFERENCE, 'p: 42') | 640 _expectIdQ(mainElement, MatchKind.REFERENCE, 'p: 42') |
| 446 ]; | 641 ]; |
| 447 return _verifyReferences(element, expected); | 642 await _verifyReferences(element, expected); |
| 448 } | 643 } |
| 449 | 644 |
| 450 Future test_searchReferences_PrefixElement() { | 645 test_searchReferences_PrefixElement() async { |
| 451 _indexTestUnit(''' | 646 _indexTestUnit(''' |
| 452 import 'dart:async' as ppp; | 647 import 'dart:async' as ppp; |
| 453 main() { | 648 main() { |
| 454 ppp.Future a; | 649 ppp.Future a; |
| 455 ppp.Stream b; | 650 ppp.Stream b; |
| 456 } | 651 } |
| 457 '''); | 652 '''); |
| 458 PrefixElement element = findNodeElementAtString('ppp;'); | 653 PrefixElement element = findNodeElementAtString('ppp;'); |
| 459 Element elementA = findElement('a'); | 654 Element elementA = findElement('a'); |
| 460 Element elementB = findElement('b'); | 655 Element elementB = findElement('b'); |
| 461 var expected = [ | 656 var expected = [ |
| 462 _expectId(elementA, MatchKind.REFERENCE, 'ppp.Future'), | 657 _expectId(elementA, MatchKind.REFERENCE, 'ppp.Future'), |
| 463 _expectId(elementB, MatchKind.REFERENCE, 'ppp.Stream') | 658 _expectId(elementB, MatchKind.REFERENCE, 'ppp.Stream') |
| 464 ]; | 659 ]; |
| 465 return _verifyReferences(element, expected); | 660 await _verifyReferences(element, expected); |
| 466 } | 661 } |
| 467 | 662 |
| 468 Future test_searchReferences_PropertyAccessorElement_getter() { | 663 test_searchReferences_PropertyAccessorElement_getter() async { |
| 469 _indexTestUnit(''' | 664 _indexTestUnit(''' |
| 470 class A { | 665 class A { |
| 471 get g => null; | 666 get ggg => null; |
| 472 main() { | 667 main() { |
| 473 g; // 1 | 668 print(ggg); // ref-nq |
| 474 this.g; // 2 | 669 print(this.ggg); // ref-q |
| 670 ggg(); // inv-nq | |
| 671 this.ggg(); // inv-q | |
| 475 } | 672 } |
| 476 } | 673 } |
| 477 '''); | 674 '''); |
| 478 PropertyAccessorElement element = findElement('g', ElementKind.GETTER); | 675 PropertyAccessorElement element = findElement('ggg', ElementKind.GETTER); |
| 479 Element mainElement = findElement('main'); | 676 Element main = findElement('main'); |
| 480 var expected = [ | 677 var expected = [ |
| 481 _expectId(mainElement, MatchKind.REFERENCE, 'g; // 1'), | 678 _expectId(main, MatchKind.REFERENCE, 'ggg); // ref-nq'), |
| 482 _expectIdQ(mainElement, MatchKind.REFERENCE, 'g; // 2') | 679 _expectIdQ(main, MatchKind.REFERENCE, 'ggg); // ref-q'), |
| 680 _expectId(main, MatchKind.INVOCATION, 'ggg(); // inv-nq'), | |
| 681 _expectIdQ(main, MatchKind.INVOCATION, 'ggg(); // inv-q'), | |
| 483 ]; | 682 ]; |
| 484 return _verifyReferences(element, expected); | 683 await _verifyReferences(element, expected); |
| 485 } | 684 } |
| 486 | 685 |
| 487 Future test_searchReferences_PropertyAccessorElement_setter() { | 686 test_searchReferences_PropertyAccessorElement_setter() async { |
| 488 _indexTestUnit(''' | 687 _indexTestUnit(''' |
| 489 class A { | 688 class A { |
| 490 set s(x) {} | 689 set s(x) {} |
| 491 main() { | 690 main() { |
| 492 s = 1; | 691 s = 1; |
| 493 this.s = 2; | 692 this.s = 2; |
| 494 } | 693 } |
| 495 } | 694 } |
| 496 '''); | 695 '''); |
| 497 PropertyAccessorElement element = findElement('s='); | 696 PropertyAccessorElement element = findElement('s='); |
| 498 Element mainElement = findElement('main'); | 697 Element mainElement = findElement('main'); |
| 499 var expected = [ | 698 var expected = [ |
| 500 _expectId(mainElement, MatchKind.REFERENCE, 's = 1'), | 699 _expectId(mainElement, MatchKind.REFERENCE, 's = 1'), |
| 501 _expectIdQ(mainElement, MatchKind.REFERENCE, 's = 2') | 700 _expectIdQ(mainElement, MatchKind.REFERENCE, 's = 2') |
| 502 ]; | 701 ]; |
| 503 return _verifyReferences(element, expected); | 702 await _verifyReferences(element, expected); |
| 504 } | 703 } |
| 505 | 704 |
| 506 Future test_searchReferences_TopLevelVariableElement() { | 705 test_searchReferences_TopLevelVariableElement() async { |
| 507 addSource( | 706 addSource( |
| 508 '/lib.dart', | 707 '/lib.dart', |
| 509 ''' | 708 ''' |
| 510 library lib; | 709 library lib; |
| 511 var V; | 710 var V; |
| 512 '''); | 711 '''); |
| 513 _indexTestUnit(''' | 712 _indexTestUnit(''' |
| 514 import 'lib.dart' show V; // imp | 713 import 'lib.dart' show V; // imp |
| 515 import 'lib.dart' as pref; | 714 import 'lib.dart' as pref; |
| 516 main() { | 715 main() { |
| 517 pref.V = 1; // q | 716 pref.V = 1; // q |
| 518 print(pref.V); // q | 717 print(pref.V); // q |
| 519 pref.V(); // q | 718 pref.V(); // q |
| 520 V = 1; // nq | 719 V = 1; // nq |
| 521 print(V); // nq | 720 print(V); // nq |
| 522 V(); // nq | 721 V(); // nq |
| 523 } | 722 } |
| 524 '''); | 723 '''); |
| 525 ImportElement importElement = testLibraryElement.imports[0]; | 724 ImportElement importElement = testLibraryElement.imports[0]; |
| 526 CompilationUnitElement impUnit = | 725 CompilationUnitElement impUnit = |
| 527 importElement.importedLibrary.definingCompilationUnit; | 726 importElement.importedLibrary.definingCompilationUnit; |
| 528 TopLevelVariableElement variable = impUnit.topLevelVariables[0]; | 727 TopLevelVariableElement variable = impUnit.topLevelVariables[0]; |
| 529 Element main = findElement('main'); | 728 Element main = findElement('main'); |
| 530 var expected = [ | 729 var expected = [ |
| 531 _expectId(testUnitElement, MatchKind.REFERENCE, 'V; // imp'), | 730 _expectIdQ(testUnitElement, MatchKind.REFERENCE, 'V; // imp'), |
| 532 _expectId(main, MatchKind.WRITE, 'V = 1; // q'), | 731 _expectIdQ(main, MatchKind.WRITE, 'V = 1; // q'), |
| 533 _expectId(main, MatchKind.READ, 'V); // q'), | 732 _expectIdQ(main, MatchKind.READ, 'V); // q'), |
| 534 _expectId(main, MatchKind.INVOCATION, 'V(); // q'), | 733 _expectIdQ(main, MatchKind.INVOCATION, 'V(); // q'), |
| 535 _expectId(main, MatchKind.WRITE, 'V = 1; // nq'), | 734 _expectId(main, MatchKind.WRITE, 'V = 1; // nq'), |
| 536 _expectId(main, MatchKind.READ, 'V); // nq'), | 735 _expectId(main, MatchKind.READ, 'V); // nq'), |
| 537 _expectId(main, MatchKind.INVOCATION, 'V(); // nq'), | 736 _expectId(main, MatchKind.INVOCATION, 'V(); // nq'), |
| 538 ]; | 737 ]; |
| 539 return _verifyReferences(variable, expected); | 738 await _verifyReferences(variable, expected); |
| 540 } | 739 } |
| 541 | 740 |
| 542 Future test_searchReferences_TypeParameterElement() { | 741 test_searchReferences_TypeParameterElement() async { |
| 543 _indexTestUnit(''' | 742 _indexTestUnit(''' |
| 544 class A<T> { | 743 class A<T> { |
| 545 main(T a, T b) {} | 744 main(T a, T b) {} |
| 546 } | 745 } |
| 547 '''); | 746 '''); |
| 548 TypeParameterElement element = findElement('T'); | 747 TypeParameterElement element = findElement('T'); |
| 549 Element aElement = findElement('a'); | 748 Element aElement = findElement('a'); |
| 550 Element bElement = findElement('b'); | 749 Element bElement = findElement('b'); |
| 551 var expected = [ | 750 var expected = [ |
| 552 _expectId(aElement, MatchKind.REFERENCE, 'T a'), | 751 _expectId(aElement, MatchKind.REFERENCE, 'T a'), |
| 553 _expectId(bElement, MatchKind.REFERENCE, 'T b') | 752 _expectId(bElement, MatchKind.REFERENCE, 'T b') |
| 554 ]; | 753 ]; |
| 555 return _verifyReferences(element, expected); | 754 await _verifyReferences(element, expected); |
| 556 } | 755 } |
| 557 | 756 |
| 558 Future test_searchSubtypes() { | 757 test_searchSubtypes() async { |
| 559 _indexTestUnit(''' | 758 _indexTestUnit(''' |
| 560 class T {} | 759 class T {} |
| 561 class A extends T {} // A | 760 class A extends T {} // A |
| 562 class B = Object with T; // B | 761 class B = Object with T; // B |
| 563 class C implements T {} // C | 762 class C implements T {} // C |
| 564 '''); | 763 '''); |
| 565 ClassElement element = findElement('T'); | 764 ClassElement element = findElement('T'); |
| 566 ClassElement elementA = findElement('A'); | 765 ClassElement elementA = findElement('A'); |
| 567 ClassElement elementB = findElement('B'); | 766 ClassElement elementB = findElement('B'); |
| 568 ClassElement elementC = findElement('C'); | 767 ClassElement elementC = findElement('C'); |
| 569 var expected = [ | 768 var expected = [ |
| 570 _expectId(elementA, MatchKind.REFERENCE, 'T {} // A'), | 769 _expectId(elementA, MatchKind.REFERENCE, 'T {} // A'), |
| 571 _expectId(elementB, MatchKind.REFERENCE, 'T; // B'), | 770 _expectId(elementB, MatchKind.REFERENCE, 'T; // B'), |
| 572 _expectId(elementC, MatchKind.REFERENCE, 'T {} // C') | 771 _expectId(elementC, MatchKind.REFERENCE, 'T {} // C') |
| 573 ]; | 772 ]; |
| 574 return searchEngine.searchSubtypes(element).then((matches) { | 773 List<SearchMatch> matches = await searchEngine.searchSubtypes(element); |
| 575 _assertMatches(matches, expected); | 774 _assertMatches(matches, expected); |
| 576 }); | |
| 577 } | 775 } |
| 578 | 776 |
| 579 Future test_searchTopLevelDeclarations() { | 777 test_searchTopLevelDeclarations() async { |
| 580 _indexTestUnit(''' | 778 _indexTestUnit(''' |
| 581 class A {} // A | 779 class A {} // A |
| 582 class B = Object with A; | 780 class B = Object with A; |
| 583 typedef C(); | 781 typedef C(); |
| 584 D() {} | 782 D() {} |
| 585 var E = null; | 783 var E = null; |
| 586 class NoMatchABCDE {} | 784 class NoMatchABCDE {} |
| 587 '''); | 785 '''); |
| 588 Element topA = findElement('A'); | 786 Element topA = findElement('A'); |
| 589 Element topB = findElement('B'); | 787 Element topB = findElement('B'); |
| 590 Element topC = findElement('C'); | 788 Element topC = findElement('C'); |
| 591 Element topD = findElement('D'); | 789 Element topD = findElement('D'); |
| 592 Element topE = findElement('E'); | 790 Element topE = findElement('E'); |
| 593 var expected = [ | 791 var expected = [ |
| 594 _expectId(topA, MatchKind.DECLARATION, 'A {} // A'), | 792 _expectId(topA, MatchKind.DECLARATION, 'A {} // A'), |
| 595 _expectId(topB, MatchKind.DECLARATION, 'B ='), | 793 _expectId(topB, MatchKind.DECLARATION, 'B ='), |
| 596 _expectId(topC, MatchKind.DECLARATION, 'C()'), | 794 _expectId(topC, MatchKind.DECLARATION, 'C()'), |
| 597 _expectId(topD, MatchKind.DECLARATION, 'D() {}'), | 795 _expectId(topD, MatchKind.DECLARATION, 'D() {}'), |
| 598 _expectId(topE, MatchKind.DECLARATION, 'E = null') | 796 _expectId(topE, MatchKind.DECLARATION, 'E = null') |
| 599 ]; | 797 ]; |
| 600 return _verifyTopLevelDeclarations('^[A-E]\$', expected); | 798 List<SearchMatch> matches = |
| 799 await searchEngine.searchTopLevelDeclarations(r'^[A-E]$'); | |
| 800 _assertMatches(matches, expected); | |
| 601 } | 801 } |
| 602 | 802 |
| 603 ExpectedMatch _expectId(Element element, MatchKind kind, String search, | 803 ExpectedMatch _expectId(Element element, MatchKind kind, String search, |
| 604 {int length, bool isResolved: true, bool isQualified: false}) { | 804 {int length, bool isResolved: true, bool isQualified: false}) { |
| 605 int offset = findOffset(search); | 805 int offset = findOffset(search); |
| 606 if (length == null) { | 806 if (length == null) { |
| 607 length = getLeadingIdentifierLength(search); | 807 length = getLeadingIdentifierLength(search); |
| 608 } | 808 } |
| 609 return new ExpectedMatch(element, kind, offset, length, | 809 return new ExpectedMatch(element, kind, offset, length, |
| 610 isResolved: isResolved, isQualified: isQualified); | 810 isResolved: isResolved, isQualified: isQualified); |
| 611 } | 811 } |
| 612 | 812 |
| 613 ExpectedMatch _expectIdQ(Element element, MatchKind kind, String search) { | 813 /** |
| 614 return _expectId(element, kind, search, isQualified: true); | 814 * Create [ExpectedMatch] for a qualified and resolved match. |
| 815 */ | |
| 816 ExpectedMatch _expectIdQ(Element element, MatchKind kind, String search, | |
| 817 {int length, bool isResolved: true}) { | |
| 818 return _expectId(element, kind, search, isQualified: true, length: length); | |
| 615 } | 819 } |
| 616 | 820 |
| 617 ExpectedMatch _expectIdU(Element element, MatchKind kind, String search) { | 821 /** |
| 822 * Create [ExpectedMatch] for a qualified and unresolved match. | |
| 823 */ | |
| 824 ExpectedMatch _expectIdQU(Element element, MatchKind kind, String search, | |
| 825 {int length}) { | |
| 618 return _expectId(element, kind, search, | 826 return _expectId(element, kind, search, |
| 619 isQualified: true, isResolved: false); | 827 isQualified: true, isResolved: false, length: length); |
| 828 } | |
| 829 | |
| 830 /** | |
| 831 * Create [ExpectedMatch] for a unqualified and unresolved match. | |
| 832 */ | |
| 833 ExpectedMatch _expectIdU(Element element, MatchKind kind, String search, | |
| 834 {int length}) { | |
| 835 return _expectId(element, kind, search, | |
| 836 isQualified: false, isResolved: false, length: length); | |
| 620 } | 837 } |
| 621 | 838 |
| 622 void _indexTestUnit(String code) { | 839 void _indexTestUnit(String code) { |
| 623 resolveTestUnit(code); | 840 resolveTestUnit(code); |
| 624 index.index(context, testUnit); | 841 index.indexUnit(testUnit); |
| 625 } | 842 } |
| 626 | 843 |
| 627 Future _verifyReferences( | 844 Future _verifyReferences( |
| 628 Element element, List<ExpectedMatch> expectedMatches) { | 845 Element element, List<ExpectedMatch> expectedMatches) async { |
| 629 return searchEngine | 846 List<SearchMatch> matches = await searchEngine.searchReferences(element); |
| 630 .searchReferences(element) | 847 _assertMatches(matches, expectedMatches); |
| 631 .then((List<SearchMatch> matches) { | 848 expect(matches, hasLength(expectedMatches.length)); |
| 632 _assertMatches(matches, expectedMatches); | |
| 633 }); | |
| 634 } | |
| 635 | |
| 636 Future _verifyTopLevelDeclarations( | |
| 637 String pattern, List<ExpectedMatch> expectedMatches) { | |
| 638 return searchEngine | |
| 639 .searchTopLevelDeclarations(pattern) | |
| 640 .then((List<SearchMatch> matches) { | |
| 641 _assertMatches(matches, expectedMatches); | |
| 642 }); | |
| 643 } | 849 } |
| 644 | 850 |
| 645 static void _assertMatches( | 851 static void _assertMatches( |
| 646 List<SearchMatch> matches, List<ExpectedMatch> expectedMatches) { | 852 List<SearchMatch> matches, List<ExpectedMatch> expectedMatches) { |
| 647 expect(matches, unorderedEquals(expectedMatches)); | 853 expect(matches, unorderedEquals(expectedMatches)); |
| 648 } | 854 } |
| 649 } | 855 } |
| OLD | NEW |