Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1183)

Side by Side Diff: test/result/result_flattenAll_test.dart

Issue 2996143002: Add methods to Result. Bump version to 2.0 and remove deprecated features. (Closed)
Patch Set: Dartformat. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698