Skip to content

Commit

Permalink
feat: test monad base
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftdorian committed Dec 5, 2021
1 parent ecd86f2 commit 9e0816b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions __tests__/monad/base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Maybe, Just, Fun, lift, bind, pure } from '../../src';

describe('monad base', () => {
describe('lift', () => {
test('returns applicative pure result', () => {
expect(lift(1).valueOf()).toEqual(pure(1).valueOf());
});
});

test('left identity', () => {
const fun: Fun<number, Maybe<number>> = (x: number) => new Just(x + 1);
const x = 1;
expect(bind(lift(x), fun).valueOf()).toEqual(fun(x).valueOf());
});

test('right identity', () => {
const stringJust: Maybe<string> = new Just('hello!');
expect(bind(stringJust, lift).valueOf()).toEqual(stringJust.valueOf());
});

test('associativity', () => {
const numberJust: Maybe<number> = new Just(1);
const funF: Fun<number, Maybe<number>> = (x: number) => new Just(x + 1);
const funG: Fun<number, Maybe<string>> = (x: number) => new Just(String(x));
const left = bind(bind(numberJust, funF), funG);
const right = bind(numberJust, (x) => bind(funF(x), funG));
expect(left.valueOf()).toEqual(right.valueOf());
});
});

0 comments on commit 9e0816b

Please sign in to comment.