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 import "dart:async"; | 6 import "dart:async"; |
7 | 7 |
8 import "package:async/async.dart"; | 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 /// A generic unreachable callback function. | 14 /// A generic unreachable callback function. |
15 /// | 15 /// |
16 /// Returns a function that fails the test if it is ever called. | 16 /// Returns a function that fails the test if it is ever called. |
17 unreachable(String name) => ([a, b]) => fail("Unreachable: $name"); | 17 unreachable(String name) => ([a, b]) => fail("Unreachable: $name"); |
18 | 18 |
| 19 // TODO(nweiz): Use the version of this in test when test#418 is fixed. |
| 20 /// A matcher that runs a callback in its own zone and asserts that that zone |
| 21 /// emits an error that matches [matcher]. |
| 22 Matcher throwsZoned(matcher) => predicate((callback) { |
| 23 var firstError = true; |
| 24 runZoned(callback, onError: expectAsync((error, stackTrace) { |
| 25 if (firstError) { |
| 26 expect(error, matcher); |
| 27 firstError = false; |
| 28 } else { |
| 29 registerException(error, stackTrace); |
| 30 } |
| 31 }, max: -1)); |
| 32 return true; |
| 33 }); |
| 34 |
| 35 /// A matcher that runs a callback in its own zone and asserts that that zone |
| 36 /// emits a [CastError]. |
| 37 final throwsZonedCastError = throwsZoned(new isInstanceOf<CastError>()); |
| 38 |
| 39 /// A matcher that matches a callback or future that throws a [CastError]. |
| 40 final throwsCastError = throwsA(new isInstanceOf<CastError>()); |
| 41 |
19 /// A badly behaved stream which throws if it's ever listened to. | 42 /// A badly behaved stream which throws if it's ever listened to. |
20 /// | 43 /// |
21 /// Can be used to test cases where a stream should not be used. | 44 /// Can be used to test cases where a stream should not be used. |
22 class UnusableStream extends Stream { | 45 class UnusableStream extends Stream { |
23 listen(onData, {onError, onDone, cancelOnError}) { | 46 listen(onData, {onError, onDone, cancelOnError}) { |
24 throw new UnimplementedError("Gotcha!"); | 47 throw new UnimplementedError("Gotcha!"); |
25 } | 48 } |
26 } | 49 } |
27 | 50 |
28 /// A dummy [StreamSink] for testing the routing of the [done] and [close] | 51 /// A dummy [StreamSink] for testing the routing of the [done] and [close] |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 stream.listen(add, onError: addError, onDone: completer.complete); | 99 stream.listen(add, onError: addError, onDone: completer.complete); |
77 return completer.future; | 100 return completer.future; |
78 } | 101 } |
79 | 102 |
80 Future close() { | 103 Future close() { |
81 _isClosed = true; | 104 _isClosed = true; |
82 _doneCompleter.complete(new Future.microtask(_onDone)); | 105 _doneCompleter.complete(new Future.microtask(_onDone)); |
83 return done; | 106 return done; |
84 } | 107 } |
85 } | 108 } |
OLD | NEW |