-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.test.ts
More file actions
149 lines (127 loc) · 3.72 KB
/
Copy pathvalidate.test.ts
File metadata and controls
149 lines (127 loc) · 3.72 KB
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
import { beforeEach, describe, expect, it } from "./suite.ts";
import { createTerm, type Term } from "../term.ts";
import { close, grow, open, text } from "../ops.ts";
import { assert, validate, validated } from "../validate.ts";
import { print } from "./print.ts";
const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes);
describe("validate", () => {
it("accepts valid ops", () => {
expect(validate([
open("root", { layout: { width: grow(), height: grow() } }),
text("hello"),
close(),
])).toBe(true);
});
it("accepts empty array", () => {
expect(validate([])).toBe(true);
});
it("rejects ops with wrong directive", () => {
expect(validate([{ directive: 0xff }])).toBe(false);
});
it("rejects open element missing id", () => {
expect(validate([{ directive: 0x02 }])).toBe(false);
});
it("rejects text missing content", () => {
expect(validate([{ directive: 0x03 }])).toBe(false);
});
it("rejects non-array", () => {
expect(validate("garbage")).toBe(false);
});
it("rejects null", () => {
expect(validate(null)).toBe(false);
});
it("assert throws TypeError on bad input", () => {
expect(() => assert([{ directive: 0x02 }])).toThrow(TypeError);
});
it("rejects padding > 255 (u8 overflow)", () => {
expect(validate([
open("x", { layout: { padding: { left: 300 } } }),
close(),
])).toBe(false);
});
it("rejects fractional padding", () => {
expect(validate([
open("x", { layout: { padding: { left: 1.5 } } }),
close(),
])).toBe(false);
});
it("rejects fontSize > 255", () => {
expect(validate([text("hi", { fontSize: 256 })])).toBe(false);
});
it("rejects gap > 65535 (u16 overflow)", () => {
expect(validate([
open("x", { layout: { gap: 70000 } }),
close(),
])).toBe(false);
});
it("rejects negative border width", () => {
expect(validate([
open("x", { border: { color: 0xFF0000, left: -1 } }),
close(),
])).toBe(false);
});
it("rejects fractional color", () => {
expect(validate([text("hi", { color: 1.5 })])).toBe(false);
});
it("accepts structured floating attach points", () => {
expect(validate([
open("x", {
floating: {
attachPoints: { element: "center-center", parent: "center-center" },
},
}),
close(),
])).toBe(true);
});
it("accepts floating expand and clipping fields", () => {
expect(validate([
open("x", {
floating: {
expand: { width: 2, height: 3 },
pointerCaptureMode: "passthrough",
clipTo: "attached-parent",
zIndex: 1024,
},
}),
close(),
])).toBe(true);
});
it("rejects numeric floating enum values", () => {
expect(validate([
// deno-lint-ignore no-explicit-any
open("x", { floating: { attachTo: 3 as any } }),
close(),
])).toBe(false);
expect(validate([
// deno-lint-ignore no-explicit-any
open("x", { floating: { attachPoints: 4 as any } }),
close(),
])).toBe(false);
});
});
describe("validated", () => {
let term: Term;
beforeEach(async () => {
term = validated(await createTerm({ width: 40, height: 10 }));
});
it("renders valid ops normally", () => {
let out = print(
decode(
term.render([
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
}),
text("Hello, World!"),
close(),
]).output,
),
40,
10,
);
expect(out).toContain("Hello, World!");
});
it("throws on invalid ops", () => {
// deno-lint-ignore no-explicit-any
expect(() => term.render([{ directive: 0xff }] as any)).toThrow(TypeError);
});
});