| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 value_future_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:scheduled_test/src/value_future.dart'; | |
| 10 import 'package:unittest/unittest.dart'; | |
| 11 | |
| 12 void main() { | |
| 13 group('works like a normal Future for', () { | |
| 14 var completer; | |
| 15 var future; | |
| 16 | |
| 17 setUp(() { | |
| 18 completer = new Completer(); | |
| 19 future = new ValueFuture(completer.future); | |
| 20 }); | |
| 21 | |
| 22 test('.asStream on success', () { | |
| 23 expect(future.asStream().toList(), completion(equals(['success']))); | |
| 24 completer.complete('success'); | |
| 25 }); | |
| 26 | |
| 27 test('.asStream on error', () { | |
| 28 expect(future.asStream().toList(), throwsA(equals('error'))); | |
| 29 completer.completeError('error'); | |
| 30 }); | |
| 31 | |
| 32 test('.then and .catchError on success', () { | |
| 33 expect(future.then((v) => "transformed $v") | |
| 34 .catchError((error) => "caught ${error}"), | |
| 35 completion(equals('transformed success'))); | |
| 36 completer.complete('success'); | |
| 37 }); | |
| 38 | |
| 39 test('.then and .catchError on error', () { | |
| 40 expect(future.then((v) => "transformed $v") | |
| 41 .catchError((error) => "caught ${error}"), | |
| 42 completion(equals('caught error'))); | |
| 43 completer.completeError('error'); | |
| 44 }); | |
| 45 | |
| 46 test('.then with onError on success', () { | |
| 47 expect(future.then((v) => "transformed $v", | |
| 48 onError: (error) => "caught ${error}"), | |
| 49 completion(equals('transformed success'))); | |
| 50 completer.complete('success'); | |
| 51 }); | |
| 52 | |
| 53 test('.then with onError on error', () { | |
| 54 expect(future.then((v) => "transformed $v", | |
| 55 onError: (error) => "caught ${error}"), | |
| 56 completion(equals('caught error'))); | |
| 57 completer.completeError('error'); | |
| 58 }); | |
| 59 | |
| 60 test('.whenComplete on success', () { | |
| 61 expect(future.whenComplete(() { | |
| 62 throw 'whenComplete'; | |
| 63 }), throwsA(equals('whenComplete'))); | |
| 64 completer.complete('success'); | |
| 65 }); | |
| 66 | |
| 67 test('.whenComplete on error', () { | |
| 68 expect(future.whenComplete(() { | |
| 69 throw 'whenComplete'; | |
| 70 }), throwsA(equals('whenComplete'))); | |
| 71 completer.completeError('error'); | |
| 72 }); | |
| 73 }); | |
| 74 | |
| 75 group('before completion', () { | |
| 76 var future; | |
| 77 | |
| 78 setUp(() { | |
| 79 future = new ValueFuture(new Completer().future); | |
| 80 }); | |
| 81 | |
| 82 test('.value is null', () { | |
| 83 expect(future.value, isNull); | |
| 84 }); | |
| 85 | |
| 86 test('.hasValue is false', () { | |
| 87 expect(future.hasValue, isFalse); | |
| 88 }); | |
| 89 }); | |
| 90 | |
| 91 group('after successful completion', () { | |
| 92 var future; | |
| 93 | |
| 94 setUp(() { | |
| 95 var completer = new Completer(); | |
| 96 future = new ValueFuture(completer.future); | |
| 97 completer.complete(12); | |
| 98 }); | |
| 99 | |
| 100 test('.value is the result of the future', () { | |
| 101 // The completer calls its listeners asynchronously. We have to wait | |
| 102 // before we can access the value of the ValueFuture. | |
| 103 expect(future.then((_) => future.value), completion(equals(12))); | |
| 104 }); | |
| 105 | |
| 106 test('.hasValue is true', () { | |
| 107 // The completer calls its listeners asynchronously. We have to wait | |
| 108 // before we can access the value of the ValueFuture. | |
| 109 expect(future.then((_) => future.hasValue), completion(isTrue)); | |
| 110 }); | |
| 111 }); | |
| 112 | |
| 113 group('after an error completion', () { | |
| 114 var future; | |
| 115 // Since the completer completes asynchronously we need to wait before the | |
| 116 // ValueFuture has a value. The safeFuture will execute its callback when | |
| 117 // this is the case. | |
| 118 var safeFuture; | |
| 119 | |
| 120 setUp(() { | |
| 121 var completer = new Completer(); | |
| 122 future = new ValueFuture(completer.future); | |
| 123 safeFuture = future.catchError((e) {}); | |
| 124 completer.completeError('bad'); | |
| 125 }); | |
| 126 | |
| 127 test('.value is null', () { | |
| 128 expect(safeFuture.then((_) => future.value), completion(isNull)); | |
| 129 }); | |
| 130 | |
| 131 test('.hasValue is false', () { | |
| 132 expect(safeFuture.then((_) => future.hasValue), completion(isFalse)); | |
| 133 }); | |
| 134 }); | |
| 135 } | |
| OLD | NEW |