Eric Bower
·
30 Jul 24
test.ts
1export { assert } from "https://deno.land/std@0.187.0/testing/asserts.ts";
2export {
3 afterAll,
4 beforeAll,
5 beforeEach,
6 describe,
7 it,
8} from "https://deno.land/std@0.163.0/testing/bdd.ts";
9export * as asserts from "https://deno.land/std@0.185.0/testing/asserts.ts";
10export { expect } from "https://deno.land/x/expect@v0.3.0/mod.ts";
11export { install, mock } from "https://deno.land/x/mock_fetch@0.3.0/mod.ts";
12
13export function isLikeSelector(selector: unknown) {
14 return (
15 selector !== null &&
16 typeof selector === "object" &&
17 Reflect.getPrototypeOf(selector) === Object.prototype &&
18 Reflect.ownKeys(selector).length > 0
19 );
20}
21
22export const CIRCULAR_SELECTOR = new Error("Encountered a circular selector");
23
24export function assertLike(
25 lhs: Record<any, any>,
26 selector: Record<any, any>,
27 circular = new Set(),
28) {
29 if (circular.has(selector)) {
30 throw CIRCULAR_SELECTOR;
31 }
32
33 circular.add(selector);
34
35 if (lhs === null || typeof lhs !== "object") {
36 return lhs;
37 }
38
39 const comparable: Record<any, any> = {};
40 for (const [key, rhs] of Object.entries(selector)) {
41 if (isLikeSelector(rhs)) {
42 comparable[key] = assertLike(Reflect.get(lhs, key), rhs, circular);
43 } else {
44 comparable[key] = Reflect.get(lhs, key);
45 }
46 }
47
48 return comparable;
49}