-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoption_test.dart
97 lines (78 loc) · 2.67 KB
/
option_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'package:either_option/src/either.dart';
import 'package:either_option/src/option.dart';
import 'package:test/test.dart';
void main() {
Option<double> safeDivision(int a, int b) {
if (b == 0) return None();
return Some(a / b);
}
group('Test Option implementation', () {
test('Basic option state', () {
final a = safeDivision(2, 0);
final b = safeDivision(4, 2);
expect(a, None());
expect(b, Some(4 / 2));
expect(a.isEmpty, true);
expect(a.isDefined, false);
expect(b.isEmpty, false);
expect(b.isDefined, true);
});
test('Static function utils', () {
final a = Option.empty();
final b = Option.of("either_option");
final c = Option.of(null);
final d = Some("either_option");
expect(a, None());
expect(b, Some("either_option"));
expect(c, None());
expect(a == c, true);
expect(b == d, true);
});
test('All useful functions', () {
final a = safeDivision(2, 0);
final b = safeDivision(4, 2);
/// fold
expect(a.fold(() => "ko", (_) => "ok"), "ko");
expect(b.fold(() => "ko", (_) => "ok"), "ok");
/// getorElse
expect(a.getOrElse(() => 0), 0);
expect(b.getOrElse(() => 0), 2);
/// orElse
expect(a.orElse(() => Some("0")), Some("0"));
expect(b.orElse(() => Some(0)), Some(2));
/// map
double triple(double val) => val * 3;
expect(a.map(triple), None());
expect(b.map(triple), Some(6));
/// flatMap
Option<double> tripleOpt(double val) => Some(val * 3);
expect(a.flatMap(tripleOpt), None());
expect(b.flatMap(tripleOpt), Some(6));
/// toLeft
expect(a.toLeft(0), Left(0));
expect(b.toLeft(0), Left(2));
/// toRight
expect(a.toRight(0), Right(0));
expect(b.toRight(0), Right(2));
/// toEither
expect(a.toEither(0), Left(0));
expect(b.toEither(0), Right(2));
/// filter
expect(Option.of(234).filter((_) => _.isEven), Some(234));
expect(Option.of(234).filter((_) => _.isOdd), None());
/// exists
expect(Option.of(234).exists((_) => _.isEven), true);
expect(Option.of(234).exists((_) => _.isOdd), false);
/// constains
expect(Option.of("foo").contains("foo"), true);
expect(Option.of("foo").contains("bar"), false);
expect(Option.empty().contains("bar"), false);
expect(Option.empty().contains(0), false);
expect(Option.of(0).contains(0), true);
expect(Option.of(0).contains(-1), false);
/// cond
final String hello = "either_option";
expect(Option.cond(hello.length < 50, hello.length), Some(hello.length));
});
});
}