-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
181 lines (127 loc) · 3.23 KB
/
test.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import assert from "assert";
import { Result, Ok, Err } from "./index";
// Result
assert.strictEqual(Result.Ok, Ok);
assert.strictEqual(Result.Err, Err);
// ok
assert.deepEqual(JSON.parse(JSON.stringify(Ok(1))), { ok: true, value: 1 });
// error
assert.deepEqual(JSON.parse(JSON.stringify(Err(1))), { ok: false, error: 1 });
// eq
assert(Ok(1).eq(Ok(1)));
assert(Err(1).eq(Err(1)));
assert(!Ok(1).eq(Ok(2)));
assert(!Err(1).eq(Err(2)));
assert(!Err<number, number>(1).eq(Ok(1)));
assert(!Ok<number, number>(1).eq(Err(1)));
// expect
try {
const x = Err("emergency failure");
x.expect("Testing expect");
} catch (e) {
assert(e instanceof Error);
assert(e.message === "Testing expect");
}
assert.equal(Ok(10).expect("Testing expectError"), 10);
// expectError
try {
const x = Ok(10);
x.expectError("Testing expectError");
} catch (e) {
assert(e instanceof Error);
assert(e.message === "Testing expectError");
}
assert.equal(Err(10).expectError("Testing expectError"), 10);
// unwrap
try {
const x = Err("emergency failure");
x.unwrap();
} catch (e) {
assert(e instanceof Error);
assert(e.message === "");
}
assert.equal(Ok(10).unwrap(), 10);
// unwrapOr
assert.equal(Ok(9).unwrapOr(2), 9);
assert.equal(Err("error").unwrapOr(2), 2);
// unwrapOrElse
const count = (x: string) => x.length;
assert.equal(Ok<number, string>(2).unwrapOrElse(count), 2);
assert.equal(Err("foo").unwrapOrElse(count), 3);
// map
assert(
Ok("foo")
.map((s) => s.length)
.eq(Ok(3))
);
assert(
Err<string, string>("error")
.map((s) => s.length)
.eq(Err("error"))
);
// mapError
assert(
Err("error")
.mapError((s) => s.length)
.eq(Err(5))
);
assert(
Ok<string, string>("foo")
.mapError((s) => s.length)
.eq(Ok("foo"))
);
// mapOr
assert.equal(
Ok("foo").mapOr(42, (s) => s.length),
3
);
assert.equal(
Err<string, string>("bar").mapOr(42, (s) => s.length),
42
);
// mapOrElse
assert.equal(
Ok("foo").mapOrElse(
(_e) => 42,
(s) => s.length
),
3
);
assert.equal(
Err<string, string>("bar").mapOrElse(
(e) => e.length * 3,
(s) => s.length
),
9
);
// and
assert(Ok<number, string>(2).and(Err("late error")).eq(Err("late error")));
assert(Err("early error").and(Ok("foo")).eq(Err("early error")));
assert(Err("not a 2").and(Err("late error")).eq(Err("not a 2")));
assert(Ok(2).and(Ok("different result type")).eq(Ok("different result type")));
// andThen
assert(
Ok(2)
.andThen((i) => Ok(i.toString()))
.eq(Ok("2"))
);
assert(
Err<number, number>(2)
.andThen((i) => Ok(i.toString()))
.eq(Err(2))
);
// or
assert(Ok(2).or(Err("late error")).eq(Ok(2)));
assert(Err<number, string>("early error").or(Ok(2)).eq(Ok(2)));
assert(Err("not a 2").or(Err("late error")).eq(Err("late error")));
assert(Ok(2).or(Ok(100)).eq(Ok(2)));
// orElse
const sq = (x: number): Result<number, number> => Ok(x * x);
const err = (x: number): Result<number, number> => Err(x);
assert(Ok<number, number>(2).orElse(sq).orElse(sq).eq(Ok(2)));
assert(Ok<number, number>(2).orElse(err).orElse(sq).eq(Ok(2)));
assert(Err<number, number>(3).orElse(sq).orElse(err).eq(Ok(9)));
assert(Err<number, number>(3).orElse(err).orElse(err).eq(Err(3)));
function foo(): Result<undefined, number> {
return Ok();
}