Debian Codegraph @

frontend/test/result.test.ts

raw
1 import { test } from "uvu";
2 import assert from "uvu/assert";
3
4 import type { Result } from "../src/lib/result";
5 import { collect, Err, err, Ok, ok } from "../src/lib/result";
6
7 test("basic result operations", () => {
8 const obj = { a: "a" };
9 const ok_obj = ok(obj);
10 assert.instance(ok_obj, Ok);
11 assert.is(ok_obj.is_ok, true);
12 assert.is(ok_obj.is_err, false);
13 assert.is(ok_obj.or_else(), ok_obj);
14 assert.is(ok_obj.unwrap(), obj);
15 assert.is(ok_obj.unwrap_or(), obj);
16 assert.throws(() => ok_obj.unwrap_err());
17 assert.is(ok_obj.and_then((obj) => ok(obj.a)).unwrap(), "a");
18
19 const err_obj: Result<unknown, unknown> = err(obj);
20 assert.instance(err_obj, Err);
21 assert.is(err_obj.is_ok, false);
22 assert.is(err_obj.is_err, true);
23 assert.is(err_obj.and_then(), err_obj);
24 assert.is(err_obj.unwrap_err(), obj);
25 assert.is(err_obj.unwrap_or(null), null);
26 assert.throws(() => err_obj.unwrap());
27 });
28
29 test("collect results", () => {
30 const a = ok("a");
31 const b = ok("b");
32 assert.equal(collect([a, b]), ok(["a", "b"]));
33
34 const e = err("e");
35 assert.is(collect([a, b, e]), e);
36 });
37
38 test.run();