-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytl.test.ts
99 lines (89 loc) · 2.03 KB
/
ytl.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
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import ytl from "./ytl.js";
type Node = {
tag: string;
attrs: Record<string, unknown>;
children: (Node | string)[];
};
// convenience/example `h` for testing
const ytml = ytl.bind((tag, attrs, ...children): Node => ({
tag,
// not efficient but that's fine for this
attrs: attrs,
children,
}));
Deno.test("ignores comments", () => {
const output = ytml`
// should be ignored
// this should define a {} b {}, but with comments breaking up b
a {} // should also be ignored
b // should be ignored {}
{ c // }
{} }
`;
assertEquals(output, [
{ tag: "a", attrs: {}, children: [] },
{
tag: "b",
attrs: {},
children: [
{ tag: "c", attrs: {}, children: [] },
],
},
]);
});
Deno.test("interpolates values as is", () => {
const attrsObj = { "objKey": "objVal", "objKey2": "objVal2" };
const output = ytml`
a nameKey="strVal" ...${attrsObj} {}
`;
assertEquals(output, [
{
tag: "a",
attrs: {
nameKey: "strVal",
objKey: "objVal",
objKey2: "objVal2",
},
children: [],
},
]);
});
Deno.test("features - basic", () => {
const attrsObj = { "objKey": "objVal", "objKey2": "objVal2" };
const output = ytml`
// a comment, ignored
// multiple root nodes
a {}
// attributes
b foo="bar" {}
// children
c foo="bar" {
d {}
// string children
"string-child"
}
f ${"interpolated key"}=${"interpolated value"} ...${attrsObj} {}
`;
assertEquals(output, [
{ tag: "a", attrs: {}, children: [] },
{ tag: "b", attrs: { foo: "bar" }, children: [] },
{
tag: "c",
attrs: { foo: "bar" },
children: [
{ tag: "d", attrs: {}, children: [] },
"string-child",
],
},
{
tag: "f",
attrs: {
"interpolated key": "interpolated value",
objKey: "objVal",
objKey2: "objVal2",
},
children: [],
},
]);
});