1
0
Fork 0
functional/predicates.test.ts

44 lines
1.1 KiB
TypeScript

import { expect, it } from "./deps.testing.ts";
import { always, and, never, not, or } from "./predicates.ts";
it("always", () => {
expect(always()).toBe(true);
expect(always(true)).toBe(true);
expect(always(false)).toBe(true);
});
it("never", () => {
expect(never()).toBe(false);
expect(never(true)).toBe(false);
expect(never(false)).toBe(false);
});
it("not", () => {
const iseven = (x: number) => x % 2 == 0;
const isodd = not(iseven);
expect(iseven(0)).toBe(true);
expect(isodd(0)).toBe(false);
expect(iseven(1)).toBe(false);
expect(isodd(1)).toBe(true);
expect(iseven(2)).toBe(true);
expect(isodd(2)).toBe(false);
});
it("and", () => {
const f = and((x: number) => x % 2 == 0, (x: number) => x > 5);
expect(f(0)).toBe(false);
expect(f(2)).toBe(false);
expect(f(8)).toBe(true);
expect(f(9)).toBe(false);
expect(f(10)).toBe(true);
});
it("or", () => {
const f = or((x: number) => x % 2 == 0, (x: number) => x > 5);
expect(f(0)).toBe(true);
expect(f(1)).toBe(false);
expect(f(2)).toBe(true);
expect(f(8)).toBe(true);
expect(f(9)).toBe(true);
});