rsult 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ import {
2
+ Err,
3
+ Ok,
4
+ Result,
5
+ result_from_promise,
6
+ try_catch,
7
+ ResultOk,
8
+ ResultErr,
9
+ isResultOk,
10
+ isResultErr,
11
+ } from './result';
12
+
13
+ describe('Result', () => {
14
+ describe('IResultCore', () => {
15
+ it('is_ok returns true for ResultOk', () => {
16
+ const result = Ok(5);
17
+ expect(result.is_ok()).toBe(true);
18
+ });
19
+
20
+ it('is_ok returns false for ResultErr', () => {
21
+ const result = Err(new Error("Error"));
22
+ expect(result.is_ok()).toBe(false);
23
+ });
24
+
25
+ it('is_err returns true for ResultErr', () => {
26
+ const result = Err(new Error("Error"));
27
+ expect(result.is_err()).toBe(true);
28
+ });
29
+
30
+ it('is_err returns false for ResultOk', () => {
31
+ const result = Ok(5);
32
+ expect(result.is_err()).toBe(false);
33
+ });
34
+
35
+ it('ok returns Some for ResultOk', () => {
36
+ const result = Ok(5);
37
+ expect(result.ok().is_some()).toBe(true);
38
+ expect(result.ok().unwrap()).toBe(5);
39
+ });
40
+
41
+ it('ok returns None for ResultErr', () => {
42
+ const result = Err(new Error("Error"));
43
+ expect(result.ok().is_none()).toBe(true);
44
+ });
45
+
46
+ it('err returns Some for ResultErr', () => {
47
+ const result = Err(new Error("Error"));
48
+ expect(result.err().is_some()).toBe(true);
49
+ expect(result.err().unwrap()).toEqual(new Error("Error"));
50
+ });
51
+
52
+ it('err returns None for ResultOk', () => {
53
+ const result = Ok(5);
54
+ expect(result.err().is_none()).toBe(true);
55
+ });
56
+
57
+ it('expect returns value for ResultOk', () => {
58
+ const result = Ok(5);
59
+ expect(result.expect("This should not fail")).toBe(5);
60
+ });
61
+
62
+ it('expect throws for ResultErr', () => {
63
+ const result = Err("This will fail");
64
+ expect(() => result.expect("This should not fail")).toThrow("This should not fail");
65
+ });
66
+
67
+ it('unwrap returns value for ResultOk', () => {
68
+ const result = Ok(5);
69
+ expect(result.unwrap()).toBe(5);
70
+ });
71
+
72
+ it('unwrap throws for ResultErr', () => {
73
+ const result = Err(new Error("Failure"));
74
+ expect(() => result.unwrap()).toThrow();
75
+ expect(result.unwrap_err().message).toBe("Failure");
76
+ });
77
+
78
+ it('expect_err returns error for ResultErr', () => {
79
+ const result = Err(new Error("Failure"));
80
+ expect(result.expect_err("Expected an error")).toEqual(new Error("Failure"));
81
+ });
82
+
83
+ it('expect_err throws for ResultOk', () => {
84
+ const result = Ok(5);
85
+ expect(() => result.expect_err("Expected an error")).toThrow("Expected an error");
86
+ });
87
+
88
+ it('unwrap_err returns error for ResultErr', () => {
89
+ const result = Err(new Error("Failure"));
90
+ expect(result.unwrap_err()).toEqual(new Error("Failure"));
91
+ });
92
+
93
+ it('unwrap_err throws for ResultOk', () => {
94
+ const result = Ok(5);
95
+ expect(() => result.unwrap_err()).toThrow();
96
+ });
97
+
98
+ it('into_ok returns value for ResultOk', () => {
99
+ const result = Ok(5);
100
+ expect(result.into_ok()).toBe(5);
101
+ });
102
+
103
+ it('into_ok throws for ResultErr', () => {
104
+ const result = Err(new Error("Failure"));
105
+ expect(() => result.into_ok()).toThrow();
106
+ });
107
+
108
+ it('into_err returns error for ResultErr', () => {
109
+ const result = Err(new Error("Failure"));
110
+ expect(result.into_err()).toEqual(new Error("Failure"));
111
+ });
112
+
113
+ it('into_err throws for ResultOk', () => {
114
+ const result = Ok(5);
115
+ expect(() => result.into_err()).toThrow();
116
+ });
117
+ });
118
+
119
+ describe('IResultExt', () => {
120
+ describe('is_ok_and', () => {
121
+ it('returns true if result is Ok and condition is met', () => {
122
+ const result = Ok(5);
123
+ expect(result.is_ok_and(x => x > 3)).toBe(true);
124
+ });
125
+
126
+ it('returns false if result is Ok and condition is not met', () => {
127
+ const result = Ok(2);
128
+ expect(result.is_ok_and(x => x > 3)).toBe(false);
129
+ });
130
+
131
+ it('returns false if result is Err', () => {
132
+ const result = Err(new Error("Error"));
133
+ expect(result.is_ok_and(x => x > 3)).toBe(false);
134
+ });
135
+ });
136
+
137
+ describe('is_err_and', () => {
138
+ it('returns true if result is Err and condition is met', () => {
139
+ const result = Err(new Error("Network failure"));
140
+ expect(result.is_err_and(e => e.message.includes("Network"))).toBe(true);
141
+ });
142
+
143
+ it('returns false if result is Err and condition is not met', () => {
144
+ const result = Err(new Error("Other error"));
145
+ expect(result.is_err_and(e => e.message.includes("Network"))).toBe(false);
146
+ });
147
+
148
+ it('returns false if result is Ok', () => {
149
+ const result = Ok(5);
150
+ expect(result.is_err_and(e => true)).toBe(false);
151
+ });
152
+ });
153
+
154
+ describe('map', () => {
155
+ it('transforms Ok value', () => {
156
+ const result = Ok(5);
157
+ const mapped = result.map(x => x * 2);
158
+ expect(mapped.unwrap()).toBe(10);
159
+ });
160
+
161
+ it('does not transform Err value', () => {
162
+ const result = Err("Error");
163
+ const mapped = result.map(x => x * 2);
164
+ expect(mapped.is_err()).toBe(true);
165
+ });
166
+ });
167
+
168
+ describe('map_or', () => {
169
+ it('transforms Ok value and returns it', () => {
170
+ const result = Ok(5);
171
+ const value = result.map_or(0, x => x * 2);
172
+ expect(value).toBe(10);
173
+ });
174
+
175
+ it('returns default value for Err', () => {
176
+ const result = Err("Error");
177
+ const value = result.map_or(0, x => x * 2);
178
+ expect(value).toBe(0);
179
+ });
180
+ });
181
+
182
+ describe('map_or_else', () => {
183
+ it('transforms Ok value and returns it', () => {
184
+ const result = Ok(5);
185
+ const value = result.map_or_else(() => 0, x => x * 2);
186
+ expect(value).toBe(10);
187
+ });
188
+
189
+ it('computes and returns default value for Err', () => {
190
+ const result = Err("Error");
191
+ const value = result.map_or_else(() => 0, x => x * 2);
192
+ expect(value).toBe(0);
193
+ });
194
+ });
195
+
196
+ describe('map_err', () => {
197
+ it('transforms Err value', () => {
198
+ const result = Err("Error");
199
+ const mappedErr = result.map_err(e => new Error(e));
200
+ expect(mappedErr.unwrap_err().message).toBe("Error");
201
+ });
202
+
203
+ it('does not transform Ok value', () => {
204
+ const result = Ok(5) as Result<number, Error>;
205
+ const mapped = result.map_err((e) => new Error(e.toString()));
206
+ expect(mapped.is_ok()).toBe(true);
207
+ });
208
+ });
209
+
210
+ describe('inspect', () => {
211
+ it('applies function to Ok value and returns unmodified Result', () => {
212
+ const result = Ok(5);
213
+ const inspectSpy = jest.fn();
214
+ result.inspect(inspectSpy);
215
+ expect(inspectSpy).toHaveBeenCalledWith(5);
216
+ expect(result.unwrap()).toBe(5);
217
+ });
218
+
219
+ it('does not apply function to Err value', () => {
220
+ const result = Err("Error");
221
+ const inspectSpy = jest.fn();
222
+ result.inspect(inspectSpy);
223
+ expect(inspectSpy).not.toHaveBeenCalled();
224
+ });
225
+ });
226
+
227
+ describe('inspect_err', () => {
228
+ it('applies function to Err value and returns unmodified Result', () => {
229
+ const result = Err(new Error("Error"));
230
+ const inspectSpy = jest.fn();
231
+ result.inspect_err(inspectSpy);
232
+ expect(inspectSpy).toHaveBeenCalledWith(new Error("Error"));
233
+ });
234
+
235
+ it('does not apply function to Ok value', () => {
236
+ const result = Ok(5);
237
+ const inspectSpy = jest.fn();
238
+ result.inspect_err(inspectSpy);
239
+ expect(inspectSpy).not.toHaveBeenCalled();
240
+ });
241
+ });
242
+
243
+ describe('and', () => {
244
+ it('returns other if result is Ok', () => {
245
+ const result = Ok(5);
246
+ const other = Ok("Hello");
247
+ const finalResult = result.and(other);
248
+ expect(finalResult.unwrap()).toBe("Hello");
249
+ });
250
+
251
+ it('returns original Err if result is Err', () => {
252
+ const result = Err("Error");
253
+ const other = Ok(5);
254
+ const finalResult = result.and(other);
255
+ expect(finalResult.is_err()).toBe(true);
256
+ });
257
+ });
258
+
259
+ describe('and_then', () => {
260
+ it('applies function if result is Ok', () => {
261
+ const result = Ok(5);
262
+ const finalResult = result.and_then(x => Ok(x * 2));
263
+ expect(finalResult.unwrap()).toBe(10);
264
+ });
265
+
266
+ it('returns original Err if result is Err', () => {
267
+ const result = Err("Error");
268
+ const finalResult = result.and_then(x => Ok(x * 2));
269
+ expect(finalResult.is_err()).toBe(true);
270
+ });
271
+ });
272
+
273
+ describe('or', () => {
274
+ it('returns original Ok if result is Ok', () => {
275
+ const result = Ok(5) as Result<number, string>;
276
+ const other = Err("Alternate Error");
277
+ const finalResult = result.or(other);
278
+ expect(finalResult.unwrap()).toBe(5);
279
+ });
280
+
281
+ it('returns other if result is Err', () => {
282
+ const result = Err("Error");
283
+ const other = Ok(5);
284
+ const finalResult = result.or(other);
285
+ expect(finalResult.unwrap()).toBe(5);
286
+ });
287
+ });
288
+
289
+ describe('or_else', () => {
290
+ it('returns original Ok if result is Ok', () => {
291
+ const result = Ok(5) as Result<number, Error>;
292
+ const finalResult = result.or_else(e => Err(e.message + "handled"));
293
+ expect(finalResult.unwrap()).toBe(5);
294
+ });
295
+
296
+ it('applies function if result is Err', () => {
297
+ const result = Err("Error") as Result<string, string>;
298
+ const finalResult = result.or_else(e => Ok(`Handled ${e}`));
299
+ expect(finalResult.unwrap()).toBe("Handled Error");
300
+ });
301
+ });
302
+
303
+ describe('unwrap_or', () => {
304
+ it('returns Ok value for ResultOk', () => {
305
+ const result = Ok(5);
306
+ expect(result.unwrap_or(0)).toBe(5);
307
+ });
308
+
309
+ it('returns default value for ResultErr', () => {
310
+ const result = Err("Error") as Result<number, string>;
311
+ expect(result.unwrap_or(0)).toBe(0);
312
+ });
313
+ });
314
+
315
+ describe('unwrap_or_else', () => {
316
+ it('returns Ok value for ResultOk', () => {
317
+ const result = Ok(5);
318
+ expect(result.unwrap_or_else(() => 0)).toBe(5);
319
+ });
320
+
321
+ it('computes and returns default value for ResultErr', () => {
322
+ const result = Err("Error") as Result<number, string>;
323
+ expect(result.unwrap_or_else(() => 0)).toBe(0);
324
+ });
325
+ });
326
+ });
327
+
328
+
329
+ describe('IResultIteration', () => {
330
+ describe('iter', () => {
331
+ it('yields contained value for ResultOk', () => {
332
+ const result = Ok(5);
333
+ const values = Array.from(result.iter());
334
+ expect(values).toEqual([5]);
335
+ });
336
+
337
+ it('yields nothing for ResultErr', () => {
338
+ const result = Err(new Error("Error"));
339
+ const values = Array.from(result.iter());
340
+ expect(values).toEqual([]);
341
+ });
342
+ });
343
+
344
+ describe('transpose', () => {
345
+ it('returns ResultOk<number, _> for Result<number, _>', () => {
346
+ const resultPromise = Ok(5);
347
+ const transposed = resultPromise.transpose();
348
+ expect(transposed).toEqual(Ok(5));
349
+ });
350
+
351
+ it('returns ResultErr<_, Error> for Result<_, Error>', () => {
352
+ const resultPromise = Err(new Error("Error"));
353
+ const transposed = resultPromise.transpose();
354
+ expect(transposed).toEqual(Err(new Error("Error")));
355
+ });
356
+ });
357
+
358
+ describe('flatten', () => {
359
+ it('flattens nested Ok Result', () => {
360
+ const nestedOk = Ok(Ok(5));
361
+ const flattened = nestedOk.flatten();
362
+ expect(flattened).toEqual(Ok(5));
363
+ });
364
+
365
+ it('flattens ResultOk containing ResultErr', () => {
366
+ const nestedErr = Ok(Err(new Error("error")));
367
+ const flattenedError = nestedErr.flatten();
368
+ expect(flattenedError).toEqual(Err(new Error("error")));
369
+ });
370
+
371
+ it('returns self if not nested', () => {
372
+ const resultOk = Ok(5);
373
+ const flattenedOk = resultOk.flatten();
374
+ expect(flattenedOk).toEqual(Ok(5));
375
+
376
+ const resultErr = Err(new Error("error"));
377
+ const flattenedErr = resultErr.flatten();
378
+ expect(flattenedErr).toEqual(Err(new Error("error")));
379
+ });
380
+ });
381
+ });
382
+
383
+ describe('Utility Methods', () => {
384
+ class MyOk<T> extends ResultOk<T, any> { }
385
+ class MyErr<E> extends ResultErr<any, E> { }
386
+
387
+ describe('isResultOk', () => {
388
+ it('returns true if value is an instance of ResultOk', () => {
389
+ const result = new MyOk<number>(5);
390
+ expect(isResultOk<number, Error>(result)).toBe(true);
391
+ });
392
+
393
+ it('returns false if value is not an instance of ResultOk', () => {
394
+ const errorResult = new MyErr<Error>(new Error("Error"));
395
+ expect(isResultOk<number, Error>(errorResult)).toBe(false);
396
+ });
397
+
398
+ it('returns false for non-Result values', () => {
399
+ expect(isResultOk<number, Error>(5)).toBe(false);
400
+ expect(isResultOk<number, Error>("test")).toBe(false);
401
+ expect(isResultOk<number, Error>(null)).toBe(false);
402
+ expect(isResultOk<number, Error>(undefined)).toBe(false);
403
+ });
404
+ });
405
+
406
+ describe('isResultErr', () => {
407
+ it('returns true if value is an instance of ResultErr', () => {
408
+ const errorResult = new MyErr<Error>(new Error("Error"));
409
+ expect(isResultErr<number, Error>(errorResult)).toBe(true);
410
+ });
411
+
412
+ it('returns false if value is not an instance of ResultErr', () => {
413
+ const result = new MyOk<number>(5);
414
+ expect(isResultErr<number, Error>(result)).toBe(false);
415
+ });
416
+
417
+ it('returns false for non-Result values', () => {
418
+ expect(isResultErr<number, Error>(5)).toBe(false);
419
+ expect(isResultErr<number, Error>("test")).toBe(false);
420
+ expect(isResultErr<number, Error>(null)).toBe(false);
421
+ expect(isResultErr<number, Error>(undefined)).toBe(false);
422
+ });
423
+ });
424
+
425
+ describe('try_catch function', () => {
426
+ it('should return Ok result for successful function execution', () => {
427
+ const result = try_catch(() => 42);
428
+ expect(result.is_ok()).toBe(true);
429
+ expect(result.unwrap()).toBe(42);
430
+ });
431
+
432
+ it('should return Err result for function that throws', () => {
433
+ const errorMsg = 'Error occurred';
434
+ const result = try_catch(() => {
435
+ throw new Error(errorMsg);
436
+ });
437
+ expect(result.is_err()).toBe(true);
438
+ expect(result.unwrap_err().message).toBe(errorMsg);
439
+ });
440
+ });
441
+
442
+ describe('result_from_promise function', () => {
443
+ it('should return Ok result for resolved promise', async () => {
444
+ const promise = Promise.resolve(42);
445
+ const result = await result_from_promise(promise);
446
+ expect(result.is_ok()).toBe(true);
447
+ expect(result.unwrap()).toBe(42);
448
+ });
449
+
450
+ it('should return Err result for rejected promise', async () => {
451
+ const errorMsg = 'Promise rejected';
452
+ const promise = Promise.reject(new Error(errorMsg));
453
+ const result = await result_from_promise(promise);
454
+ expect(result.is_err()).toBe(true);
455
+ expect((await result.unwrap_err()).message).toBe(errorMsg);
456
+ });
457
+ });
458
+ });
459
+ });