-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrusty-enum.test.ts
178 lines (153 loc) · 5.32 KB
/
rusty-enum.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Enum, EnumPromise, EnumType, asyncMatch, ifLet, intoOptionPromise, intoResultPromise } from "../src";
import { enumifyFn } from "../src/utils";
interface Message {
Quit: null,
Move: { x: number, y: number },
Write: string,
ChangeColor: [number, number, number]
};
const Message = Enum<Message>();
function handleMsg(msg: EnumType<Message>) {
return msg.match({
Move: ({ x, y }) => `moved to (${x}, ${y})`,
Write: (s) => `wrote '${s}'`,
ChangeColor: (r, g, b) => `color changed to rgb(${r}, ${g}, ${b})`,
Quit: () => "someone quit",
});
}
function handleMsgWithDefault(msg: EnumType<Message>) {
return msg.match({
Write: () => "a message is written",
_: () => "nothing is written",
});
}
async function msgPromise(): EnumPromise<Message> {
return Message.Move({ x: 42, y: 64 });
}
describe("Rusty enum", () => {
let quitMsg: EnumType<Message>;
let moveMsg: EnumType<Message>;
let writeMsg: EnumType<Message>;
let changeColorMsg: EnumType<Message>;
beforeAll(() => {
quitMsg = Message.Quit();
expect(quitMsg._variant).toBe("Quit");
moveMsg = Message.Move({ x: 42, y: 64 });
expect(moveMsg._variant).toBe("Move");
expect((moveMsg._data as any).x).toBe(42);
expect((moveMsg._data as any).y).toBe(64);
writeMsg = Message.Write("some text");
expect(writeMsg._variant).toBe("Write");
expect(writeMsg._data).toBe("some text");
changeColorMsg = Message.ChangeColor(102, 204, 255);
expect(changeColorMsg._variant).toBe("ChangeColor");
expect(changeColorMsg._data).toEqual([102, 204, 255]);
});
test("Match method exact match", () => {
expect(handleMsg(quitMsg)).toBe("someone quit");
expect(handleMsg(moveMsg)).toBe("moved to (42, 64)");
expect(handleMsg(writeMsg)).toBe("wrote 'some text'");
expect(handleMsg(changeColorMsg)).toBe("color changed to rgb(102, 204, 255)");
});
test("Match method with default", () => {
expect(handleMsgWithDefault(quitMsg)).toEqual("nothing is written");
expect(handleMsgWithDefault(moveMsg)).toEqual("nothing is written");
expect(handleMsgWithDefault(changeColorMsg)).toEqual("nothing is written");
expect(handleMsgWithDefault(writeMsg)).toEqual("a message is written");
});
test("isVariant method", () => {
expect(quitMsg.isQuit()).toBe(true);
expect(quitMsg.isMove()).toBe(false);
expect(quitMsg.isWrite()).toBe(false);
expect(quitMsg.isChangeColor()).toBe(false);
});
test("ifLet function", () => {
const moveX = ifLet(moveMsg, "Move", ({ x }) => x);
expect(moveX).toEqual(42);
let cbCalled = false;
const moveY = ifLet(quitMsg, "Move", ({ y }) => {
cbCalled = true;
return y;
});
expect(moveY).toBeNull();
expect(cbCalled).toEqual(false);
});
test("async match", async () => {
const moveMsgPromise = msgPromise();
const x = await asyncMatch(moveMsgPromise, {
Move({ x }) {
return x;
},
_: () => 0
});
expect(x).toEqual(42);
});
describe("async result", () => {
test("resolve", async () => {
const resolvePromise: Promise<number> = new Promise((res, _) => res(42));
const resolveResult = await intoResultPromise(resolvePromise);
expect(resolveResult._data).toEqual(42);
expect(resolveResult._variant).toEqual("Ok");
});
test("reject without mapping error", async () => {
const rejectPromise: Promise<number> = new Promise((_, rej) => rej(42));
const rejectResult = await intoResultPromise(rejectPromise);
expect(rejectResult._data).toEqual(42);
expect(rejectResult._variant).toEqual("Err");
});
test("reject with error mapped", async () => {
const rejectPromise: Promise<number> = new Promise((_, rej) => rej(42));
const rejectResult = await intoResultPromise<number, string>(rejectPromise, (err) => err.toString());
expect(rejectResult._data).toEqual("42");
expect(rejectResult._variant).toEqual("Err");
})
});
describe("async option", () => {
test("reject", async () => {
const resolvePromise: Promise<number> = new Promise((_, rej) => rej(42));
const resolveOption = await intoOptionPromise(resolvePromise);
expect(resolveOption._data).toBeUndefined();
expect(resolveOption._variant).toEqual("None");
});
test("resolve", async () => {
const rejectPromise: Promise<number> = new Promise((res, _) => res(42));
const rejectOption = await intoOptionPromise(rejectPromise);
expect(rejectOption._data).toEqual(42);
expect(rejectOption._variant).toEqual("Some");
});
});
describe('enumify function', () => {
function foo(arg: number) {
if (isNaN(arg)) {
throw "Sample error string";
} else {
return 42;
}
}
const enumifiedFoo = enumifyFn<string, typeof foo>(foo);
test("return Ok(foo()) by default", () => {
const res = enumifiedFoo(42);
res.match({
Ok(p) {
expect(true);
expect(p).toEqual(42);
},
Err(p) {
expect(false);
},
})
});
test("return Error(error thrown by foo()) upon error", () => {
const res = enumifiedFoo(NaN);
res.match({
Ok(p) {
expect(false);
},
Err(p) {
expect(true);
expect(p).toEqual("Sample error string");
},
})
});
});
});