functional/comparison.test.ts

58 lines
1.6 KiB
TypeScript

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { bool, cmp, is } from "./comparison.ts";
Deno.test("cmp", () => {
assertEquals([8, 3, 5].sort(cmp()), [3, 5, 8]);
assertEquals([8, 3, 5, 8].sort(cmp({ reverse: true })), [8, 8, 5, 3]);
assertEquals([-2, 8, -7].sort(cmp({ key: Math.abs })), [-2, -7, 8]);
assertEquals(["c", "a", "b"].sort(cmp()), ["a", "b", "c"]);
});
Deno.test("is", () => {
const f1 = is(5);
assertEquals(f1(5), true);
assertEquals(f1(4), false);
assertEquals(f1("a" as any), false);
assertEquals(f1(null as any), false);
assertEquals(f1(undefined as any), false);
const obj: { x: number } = { x: 1 };
let f2 = is(obj);
assertEquals(f2(obj), true);
assertEquals(f2({ x: 1 }), false);
assertEquals(f2({ y: 1 } as any), false);
});
Deno.test("bool", () => {
assertEquals(bool(null), false);
assertEquals(bool(undefined), false);
assertEquals(bool(false), false);
assertEquals(bool(true), true);
assertEquals(bool(-1), true);
assertEquals(bool(0), false);
assertEquals(bool(1), true);
assertEquals(bool(""), false);
assertEquals(bool(" "), true);
assertEquals(bool("abc"), true);
assertEquals(bool([]), false);
assertEquals(bool([0]), true);
assertEquals(bool([1, 2, 3]), true);
assertEquals(bool(new Set()), false);
assertEquals(bool(new Set([0])), true);
assertEquals(bool(new Set([1, 2, 3])), true);
assertEquals(bool({}), false);
assertEquals(bool({ a: 5 }), true);
class Obj1 {}
class Obj2 {
private x = 0;
}
assertEquals(bool(new Obj1()), false);
assertEquals(bool(new Obj2()), true);
});