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

Commit

Permalink
feat: task tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Apr 12, 2021
1 parent 57d9a84 commit 0da0ab9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
10 changes: 7 additions & 3 deletions task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import { Lazy } from "./types.ts";

import { apply, flow, wait } from "./fns.ts";
import { apply, flow, wait, identity } from "./fns.ts";
import { createDo } from "./derivations.ts";

/*******************************************************************************
Expand Down Expand Up @@ -30,6 +30,8 @@ declare module "./hkt.ts" {
* Combinators
******************************************************************************/

export const make = <A>(a: A): Task<A> => () => Promise.resolve(a);

export const delay = (ms: number) =>
<A>(ma: Task<A>): Task<A> => () => wait(ms).then(ma);

Expand Down Expand Up @@ -75,7 +77,7 @@ export const Monad: TC.Monad<URI> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: (tta) => () => tta().then(apply()),
join: Chain.chain(identity),
chain: Chain.chain,
};

Expand All @@ -92,7 +94,7 @@ export const MonadSeq: TC.Monad<URI> = {
of: Applicative.of,
ap: ApplySeq.ap,
map: Functor.map,
join: (tta) => () => tta().then(apply()),
join: Chain.chain(identity),
chain: Chain.chain,
};

Expand All @@ -102,6 +104,8 @@ export const MonadSeq: TC.Monad<URI> = {

export const { of, ap, map, join, chain } = Monad;

export const { ap: apSeq } = ApplySeq;

/*******************************************************************************
* Do Notation
******************************************************************************/
Expand Down
69 changes: 69 additions & 0 deletions testing/task.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import * as T from "../task.ts";
import { _, pipe } from "../fns.ts";

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

const assertEqualsT = async (a: T.Task<unknown>, b: T.Task<unknown>) =>
assertEquals(await a(), await b());

Deno.test("Task make", async () => {
await assertEqualsT(T.make(0), T.make(0));
});

Deno.test("Task delay", async () => {
await assertEqualsT(pipe(T.make(0), T.delay(200)), T.make(0));
});

Deno.test("Task fromThunk", async () => {
await assertEqualsT(T.fromThunk(() => 0), T.make(0));
});

Deno.test("Task tryCatch", async () => {
await assertEqualsT(T.tryCatch(_, () => 0), T.make(0));
await assertEqualsT(T.tryCatch(() => 1, () => 0), T.make(1));
});

Deno.test("Task of", async () => {
await assertEqualsT(T.of(1), T.make(1));
});

Deno.test("Task ap", async () => {
await assertEqualsT(pipe(T.of(1), T.ap(T.of(AS.add))), T.of(2));
});

Deno.test("Task map", async () => {
await assertEqualsT(pipe(T.of(1), T.map(AS.add)), T.of(2));
});

Deno.test("Task join", async () => {
await assertEqualsT(T.join(T.of(T.of(1))), T.of(1));
});

Deno.test("Task chain", async () => {
await assertEqualsT(pipe(T.of(1), T.chain((n) => T.of(n + 1))), T.of(2));
});

Deno.test("Task apSeq", async () => {
await assertEqualsT(pipe(T.of(1), T.apSeq(T.of(AS.add))), T.of(2));
});

Deno.test("Task Do, bind, bindTo", () => {
assertEqualsT(
pipe(
T.Do<number, number, number>(),
T.bind("one", () => T.make(1)),
T.bind("two", ({ one }) => T.make(one + one)),
T.map(({ one, two }) => one + two),
),
T.make(3),
);
assertEqualsT(
pipe(
T.make(1),
T.bindTo("one"),
),
T.make({ one: 1 }),
);
});

0 comments on commit 0da0ab9

Please sign in to comment.