Chromium Code Reviews| 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 import 'package:async_helper/async_helper.dart'; | 6 import 'package:async_helper/async_helper.dart'; |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 Future testWaitEmpty() { | 10 Future testWaitEmpty() { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 } | 177 } |
| 178 | 178 |
| 179 Future testForEach() { | 179 Future testForEach() { |
| 180 var seen = <int>[]; | 180 var seen = <int>[]; |
| 181 return Future.forEach([1, 2, 3, 4, 5], (n) { | 181 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 182 seen.add(n); | 182 seen.add(n); |
| 183 return new Future.value(); | 183 return new Future.value(); |
| 184 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | 184 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 Future testForEachSync() { | |
| 188 var seen = <int>[]; | |
| 189 return Future.forEach([1, 2, 3, 4, 5], (n) { | |
| 190 seen.add(n); | |
| 191 return; | |
| 192 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
The entire "(n) { seen.add(n); return; }" function
nweiz
2014/06/26 19:48:11
Done.
| |
| 193 } | |
| 194 | |
| 187 Future testForEachWithException() { | 195 Future testForEachWithException() { |
| 188 var seen = <int>[]; | 196 var seen = <int>[]; |
| 189 return Future.forEach([1, 2, 3, 4, 5], (n) { | 197 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 190 if (n == 4) throw 'correct exception'; | 198 if (n == 4) throw 'correct exception'; |
| 191 seen.add(n); | 199 seen.add(n); |
| 192 return new Future.value(); | 200 return new Future.value(); |
| 193 }).then((_) { | 201 }).then((_) { |
| 194 throw 'incorrect exception'; | 202 throw 'incorrect exception'; |
| 195 }).catchError((error) { | 203 }).catchError((error) { |
| 196 Expect.equals('correct exception', error); | 204 Expect.equals('correct exception', error); |
| 197 }); | 205 }); |
| 198 } | 206 } |
| 199 | 207 |
| 208 Future testDoWhile() { | |
| 209 var count = 0; | |
| 210 return Future.doWhile(() { | |
| 211 count++; | |
| 212 return new Future(() => count < 10); | |
| 213 }).then((_) => Expect.equals(10, count)); | |
| 214 } | |
| 215 | |
| 216 Future testDoWhileSync() { | |
| 217 var count = 0; | |
| 218 return Future.doWhile(() { | |
| 219 count++; | |
| 220 return count < 10; | |
| 221 }).then((_) => Expect.equals(10, count)); | |
| 222 } | |
| 223 | |
| 224 Future testDoWhileWithException() { | |
| 225 var count = 0; | |
| 226 return Future.doWhile(() { | |
| 227 count++; | |
| 228 if (count == 4) throw 'correct exception' | |
| 229 return new Future(() => true); | |
| 230 }).then((_) { | |
| 231 throw 'incorrect exception'; | |
| 232 }).catchError((error) { | |
| 233 Expect.equals('correct exception', error); | |
| 234 }); | |
| 235 } | |
| 236 | |
| 200 main() { | 237 main() { |
| 201 List<Future> futures = new List<Future>(); | 238 List<Future> futures = new List<Future>(); |
| 202 | 239 |
| 203 futures.add(testWaitEmpty()); | 240 futures.add(testWaitEmpty()); |
| 204 futures.add(testCompleteAfterWait()); | 241 futures.add(testCompleteAfterWait()); |
| 205 futures.add(testCompleteBeforeWait()); | 242 futures.add(testCompleteBeforeWait()); |
| 206 futures.add(testWaitWithMultipleValues()); | 243 futures.add(testWaitWithMultipleValues()); |
| 207 futures.add(testWaitWithSingleError()); | 244 futures.add(testWaitWithSingleError()); |
| 208 futures.add(testWaitWithMultipleErrors()); | 245 futures.add(testWaitWithMultipleErrors()); |
| 209 futures.add(testWaitWithMultipleErrorsEager()); | 246 futures.add(testWaitWithMultipleErrorsEager()); |
| 210 futures.add(testWaitWithSingleErrorWithStackTrace()); | 247 futures.add(testWaitWithSingleErrorWithStackTrace()); |
| 211 futures.add(testWaitWithMultipleErrorsWithStackTrace()); | 248 futures.add(testWaitWithMultipleErrorsWithStackTrace()); |
| 212 futures.add(testWaitWithMultipleErrorsWithStackTraceEager()); | 249 futures.add(testWaitWithMultipleErrorsWithStackTraceEager()); |
| 213 futures.add(testEagerWait()); | 250 futures.add(testEagerWait()); |
| 214 futures.add(testForEachEmpty()); | 251 futures.add(testForEachEmpty()); |
| 215 futures.add(testForEach()); | 252 futures.add(testForEach()); |
| 253 futures.add(testForEachSync()); | |
| 216 futures.add(testForEachWithException()); | 254 futures.add(testForEachWithException()); |
| 255 futures.add(testDoWhile()); | |
| 256 futures.add(testDoWhileSync()); | |
| 257 futures.add(testDoWhileWithException()); | |
| 217 | 258 |
| 218 asyncStart(); | 259 asyncStart(); |
| 219 Future.wait(futures).then((List list) { | 260 Future.wait(futures).then((List list) { |
| 220 Expect.equals(14, list.length); | 261 Expect.equals(14, list.length); |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
14 -> 18 ?
nweiz
2014/06/26 19:48:11
Done.
| |
| 221 asyncEnd(); | 262 asyncEnd(); |
| 222 }); | 263 }); |
| 223 } | 264 } |
| OLD | NEW |