1
0
Fork 0
functional/predicates.test.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-05-13 09:46:10 +00:00
import { expect, test } from "./deps.test.ts";
2020-05-11 22:34:57 +00:00
import { always, and, never, not, or } from "./predicates.ts";
2020-05-13 09:46:10 +00:00
test("always", () => {
expect(always()).toBe(true);
expect(always(true)).toBe(true);
expect(always(false)).toBe(true);
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("never", () => {
expect(never()).toBe(false);
expect(never(true)).toBe(false);
expect(never(false)).toBe(false);
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("not", () => {
2020-05-11 22:34:57 +00:00
const iseven = (x: number) => x % 2 == 0;
const isodd = not(iseven);
2020-05-13 09:46:10 +00:00
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);
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("and", () => {
2020-05-11 22:34:57 +00:00
const f = and((x: number) => x % 2 == 0, (x: number) => x > 5);
2020-05-13 09:46:10 +00:00
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);
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("or", () => {
2020-05-11 22:34:57 +00:00
const f = or((x: number) => x % 2 == 0, (x: number) => x > 5);
2020-05-13 09:46:10 +00:00
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);
2020-05-11 22:34:57 +00:00
});