Updated testing tools

This commit is contained in:
Michaël Lemaire 2020-05-13 11:46:10 +02:00
parent d64e330da1
commit 59ca64e7b2
8 changed files with 132 additions and 135 deletions

View file

@ -1,6 +1,6 @@
root = true
[*.ts]
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true

View file

@ -1,58 +1,58 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.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"]);
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"]);
});
Deno.test("is", () => {
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);
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);
assertEquals(f2(obj), true);
assertEquals(f2({ x: 1 }), false);
assertEquals(f2({ y: 1 } as any), false);
expect(f2(obj)).toBe(true);
expect(f2({ x: 1 })).toBe(false);
expect(f2({ y: 1 } as any)).toBe(false);
});
Deno.test("bool", () => {
assertEquals(bool(null), false);
assertEquals(bool(undefined), false);
test("bool", () => {
expect(bool(null)).toBe(false);
expect(bool(undefined)).toBe(false);
assertEquals(bool(false), false);
assertEquals(bool(true), true);
expect(bool(false)).toBe(false);
expect(bool(true)).toBe(true);
assertEquals(bool(-1), true);
assertEquals(bool(0), false);
assertEquals(bool(1), true);
expect(bool(-1)).toBe(true);
expect(bool(0)).toBe(false);
expect(bool(1)).toBe(true);
assertEquals(bool(""), false);
assertEquals(bool(" "), true);
assertEquals(bool("abc"), true);
expect(bool("")).toBe(false);
expect(bool(" ")).toBe(true);
expect(bool("abc")).toBe(true);
assertEquals(bool([]), false);
assertEquals(bool([0]), true);
assertEquals(bool([1, 2, 3]), true);
expect(bool([])).toBe(false);
expect(bool([0])).toBe(true);
expect(bool([1, 2, 3])).toBe(true);
assertEquals(bool(new Set()), false);
assertEquals(bool(new Set([0])), true);
assertEquals(bool(new Set([1, 2, 3])), true);
expect(bool(new Set())).toBe(false);
expect(bool(new Set([0]))).toBe(true);
expect(bool(new Set([1, 2, 3]))).toBe(true);
assertEquals(bool({}), false);
assertEquals(bool({ a: 5 }), true);
expect(bool({})).toBe(false);
expect(bool({ a: 5 })).toBe(true);
class Obj1 {}
class Obj2 {
private x = 0;
}
assertEquals(bool(new Obj1()), false);
assertEquals(bool(new Obj2()), true);
expect(bool(new Obj1())).toBe(false);
expect(bool(new Obj2())).toBe(true);
});

View file

@ -1,30 +1,30 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.ts";
import { identity, nop, partial, pipe } from "./composition.ts";
Deno.test("nop", () => {
assertEquals(nop(), undefined);
test("nop", () => {
expect(nop()).toBeUndefined();
});
Deno.test("identity", () => {
assertEquals(identity(5), 5);
assertEquals(identity(null), null);
test("identity", () => {
expect(identity(5)).toBe(5);
expect(identity(null)).toBe(null);
});
Deno.test("partial", () => {
test("partial", () => {
const func1 = (conf: { a: number; b: string; c: number }) =>
`${conf.a}${conf.b}${conf.c}`;
const pfunc1 = partial({ b: "test" }, func1);
assertEquals(pfunc1({ a: 5, c: 8 }), "5test8");
expect(pfunc1({ a: 5, c: 8 })).toEqual("5test8");
const func2 = (conf: { a: number; b: string }, c: number) =>
`${conf.a}${conf.b}${c}`;
const pfunc2 = partial({ b: "test" }, func2);
assertEquals(pfunc2({ a: 2 }, 3), "2test3");
expect(pfunc2({ a: 2 }, 3)).toEqual("2test3");
});
Deno.test("pipe", () => {
test("pipe", () => {
const f = pipe((x: number) => x * 2, (x: number) => x + 1);
assertEquals(f(1), 3);
assertEquals(f(2), 5);
assertEquals(f(3), 7);
expect(f(1)).toBe(3);
expect(f(2)).toBe(5);
expect(f(3)).toBe(7);
});

1
deps.test.ts Normal file
View file

@ -0,0 +1 @@
export * from "https://code.thunderk.net/typescript/deno_tools/raw/1.0.0/testing.ts";

View file

@ -1,47 +1,46 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.ts";
import { at, first, fmap, last, second, third } from "./iterables.ts";
Deno.test("at", () => {
test("at", () => {
const second = at(1);
assertEquals(second([1, 4, 8]), 4);
assertEquals(second([1]), undefined);
expect(second([1, 4, 8])).toBe(4);
expect(second([1])).toBeUndefined();
const second_from_end = at(-2);
assertEquals(second_from_end([1, 4, 6, 8]), 6);
assertEquals(second_from_end([1]), undefined);
expect(second_from_end([1, 4, 6, 8])).toBe(6);
expect(second_from_end([1])).toBeUndefined();
});
Deno.test("first", () => {
assertEquals(first([1, 4, 8]), 1);
assertEquals(first([1]), 1);
assertEquals(first([]), undefined);
test("first", () => {
expect(first([1, 4, 8])).toBe(1);
expect(first([1])).toBe(1);
expect(first([])).toBeUndefined();
});
Deno.test("second", () => {
assertEquals(second([1, 4, 8]), 4);
assertEquals(second([1]), undefined);
assertEquals(second([]), undefined);
test("second", () => {
expect(second([1, 4, 8])).toBe(4);
expect(second([1])).toBeUndefined();
expect(second([])).toBeUndefined();
});
Deno.test("third", () => {
assertEquals(third([1, 4, 8]), 8);
assertEquals(third([1, 4]), undefined);
assertEquals(third([]), undefined);
test("third", () => {
expect(third([1, 4, 8])).toBe(8);
expect(third([1, 4])).toBeUndefined();
expect(third([])).toBeUndefined();
});
Deno.test("last", () => {
assertEquals(last([1, 4, 8]), 8);
assertEquals(last([1]), 1);
assertEquals(last([]), undefined);
test("last", () => {
expect(last([1, 4, 8])).toBe(8);
expect(last([1])).toBe(1);
expect(last([])).toBeUndefined();
});
Deno.test("fmap", () => {
assertEquals(fmap()([1, 2, 3]), [1, 2, 3]);
assertEquals(fmap((x: number) => x * 2)([1, 2, 3]), [2, 4, 6]);
assertEquals(fmap(undefined, (x: number) => x % 2 == 0)([1, 2, 3]), [2]);
assertEquals(fmap((x: number) => x * 2)([1, 2, 3]), [2, 4, 6]);
assertEquals(
fmap((x: number) => x * 2, (x: number) => x < 5)([1, 2, 3]),
test("fmap", () => {
expect(fmap()([1, 2, 3])).toEqual([1, 2, 3]);
expect(fmap((x: number) => x * 2)([1, 2, 3])).toEqual([2, 4, 6]);
expect(fmap(undefined, (x: number) => x % 2 == 0)([1, 2, 3])).toEqual([2]);
expect(fmap((x: number) => x * 2)([1, 2, 3])).toEqual([2, 4, 6]);
expect(fmap((x: number) => x * 2, (x: number) => x < 5)([1, 2, 3])).toEqual(
[2, 4],
);
});

View file

@ -1,8 +1,8 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.ts";
import { attr } from "./objects.ts";
Deno.test("attr", () => {
test("attr", () => {
const getx = attr("x");
assertEquals(getx({ x: 4, y: 5 }), 4);
assertEquals(getx({ x: undefined, y: 5 }), undefined);
expect(getx({ x: 4, y: 5 })).toBe(4);
expect(getx({ x: undefined, y: 5 })).toBeUndefined();
});

View file

@ -1,43 +1,43 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.ts";
import { always, and, never, not, or } from "./predicates.ts";
Deno.test("always", () => {
assertEquals(always(), true);
assertEquals(always(true), true);
assertEquals(always(false), true);
test("always", () => {
expect(always()).toBe(true);
expect(always(true)).toBe(true);
expect(always(false)).toBe(true);
});
Deno.test("never", () => {
assertEquals(never(), false);
assertEquals(never(true), false);
assertEquals(never(false), false);
test("never", () => {
expect(never()).toBe(false);
expect(never(true)).toBe(false);
expect(never(false)).toBe(false);
});
Deno.test("not", () => {
test("not", () => {
const iseven = (x: number) => x % 2 == 0;
const isodd = not(iseven);
assertEquals(iseven(0), true);
assertEquals(isodd(0), false);
assertEquals(iseven(1), false);
assertEquals(isodd(1), true);
assertEquals(iseven(2), true);
assertEquals(isodd(2), false);
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);
});
Deno.test("and", () => {
test("and", () => {
const f = and((x: number) => x % 2 == 0, (x: number) => x > 5);
assertEquals(f(0), false);
assertEquals(f(2), false);
assertEquals(f(8), true);
assertEquals(f(9), false);
assertEquals(f(10), true);
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);
});
Deno.test("or", () => {
test("or", () => {
const f = or((x: number) => x % 2 == 0, (x: number) => x > 5);
assertEquals(f(0), true);
assertEquals(f(1), false);
assertEquals(f(2), true);
assertEquals(f(8), true);
assertEquals(f(9), true);
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);
});

View file

@ -1,10 +1,7 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
import { expect, test } from "./deps.test.ts";
import { isinstance, nn, nnu, nu } from "./typing.ts";
Deno.test("isinstance", () => {
test("isinstance", () => {
class A {}
class A1 extends A {}
class A2 extends A {}
@ -12,29 +9,29 @@ Deno.test("isinstance", () => {
const f: any[] = [5, null, undefined, new A(), new A1(), new B(), new A2()];
const result = f.filter(isinstance(A));
assertEquals(result, [f[3], f[4], f[6]]);
expect(result).toEqual([f[3], f[4], f[6]]);
});
Deno.test("nn", () => {
assertEquals(nn(undefined), undefined);
assertEquals(nn(0), 0);
assertEquals(nn(""), "");
assertEquals(nn([]), []);
assertThrows(() => nn(null), Error, "null value");
test("nn", () => {
expect(nn(undefined)).toBeUndefined();
expect(nn(0)).toBe(0);
expect(nn("")).toBe("");
expect(nn([])).toEqual([]);
expect(() => nn(null)).toThrow("null value");
});
Deno.test("nu", () => {
assertEquals(nu(null), null);
assertEquals(nu(0), 0);
assertEquals(nu(""), "");
assertEquals(nu([]), []);
assertThrows(() => nu(undefined), Error, "undefined value");
test("nu", () => {
expect(nu(null)).toBe(null);
expect(nu(0)).toBe(0);
expect(nu("")).toBe("");
expect(nu([])).toEqual([]);
expect(() => nu(undefined)).toThrow("undefined value");
});
Deno.test("nnu", () => {
assertEquals(nnu(0), 0);
assertEquals(nnu(""), "");
assertEquals(nnu([]), []);
assertThrows(() => nnu(undefined), Error, "undefined value");
assertThrows(() => nnu(null), Error, "null value");
test("nnu", () => {
expect(nnu(0)).toBe(0);
expect(nnu("")).toBe("");
expect(nnu([])).toEqual([]);
expect(() => nnu(undefined)).toThrow("undefined value");
expect(() => nnu(null)).toThrow("null value");
});