Skip to content

Commit

Permalink
feat: test functor fmap laws
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftdorian committed Dec 5, 2021
1 parent bb75f9f commit 99f7cb8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions __tests__/functor/fmap.spec.ts
Expand Up @@ -4,6 +4,14 @@ function add1(x: number): number {
return x + 1;
}

function multiply2(x: number): number {
return x * 2;
}

function id<A>(value: A): A {
return value;
}

describe('fmap', () => {
test('returns Nothing if input is Nothing', () => {
const numberNothing: Maybe<number> = new Nothing();
Expand All @@ -20,4 +28,17 @@ describe('fmap', () => {
const numberJust: Maybe<number> = new Just(x);
expect(fmap(add1, numberJust).valueOf()).toEqual(add1(x));
});

test('fmap id = id', () => {
const numberJust: Maybe<number> = new Just(1);
expect(fmap(id, numberJust).valueOf()).toEqual(numberJust.valueOf());
});

test('fmap (f . g) = fmap f . fmap g', () => {
const x = 1;
const numberJust: Maybe<number> = new Just(x);
const left = fmap((x) => multiply2(add1(x)), numberJust);
const right = fmap(multiply2, fmap(add1, numberJust));
expect(left.valueOf()).toEqual(right.valueOf());
});
});

0 comments on commit 99f7cb8

Please sign in to comment.