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 import "dart:async"; |
7 | 7 |
8 import "dart:async"; | 8 import "package:async/async.dart"; |
9 import "package:test/test.dart"; | 9 import "package:test/test.dart"; |
10 | 10 |
11 /// A zero-millisecond timer should wait until after all microtasks. | 11 /// A zero-millisecond timer should wait until after all microtasks. |
12 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); | 12 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); |
13 | 13 |
| 14 typedef void OptionalArgAction([a, b]); |
| 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 OptionalArgAction unreachable(String name) => |
| 20 ([a, b]) => fail("Unreachable: $name"); |
| 21 |
| 22 // TODO(nweiz): Use the version of this in test when test#418 is fixed. |
| 23 /// A matcher that runs a callback in its own zone and asserts that that zone |
| 24 /// emits an error that matches [matcher]. |
| 25 Matcher throwsZoned(matcher) => predicate((callback) { |
| 26 var firstError = true; |
| 27 runZoned(callback, |
| 28 onError: expectAsync2((error, stackTrace) { |
| 29 if (firstError) { |
| 30 expect(error, matcher); |
| 31 firstError = false; |
| 32 } else { |
| 33 registerException(error, stackTrace); |
| 34 } |
| 35 }, max: -1)); |
| 36 return true; |
| 37 }); |
| 38 |
| 39 /// A matcher that runs a callback in its own zone and asserts that that zone |
| 40 /// emits a [CastError]. |
| 41 final throwsZonedCastError = throwsZoned(new isInstanceOf<CastError>()); |
| 42 |
| 43 /// A matcher that matches a callback or future that throws a [CastError]. |
| 44 final throwsCastError = throwsA(new isInstanceOf<CastError>()); |
18 | 45 |
19 /// A badly behaved stream which throws if it's ever listened to. | 46 /// A badly behaved stream which throws if it's ever listened to. |
20 /// | 47 /// |
21 /// Can be used to test cases where a stream should not be used. | 48 /// Can be used to test cases where a stream should not be used. |
22 class UnusableStream extends Stream { | 49 class UnusableStream extends Stream { |
23 listen(onData, {onError, onDone, cancelOnError}) { | 50 listen(onData, {onError, onDone, cancelOnError}) { |
24 throw new UnimplementedError("Gotcha!"); | 51 throw new UnimplementedError("Gotcha!"); |
25 } | 52 } |
26 } | 53 } |
| 54 |
| 55 /// A dummy [StreamSink] for testing the routing of the [done] and [close] |
| 56 /// futures. |
| 57 /// |
| 58 /// The [completer] field allows the user to control the future returned by |
| 59 /// [done] and [close]. |
| 60 class CompleterStreamSink<T> implements StreamSink<T> { |
| 61 final completer = new Completer(); |
| 62 |
| 63 Future get done => completer.future; |
| 64 |
| 65 void add(T event) {} |
| 66 void addError(error, [StackTrace stackTrace]) {} |
| 67 Future addStream(Stream<T> stream) async {} |
| 68 Future close() => completer.future; |
| 69 } |
| 70 |
| 71 /// A [StreamSink] that collects all events added to it as results. |
| 72 /// |
| 73 /// This is used for testing code that interacts with sinks. |
| 74 class TestSink<T> implements StreamSink<T> { |
| 75 /// The results corresponding to events that have been added to the sink. |
| 76 final results = <Result<T>>[]; |
| 77 |
| 78 /// Whether [close] has been called. |
| 79 bool get isClosed => _isClosed; |
| 80 var _isClosed = false; |
| 81 |
| 82 Future get done => _doneCompleter.future; |
| 83 final _doneCompleter = new Completer(); |
| 84 |
| 85 final Function _onDone; |
| 86 |
| 87 /// Creates a new sink. |
| 88 /// |
| 89 /// If [onDone] is passed, it's called when the user calls [close]. Its result |
| 90 /// is piped to the [done] future. |
| 91 TestSink({onDone()}) : _onDone = onDone ?? (() {}); |
| 92 |
| 93 void add(T event) { |
| 94 results.add(new Result<T>.value(event)); |
| 95 } |
| 96 |
| 97 void addError(error, [StackTrace stackTrace]) { |
| 98 results.add(new Result<T>.error(error, stackTrace)); |
| 99 } |
| 100 |
| 101 Future addStream(Stream<T> stream) { |
| 102 var completer = new Completer.sync(); |
| 103 stream.listen(add, onError: addError, onDone: completer.complete); |
| 104 return completer.future; |
| 105 } |
| 106 |
| 107 Future close() { |
| 108 _isClosed = true; |
| 109 _doneCompleter.complete(new Future.microtask(_onDone)); |
| 110 return done; |
| 111 } |
| 112 } |
OLD | NEW |