Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
feat: reader tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Apr 11, 2021
1 parent dc6978d commit 4bbd7ec
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 53 deletions.
4 changes: 3 additions & 1 deletion reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ declare module "./hkt.ts" {
* Constructors
******************************************************************************/

export const make: <R>(r: R) => Reader<R, R> = constant;

export const ask: <R>() => Reader<R, R> = () => identity;

export const asks: <R, A>(f: (r: R) => A) => Reader<R, A> = identity;
Expand All @@ -42,7 +44,7 @@ export const Functor: TC.Functor<URI> = {
};

export const Apply: TC.Apply<URI> = {
ap: (tfab) => (ta) => (r) => pipe(ta(r), tfab(r)),
ap: (tfai) => (ta) => (r) => pipe(ta(r), tfai(r)),
map: Functor.map,
};

Expand Down
44 changes: 1 addition & 43 deletions testing/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type MonadTest<
* Utility Functions
******************************************************************************/

export const add = (n: number) => n + n;
export const add = (n: number) => n + 1;

export const multiply = (n: number) => n * n;

Expand Down Expand Up @@ -830,45 +830,3 @@ export const assertComonad = <
* Assert: Traversable
* TODO
******************************************************************************/

/*******************************************************************************
* Assert: Monad Helper
******************************************************************************/

export const testMonad = async <URI extends URIS>(
M: TC.Monad<URI>,
// deno-lint-ignore no-explicit-any
eq: (
a: Kind<URI, [any, any, any, any]>,
b: Kind<URI, [any, any, any, any]>,
) => Promise<boolean>,
) => {
const ta = M.of(1);
const tb = M.of(2);
const fai = (n: number) => n + 1;
const tfai = M.of(fai);
const fati = (n: number) => M.of(n + 1);

// of
assertEquals(await eq(ta, ta), true);
assertEquals(await eq(ta, tb), false);

// ap
const r1 = pipe(ta, M.ap(tfai));
assertEquals(await eq(r1, ta), false);
assertEquals(await eq(r1, tb), true);

// map
const r2 = pipe(ta, M.map(fai));
assertEquals(await eq(r2, ta), false);
assertEquals(await eq(r2, tb), true);

// join
assertEquals(await eq(M.join(M.of(ta)), ta), true);
assertEquals(await eq(M.join(M.of(ta)), tb), false);

// chain
const r3 = pipe(ta, M.chain(fati));
assertEquals(await eq(r3, ta), false);
assertEquals(await eq(r3, tb), true);
};
9 changes: 0 additions & 9 deletions testing/either.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import type * as HKT from "../hkt.ts";

import * as E from "../either.ts";
import * as O from "../option.ts";
import { ordNumber } from "../ord.ts";
Expand Down Expand Up @@ -205,7 +203,6 @@ Deno.test("Either getRightMonoid", () => {

Deno.test("Either getRightMonad", () => {
const Monad = E.getRightMonad(semigroupSum);
const { equals } = E.getSetoid(setoidNumber, setoidNumber);

AS.assertMonad(Monad, {
a: 1,
Expand All @@ -217,8 +214,6 @@ Deno.test("Either getRightMonad", () => {
tfai: E.right((n: number) => n + 1),
tfij: E.right((n: number) => n + 1),
});

AS.testMonad(Monad, (a, b) => Promise.resolve(equals(a)(b)));
});

Deno.test("Either Functor", () => {
Expand Down Expand Up @@ -264,8 +259,6 @@ Deno.test("Either Chain", () => {
});

Deno.test("Either Monad", () => {
const { equals } = E.getSetoid(setoidNumber, setoidNumber);

AS.assertMonad(E.Monad, {
a: 1,
ta: E.right(1),
Expand All @@ -276,8 +269,6 @@ Deno.test("Either Monad", () => {
fati: (n: number) => E.right(n),
fitj: (n: number) => E.right(n),
});

AS.testMonad(E.Monad, (a, b) => Promise.resolve(equals(a)(b)));
});

Deno.test("Either Bifunctor", () => {
Expand Down
59 changes: 59 additions & 0 deletions testing/reader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import * as R from "../reader.ts";
import { pipe } from "../fns.ts";

import * as AS from "./assert.ts";

// deno-lint-ignore no-explicit-any
const assertEqualsR = (a: R.Reader<number, any>, b: R.Reader<number, any>) =>
assertEquals(a(0), b(0));

Deno.test("Reader ask", () => {
assertEqualsR(R.ask<number>(), R.ask<number>());
});

Deno.test("Reader asks", () => {
assertEqualsR(R.asks(AS.add), R.asks(AS.add));
});

Deno.test("Reader of", () => {
assertEqualsR(R.of(0), R.ask());
});

Deno.test("Reader ap", () => {
assertEqualsR(pipe(R.make(0), R.ap(R.of(AS.add))), R.make(1));
});

Deno.test("Reader map", () => {
assertEqualsR(pipe(R.make(0), R.map(AS.add)), R.make(1));
});

Deno.test("Reader join", () => {
const tta = R.asks((n: number) => R.make(n));
assertEquals(R.join(tta)(0), 0);
});

Deno.test("Reader chain", () => {
const chain = R.chain((n: number) => R.make(n + 1));
assertEqualsR(chain(R.make(0)), R.make(1));
});

Deno.test("Reader Do, bind, bindTo", () => {
assertEqualsR(
pipe(
R.Do<number, number, number>(),
R.bind("one", () => R.make(1)),
R.bind("two", ({ one }) => R.make(one + one)),
R.map(({ one, two }) => one + two),
),
R.make(3),
);
assertEqualsR(
pipe(
R.make(1),
R.bindTo("one"),
),
R.asks((_: number) => ({ one: 1 })),
);
});

0 comments on commit 4bbd7ec

Please sign in to comment.