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 main() { | |
13 expectAll(result, expectation) { | |
14 if (expectation.isError) { | |
15 expect(result, expectation); | |
16 } else { | |
17 expect(result.isValue, true); | |
18 expect(result.asValue.value, expectation.asValue.value); | |
19 } | |
20 } | |
21 | |
22 test("empty", () { | |
23 expectAll(Result.flattenAll<int>(results(0)), res([])); | |
24 }); | |
25 test("single value", () { | |
26 expectAll(Result.flattenAll<int>(results(1)), res([0])); | |
27 }); | |
28 test("single error", () { | |
29 expectAll(Result.flattenAll<int>(results(1, throwWhen: (_) => true)), | |
30 err(0)); | |
31 }); | |
32 test("multiple values", () { | |
33 expectAll(Result.flattenAll<int>(results(5)), res([0, 1, 2, 3, 4])); | |
34 }); | |
35 test("multiple errors", () { | |
36 expectAll(Result.flattenAll<int>(results(5, throwWhen: (x) => x.isOdd)), | |
37 err(1)); // First error is result. | |
38 }); | |
39 test("error last", () { | |
40 expectAll(Result.flattenAll<int>(results(5, throwWhen: (x) => x == 4)), | |
41 err(4)); | |
42 }); | |
43 } | |
44 | |
45 Iterable<Result<int>> results(int count, { bool throwWhen(int index) }) sync* { | |
floitsch
2017/08/24 17:46:39
Move to the top.
| |
46 for (int i = 0; i < count; i++) { | |
47 if (throwWhen != null && throwWhen(i)) { | |
48 yield err(i); | |
49 } else { | |
50 yield res(i); | |
51 } | |
52 } | |
53 } | |
OLD | NEW |