-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.ts
85 lines (74 loc) · 2.41 KB
/
stack_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
import { test } from "@cross/test";
import { delay } from "@std/async/delay";
import { assertEquals, assertRejects } from "@std/assert";
import { flushPromises } from "./flush_promises.ts";
import { peekPromiseState } from "./peek_promise_state.ts";
import { Stack } from "./stack.ts";
test("Stack 'pop' returns pushed items", async () => {
const q = new Stack<number>();
q.push(1);
q.push(2);
q.push(3);
assertEquals(await q.pop(), 3);
assertEquals(await q.pop(), 2);
assertEquals(await q.pop(), 1);
});
test("Stack 'pop' waits for an item is pushed", async () => {
const q = new Stack<number>();
const popper = q.pop();
await flushPromises();
assertEquals(await peekPromiseState(popper), "pending");
q.push(1);
await flushPromises();
assertEquals(await peekPromiseState(popper), "fulfilled");
assertEquals(await popper, 1);
});
test("Stack 'pop' with non-aborted signal", async () => {
const controller = new AbortController();
const q = new Stack<number>();
const popper = q.pop({ signal: controller.signal });
await flushPromises();
assertEquals(await peekPromiseState(popper), "pending");
});
test("Stack 'pop' with signal aborted after delay", async () => {
const controller = new AbortController();
const q = new Stack<number>();
const reason = new Error("Aborted");
delay(100).then(() => controller.abort(reason));
await assertRejects(
() => q.pop({ signal: controller.signal }),
Error,
"Aborted",
);
});
test("Stack 'pop' with signal already aborted", async () => {
const controller = new AbortController();
const q = new Stack<number>();
const reason = new Error("Aborted");
controller.abort(reason);
await assertRejects(
() => q.pop({ signal: controller.signal }),
Error,
"Aborted",
);
});
test("Stack with falsy value is accepted", async () => {
const q = new Stack<number>();
const popper = q.pop();
await flushPromises();
assertEquals(await peekPromiseState(popper), "pending");
q.push(0);
await flushPromises();
assertEquals(await peekPromiseState(popper), "fulfilled");
assertEquals(await popper, 0);
});
test("Stack with null is accepted", async () => {
const q = new Stack<null>();
const popper = q.pop();
await flushPromises();
assertEquals(await peekPromiseState(popper), "pending");
q.push(null);
await flushPromises();
assertEquals(await peekPromiseState(popper), "fulfilled");
assertEquals(await popper, null);
});