-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.test.ts
229 lines (196 loc) · 6.03 KB
/
option.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";
import { UnsafeExtractError } from "../src/errors.js";
import { Exception } from "../src/exceptions.js";
import type { Option } from "../src/option.js";
import { None, Some } from "../src/option.js";
import { Failure, Success } from "../src/result.js";
const id = <A>(value: A): A => value;
const genSome = <A>(genValue: fc.Arbitrary<A>): fc.Arbitrary<Some<A>> =>
genValue.map((value) => new Some(value));
const genNone: fc.Arbitrary<None> = fc.constant(None.instance);
const genOption = <A>(genValue: fc.Arbitrary<A>): fc.Arbitrary<Option<A>> =>
fc.oneof(genSome(genValue), genNone);
describe("Option", () => {
describe("map", () => {
it("should preserve identity morphisms", () => {
expect.assertions(100);
fc.assert(
fc.property(genOption(fc.anything()), (option) => {
expect(option.map((value) => id(value))).toStrictEqual(option);
}),
);
});
});
describe("flatMap", () => {
it("should have a left identity", () => {
expect.assertions(100);
fc.assert(
fc.property(
fc.anything(),
fc.func(genOption(fc.anything())),
(value, arrow) => {
expect(
new Some(value).flatMap((value) => arrow(value)),
).toStrictEqual(arrow(value));
},
),
);
});
it("should have a right identity", () => {
expect.assertions(100);
fc.assert(
fc.property(genOption(fc.anything()), (option) => {
expect(option.flatMap((value) => new Some(value))).toStrictEqual(
option,
);
}),
);
});
it("should be associative", () => {
expect.assertions(100);
fc.assert(
fc.property(
genOption(fc.anything()),
fc.func(genOption(fc.anything())),
fc.func(genOption(fc.anything())),
(option, arrow1, arrow2) => {
expect(
option.flatMap((value1) =>
arrow1(value1).flatMap((value2) => arrow2(value2)),
),
).toStrictEqual(
option
.flatMap((value1) => arrow1(value1))
.flatMap((value2) => arrow2(value2)),
);
},
),
);
});
});
describe("filter", () => {
it("should be distributive", () => {
expect.assertions(100);
fc.assert(
fc.property(
genOption(fc.anything()),
fc.func(fc.boolean()),
fc.func(fc.boolean()),
(option, predicate1, predicate2) => {
expect(
option
.filter((value) => predicate1(value))
.filter((value) => predicate2(value)),
).toStrictEqual(
option.filter((value) => predicate1(value) && predicate2(value)),
);
},
),
);
});
it("should have an identity input", () => {
expect.assertions(100);
fc.assert(
fc.property(genOption(fc.anything()), (option) => {
expect(option.filter(() => true)).toStrictEqual(option);
}),
);
});
it("should have an annihilating input", () => {
expect.assertions(100);
fc.assert(
fc.property(genOption(fc.anything()), (option) => {
expect(option.filter(() => false)).toStrictEqual(None.instance);
}),
);
});
});
describe("safeExtract", () => {
it("should extract the value from Some", () => {
expect.assertions(100);
fc.assert(
fc.property(
fc.anything(),
fc.func(fc.anything()),
(value, getDefaultValue) => {
expect(new Some(value).safeExtract(getDefaultValue)).toStrictEqual(
value,
);
},
),
);
});
it("should return the default value for None", () => {
expect.assertions(100);
fc.assert(
fc.property(fc.func(fc.anything()), (getDefaultValue) => {
expect(None.instance.safeExtract(getDefaultValue)).toStrictEqual(
getDefaultValue(),
);
}),
);
});
});
describe("unsafeExtract", () => {
it("should extract the value from Some", () => {
expect.assertions(100);
fc.assert(
fc.property(fc.anything(), fc.string(), (value, message) => {
expect(new Some(value).unsafeExtract(message)).toStrictEqual(value);
}),
);
});
it("should throw an UnsafeExtractError for None", () => {
expect.assertions(200);
fc.assert(
fc.property(fc.string(), (message) => {
const error = new UnsafeExtractError(message);
expect(() => None.instance.unsafeExtract(message)).toThrow(error);
expect(() => None.instance.unsafeExtract(message)).toThrow(
UnsafeExtractError,
);
}),
);
});
it("should throw the given exception for None", () => {
expect.assertions(200);
fc.assert(
fc.property(fc.string(), (message) => {
class SomeException extends Exception {}
const error = new SomeException(message);
expect(() => None.instance.unsafeExtract(error)).toThrow(error);
expect(() => None.instance.unsafeExtract(error)).toThrow(
SomeException,
);
}),
);
});
});
describe("toResult", () => {
it("should convert Some to Success", () => {
expect.assertions(100);
fc.assert(
fc.property(
fc.anything(),
fc.func(fc.anything()),
(value, getError) => {
expect(new Some(value).toResult(getError)).toStrictEqual(
new Success(value),
);
},
),
);
});
it("should convert None to Failure", () => {
expect.assertions(100);
fc.assert(
fc.property(genNone, fc.func(fc.anything()), (none, getError) => {
expect(none.toResult(getError)).toStrictEqual(
new Failure(getError()),
);
}),
);
});
});
});