58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { expect, test } from "./deps.test.ts";
|
|
import { bool, cmp, is } from "./comparison.ts";
|
|
|
|
test("cmp", () => {
|
|
expect([8, 3, 5].sort(cmp())).toEqual([3, 5, 8]);
|
|
expect([8, 3, 5, 8].sort(cmp({ reverse: true }))).toEqual([8, 8, 5, 3]);
|
|
expect([-2, 8, -7].sort(cmp({ key: Math.abs }))).toEqual([-2, -7, 8]);
|
|
expect(["c", "a", "b"].sort(cmp())).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("is", () => {
|
|
const f1 = is(5);
|
|
expect(f1(5)).toBe(true);
|
|
expect(f1(4)).toBe(false);
|
|
expect(f1("a" as any)).toBe(false);
|
|
expect(f1(null as any)).toBe(false);
|
|
expect(f1(undefined as any)).toBe(false);
|
|
|
|
const obj: { x: number } = { x: 1 };
|
|
let f2 = is(obj);
|
|
expect(f2(obj)).toBe(true);
|
|
expect(f2({ x: 1 })).toBe(false);
|
|
expect(f2({ y: 1 } as any)).toBe(false);
|
|
});
|
|
|
|
test("bool", () => {
|
|
expect(bool(null)).toBe(false);
|
|
expect(bool(undefined)).toBe(false);
|
|
|
|
expect(bool(false)).toBe(false);
|
|
expect(bool(true)).toBe(true);
|
|
|
|
expect(bool(-1)).toBe(true);
|
|
expect(bool(0)).toBe(false);
|
|
expect(bool(1)).toBe(true);
|
|
|
|
expect(bool("")).toBe(false);
|
|
expect(bool(" ")).toBe(true);
|
|
expect(bool("abc")).toBe(true);
|
|
|
|
expect(bool([])).toBe(false);
|
|
expect(bool([0])).toBe(true);
|
|
expect(bool([1, 2, 3])).toBe(true);
|
|
|
|
expect(bool(new Set())).toBe(false);
|
|
expect(bool(new Set([0]))).toBe(true);
|
|
expect(bool(new Set([1, 2, 3]))).toBe(true);
|
|
|
|
expect(bool({})).toBe(false);
|
|
expect(bool({ a: 5 })).toBe(true);
|
|
|
|
class Obj1 {}
|
|
class Obj2 {
|
|
private x = 0;
|
|
}
|
|
expect(bool(new Obj1())).toBe(false);
|
|
expect(bool(new Obj2())).toBe(true);
|
|
});
|