forked from getappmap/appmap-agent-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__fixture__.mjs
69 lines (64 loc) · 1.55 KB
/
__fixture__.mjs
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 { strict as Assert } from "node:assert";
const {
undefined,
Error,
Infinity,
JSON: { stringify: stringifyJSON },
Array: { isArray },
Reflect: { ownKeys },
} = globalThis;
Error.stackTraceLimit = Infinity;
export const {
ok: assert,
match: assertMatch,
fail: assertFail,
deepEqual: assertDeepEqual,
equal: assertEqual,
throws: assertThrow,
rejects: assertReject,
notEqual: assertNotEqual,
} = Assert;
const matchJSON = (json, pattern, path) => {
if (isArray(pattern)) {
assertEqual(isArray(json), true, path);
assertEqual(pattern.length <= json.length, true, path);
for (let index = 0; index < pattern.length; index += 1) {
matchJSON(json[index], pattern[index], `${path}/${index}`);
}
} else if (typeof pattern === "object" && pattern !== null) {
assertEqual(typeof json, "object", path);
assertNotEqual(json, null, path);
for (const key of ownKeys(pattern)) {
matchJSON(json[key], pattern[key], `${path}/${key}`);
}
} else {
assertEqual(
json === null ||
typeof json === "boolean" ||
typeof json === "number" ||
typeof json === "string",
true,
path,
);
if (pattern !== undefined) {
assertEqual(json, pattern, path);
}
}
};
export const assertMatchJSON = (json, pattern) => {
try {
matchJSON(json, pattern, "");
} catch ({ message }) {
throw new Error(
stringifyJSON(
{
message,
actual: json,
expected: pattern,
},
null,
2,
),
);
}
};