diff options
Diffstat (limited to 'tests/js/utils.js')
| -rw-r--r-- | tests/js/utils.js | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/tests/js/utils.js b/tests/js/utils.js new file mode 100644 index 0000000..1ecc0a4 --- /dev/null +++ b/tests/js/utils.js @@ -0,0 +1,135 @@ +import { eq, keys } from "../../src/utils.js"; + +const test_eq = t => { + t.start("eq()"); + t.test("scalar values equality", () => { + t.assert(eq(0, 0)); + t.assert(eq(100, 100)); + t.assert(eq(1.5, 1.5)); + t.assert(eq(-9, -9)); + + t.assert(!eq(0, 1)); + t.assert(!eq(100, 99)); + t.assert(!eq(1.5, 1.4)); + t.assert(!eq(-9, 9)); + + + t.assert(eq(null, null)); + t.assert(eq(undefined, undefined)); + t.assert(eq("", "")); + t.assert(eq("a string", "a string")); + + t.assert(!eq(null, undefined)); + t.assert(!eq(undefined, 0)); + t.assert(!eq("", "0")); + t.assert(!eq("a string", "another string")); + + + t.assert(eq(true, true)); + t.assert(eq(false, false)); + + t.assert(!eq(true, false)); + + + t.assert(eq(1n, 1n)); + t.assert(eq(99999999999999n, 99999999999999n)); + + // t.assert(eq(1, 1n)); + }); + + t.test("array equality", () => { + t.assert(eq([], [])); + + + t.assert(eq([0, 1, 2], [0, 1, 2])); + t.assert(eq([0, 1, 2], new Array(0, 1, 2))); + + t.assert(!eq([0, 1, 2], [0, 1])); + t.assert(!eq([0, 1], new Array(0, 1, 2))); + + + t.assert(eq([undefined], [undefined])); + t.assert(eq([null, 0, "", true], [null, 0, "", true])); + + + t.assert(eq([[[[0]]]], [[[[0]]]])); + + t.assert(!eq([[[[0]]]], [0])); + }); + + t.test("object equality", () => { + t.assert(eq({}, {})); + t.assert(eq({ a: 1 }, { a: 1 })); + + t.assert(!eq({ a: 1, b: undefined }, { a: 1 })); + + + t.assert(eq( + { a: 1, b: { c: { d: "e" } } }, + { a: 1, b: { c: { d: "e" } } }, + )); + + class C {} + // ... + }); + + t.test("mixed values", () => { + t.assert(eq( + {a: ["", 1, 2, 3, [{ b: { c: [ "d", "e" ]}}]]}, + {a: ["", 1, 2, 3, [{ b: { c: [ "d", "e" ]}}]]}, + )); + + t.assert(!eq(null, {})); + + t.assert(!eq([], {})); + + + t.assert(eq(new Date(123), new Date(123))); + t.assert(eq({ d: new Date(123) }, { d: new Date(123) })); + + // FIXME + // t.assert(!eq(new Date(123), new Date(321))); + // t.assert(!eq({ d: new Date(123) }, { d: new Date(321) })); + }); +}; + +const test_keys = t => { + t.start("keys()"); + t.test("happy paths", () => { + t.assertEq( + { a: 1, b: 2 }, + keys(["a", "b"], { a: 1, b: 2, c: 3 }), + ); + }); + + t.test("stress scenarios", () => { + t.assertEq( + {}, + keys([], {}), + "empty selection of empty object", + ); + + t.assertEq( + {}, + keys([], {a: 1}), + "empty selection of non-empty object", + ); + + t.assertEq( + {}, + keys(["a"], {}), + "non-empty selection of empty object", + ); + + t.assertEq( + { a: undefined, b: null }, + keys(["a", "b", "c"], { a: undefined, b: null }), + "falsy values", + ); + }); +}; + +export const tests = [ + test_eq, + test_keys, +]; |
