-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_test.ts
157 lines (139 loc) · 3.02 KB
/
mod_test.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
import { assertEquals } from "@std/assert";
import {
flatMap,
isNone,
isSome,
map,
match,
maybe,
none,
some,
toNullable,
toUndefined,
unwrapOr,
} from "./mod.ts";
Deno.test({
name: "none() creates { kind: 'none' }",
fn() {
assertEquals(none(), { kind: "none" });
},
});
Deno.test({
name: "some(value) creates { kind: 'some', value }",
fn() {
assertEquals(some(42), { kind: "some", value: 42 });
},
});
Deno.test({
name: "isSome() checks if Maybe is 'some'",
fn() {
assertEquals(isSome(some(42)), true);
assertEquals(isSome(none()), false);
},
});
Deno.test({
name: "isNone() checks if Maybe is 'none'",
fn() {
assertEquals(isNone(some(42)), false);
assertEquals(isNone(none()), true);
},
});
Deno.test({
name: "maybe(null or undefined) creates none()",
fn() {
assertEquals(maybe(undefined), none());
assertEquals(maybe(null), none());
},
});
Deno.test({
name: "maybe(value) creates some(value)",
fn() {
assertEquals(maybe(42), some(42));
assertEquals(maybe(true), some(true));
assertEquals(maybe(false), some(false));
assertEquals(maybe(NaN), some(NaN));
},
});
Deno.test({
name: "unwrapOr(some) returns the wrapped value",
fn() {
assertEquals(unwrapOr(some(42), 24), 42);
assertEquals(unwrapOr(some(false), true), false);
},
});
Deno.test({
name: "unwrapOr(none) returns the default value",
fn() {
assertEquals(unwrapOr(none(), 24), 24);
assertEquals(unwrapOr(none(), true), true);
},
});
Deno.test({
name: "toNullable(some) returns the wrapped value",
fn() {
assertEquals(toNullable(some(42)), 42);
},
});
Deno.test({
name: "toNullable(none) returns null",
fn() {
assertEquals(toNullable(none()), null);
},
});
Deno.test({
name: "toUndefined(some) returns the wrapped value",
fn() {
assertEquals(toUndefined(some(42)), 42);
},
});
Deno.test({
name: "toUndefined(none) returns undefined",
fn() {
assertEquals(toUndefined(none()), undefined);
},
});
Deno.test({
name: "map(some) applies the function to the value",
fn() {
assertEquals(map(some(10), (v) => v ** 2), some(100));
},
});
Deno.test({
name: "map(none) does nothing",
fn() {
assertEquals(map(none(), (v) => v ** 2), none());
},
});
Deno.test({
name: "flatMap(some) applies the function and flattens",
fn() {
assertEquals(flatMap(some(4), (v) => some(v ** 2)), some(16));
assertEquals(flatMap(some(4), (_) => none()), none());
},
});
Deno.test({
name: "flatMap(none) does nothing",
fn() {
assertEquals(flatMap(none(), (v) => some(v ** 2)), none());
},
});
Deno.test({
name: "match(some) executes the 'some' handler",
fn() {
const result = match(some(42), {
some: (v) => `got ${v}`,
none: () => "no value",
});
assertEquals(result, "got 42");
},
});
Deno.test({
name: "match(none) executes the 'none' handler",
fn() {
const result = match(none(), {
some: (v) => `got ${v}`,
none: () => "no value",
});
assertEquals(result, "no value");
},
});