| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 library futures_test; | 5 library futures_test; |
| 6 |
| 6 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
| 7 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 8 import 'dart:async'; | 9 import 'dart:async'; |
| 9 | 10 |
| 10 Future testWaitEmpty() { | 11 Future testWaitEmpty() { |
| 11 List<Future> futures = new List<Future>(); | 12 List<Future> futures = new List<Future>(); |
| 12 return Future.wait(futures); | 13 return Future.wait(futures); |
| 13 } | 14 } |
| 14 | 15 |
| 15 Future testCompleteAfterWait() { | 16 Future testCompleteAfterWait() { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 throw 'incorrect error 2'; | 90 throw 'incorrect error 2'; |
| 90 }).catchError((error, stackTrace) { | 91 }).catchError((error, stackTrace) { |
| 91 Expect.equals('correct error', error); | 92 Expect.equals('correct error', error); |
| 92 Expect.isNull(stackTrace); | 93 Expect.isNull(stackTrace); |
| 93 }); | 94 }); |
| 94 } | 95 } |
| 95 | 96 |
| 96 StackTrace get currentStackTrace { | 97 StackTrace get currentStackTrace { |
| 97 try { | 98 try { |
| 98 throw 0; | 99 throw 0; |
| 99 } catch(e, st) { | 100 } catch (e, st) { |
| 100 return st; | 101 return st; |
| 101 } | 102 } |
| 102 return null; | 103 return null; |
| 103 } | 104 } |
| 104 | 105 |
| 105 Future testWaitWithSingleErrorWithStackTrace() { | 106 Future testWaitWithSingleErrorWithStackTrace() { |
| 106 List<Future> futures = new List<Future>(); | 107 List<Future> futures = new List<Future>(); |
| 107 Completer c1 = new Completer(); | 108 Completer c1 = new Completer(); |
| 108 Completer c2 = new Completer(); | 109 Completer c2 = new Completer(); |
| 109 futures.add(c1.future); | 110 futures.add(c1.future); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 return Future.wait(futures, eagerError: true).then((_) { | 149 return Future.wait(futures, eagerError: true).then((_) { |
| 149 throw 'incorrect error 2'; | 150 throw 'incorrect error 2'; |
| 150 }).catchError((error, stackTrace) { | 151 }).catchError((error, stackTrace) { |
| 151 Expect.equals('correct error', error); | 152 Expect.equals('correct error', error); |
| 152 Expect.isNotNull(stackTrace); | 153 Expect.isNotNull(stackTrace); |
| 153 }); | 154 }); |
| 154 } | 155 } |
| 155 | 156 |
| 156 Future testEagerWait() { | 157 Future testEagerWait() { |
| 157 var st; | 158 var st; |
| 158 try { throw 0; } catch (e, s) { st = s; } | 159 try { |
| 160 throw 0; |
| 161 } catch (e, s) { |
| 162 st = s; |
| 163 } |
| 159 Completer c1 = new Completer(); | 164 Completer c1 = new Completer(); |
| 160 Completer c2 = new Completer(); | 165 Completer c2 = new Completer(); |
| 161 List<Future> futures = <Future>[c1.future, c2.future]; | 166 List<Future> futures = <Future>[c1.future, c2.future]; |
| 162 Future waited = Future.wait(futures, eagerError: true); | 167 Future waited = Future.wait(futures, eagerError: true); |
| 163 var result = waited.then((v) { throw "should not be called"; }, | 168 var result = waited.then((v) { |
| 164 onError: (e, s) { | 169 throw "should not be called"; |
| 165 Expect.equals(e, 42); | 170 }, onError: (e, s) { |
| 166 Expect.identical(st, s); | 171 Expect.equals(e, 42); |
| 167 return true; | 172 Expect.identical(st, s); |
| 168 }); | 173 return true; |
| 174 }); |
| 169 c1.completeError(42, st); | 175 c1.completeError(42, st); |
| 170 return result; | 176 return result; |
| 171 } | 177 } |
| 172 | 178 |
| 173 Future testForEachEmpty() { | 179 Future testForEachEmpty() { |
| 174 return Future.forEach([], (_) { | 180 return Future.forEach([], (_) { |
| 175 throw 'should not be called'; | 181 throw 'should not be called'; |
| 176 }); | 182 }); |
| 177 } | 183 } |
| 178 | 184 |
| 179 Future testForEach() { | 185 Future testForEach() { |
| 180 var seen = <int>[]; | 186 var seen = <int>[]; |
| 181 return Future.forEach([1, 2, 3, 4, 5], (n) { | 187 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 182 seen.add(n); | 188 seen.add(n); |
| 183 return new Future.value(); | 189 return new Future.value(); |
| 184 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | 190 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); |
| 185 } | 191 } |
| 186 | 192 |
| 187 Future testForEachSync() { | 193 Future testForEachSync() { |
| 188 var seen = <int>[]; | 194 var seen = <int>[]; |
| 189 return Future.forEach([1, 2, 3, 4, 5], seen.add) | 195 return Future.forEach([1, 2, 3, 4, 5], seen.add).then( |
| 190 .then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | 196 (_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); |
| 191 } | 197 } |
| 192 | 198 |
| 193 Future testForEachWithException() { | 199 Future testForEachWithException() { |
| 194 var seen = <int>[]; | 200 var seen = <int>[]; |
| 195 return Future.forEach([1, 2, 3, 4, 5], (n) { | 201 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 196 if (n == 4) throw 'correct exception'; | 202 if (n == 4) throw 'correct exception'; |
| 197 seen.add(n); | 203 seen.add(n); |
| 198 return new Future.value(); | 204 return new Future.value(); |
| 199 }).then((_) { | 205 }).then((_) { |
| 200 throw 'incorrect exception'; | 206 throw 'incorrect exception'; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 futures.add(testDoWhile()); | 259 futures.add(testDoWhile()); |
| 254 futures.add(testDoWhileSync()); | 260 futures.add(testDoWhileSync()); |
| 255 futures.add(testDoWhileWithException()); | 261 futures.add(testDoWhileWithException()); |
| 256 | 262 |
| 257 asyncStart(); | 263 asyncStart(); |
| 258 Future.wait(futures).then((List list) { | 264 Future.wait(futures).then((List list) { |
| 259 Expect.equals(18, list.length); | 265 Expect.equals(18, list.length); |
| 260 asyncEnd(); | 266 asyncEnd(); |
| 261 }); | 267 }); |
| 262 } | 268 } |
| OLD | NEW |