Chromium Code Reviews| Index: test/result/result_flattenAll_test.dart |
| diff --git a/test/result/result_flattenAll_test.dart b/test/result/result_flattenAll_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9aa64bbfe2c7748cf69f1c7fcf6a02f821ce02e5 |
| --- /dev/null |
| +++ b/test/result/result_flattenAll_test.dart |
| @@ -0,0 +1,53 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "package:async/async.dart"; |
| +import "package:test/test.dart"; |
| + |
| +final someStack = StackTrace.current; |
| +Result<T> res<T>(T n) => new Result<T>.value(n); |
| +Result err(n) => new ErrorResult("$n", someStack); |
| + |
| +main() { |
| + expectAll(result, expectation) { |
| + if (expectation.isError) { |
| + expect(result, expectation); |
| + } else { |
| + expect(result.isValue, true); |
| + expect(result.asValue.value, expectation.asValue.value); |
| + } |
| + } |
| + |
| + test("empty", () { |
| + expectAll(Result.flattenAll<int>(results(0)), res([])); |
| + }); |
| + test("single value", () { |
| + expectAll(Result.flattenAll<int>(results(1)), res([0])); |
| + }); |
| + test("single error", () { |
| + expectAll(Result.flattenAll<int>(results(1, throwWhen: (_) => true)), |
| + err(0)); |
| + }); |
| + test("multiple values", () { |
| + expectAll(Result.flattenAll<int>(results(5)), res([0, 1, 2, 3, 4])); |
| + }); |
| + test("multiple errors", () { |
| + expectAll(Result.flattenAll<int>(results(5, throwWhen: (x) => x.isOdd)), |
| + err(1)); // First error is result. |
| + }); |
| + test("error last", () { |
| + expectAll(Result.flattenAll<int>(results(5, throwWhen: (x) => x == 4)), |
| + err(4)); |
| + }); |
| +} |
| + |
| +Iterable<Result<int>> results(int count, { bool throwWhen(int index) }) sync* { |
|
floitsch
2017/08/24 17:46:39
Move to the top.
|
| + for (int i = 0; i < count; i++) { |
| + if (throwWhen != null && throwWhen(i)) { |
| + yield err(i); |
| + } else { |
| + yield res(i); |
| + } |
| + } |
| +} |