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