-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ts
49 lines (42 loc) · 1.16 KB
/
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
export {
afterAll,
beforeAll,
beforeEach,
describe,
it,
} from "jsr:@std/testing/bdd";
export { assert } from "jsr:@std/assert";
export * as asserts from "jsr:@std/assert";
export { expect } from "jsr:@std/expect";
export { install, mock } from "https://deno.land/x/[email protected]/mod.ts";
export function isLikeSelector(selector: unknown) {
return (
selector !== null &&
typeof selector === "object" &&
Reflect.getPrototypeOf(selector) === Object.prototype &&
Reflect.ownKeys(selector).length > 0
);
}
export const CIRCULAR_SELECTOR = new Error("Encountered a circular selector");
export function assertLike(
lhs: Record<any, any>,
selector: Record<any, any>,
circular = new Set(),
) {
if (circular.has(selector)) {
throw CIRCULAR_SELECTOR;
}
circular.add(selector);
if (lhs === null || typeof lhs !== "object") {
return lhs;
}
const comparable: Record<any, any> = {};
for (const [key, rhs] of Object.entries(selector)) {
if (isLikeSelector(rhs)) {
comparable[key] = assertLike(Reflect.get(lhs, key), rhs, circular);
} else {
comparable[key] = Reflect.get(lhs, key);
}
}
return comparable;
}