| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016, 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 import 'dart:async'; | |
| 6 import "package:expect/expect.dart"; | |
| 7 import "package:async_helper/async_helper.dart"; | |
| 8 | |
| 9 class A { const A(); } | |
| 10 class B extends A { const B(); } | |
| 11 | |
| 12 /// Stream which emits an error if it's not canceled at the correct time. | |
| 13 /// | |
| 14 /// Must be canceled after at most [maxEvents] events. | |
| 15 Stream makeStream(int maxEvents) { | |
| 16 var c; | |
| 17 int event = 0; | |
| 18 bool canceled = false; | |
| 19 c = new StreamController(onListen: () { | |
| 20 new Timer.periodic(const Duration(milliseconds: 10), (t) { | |
| 21 if (canceled) { | |
| 22 t.cancel(); | |
| 23 return; | |
| 24 } | |
| 25 if (event == maxEvents) { | |
| 26 c.addError("NOT CANCELED IN TIME: $maxEvents"); | |
| 27 c.close(); | |
| 28 t.cancel(); | |
| 29 } else { | |
| 30 c.add(event++); | |
| 31 } | |
| 32 }); | |
| 33 }, onCancel: () { | |
| 34 canceled = true; | |
| 35 }); | |
| 36 return c.stream; | |
| 37 } | |
| 38 | |
| 39 main() { | |
| 40 asyncStart(); | |
| 41 tests().then((_) { asyncEnd(); }); | |
| 42 } | |
| 43 | |
| 44 tests() async { | |
| 45 await expectThrowsAsync(makeStream(4).take(5).toList(), "5/4"); | |
| 46 await expectThrowsAsync(makeStream(0).take(1).toList(), "1/0"); | |
| 47 | |
| 48 Expect.listEquals([0, 1, 2, 3, 4], await makeStream(5).take(5).toList()); | |
| 49 | |
| 50 Expect.listEquals([0, 1, 2, 3], await makeStream(5).take(4).toList()); | |
| 51 | |
| 52 Expect.listEquals([0], await makeStream(5).take(1).toList()); | |
| 53 | |
| 54 Expect.listEquals([], await makeStream(5).take(0).toList()); | |
| 55 | |
| 56 Expect.listEquals([], await makeStream(0).take(0).toList()); | |
| 57 } | |
| 58 | |
| 59 Future expectThrowsAsync(Future computation, String name) { | |
| 60 return computation.then((_) { | |
| 61 Expect.fail("$name: Did not throw"); | |
| 62 }, onError: (e, s){}); | |
| 63 } | |
| OLD | NEW |