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

Side by Side Diff: pkg/analysis_server/test/analysis_notification_navigation_test.dart

Issue 406583002: Use futures uniformly rather than a mix of futures and CPS in tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.domain.analysis.notification.navigation; 5 library test.domain.analysis.notification.navigation;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/analysis_server.dart'; 9 import 'package:analysis_server/src/analysis_server.dart';
10 import 'package:analysis_server/src/computer/element.dart'; 10 import 'package:analysis_server/src/computer/element.dart';
(...skipping 19 matching lines...) Expand all
30 List<Element> testTargets; 30 List<Element> testTargets;
31 Element testTarget; 31 Element testTarget;
32 32
33 /** 33 /**
34 * Validates that there is a target in [testTargets] with [file], at [offset] 34 * Validates that there is a target in [testTargets] with [file], at [offset]
35 * and with the given [length]. 35 * and with the given [length].
36 */ 36 */
37 void assertHasFileTarget(String file, int offset, int length) { 37 void assertHasFileTarget(String file, int offset, int length) {
38 for (Element target in testTargets) { 38 for (Element target in testTargets) {
39 Location location = target.location; 39 Location location = target.location;
40 if (location.file == file && location.offset == offset && location.length == 40 if (location.file == file &&
41 length) { 41 location.offset == offset &&
42 location.length == length) {
42 testTarget = target; 43 testTarget = target;
43 return; 44 return;
44 } 45 }
45 } 46 }
46 fail( 47 fail(
47 'Expected to find target (file=$file; offset=$offset; length=$length) in \n' 48 'Expected to find target (file=$file; offset=$offset; length=$length) in \n'
48 '${testRegion} in\n' '${regions.join('\n')}'); 49 '${testRegion} in\n' '${regions.join('\n')}');
49 } 50 }
50 51
51 void assertHasOperatorRegion(String regionSearch, int regionLength, 52 void assertHasOperatorRegion(String regionSearch, int regionLength,
52 String targetSearch, int targetLength) { 53 String targetSearch, int targetLength) {
53 assertHasRegion(regionSearch, regionLength); 54 assertHasRegion(regionSearch, regionLength);
54 assertHasTarget(targetSearch, targetLength); 55 assertHasTarget(targetSearch, targetLength);
55 } 56 }
56 57
57 /** 58 /**
58 * Validates that there is a region at the offset of [search] in [testFile]. 59 * Validates that there is a region at the offset of [search] in [testFile].
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 * If [length] is `-1`, then it is ignored. 131 * If [length] is `-1`, then it is ignored.
131 * 132 *
132 * If [exists] is `true`, then fails if such region does not exist. 133 * If [exists] is `true`, then fails if such region does not exist.
133 * Otherwise remembers this it into [testRegion]. 134 * Otherwise remembers this it into [testRegion].
134 * Also fills [testTargets] with its targets. 135 * Also fills [testTargets] with its targets.
135 * 136 *
136 * If [exists] is `false`, then fails if such region exists. 137 * If [exists] is `false`, then fails if such region exists.
137 */ 138 */
138 void findRegion(int offset, int length, [bool exists]) { 139 void findRegion(int offset, int length, [bool exists]) {
139 for (NavigationRegion region in regions) { 140 for (NavigationRegion region in regions) {
140 if (region.offset == offset && (length == -1 || region.length == length)) 141 if (region.offset == offset &&
141 { 142 (length == -1 || region.length == length)) {
142 if (exists == false) { 143 if (exists == false) {
143 fail('Not expected to find (offset=$offset; length=$length) in\n' 144 fail(
144 '${regions.join('\n')}'); 145 'Not expected to find (offset=$offset; length=$length) in\n'
146 '${regions.join('\n')}');
145 } 147 }
146 testRegion = region; 148 testRegion = region;
147 testTargets = region.targets; 149 testTargets = region.targets;
148 return; 150 return;
149 } 151 }
150 } 152 }
151 if (exists == true) { 153 if (exists == true) {
152 fail('Expected to find (offset=$offset; length=$length) in\n' 154 fail(
153 '${regions.join('\n')}'); 155 'Expected to find (offset=$offset; length=$length) in\n'
156 '${regions.join('\n')}');
154 } 157 }
155 } 158 }
156 159
157 Future prepareNavigation(then()) { 160 Future prepareNavigation() {
158 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile); 161 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile);
159 return waitForTasksFinished().then((_) { 162 return waitForTasksFinished();
160 then();
161 });
162 } 163 }
163 164
164 void processNotification(Notification notification) { 165 void processNotification(Notification notification) {
165 if (notification.event == ANALYSIS_NAVIGATION) { 166 if (notification.event == ANALYSIS_NAVIGATION) {
166 String file = notification.getParameter(FILE); 167 String file = notification.getParameter(FILE);
167 if (file == testFile) { 168 if (file == testFile) {
168 regions = <NavigationRegion>[]; 169 regions = <NavigationRegion>[];
169 List<Map<String, Object>> regionsJson = notification.getParameter( 170 List<Map<String, Object>> regionsJson =
170 REGIONS); 171 notification.getParameter(REGIONS);
171 for (Map<String, Object> regionJson in regionsJson) { 172 for (Map<String, Object> regionJson in regionsJson) {
172 var regionOffset = regionJson[OFFSET]; 173 var regionOffset = regionJson[OFFSET];
173 var regionLength = regionJson[LENGTH]; 174 var regionLength = regionJson[LENGTH];
174 List<Element> targets = <Element>[]; 175 List<Element> targets = <Element>[];
175 for (Map<String, Object> targetJson in regionJson[TARGETS]) { 176 for (Map<String, Object> targetJson in regionJson[TARGETS]) {
176 targets.add(new Element.fromJson(targetJson)); 177 targets.add(new Element.fromJson(targetJson));
177 } 178 }
178 var region = new NavigationRegion(regionOffset, regionLength, 179 var region =
179 targets); 180 new NavigationRegion(regionOffset, regionLength, targets);
180 regions.add(region); 181 regions.add(region);
181 } 182 }
182 } 183 }
183 } 184 }
184 } 185 }
185 186
186 @override 187 @override
187 void setUp() { 188 void setUp() {
188 super.setUp(); 189 super.setUp();
189 createProject(); 190 createProject();
190 } 191 }
191 192
192 test_afterAnalysis() { 193 test_afterAnalysis() {
193 addTestFile(''' 194 addTestFile('''
194 class AAA {} 195 class AAA {}
195 AAA aaa; 196 AAA aaa;
196 '''); 197 ''');
197 return waitForTasksFinished().then((_) { 198 return waitForTasksFinished().then((_) {
198 return prepareNavigation(() { 199 return prepareNavigation().then((_) {
199 assertHasRegionTarget('AAA aaa;', 'AAA {}'); 200 assertHasRegionTarget('AAA aaa;', 'AAA {}');
200 }); 201 });
201 }); 202 });
202 } 203 }
203 204
204 test_constructor_named() { 205 test_constructor_named() {
205 addTestFile(''' 206 addTestFile('''
206 class A { 207 class A {
207 A.named(BBB p) {} 208 A.named(BBB p) {}
208 } 209 }
209 class BBB {} 210 class BBB {}
210 '''); 211 ''');
211 return prepareNavigation(() { 212 return prepareNavigation().then((_) {
212 // has region for complete "A.named" 213 // has region for complete "A.named"
213 assertHasRegionString('A.named'); 214 assertHasRegionString('A.named');
214 assertHasTarget('named(BBB'); 215 assertHasTarget('named(BBB');
215 // no separate regions for "A" and "named" 216 // no separate regions for "A" and "named"
216 assertNoRegion('A.named(', 'A'.length); 217 assertNoRegion('A.named(', 'A'.length);
217 assertNoRegion('named(', 'named'.length); 218 assertNoRegion('named(', 'named'.length);
218 // validate that we don't forget to resolve parameters 219 // validate that we don't forget to resolve parameters
219 assertHasRegionTarget('BBB p', 'BBB {}'); 220 assertHasRegionTarget('BBB p', 'BBB {}');
220 }); 221 });
221 } 222 }
222 223
223 test_constructor_unnamed() { 224 test_constructor_unnamed() {
224 addTestFile(''' 225 addTestFile('''
225 class A { 226 class A {
226 A(BBB p) {} 227 A(BBB p) {}
227 } 228 }
228 class BBB {} 229 class BBB {}
229 '''); 230 ''');
230 return prepareNavigation(() { 231 return prepareNavigation().then((_) {
231 // has region for complete "A.named" 232 // has region for complete "A.named"
232 assertHasRegion("A(BBB"); 233 assertHasRegion("A(BBB");
233 assertHasTarget("A(BBB", 0); 234 assertHasTarget("A(BBB", 0);
234 // validate that we don't forget to resolve parameters 235 // validate that we don't forget to resolve parameters
235 assertHasRegionTarget('BBB p', 'BBB {}'); 236 assertHasRegionTarget('BBB p', 'BBB {}');
236 }); 237 });
237 } 238 }
238 239
239 test_fieldFormalParameter() { 240 test_fieldFormalParameter() {
240 addTestFile(''' 241 addTestFile('''
241 class AAA { 242 class AAA {
242 int fff = 123; 243 int fff = 123;
243 AAA(this.fff); 244 AAA(this.fff);
244 } 245 }
245 '''); 246 ''');
246 return prepareNavigation(() { 247 return prepareNavigation().then((_) {
247 assertHasRegionTarget('fff);', 'fff = 123'); 248 assertHasRegionTarget('fff);', 'fff = 123');
248 }); 249 });
249 } 250 }
250 251
251 test_identifier_resolved() { 252 test_identifier_resolved() {
252 addTestFile(''' 253 addTestFile('''
253 class AAA {} 254 class AAA {}
254 main() { 255 main() {
255 AAA aaa = null; 256 AAA aaa = null;
256 print(aaa); 257 print(aaa);
257 } 258 }
258 '''); 259 ''');
259 return prepareNavigation(() { 260 return prepareNavigation().then((_) {
260 assertHasRegionTarget('AAA aaa', 'AAA {}'); 261 assertHasRegionTarget('AAA aaa', 'AAA {}');
261 assertHasRegionTarget('aaa);', 'aaa = null'); 262 assertHasRegionTarget('aaa);', 'aaa = null');
262 assertHasRegionTarget('main() {', 'main() {'); 263 assertHasRegionTarget('main() {', 'main() {');
263 }); 264 });
264 } 265 }
265 266
266 test_identifier_unresolved() { 267 test_identifier_unresolved() {
267 addTestFile(''' 268 addTestFile('''
268 main() { 269 main() {
269 print(vvv); 270 print(vvv);
270 } 271 }
271 '''); 272 ''');
272 return prepareNavigation(() { 273 return prepareNavigation().then((_) {
273 assertNoRegionString('vvv'); 274 assertNoRegionString('vvv');
274 }); 275 });
275 } 276 }
276 277
277 test_instanceCreation_implicit() { 278 test_instanceCreation_implicit() {
278 addTestFile(''' 279 addTestFile('''
279 class A { 280 class A {
280 } 281 }
281 main() { 282 main() {
282 new A(); 283 new A();
283 } 284 }
284 '''); 285 ''');
285 return prepareNavigation(() { 286 return prepareNavigation().then((_) {
286 assertHasRegionString('new A'); 287 assertHasRegionString('new A');
287 assertHasTarget('A {'); 288 assertHasTarget('A {');
288 }); 289 });
289 } 290 }
290 291
291 test_instanceCreation_named() { 292 test_instanceCreation_named() {
292 addTestFile(''' 293 addTestFile('''
293 class A { 294 class A {
294 A.named() {} 295 A.named() {}
295 } 296 }
296 main() { 297 main() {
297 new A.named(); 298 new A.named();
298 } 299 }
299 '''); 300 ''');
300 return prepareNavigation(() { 301 return prepareNavigation().then((_) {
301 assertHasRegionString('new A.named'); 302 assertHasRegionString('new A.named');
302 assertHasTarget('named() {}'); 303 assertHasTarget('named() {}');
303 }); 304 });
304 } 305 }
305 306
306 test_instanceCreation_unnamed() { 307 test_instanceCreation_unnamed() {
307 addTestFile(''' 308 addTestFile('''
308 class A { 309 class A {
309 A() {} 310 A() {}
310 } 311 }
311 main() { 312 main() {
312 new A(); 313 new A();
313 } 314 }
314 '''); 315 ''');
315 return prepareNavigation(() { 316 return prepareNavigation().then((_) {
316 assertHasRegionString('new A'); 317 assertHasRegionString('new A');
317 assertHasTarget("A() {}", 0); 318 assertHasTarget("A() {}", 0);
318 }); 319 });
319 } 320 }
320 321
321 test_operator_arithmetic() { 322 test_operator_arithmetic() {
322 addTestFile(''' 323 addTestFile('''
323 class A { 324 class A {
324 A operator +(other) => null; 325 A operator +(other) => null;
325 A operator -() => null; 326 A operator -() => null;
326 A operator -(other) => null; 327 A operator -(other) => null;
327 A operator *(other) => null; 328 A operator *(other) => null;
328 A operator /(other) => null; 329 A operator /(other) => null;
329 } 330 }
330 main() { 331 main() {
331 var a = new A(); 332 var a = new A();
332 a - 1; 333 a - 1;
333 a + 2; 334 a + 2;
334 -a; // unary 335 -a; // unary
335 --a; 336 --a;
336 ++a; 337 ++a;
337 a--; // mm 338 a--; // mm
338 a++; // pp 339 a++; // pp
339 a -= 3; 340 a -= 3;
340 a += 4; 341 a += 4;
341 a *= 5; 342 a *= 5;
342 a /= 6; 343 a /= 6;
343 } 344 }
344 '''); 345 ''');
345 return prepareNavigation(() { 346 return prepareNavigation().then((_) {
346 assertHasOperatorRegion('- 1', 1, '-(other) => null', 1); 347 assertHasOperatorRegion('- 1', 1, '-(other) => null', 1);
347 assertHasOperatorRegion('+ 2', 1, '+(other) => null', 1); 348 assertHasOperatorRegion('+ 2', 1, '+(other) => null', 1);
348 assertHasOperatorRegion('-a; // unary', 1, '-() => null', 1); 349 assertHasOperatorRegion('-a; // unary', 1, '-() => null', 1);
349 assertHasOperatorRegion('--a;', 2, '-(other) => null', 1); 350 assertHasOperatorRegion('--a;', 2, '-(other) => null', 1);
350 assertHasOperatorRegion('++a;', 2, '+(other) => null', 1); 351 assertHasOperatorRegion('++a;', 2, '+(other) => null', 1);
351 assertHasOperatorRegion('--; // mm', 2, '-(other) => null', 1); 352 assertHasOperatorRegion('--; // mm', 2, '-(other) => null', 1);
352 assertHasOperatorRegion('++; // pp', 2, '+(other) => null', 1); 353 assertHasOperatorRegion('++; // pp', 2, '+(other) => null', 1);
353 assertHasOperatorRegion('-= 3', 2, '-(other) => null', 1); 354 assertHasOperatorRegion('-= 3', 2, '-(other) => null', 1);
354 assertHasOperatorRegion('+= 4', 2, '+(other) => null', 1); 355 assertHasOperatorRegion('+= 4', 2, '+(other) => null', 1);
355 assertHasOperatorRegion('*= 5', 2, '*(other) => null', 1); 356 assertHasOperatorRegion('*= 5', 2, '*(other) => null', 1);
(...skipping 10 matching lines...) Expand all
366 A operator [](index) => null; 367 A operator [](index) => null;
367 operator []=(index, A value) {} 368 operator []=(index, A value) {}
368 } 369 }
369 main() { 370 main() {
370 var b = new B(); 371 var b = new B();
371 b[0] // []; 372 b[0] // [];
372 b[1] = 1; // []=; 373 b[1] = 1; // []=;
373 b[2] += 2; 374 b[2] += 2;
374 } 375 }
375 '''); 376 ''');
376 return prepareNavigation(() { 377 return prepareNavigation().then((_) {
377 assertHasOperatorRegion('] // []', 1, '[](index)', 2); 378 assertHasOperatorRegion('] // []', 1, '[](index)', 2);
378 assertHasOperatorRegion('] = 1;', 1, '[]=(index,', 3); 379 assertHasOperatorRegion('] = 1;', 1, '[]=(index,', 3);
379 assertHasOperatorRegion('] += 2;', 1, '[]=(index,', 3); 380 assertHasOperatorRegion('] += 2;', 1, '[]=(index,', 3);
380 assertHasOperatorRegion('+= 2;', 2, '+(other)', 1); 381 assertHasOperatorRegion('+= 2;', 2, '+(other)', 1);
381 }); 382 });
382 } 383 }
383 384
384 test_partOf() { 385 test_partOf() {
385 var libCode = 'library lib; part "test.dart";'; 386 var libCode = 'library lib; part "test.dart";';
386 var libFile = addFile('$projectPath/bin/lib.dart', libCode); 387 var libFile = addFile('$projectPath/bin/lib.dart', libCode);
387 addTestFile('part of lib;'); 388 addTestFile('part of lib;');
388 return prepareNavigation(() { 389 return prepareNavigation().then((_) {
389 assertHasRegionString('part of lib'); 390 assertHasRegionString('part of lib');
390 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length); 391 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
391 }); 392 });
392 } 393 }
393 394
394 test_string_export() { 395 test_string_export() {
395 var libCode = 'library lib;'; 396 var libCode = 'library lib;';
396 var libFile = addFile('$projectPath/bin/lib.dart', libCode); 397 var libFile = addFile('$projectPath/bin/lib.dart', libCode);
397 addTestFile('export "lib.dart";'); 398 addTestFile('export "lib.dart";');
398 return prepareNavigation(() { 399 return prepareNavigation().then((_) {
399 assertHasRegionString('export "lib.dart"'); 400 assertHasRegionString('export "lib.dart"');
400 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length); 401 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
401 }); 402 });
402 } 403 }
403 404
404 test_string_export_unresolvedUri() { 405 test_string_export_unresolvedUri() {
405 addTestFile('export "no.dart";'); 406 addTestFile('export "no.dart";');
406 return prepareNavigation(() { 407 return prepareNavigation().then((_) {
407 assertNoRegionString('export "no.dart"'); 408 assertNoRegionString('export "no.dart"');
408 }); 409 });
409 } 410 }
410 411
411 test_string_import() { 412 test_string_import() {
412 var libCode = 'library lib;'; 413 var libCode = 'library lib;';
413 var libFile = addFile('$projectPath/bin/lib.dart', libCode); 414 var libFile = addFile('$projectPath/bin/lib.dart', libCode);
414 addTestFile('import "lib.dart";'); 415 addTestFile('import "lib.dart";');
415 return prepareNavigation(() { 416 return prepareNavigation().then((_) {
416 assertHasRegionString('import "lib.dart"'); 417 assertHasRegionString('import "lib.dart"');
417 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length); 418 assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
418 }); 419 });
419 } 420 }
420 421
421 test_string_import_noUri() { 422 test_string_import_noUri() {
422 addTestFile('import ;'); 423 addTestFile('import ;');
423 return prepareNavigation(() { 424 return prepareNavigation().then((_) {
424 assertNoRegionAt('import ;'); 425 assertNoRegionAt('import ;');
425 }); 426 });
426 } 427 }
427 428
428 test_string_import_unresolvedUri() { 429 test_string_import_unresolvedUri() {
429 addTestFile('import "no.dart";'); 430 addTestFile('import "no.dart";');
430 return prepareNavigation(() { 431 return prepareNavigation().then((_) {
431 assertNoRegionString('import "no.dart"'); 432 assertNoRegionString('import "no.dart"');
432 }); 433 });
433 } 434 }
434 435
435 test_string_part() { 436 test_string_part() {
436 var unitCode = 'part of lib; f() {}'; 437 var unitCode = 'part of lib; f() {}';
437 var unitFile = addFile('$projectPath/bin/test_unit.dart', unitCode); 438 var unitFile = addFile('$projectPath/bin/test_unit.dart', unitCode);
438 addTestFile(''' 439 addTestFile('''
439 library lib; 440 library lib;
440 part "test_unit.dart"; 441 part "test_unit.dart";
441 '''); 442 ''');
442 return prepareNavigation(() { 443 return prepareNavigation().then((_) {
443 assertHasRegionString('part "test_unit.dart"'); 444 assertHasRegionString('part "test_unit.dart"');
444 assertHasFileTarget(unitFile, 0, 0); 445 assertHasFileTarget(unitFile, 0, 0);
445 }); 446 });
446 } 447 }
447 448
448 test_string_part_unresolvedUri() { 449 test_string_part_unresolvedUri() {
449 addTestFile(''' 450 addTestFile('''
450 library lib; 451 library lib;
451 part "test_unit.dart"; 452 part "test_unit.dart";
452 '''); 453 ''');
453 return prepareNavigation(() { 454 return prepareNavigation().then((_) {
454 assertNoRegionString('part "test_unit.dart"'); 455 assertNoRegionString('part "test_unit.dart"');
455 }); 456 });
456 } 457 }
457 458
458 test_targetElement() { 459 test_targetElement() {
459 addTestFile(''' 460 addTestFile('''
460 class AAA {} 461 class AAA {}
461 main() { 462 main() {
462 AAA aaa = null; 463 AAA aaa = null;
463 } 464 }
464 '''); 465 ''');
465 return prepareNavigation(() { 466 return prepareNavigation().then((_) {
466 assertHasRegionTarget('AAA aaa', 'AAA {}'); 467 assertHasRegionTarget('AAA aaa', 'AAA {}');
467 expect(testTarget.kind, ElementKind.CLASS); 468 expect(testTarget.kind, ElementKind.CLASS);
468 expect(testTarget.name, 'AAA'); 469 expect(testTarget.name, 'AAA');
469 expect(testTarget.isAbstract, false); 470 expect(testTarget.isAbstract, false);
470 expect(testTarget.parameters, isNull); 471 expect(testTarget.parameters, isNull);
471 expect(testTarget.returnType, isNull); 472 expect(testTarget.returnType, isNull);
472 }); 473 });
473 } 474 }
474 } 475 }
475 476
476 477
477 class NavigationRegion { 478 class NavigationRegion {
478 final int offset; 479 final int offset;
479 final int length; 480 final int length;
480 final List<Element> targets; 481 final List<Element> targets;
481 482
482 NavigationRegion(this.offset, this.length, this.targets); 483 NavigationRegion(this.offset, this.length, this.targets);
483 484
484 @override 485 @override
485 String toString() { 486 String toString() {
486 return 'NavigationRegion(offset=$offset; length=$length; targets=$targets'; 487 return 'NavigationRegion(offset=$offset; length=$length; targets=$targets';
487 } 488 }
488 } 489 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698