-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
185 lines (162 loc) · 5.12 KB
/
index.spec.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
import { isMaybe, Maybe, NotFoundError } from "./index";
describe("maybe", () => {
describe("with None", () => {
test("factory asNone", () => {
expect(Maybe.asNone()).toBe(Maybe.None);
});
test("functions", () => {
expect(Maybe.None.isNone).toBe(true);
expect(Maybe.None.isValue).toBe(false);
expect(() => Maybe.None.unwrap()).toThrow();
expect(Maybe.None.unwrapOr("hello")).toEqual("hello");
expect(() => Maybe.None.unwrapOrThrow("hello")).toThrow();
const myError = new Error("mine");
expect(() => Maybe.None.unwrapOrThrow(myError)).toThrow(myError);
expect(Maybe.None.unwrapOrNull()).toBeNull();
expect(Maybe.None.map(() => true)).toBe(Maybe.None);
expect(Maybe.None.mapOr("orThis", () => true)).toEqual("orThis");
expect(
Maybe.None.mapOrElse(
() => "orElse",
() => true,
),
).toEqual("orElse");
expect(Maybe.None.andThen("ignored")).toBe(Maybe.None);
expect(Maybe.None.toString()).toEqual("None");
});
test("iterator", () => {
let index = 0;
for (const _item of Maybe.None) {
index++;
}
expect(index).toBe(0);
});
});
describe("with NotFound", () => {
test("factory notFound and unwrap", () => {
const uut = Maybe.notFound("one", "two");
expect(isMaybe(uut)).toBeTruthy();
expect(uut.isNone).toBe(true);
expect(() => uut.unwrap()).toThrow(
new NotFoundError("NotFound: one two"),
);
});
});
describe("with Some value", () => {
test("factory withValue / isMaybe", () => {
const uut = Maybe.withValue("hi");
expect(isMaybe(uut)).toBeTruthy();
});
test("functions", () => {
const content = { id: "utest" };
const uut = Maybe.withValue(content);
expect(uut.isNone).toBe(false);
expect(uut.isValue).toBe(true);
expect(uut.unwrap()).toBe(content);
expect(uut.unwrapOr("hello")).toBe(content);
expect(uut.unwrapOrThrow("hello")).toBe(content);
expect(uut.unwrapOrNull()).toBe(content);
const mapValue = uut.map((value) => {
expect(value).toBe(content);
return true;
});
expect(mapValue.isValue).toBe(true);
expect(mapValue.unwrap()).toBe(true);
const mapOrValue = uut.mapOr("unused", (value) => {
expect(value).toBe(content);
return "mapped";
});
expect(mapOrValue).toBe("mapped");
const mapOrElseValue = uut.mapOrElse(
() => "unused",
(value) => {
expect(value).toBe(content);
return "mapped";
},
);
expect(mapOrElseValue).toBe("mapped");
const andThenValue = uut.andThen((value) => {
expect(value).toBe(content);
return Maybe.Empty;
});
expect(andThenValue).toBe(Maybe.Empty);
expect(uut.toString()).toEqual('Value({"id":"utest"})');
});
test("iterator on values", () => {
let index = 0;
for (const value of Maybe.withValue([1, 2])) {
index++;
expect(value).toBe(index);
}
expect(index).toBe(2);
});
test("iterator on Empty", () => {
let index = 0;
for (const _value of Maybe.Empty) {
index++;
}
expect(index).toBe(0);
});
});
describe("Maybe.wrap", () => {
test("with values", () => {
const num = Maybe.wrap(3);
expect(num.isValue).toBe(true);
expect(num.unwrap()).toBe(3);
const content = { hello: "world" };
const ob = Maybe.wrap(content);
expect(ob.isValue).toBe(true);
expect(ob.unwrap()).toBe(content);
});
test("with not-values", () => {
expect(Maybe.wrap(null)).toBe(Maybe.None);
expect(Maybe.wrap(undefined)).toBe(Maybe.None);
});
});
describe("allOrNone", () => {
test("with a None", () => {
const maybe = Maybe.allOrNone(
Maybe.Empty,
Maybe.withValue(3),
Maybe.None,
);
expect(maybe).toBe(Maybe.None);
});
test("with only values", () => {
const maybe = Maybe.allOrNone(Maybe.Empty, Maybe.withValue(3));
expect(maybe.unwrap()).toEqual([undefined, 3]);
});
});
describe("allValues", () => {
test("with a None", () => {
const maybe = Maybe.allValues(
Maybe.withValue(1),
Maybe.None,
Maybe.withValue(3),
);
expect(maybe).toEqual([1, 3]);
});
test("with only values", () => {
const maybe = Maybe.allValues(Maybe.withValue(1), Maybe.withValue(3));
expect(maybe).toEqual([1, 3]);
});
});
describe("any", () => {
test("with only None", () => {
const maybe = Maybe.any(Maybe.asNone(), Maybe.None);
expect(maybe).toBe(Maybe.None);
});
test("with values", () => {
const maybe = Maybe.any(Maybe.withValue(3), Maybe.None, Maybe.empty());
expect(maybe.unwrap()).toEqual(3);
});
});
test("unwrap", () => {
expect(Maybe.unwrap(Maybe.withValue(3))).toBe(3);
expect(() => Maybe.unwrap(Maybe.None)).toThrow();
});
test("unwrapOrNull", () => {
expect(Maybe.unwrapOrNull(Maybe.withValue(3))).toBe(3);
expect(Maybe.unwrapOrNull(Maybe.None)).toBeNull();
});
});