| 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 substitute_future_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:scheduled_test/src/substitute_future.dart'; | |
| 10 import 'package:unittest/unittest.dart'; | |
| 11 | |
| 12 void main() { | |
| 13 group('with no substitution, works like a normal Future for', () { | |
| 14 var completer; | |
| 15 var future; | |
| 16 | |
| 17 setUp(() { | |
| 18 completer = new Completer(); | |
| 19 future = new SubstituteFuture(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((e) => "caught ${e.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((e) => "caught ${e.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: (e) => "caught ${e.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: (e) => "caught ${e.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('with a single substitution', () { | |
| 76 var oldCompleter; | |
| 77 var oldFuture; | |
| 78 var newCompleter; | |
| 79 var future; | |
| 80 | |
| 81 setUp(() { | |
| 82 oldCompleter = new Completer(); | |
| 83 future = new SubstituteFuture(oldCompleter.future); | |
| 84 newCompleter = new Completer(); | |
| 85 oldFuture = future.substitute(newCompleter.future); | |
| 86 }); | |
| 87 | |
| 88 test('pipes a success from the new future to the substitute future, and ' | |
| 89 'from the old future to the return value of .substitute', () { | |
| 90 expect(oldFuture, completion(equals('old'))); | |
| 91 expect(future, completion(equals('new'))); | |
| 92 | |
| 93 oldCompleter.complete('old'); | |
| 94 newCompleter.complete('new'); | |
| 95 }); | |
| 96 | |
| 97 test('pipes an error from the new future to the substitute future, and ' | |
| 98 'from the old future to the return value of .substitute', () { | |
| 99 expect(oldFuture, throwsA(equals('old'))); | |
| 100 expect(future, throwsA(equals('new'))); | |
| 101 | |
| 102 oldCompleter.completeError('old'); | |
| 103 newCompleter.completeError('new'); | |
| 104 }); | |
| 105 }); | |
| 106 | |
| 107 group('with multiple substitutions', () { | |
| 108 var completer1; | |
| 109 var completer2; | |
| 110 var completer3; | |
| 111 var future1; | |
| 112 var future2; | |
| 113 var future; | |
| 114 | |
| 115 setUp(() { | |
| 116 completer1 = new Completer(); | |
| 117 completer2 = new Completer(); | |
| 118 completer3 = new Completer(); | |
| 119 future = new SubstituteFuture(completer1.future); | |
| 120 future1 = future.substitute(completer2.future); | |
| 121 future2 = future.substitute(completer3.future); | |
| 122 }); | |
| 123 | |
| 124 test('pipes a success from the newest future to the substitute future, and ' | |
| 125 'from the old futures to the return values of .substitute', () { | |
| 126 expect(future1, completion(equals(1))); | |
| 127 expect(future2, completion(equals(2))); | |
| 128 expect(future, completion(equals(3))); | |
| 129 | |
| 130 completer1.complete(1); | |
| 131 completer2.complete(2); | |
| 132 completer3.complete(3); | |
| 133 }); | |
| 134 | |
| 135 test('pipes an error from the newest future to the substitute future, and ' | |
| 136 'from the old futures to the return values of .substitute', () { | |
| 137 expect(future1, throwsA(equals(1))); | |
| 138 expect(future2, throwsA(equals(2))); | |
| 139 expect(future, throwsA(equals(3))); | |
| 140 | |
| 141 completer1.completeError(1); | |
| 142 completer2.completeError(2); | |
| 143 completer3.completeError(3); | |
| 144 }); | |
| 145 }); | |
| 146 | |
| 147 test('substituting after a future has completed is an error', () { | |
| 148 var completer = new Completer(); | |
| 149 var future = new SubstituteFuture(completer.future); | |
| 150 completer.complete('success'); | |
| 151 expect(() => future.substitute(new Future.immediate(null)), | |
| 152 throwsStateError); | |
| 153 }); | |
| 154 } | |
| OLD | NEW |