1import { Number, UndefinedOr } from "effect"
2import { describe, it } from "vitest"
3import { strictEqual, throws } from "./utils/assert.ts"
4
5describe("UndefinedOr", () => {
6 it("map", () => {
7 const f = (a: number) => a + 1
8 strictEqual(UndefinedOr.map(f)(1), 2)
9 strictEqual(UndefinedOr.map(1, f), 2)
10 strictEqual(UndefinedOr.map(f)(undefined), undefined)
11 strictEqual(UndefinedOr.map(undefined, f), undefined)
12 })
13
14 it("match", () => {
15 strictEqual(UndefinedOr.match(1, { onDefined: (a) => a, onUndefined: () => 0 }), 1)
16 strictEqual(UndefinedOr.match(undefined, { onDefined: (a) => a, onUndefined: () => 0 }), 0)
17 })
18
19 it("getOrThrowWith", () => {
20 strictEqual(UndefinedOr.getOrThrowWith(1, () => new Error("test")), 1)
21 throws(() => UndefinedOr.getOrThrowWith(undefined, () => new Error("test")), new Error("test"))
22 })
23
24 it("getOrThrow returns defined values and throws the default missing error", () => {
25 strictEqual(UndefinedOr.getOrThrow(1), 1)
26 throws(() => UndefinedOr.getOrThrow(undefined), new Error("getOrThrow called on a undefined"))
27 })
28
29 it("liftThrowable", () => {
30 const f = (a: number) => {
31 if (a === 0) {
32 throw new Error("test")
33 }
34 return a + 1
35 }
36 strictEqual(UndefinedOr.liftThrowable(f)(1), 2)
37 strictEqual(UndefinedOr.liftThrowable(f)(0), undefined)
38 })
39
40 it("makeReducer", () => {
41 const R = UndefinedOr.makeReducer(Number.ReducerSum)
42
43 strictEqual(R.combine(1, 2), 3)
44 strictEqual(R.combine(1, undefined), 1)
45 strictEqual(R.combine(undefined, 2), 2)
46 strictEqual(R.combine(undefined, undefined), undefined)
47 })
48
49 it("makeReducerFailFast", () => {
50 const R = UndefinedOr.makeReducerFailFast(Number.ReducerSum)
51
52 strictEqual(R.combine(1, 2), 3)
53 strictEqual(R.combine(1, undefined), undefined)
54 strictEqual(R.combine(undefined, 2), undefined)
55 strictEqual(R.combine(undefined, undefined), undefined)
56
57 strictEqual(R.combine(undefined, R.initialValue), undefined)
58 strictEqual(R.combine(R.initialValue, undefined), undefined)
59 strictEqual(R.combine(1, R.initialValue), 1)
60 strictEqual(R.combine(R.initialValue, 1), 1)
61
62 strictEqual(R.combineAll([1, undefined, 2]), undefined)
63 strictEqual(R.combineAll([1, 2]), 3)
64 })
65})