OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 timer_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import '../../../pkg/unittest/lib/unittest.dart'; |
| 9 |
| 10 main() { |
| 11 // Test that the stream generates the integers 0..9. |
| 12 testTenStream(String name, Stream<int> stream, |
| 13 action(int count, StreamIterator iter)) { |
| 14 test(name, () { |
| 15 Function onDone = expectAsync0((){}); |
| 16 StreamIterator<int> iter = new StreamIterator<int>(stream); |
| 17 |
| 18 void testNext(int expectedNext) { |
| 19 if (expectedNext < 10) { |
| 20 iter.moveNext().then((v) { |
| 21 expect(v, isTrue); |
| 22 expect(iter.current, equals(expectedNext)); |
| 23 testNext(expectedNext + 1); // May change "current". |
| 24 action(expectedNext, iter); |
| 25 }); |
| 26 } else { |
| 27 iter.moveNext().then((v) { |
| 28 expect(v, isFalse); |
| 29 onDone(); |
| 30 }); |
| 31 } |
| 32 } |
| 33 testNext(0); |
| 34 }); |
| 35 } |
| 36 |
| 37 testTenStream("fromIterable", |
| 38 new Stream.fromIterable(new Iterable.generate(10, (x) => x)), |
| 39 (count, iter) {}); |
| 40 |
| 41 testTenStream("periodic", |
| 42 new Stream.periodic(const Duration(milliseconds: 5), (x) => x), |
| 43 (count, iter) { if (count == 9) iter.cancel(); }); |
| 44 |
| 45 StreamController<int> c = new StreamController<int>(); |
| 46 c.add(0); |
| 47 testTenStream("controller", c.stream, |
| 48 (count, iter) { |
| 49 if (count < 9) { |
| 50 c.add(count + 1); |
| 51 } else { |
| 52 c.close(); |
| 53 } |
| 54 }); |
| 55 } |
OLD | NEW |