-
Notifications
You must be signed in to change notification settings - Fork 7
/
either_test.dart
148 lines (125 loc) · 4.22 KB
/
either_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'package:test/test.dart';
import 'package:either_dart/either.dart';
void main() {
test('right generic is right', () {
final maybe = Right<String, bool>(true);
expect(maybe.isRight, true);
expect(maybe.isLeft, false);
expect(() => maybe.left, throwsA(isException));
expect(maybe.right, true);
});
test('left generic is left', () {
final maybe = Left<String, bool>("true");
expect(maybe.isLeft, true);
expect(maybe.isRight, false);
expect(() => maybe.right, throwsA(isException));
expect(maybe.left, "true");
});
test('map right', () {
final maybe = Right<String, bool>(true);
maybe.either((left) => expect(false, true), (right) => expect(right, true));
maybe
.map((right) => false)
.either((left) => expect(false, true), (right) => expect(right, false));
maybe.either((left) => expect(false, true), (right) => expect(right, true));
});
test('map left', () {
final maybe = Left<bool, String>(true);
maybe.either((left) => expect(left, true), (right) => expect(false, true));
maybe
.map((right) => false)
.either((left) => expect(left, true), (right) => expect(true, false));
;
});
test('mapLeft right', () {
final maybe = Right<String, bool>(true);
maybe
.mapLeft((left) => false)
.either((left) => expect(false, true), (right) => expect(right, true));
});
test('mapLeft left', () {
final maybe = Left<bool, String>(true);
maybe
.mapLeft((left) => false)
.either((left) => expect(left, false), (right) => expect(true, false));
});
test('fold', () {
expect(
Left<String, String>("").fold<bool>((left) => true, (right) => false),
true);
expect(
Right<String, String>("").fold<bool>((left) => true, (right) => false),
false);
});
test('cond', () {
expect(Either.cond(true, "left", "right").isRight, true);
expect(Either.cond(false, "left", "right").isLeft, true);
expect(
Either.condLazy(true, () => throw Exception("not lazy"), () => "right")
.isRight,
true);
expect(
Either.condLazy(false, () => "left", () => throw Exception("not lazy"))
.isLeft,
true);
});
test('swap', () {
final maybe = Right<String, bool>(true).swap();
expect(maybe.isRight, false);
expect(maybe.isLeft, true);
final maybe2 = Left<bool, String>(true).swap();
expect(maybe2.isRight, true);
expect(maybe2.isLeft, false);
final maybe3 = maybe.swap();
expect(maybe3.isRight, true);
expect(maybe3.isLeft, false);
});
test('tryCatch', () {
expect(
Either.tryCatch<bool, String, Exception>(
(err) => true, () => throw Exception("not right")).isLeft,
true);
expect(
Either.tryCatch<bool, String, Exception>((err) => false, () => "right")
.isRight,
true);
});
group('equal', () {
test('Left and Left', () {
final x = Left('test');
final z = Left('test');
expect(x, z);
expect(x.hashCode, z.hashCode);
expect(x == z, true);
expect(x != z, false);
expect(Left('1111'), isNot(equals(Left('2222'))));
expect(Left('1111'), equals(Left('1111')));
expect(Left(null), Left(null));
expect(Left(null).hashCode, Left(null).hashCode);
});
test('Right and Left', () {
final x = Right('test');
final z = Left('test');
expect(x != z, true);
expect(x == z, false);
expect(Left('1111'), isNot(equals(Right('2222'))));
expect(Left('1111'), isNot(equals(Right('1111'))));
expect(Right('1111'), isNot(equals(Left('2222'))));
expect(Right('1111'), isNot(equals(Left('1111'))));
expect(Left(null), isNot(equals(Right(null))));
expect(Right(null), isNot(equals(Left(null))));
});
test('Right and Right', () {
final x = Right('test');
final z = Right('test');
expect(x, z);
expect(x.hashCode, z.hashCode);
expect(x == z, true);
expect(x != z, false);
expect(Right('1111'), isNot(equals(Right('2222'))));
expect(Right('1111'), equals(Right('1111')));
expect(Right(null), Right(null));
expect(Right(null).hashCode, Right(null).hashCode);
});
});
}