-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.test.ts
69 lines (68 loc) · 2.26 KB
/
do.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
import { isInt32 } from "emnorst";
import { describe, expect, test } from "vitest";
import { qo } from "./do";
import { ParseAError, parseA } from "./parsea";
import { anyEl, eoi, fail, satisfy } from "./primitive";
describe("qo", () => {
test("error", () => {
const parser = qo(() => {
throw null;
});
expect(() => parseA(parser, [])).toThrow();
});
test("do", () => {
const parser = qo(perform => {
const a = perform(satisfy<number>(isInt32));
const b = perform(satisfy<string>(el => typeof el === "string"));
return { a, b };
});
expect(parseA(parser, [5, "*"])).toEqual({ a: 5, b: "*" });
expect(() => parseA(parser, [20, 5])).toThrow(ParseAError);
});
test("option", () => {
const parser = qo(perform => perform.option(anyEl()));
expect(parseA(parser, [])).toBeUndefined();
expect(parseA(parser, [0])).toBe(0);
});
test("try > rollback state", () => {
const parser = qo(perform => {
perform.try(() => {
perform(anyEl());
perform(fail());
});
perform(anyEl());
});
expect(parseA(parser, ["hoge"])).toBeUndefined();
});
test("try > no rollback if allowPartial", () => {
const parser = qo(perform => {
perform.try(() => {
perform(anyEl());
perform(fail(), { allowPartial: true });
});
perform(eoi);
});
expect(parseA(parser, ["hoge"])).toBeUndefined();
});
test("while", () => {
const parser = qo(perform => {
const result: unknown[] = [];
perform.while(() => {
result.push(perform(anyEl()));
});
return result;
});
expect(parseA(parser, [0, 1, 2, 3])).toEqual([0, 1, 2, 3]);
});
test("while + allowPartial", () => {
const parser = qo(perform => {
const result: unknown[] = [];
perform.while(() => {
result.push(perform(anyEl()));
perform(anyEl(), { allowPartial: true });
});
return result;
});
expect(parseA(parser, ["hoge"])).toEqual(["hoge"]);
});
});