-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrycat.test.ts
More file actions
295 lines (252 loc) · 8.1 KB
/
trycat.test.ts
File metadata and controls
295 lines (252 loc) · 8.1 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { describe, expect, expectTypeOf, it, test, vi } from "vitest"
import { type Result, err, ok, tryp, trys, Err } from "./trycat"
describe("Ok", () => {
describe("isOk", () => {
it("returns true", () => {
const result = ok("test")
expect(result.isOk()).toBeTruthy()
})
it("performs type casting to Ok", () => {
const result: Result<number, string> = ok(2)
if (result.isOk()) {
expectTypeOf(result).toMatchTypeOf(ok(2))
}
})
})
describe("isErr", () => {
it("returns false", () => {
const result = ok("test")
expect(result.isErr()).toBeFalsy()
})
})
test("it accepts empty value", () => {
const result = ok()
expect(result.value).toBeUndefined()
})
test("inspect calls the given function with the contained value", () => {
const value = [1, 2, 3]
const fn = vi.fn()
const result = ok(value).inspect(fn)
expect(fn).toBeCalledWith(value)
expect(result.value).toEqual(value)
})
test("inspectErr does nothing and returns Ok", () => {
const fn = vi.fn()
const result = ok("test").inspectErr(fn)
expect(fn).not.toBeCalled()
expect(result.value).toEqual("test")
})
test("map maps the contained value to a new value stored in a new Ok", () => {
const result = ok("2")
const mapped = result.map((str) => Number.parseInt(str))
expect(result.value).toEqual("2")
expect(mapped.value).toEqual(2)
})
test("mapOr maps the contained value to a new value and returns it", () => {
const result = ok('{"hello":"world"}')
const json = result.mapOr({}, (json) => JSON.parse(json))
expect(json).toEqual({ hello: "world" })
})
test("mapOrElse maps the contained value to a new value and returns it", () => {
const result = ok(10)
const errMap = vi.fn()
const value = result.mapOrElse(errMap, (num) => num + 10)
expect(errMap).not.toBeCalled()
expect(value).toEqual(20)
})
test("mapErr does nothing and returns this", () => {
const errMap = vi.fn()
const result = ok(2).mapErr(errMap)
expect(errMap).not.toBeCalled()
expect(result.value).toEqual(2)
})
test("or returns this", () => {
const result = ok("boomer").or(err(404))
expect(result.value).toEqual("boomer")
})
test("orElse returns this", () => {
const otherFn = vi.fn()
const result = ok("chat").or(err("lmfao"))
expect(otherFn).not.toBeCalled()
expect(result.value).toEqual("chat")
})
test("and returns the given Result", () => {
const result = ok(456).and(ok(123))
expect(result.value).toEqual(123)
})
test("andThen calls the given function with the contained value and returns the other Result", () => {
const result = ok("hello").andThen((str) => ok(`${str} world`))
expect(result.value).toEqual("hello world")
})
test("unwrap returns the contained value", () => {
const result = ok("present").unwrap()
expect(result).toEqual("present")
})
test("unwrapOr returns the contained value", () => {
const result = ok("present").unwrapOr("else")
expect(result).toEqual("present")
})
test("expect returns the contained value", () => {
const result = ok("mai").expect("error message")
expect(result).toEqual("mai")
})
test("unwrapErr throws the contained value", () => {
expect(() => {
ok("mai").unwrapErr()
}).toThrow("mai")
})
test("expectErr throws with the given error message", () => {
expect(() => {
ok("mai").expectErr("mai exists")
}).toThrow("mai exists: mai")
})
})
describe("Err", () => {
test("isOk returns false", () => {
const result = err("error")
expect(result.isOk()).toBeFalsy()
})
test("isErr returns true", () => {
const result = err("error")
expect(result.isErr()).toBeTruthy()
})
test("isErr casts Result to Err if true", () => {
const result = err("error")
if (result.isErr()) {
expect(result.error).toEqual("error")
} else {
throw new Error("isErr should not return false")
}
})
test("it accepts empty error", () => {
const result = err()
expect(result.error).toBeUndefined()
})
test("inspect does nothing and returns Err", () => {
const fn = vi.fn()
const result = err(401).inspect(fn)
expect(fn).not.toBeCalled()
expect(result.error).toEqual(401)
})
test("inspectErr calls the given function with the contained error value and returns Err", () => {
const fn = vi.fn()
const result = err(500).inspectErr(fn)
expect(fn).toBeCalledWith(500)
expect(result.error).toEqual(500)
})
test("map does nothing and returns Err", () => {
const fn = vi.fn()
const someErrors = ["some", "error"]
const result = err(someErrors).map(fn)
expect(fn).not.toBeCalled()
expect(result.error).toEqual(someErrors)
})
test("mapOr returns the default value", () => {
const fn = vi.fn()
const result = err("fs error").mapOr("", fn)
expect(fn).not.toBeCalled()
expect(result).toEqual("")
})
test("mapOrElse calls the first function with the contained error value and returns the value returned by the function", () => {
const fn = vi.fn()
const result = err("asd").mapOrElse((error) => `${error}asd`, fn)
expect(fn).not.toBeCalled()
expect(result).toEqual("asdasd")
})
test("mapErr calls the given function with the contained error value and returns a new Err with the value returned by the function", () => {
const result = err('{"error":"wrong_password"}').mapErr((json) => JSON.parse(json))
expect(result.error).toEqual({ error: "wrong_password" })
})
test("or returns the given Result", () => {
const result = err("idk").or(ok("hello"))
expect(result.value).toEqual("hello")
})
test("orElse calls the given function with the contained error and returns the Result returned by the function", () => {
const result = err("mai").orElse((name) => ok(`${name} san`))
expect(result.value).toEqual("mai san")
})
test("and returns this Err", () => {
const result = err("failure").and(ok("hello"))
expect(result.error).toEqual("failure")
})
test("andThen returns this Err", () => {
const fn = vi.fn()
const result = err("failure").andThen(fn)
expect(fn).not.toBeCalled()
expect(result.error).toEqual("failure")
})
test("unwrap throws an error", () => {
expect(() => {
const result = err("failure").unwrap()
}).toThrow("failure")
})
test("unwrapOrElse calls the given function with the contained error value and returns the value returned by the function", () => {
const result = err(1).unwrapOrElse((error) => error + 1)
expect(result).toEqual(2)
})
test("expect throws with the given error message", () => {
expect(() => {
err("internal").expect("error message")
}).toThrow("error message: internal")
})
test("expectErr returns the contained error value", () => {
const result = err(123)
expect(result.expectErr("should error")).toEqual(123)
})
})
describe("trys", () => {
it("returns the value returned by the given function as an Ok", () => {
const result = trys(() => {
return 123
})
if (result.isOk()) {
expect(result.value).toEqual(123)
} else {
throw new Error("Expected an Ok value, received an Err")
}
})
it("catches the thrown error and returns it as an Err", () => {
const result = trys(() => {
throw "FAILED"
})
if (result.isErr()) {
expect(result.error).toEqual("FAILED")
} else {
throw new Error("Expected an Err, received an Ok")
}
})
it("works with void function", () => {
const result = trys(() => {})
if (result.isOk()) {
expect(result.value).toBeUndefined()
} else {
throw new Error("Expected an Ok value, received an Err")
}
})
})
describe("tryp", () => {
it("returns the resolved value of the given promise as an Ok", async () => {
const result = await tryp(Promise.resolve(123))
if (result.isOk()) {
expect(result.value).toEqual(123)
} else {
throw new Error("Expected an Ok value, received an Err")
}
})
it("returns the error returned by the Promise as an Err", async () => {
const result = await tryp(Promise.reject("FAILED"))
if (result.isErr()) {
expect(result.error).toEqual("FAILED")
} else {
throw new Error("Expected an Err, received an Ok")
}
})
it("works with void Promise", async () => {
const result = await tryp(Promise.resolve())
if (result.isOk()) {
expect(result.value).toBeUndefined()
} else {
throw new Error("Expected an Ok value, received an Err")
}
})
})