OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import "package:async/async.dart"; |
| 6 import "package:test/test.dart"; |
| 7 |
| 8 final someStack = StackTrace.current; |
| 9 Result<T> res<T>(T n) => new Result<T>.value(n); |
| 10 Result err(n) => new ErrorResult("$n", someStack); |
| 11 |
| 12 /// Helper function creating an iterable of results. |
| 13 Iterable<Result<int>> results(int count, {bool throwWhen(int index)}) sync* { |
| 14 for (int i = 0; i < count; i++) { |
| 15 if (throwWhen != null && throwWhen(i)) { |
| 16 yield err(i); |
| 17 } else { |
| 18 yield res(i); |
| 19 } |
| 20 } |
| 21 } |
| 22 |
| 23 main() { |
| 24 expectAll(result, expectation) { |
| 25 if (expectation.isError) { |
| 26 expect(result, expectation); |
| 27 } else { |
| 28 expect(result.isValue, true); |
| 29 expect(result.asValue.value, expectation.asValue.value); |
| 30 } |
| 31 } |
| 32 |
| 33 test("empty", () { |
| 34 expectAll(Result.flattenAll<int>(results(0)), res([])); |
| 35 }); |
| 36 test("single value", () { |
| 37 expectAll(Result.flattenAll<int>(results(1)), res([0])); |
| 38 }); |
| 39 test("single error", () { |
| 40 expectAll( |
| 41 Result.flattenAll<int>(results(1, throwWhen: (_) => true)), err(0)); |
| 42 }); |
| 43 test("multiple values", () { |
| 44 expectAll(Result.flattenAll<int>(results(5)), res([0, 1, 2, 3, 4])); |
| 45 }); |
| 46 test("multiple errors", () { |
| 47 expectAll(Result.flattenAll<int>(results(5, throwWhen: (x) => x.isOdd)), |
| 48 err(1)); // First error is result. |
| 49 }); |
| 50 test("error last", () { |
| 51 expectAll( |
| 52 Result.flattenAll<int>(results(5, throwWhen: (x) => x == 4)), err(4)); |
| 53 }); |
| 54 } |
OLD | NEW |