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
|
import {
leafValues,
matchRoute,
} from "../src/content/sw.exported.js";
const test_leafValues = t => {
t.start("leafValues()");
t.test("noop on empty object", () => {
t.assert.deepEqual(leafValues({}), []);
});
t.test("flat array for flat tree", () => {
});
t.test("array of empty arrays for leafless tree", () => {
t.assert.deepEqual(leafValues({ a: {} }), [[]]);
});
};
const test_buildTable = t => {
t.start("buildTable()");
t.test("", () => {
});
};
const test_matchRoutes = t => {
t.start("matchRoutes()");
t.test("null on empty values", () => {
t.assert.equal(matchRoute([""], {}), null);
t.assert.equal(matchRoute(["", ""], {}), null);
t.assert.equal(matchRoute(["some", ""], {}), null);
t.assert.equal(matchRoute(["path", ""], undefined), null);
t.assert.equal(matchRoute([""], { k: "v" }), null);
t.assert.equal(matchRoute([""], {some:{nested:"values"}}), null);
});
t.test("shallow routes match returns the matched value", () => {
const table = {
boolean: { "": "values" },
true: { "": true },
false: { "": false },
string: { "": "a-string" },
number: { "": 123 },
};
t.assert.equal(matchRoute(["boolean", ""], table), "values");
t.assert.equal(matchRoute(["true", ""], table), true);
t.assert.equal(matchRoute(["false", ""], table), false);
t.assert.equal(matchRoute(["string", ""], table), "a-string");
t.assert.equal(matchRoute(["number", ""], table), 123);
});
t.test("special case for `null` provides equivalent value", () => {
// given that `null` us an object
t.assert.equal(typeof null, typeof {});
t.assert.equal(typeof null, "object");
// the else case of the recursion base returns a differenc null
// from the one in the `routes` object.
const table = {
path: {
"": null,
},
};
t.assert.equal(matchRoute(["path", ""], table), null);
});
t.test("we can match nested routes", () => {
const table = {
p0: {
p1: {
"": "match",
},
},
};
t.assert.equal(matchRoute(["not", "valid", ""], table), null);
t.assert.equal(matchRoute(["p0", ""], table), null);
t.assert.equal(matchRoute(["p0", "p1", ""], table), "match");
t.assert.equal(matchRoute([ "p1", ""], table), null);
});
t.test("longer routes have more priority than shorter ones", () => {
// FIXME
// /root/* => catchall
// /root/skip/leaf =>
const table = {
root: {
"": "lower",
"*": {
"": "catchall",
},
skip: {
leaf: {
"": "higher",
},
},
},
};
t.assert.equal(
matchRoute(["root", "skip", "leaf", ""], table),
"higher",
);
t.assert.equal(
matchRoute(["root", "skip", ""], table),
"catchall",
);
t.assert.equal(
matchRoute(["root", ""], table),
"lower",
);
});
t.test("catchall at root captures everything", () => {
const routes = {
"*": {
"": "all",
},
};
t.assert.equal(
matchRoute(["anything", "goes", "here", ""], routes),
"all",
);
t.assert.equal(matchRoute(["here", ""], routes), "all");
t.assert.equal(matchRoute(["here", ""], routes), "all");
t.assert.equal(matchRoute([""], routes), "all");
});
};
export const allTests = [
test_leafValues,
test_buildTable,
test_matchRoutes,
];
|