OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Helper utilities for testing. | 5 /// Helper utilities for testing. |
6 library async.test.util; | 6 library async.test.util; |
7 | 7 |
8 import "dart:async"; | 8 import "dart:async"; |
| 9 |
| 10 import "package:async/async.dart"; |
9 import "package:test/test.dart"; | 11 import "package:test/test.dart"; |
10 | 12 |
11 /// A zero-millisecond timer should wait until after all microtasks. | 13 /// A zero-millisecond timer should wait until after all microtasks. |
12 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); | 14 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); |
13 | 15 |
14 /// A generic unreachable callback function. | 16 /// A generic unreachable callback function. |
15 /// | 17 /// |
16 /// Returns a function that fails the test if it is ever called. | 18 /// Returns a function that fails the test if it is ever called. |
17 unreachable(String name) => ([a, b]) => fail("Unreachable: $name"); | 19 unreachable(String name) => ([a, b]) => fail("Unreachable: $name"); |
18 | 20 |
(...skipping 14 matching lines...) Expand all Loading... |
33 class CompleterStreamSink<T> implements StreamSink<T> { | 35 class CompleterStreamSink<T> implements StreamSink<T> { |
34 final completer = new Completer(); | 36 final completer = new Completer(); |
35 | 37 |
36 Future get done => completer.future; | 38 Future get done => completer.future; |
37 | 39 |
38 void add(T event) {} | 40 void add(T event) {} |
39 void addError(error, [StackTrace stackTrace]) {} | 41 void addError(error, [StackTrace stackTrace]) {} |
40 Future addStream(Stream<T> stream) async {} | 42 Future addStream(Stream<T> stream) async {} |
41 Future close() => completer.future; | 43 Future close() => completer.future; |
42 } | 44 } |
| 45 |
| 46 /// A [StreamSink] that collects all events added to it as results. |
| 47 /// |
| 48 /// This is used for testing code that interacts with sinks. |
| 49 class TestSink<T> implements StreamSink<T> { |
| 50 /// The results corresponding to events that have been added to the sink. |
| 51 final results = <Result<T>>[]; |
| 52 |
| 53 /// Whether [close] has been called. |
| 54 bool get isClosed => _isClosed; |
| 55 var _isClosed = false; |
| 56 |
| 57 Future get done => _doneCompleter.future; |
| 58 final _doneCompleter = new Completer(); |
| 59 |
| 60 final Function _onDone; |
| 61 |
| 62 /// Creates a new sink. |
| 63 /// |
| 64 /// If [onDone] is passed, it's called when the user calls [close]. Its result |
| 65 /// is piped to the [done] future. |
| 66 TestSink({onDone()}) : _onDone = onDone ?? (() {}); |
| 67 |
| 68 void add(T event) { |
| 69 results.add(new Result<T>.value(event)); |
| 70 } |
| 71 |
| 72 void addError(error, [StackTrace stackTrace]) { |
| 73 results.add(new Result<T>.error(error, stackTrace)); |
| 74 } |
| 75 |
| 76 Future addStream(Stream<T> stream) { |
| 77 var completer = new Completer.sync(); |
| 78 stream.listen(add, onError: addError, onDone: completer.complete); |
| 79 return completer.future; |
| 80 } |
| 81 |
| 82 Future close() { |
| 83 _isClosed = true; |
| 84 _doneCompleter.complete(new Future.microtask(_onDone)); |
| 85 return done; |
| 86 } |
| 87 } |
OLD | NEW |