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

Side by Side Diff: pkg/scheduled_test/test/scheduled_test_test.dart

Issue 12209073: Add a scheduled test library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make everything play nicely with the outside world. Created 7 years, 10 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
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 library scheduled_test_test;
6
7 import 'dart:async';
8
9 import 'package:scheduled_test/scheduled_test.dart';
10 import 'package:scheduled_test/src/utils.dart';
11 import 'metatest.dart';
12
13 void main() {
14 expectTestsPass('a scheduled test with a correct synchronous expectation '
15 'should pass', () {
16 test('test', () {
17 expect('foo', equals('foo'));
18 });
19 });
20
21 expectTestsFail('a scheduled test with an incorrect synchronous expectation '
22 'should fail', () {
23 test('test', () {
24 expect('foo', equals('bar'));
25 });
26 });
27
28 expectTestsPass('a scheduled test with a correct asynchronous expectation '
29 'should pass', () {
30 test('test', () {
31 expect(new Future.immediate('foo'), completion(equals('foo')));
32 });
33 });
34
35 expectTestsFail('a scheduled test with an incorrect asynchronous expectation '
36 'should fail', () {
37 test('test', () {
38 expect(new Future.immediate('foo'), completion(equals('bar')));
39 });
40 });
41
42 expectTestsPass('a passing scheduled synchronous expect should register', () {
43 test('test', () {
44 schedule(() => expect('foo', equals('foo')));
45 });
46 });
47
48 expectTestsFail('a failing scheduled synchronous expect should register', () {
49 test('test', () {
50 schedule(() => expect('foo', equals('bar')));
51 });
52 });
53
54 expectTestsPass('a passing scheduled asynchronous expect should '
55 'register', () {
56 test('test', () {
57 schedule(() =>
58 expect(new Future.immediate('foo'), completion(equals('foo'))));
59 });
60 });
61
62 expectTestsFail('a failing scheduled synchronous expect should '
63 'register', () {
64 test('test', () {
65 schedule(() =>
66 expect(new Future.immediate('foo'), completion(equals('bar'))));
67 });
68 });
69
70 expectTestsPass('scheduled blocks should be run in order after the '
71 'synchronous setup', () {
72 test('test', () {
73 var list = [1];
74 schedule(() => list.add(2));
75 list.add(3);
76 schedule(() => expect(list, equals([1, 3, 4, 2])));
77 list.add(4);
78 });
79 });
80
81 expectTestsPass('scheduled blocks should forward their return values as '
82 'Futures', () {
83 test('synchronous value', () {
84 var future = schedule(() => 'value');
85 expect(future, completion(equals('value')));
86 });
87
88 test('asynchronous value', () {
89 var future = schedule(() => new Future.immediate('value'));
90 expect(future, completion(equals('value')));
91 });
92 });
93
94 expectTestsPass('scheduled blocks should wait for their Future return values '
95 'to complete before proceeding', () {
96 test('test', () {
97 var value = 'unset';
98 schedule(() => sleep(500).then((_) {
99 value = 'set';
100 }));
101 schedule(() => expect(value, equals('set')));
102 });
103 });
104
105 expectTestsFail('a test failure in a chained future in a scheduled block '
106 'should be registered', () {
107 test('test', () {
108 schedule(() => new Future.immediate('foo')
109 .then((v) => expect(v, equals('bar'))));
110 });
111 });
112
113 expectTestsFail('an error in a chained future in a scheduled block should be '
114 'registered', () {
115 test('test', () {
116 schedule(() => new Future.immediate(null).then((_) {
117 throw 'error';
118 }));
119 });
120 });
121
122 expectTestsFail('an out-of-band failure in wrapAsync is handled', () {
123 test('test', () {
124 schedule(() {
125 sleep(10).then(wrapAsync((_) => expect('foo', equals('bar'))));
126 });
127 schedule(() => sleep(50));
128 });
129 });
130
131 expectTestsFail('an out-of-band failure in wrapAsync that finishes after the '
132 'schedule is handled', () {
133 test('test', () {
134 schedule(() {
135 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar'))));
136 });
137 schedule(() => sleep(10));
138 });
139 });
140
141 expectTestsFail('an out-of-band error reported via signalError is '
142 'handled', () {
143 test('test', () {
144 schedule(() {
145 sleep(10).then((_) => currentSchedule.signalError('bad'));
146 });
147 schedule(() => sleep(50));
148 });
149 });
150
151 expectTestsFail('an out-of-band error reported via signalError that finished '
152 'after the schedule is handled', () {
153 test('test', () {
154 schedule(() {
155 var done = wrapAsync((_) {});
156 sleep(50).then((_) {
157 currentSchedule.signalError('bad');
158 done(null);
159 });
160 });
161 schedule(() => sleep(10));
162 });
163 });
164
165 expectTestsFail('a synchronous error reported via signalError is handled', () {
166 test('test', () {
167 currentSchedule.signalError('bad');
168 });
169 });
170
171 expectTestsPass('the onComplete queue is run if a test is successful', () {
172 var onCompleteRun = false;
173 test('test 1', () {
174 currentSchedule.onComplete.schedule(() {
175 onCompleteRun = true;
176 });
177
178 schedule(() => expect('foo', equals('foo')));
179 });
180
181 test('test 2', () {
182 expect(onCompleteRun, isTrue);
183 });
184 });
185
186 expectTestsPass('the onComplete queue is run after an out-of-band callback',
187 () {
188 var outOfBandRun = false;
189 test('test1', () {
190 currentSchedule.onComplete.schedule(() {
191 expect(outOfBandRun, isTrue);
192 });
193
194 sleep(50).then(wrapAsync((_) {
195 outOfBandRun = true;
196 }));
197 });
198 });
199
200 expectTestsPass('the onComplete queue is run after an out-of-band callback '
201 'and waits for another out-of-band callback', () {
202 var outOfBand1Run = false;
203 var outOfBand2Run = false;
204 test('test1', () {
205 currentSchedule.onComplete.schedule(() {
206 expect(outOfBand1Run, isTrue);
207
208 sleep(50).then(wrapAsync((_) {
209 outOfBand2Run = true;
210 }));
211 });
212
213 sleep(50).then(wrapAsync((_) {
214 outOfBand1Run = true;
215 }));
216 });
217
218 test('test2', () => expect(outOfBand2Run, isTrue));
219 });
220
221 expectTestsFail('an out-of-band callback in the onComplete queue blocks the '
222 'test', () {
223 var outOfBandRun = false;
224 test('test', () {
225 currentSchedule.onComplete.schedule(() {
226 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar'))));
227 });
228 });
229 });
230
231 expectTestsPass('an out-of-band callback blocks onComplete even with an '
232 'unrelated error', () {
233 var outOfBandRun = false;
234 var outOfBandSetInOnComplete = false;
235 test('test 1', () {
236 currentSchedule.onComplete.schedule(() {
237 outOfBandSetInOnComplete = outOfBandRun;
238 });
239
240 sleep(50).then(wrapAsync((_) {
241 outOfBandRun = true;
242 }));
243
244 schedule(() => expect('foo', equals('bar')));
245 });
246
247 test('test 2', () => expect(outOfBandSetInOnComplete, isTrue));
248 }, passing: ['test 2']);
249
250 expectTestsPass('the onComplete queue is run after an asynchronous error',
251 () {
252 var onCompleteRun = false;
253 test('test 1', () {
254 currentSchedule.onComplete.schedule(() {
255 onCompleteRun = true;
256 });
257
258 schedule(() => expect('foo', equals('bar')));
259 });
260
261 test('test 2', () {
262 expect(onCompleteRun, isTrue);
263 });
264 }, passing: ['test 2']);
265
266 expectTestsPass('the onComplete queue is run after a synchronous error', () {
267 var onCompleteRun = false;
268 test('test 1', () {
269 currentSchedule.onComplete.schedule(() {
270 onCompleteRun = true;
271 });
272
273 throw 'error';
274 });
275
276 test('test 2', () {
277 expect(onCompleteRun, isTrue);
278 });
279 }, passing: ['test 2']);
280
281 expectTestsPass('the onComplete queue is run after an out-of-band error', () {
282 var onCompleteRun = false;
283 test('test 1', () {
284 currentSchedule.onComplete.schedule(() {
285 onCompleteRun = true;
286 });
287
288 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar'))));
289 });
290
291 test('test 2', () {
292 expect(onCompleteRun, isTrue);
293 });
294 }, passing: ['test 2']);
295
296 expectTestsPass('currentSchedule.error contains the error in the onComplete '
297 'queue', () {
298 var error;
299 test('test 1', () {
300 currentSchedule.onComplete.schedule(() {
301 error = currentSchedule.error;
302 });
303
304 throw 'error';
305 });
306
307 test('test 2', () {
308 expect(error, new isInstanceOf<ScheduleError>());
309 expect(error.error, equals('error'));
310 });
311 }, passing: ['test 2']);
312
313 expectTestsPass('onComplete tasks can be scheduled during normal tasks', () {
314 var onCompleteRun = false;
315 test('test 1', () {
316 schedule(() {
317 currentSchedule.onComplete.schedule(() {
318 onCompleteRun = true;
319 });
320 });
321 });
322
323 test('test 2', () {
324 expect(onCompleteRun, isTrue);
325 });
326 });
327
328 expectTestsFail('failures in onComplete cause test failures', () {
329 test('test', () {
330 currentSchedule.onComplete.schedule(() {
331 expect('foo', equals('bar'));
332 });
333 });
334 });
335
336 expectTestsPass('the onException queue is not run if a test is successful',
337 () {
338 var onExceptionRun = false;
339 test('test 1', () {
340 currentSchedule.onException.schedule(() {
341 onExceptionRun = true;
342 });
343
344 schedule(() => expect('foo', equals('foo')));
345 });
346
347 test('test 2', () {
348 expect(onExceptionRun, isFalse);
349 });
350 });
351
352 expectTestsPass('the onException queue is run after an asynchronous error',
353 () {
354 var onExceptionRun = false;
355 test('test 1', () {
356 currentSchedule.onException.schedule(() {
357 onExceptionRun = true;
358 });
359
360 schedule(() => expect('foo', equals('bar')));
361 });
362
363 test('test 2', () {
364 expect(onExceptionRun, isTrue);
365 });
366 }, passing: ['test 2']);
367
368 expectTestsPass('the onException queue is run after a synchronous error', () {
369 var onExceptionRun = false;
370 test('test 1', () {
371 currentSchedule.onException.schedule(() {
372 onExceptionRun = true;
373 });
374
375 throw 'error';
376 });
377
378 test('test 2', () {
379 expect(onExceptionRun, isTrue);
380 });
381 }, passing: ['test 2']);
382
383 expectTestsPass('the onException queue is run after an out-of-band error', () {
384 var onExceptionRun = false;
385 test('test 1', () {
386 currentSchedule.onException.schedule(() {
387 onExceptionRun = true;
388 });
389
390 sleep(50).then(wrapAsync((_) => expect('foo', equals('bar'))));
391 });
392
393 test('test 2', () {
394 expect(onExceptionRun, isTrue);
395 });
396 }, passing: ['test 2']);
397
398 expectTestsPass('currentSchedule.error contains the error in the onException '
399 'queue', () {
400 var error;
401 test('test 1', () {
402 currentSchedule.onException.schedule(() {
403 error = currentSchedule.error;
404 });
405
406 throw 'error';
407 });
408
409 test('test 2', () {
410 expect(error, new isInstanceOf<ScheduleError>());
411 expect(error.error, equals('error'));
412 });
413 }, passing: ['test 2']);
414
415 expectTestsPass('currentSchedule.error contains an error passed into '
416 'signalError synchronously', () {
417 var error;
418 test('test 1', () {
419 currentSchedule.onException.schedule(() {
420 error = currentSchedule.error;
421 });
422
423 currentSchedule.signalError('error');
424 });
425
426 test('test 2', () {
427 expect(error, new isInstanceOf<ScheduleError>());
428 expect(error.error, equals('error'));
429 });
430 }, passing: ['test 2']);
431
432 expectTestsPass('currentSchedule.error contains an error passed into '
433 'signalError asynchronously', () {
434 var error;
435 test('test 1', () {
436 currentSchedule.onException.schedule(() {
437 error = currentSchedule.error;
438 });
439
440 schedule(() => currentSchedule.signalError('error'));
441 });
442
443 test('test 2', () {
444 expect(error, new isInstanceOf<ScheduleError>());
445 expect(error.error, equals('error'));
446 });
447 }, passing: ['test 2']);
448
449 expectTestsPass('currentSchedule.error contains an error passed into '
450 'signalError out-of-band', () {
451 var error;
452 test('test 1', () {
453 currentSchedule.onException.schedule(() {
454 error = currentSchedule.error;
455 });
456
457 sleep(50).then(wrapAsync((_) => currentSchedule.signalError('error')));
458 });
459
460 test('test 2', () {
461 expect(error, new isInstanceOf<ScheduleError>());
462 expect(error.error, equals('error'));
463 });
464 }, passing: ['test 2']);
465
466 expectTestsPass('currentSchedule.currentTask returns the current task while '
467 'executing a task', () {
468 test('test', () {
469 schedule(() => expect('foo', equals('foo')), 'task 1');
470
471 schedule(() {
472 expect(currentSchedule.currentTask.description, equals('task 2'));
473 }, 'task 2');
474
475 schedule(() => expect('bar', equals('bar')), 'task 3');
476 });
477 });
478
479 expectTestsPass('currentSchedule.currentTask is null before the schedule has '
480 'started', () {
481 test('test', () {
482 schedule(() => expect('foo', equals('foo')));
483
484 expect(currentSchedule.currentTask, isNull);
485 });
486 });
487
488 expectTestsPass('currentSchedule.currentTask is null after the schedule has '
489 'completed', () {
490 test('test', () {
491 expect(sleep(50).then((_) {
492 expect(currentSchedule.currentTask, isNull);
493 }), completes);
494
495 schedule(() => expect('foo', equals('foo')));
496 });
497 });
498
499 expectTestsPass('currentSchedule.currentQueue returns the current queue while '
500 'executing a task', () {
501 test('test', () {
502 schedule(() {
503 expect(currentSchedule.currentQueue.name, equals('tasks'));
504 });
505 });
506 });
507
508 expectTestsPass('currentSchedule.currentQueue is null before the schedule has '
509 'started', () {
510 test('test', () {
511 schedule(() => expect('foo', equals('foo')));
512
513 expect(currentSchedule.currentQueue, isNull);
514 });
515 });
516
517 expectTestsPass('setUp is run before each test', () {
518 var setUpRun = false;
519 setUp(() {
520 setUpRun = true;
521 });
522
523 test('test 1', () {
524 expect(setUpRun, isTrue);
525 setUpRun = false;
526 });
527
528 test('test 2', () {
529 expect(setUpRun, isTrue);
530 setUpRun = false;
531 });
532 });
533
534 expectTestsPass('setUp can schedule events', () {
535 var setUpRun = false;
536 setUp(() {
537 schedule(() {
538 setUpRun = true;
539 });
540 currentSchedule.onComplete.schedule(() {
541 setUpRun = false;
542 });
543 });
544
545 test('test 1', () {
546 expect(setUpRun, isFalse);
547 schedule(() => expect(setUpRun, isTrue));
548 });
549
550 test('test 2', () {
551 expect(setUpRun, isFalse);
552 schedule(() => expect(setUpRun, isTrue));
553 });
554 });
555
556 expectTestsFail('synchronous errors in setUp will cause tests to fail', () {
557 setUp(() => expect('foo', equals('bar')));
558 test('test 1', () => expect('foo', equals('foo')));
559 test('test 2', () => expect('foo', equals('foo')));
560 });
561
562 expectTestsFail('scheduled errors in setUp will cause tests to fail', () {
563 setUp(() => schedule(() => expect('foo', equals('bar'))));
564 test('test 1', () => expect('foo', equals('foo')));
565 test('test 2', () => expect('foo', equals('foo')));
566 });
567
568 expectTestsPass('synchronous errors in setUp will cause onException to run',
569 () {
570 var onExceptionRun = false;
571 setUp(() {
572 currentSchedule.onException.schedule(() {
573 onExceptionRun = true;
574 });
575
576 if (!onExceptionRun) expect('foo', equals('bar'));
577 });
578
579 test('test 1', () => expect('foo', equals('foo')));
580 test('test 2', () => expect(onExceptionRun, isTrue));
581 }, passing: ['test 2']);
582
583 expectTestsPass("setUp doesn't apply to child groups", () {
584 var setUpRun = false;
585 setUp(() {
586 setUpRun = true;
587 currentSchedule.onComplete.schedule(() {
588 setUpRun = false;
589 });
590 });
591
592 test('outer', () {
593 expect(setUpRun, isTrue);
594 });
595
596 group('group', () {
597 test('inner', () {
598 expect(setUpRun, isFalse);
599 });
600 });
601 });
602
603 expectTestsPass("setUp doesn't apply to parent groups", () {
604 var setUpRun = false;
605 group('group', () {
606 setUp(() {
607 setUpRun = true;
608 currentSchedule.onComplete.schedule(() {
609 setUpRun = false;
610 });
611 });
612
613 test('inner', () {
614 expect(setUpRun, isTrue);
615 });
616 });
617
618 test('outer', () {
619 expect(setUpRun, isFalse);
620 });
621 });
622
623 expectTestsPass("setUp doesn't apply to sibling groups", () {
624 var setUpRun = false;
625 group('group 1', () {
626 setUp(() {
627 setUpRun = true;
628 currentSchedule.onComplete.schedule(() {
629 setUpRun = false;
630 });
631 });
632
633 test('test 1', () {
634 expect(setUpRun, isTrue);
635 });
636 });
637
638 group('group 2', () {
639 test('test 2', () {
640 expect(setUpRun, isFalse);
641 });
642 });
643 });
644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698