From 99f7cb87b68d0844cebe238c0f18ec2e2352db8c Mon Sep 17 00:00:00 2001 From: kraftdorian Date: Sun, 5 Dec 2021 23:19:53 +0100 Subject: [PATCH] feat: test functor fmap laws --- __tests__/functor/fmap.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/__tests__/functor/fmap.spec.ts b/__tests__/functor/fmap.spec.ts index e5ff104..3c42c9d 100644 --- a/__tests__/functor/fmap.spec.ts +++ b/__tests__/functor/fmap.spec.ts @@ -4,6 +4,14 @@ function add1(x: number): number { return x + 1; } +function multiply2(x: number): number { + return x * 2; +} + +function id(value: A): A { + return value; +} + describe('fmap', () => { test('returns Nothing if input is Nothing', () => { const numberNothing: Maybe = new Nothing(); @@ -20,4 +28,17 @@ describe('fmap', () => { const numberJust: Maybe = new Just(x); expect(fmap(add1, numberJust).valueOf()).toEqual(add1(x)); }); + + test('fmap id = id', () => { + const numberJust: Maybe = 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 = new Just(x); + const left = fmap((x) => multiply2(add1(x)), numberJust); + const right = fmap(multiply2, fmap(add1, numberJust)); + expect(left.valueOf()).toEqual(right.valueOf()); + }); });