Skip to content

Commit

Permalink
test(liftFN): add compatibility tests
Browse files Browse the repository at this point in the history
- create extendable base of monadic types

Closes #60
  • Loading branch information
char0n committed Apr 24, 2017
1 parent 1c360af commit 92c52a4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/internal/algebraicStructures/Identity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { applicativeTrait, applyTrait, functorTrait } from './traits';


export default { ...applicativeTrait, ...applyTrait, ...functorTrait };
23 changes: 23 additions & 0 deletions src/internal/algebraicStructures/traits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fantasyLand from 'fantasy-land';


export const applicativeTrait = {
of(value) {
return { ...this, value };
},
};
applicativeTrait[fantasyLand.of] = applicativeTrait.of;

export const functorTrait = {
map(fn) {
return this.of(fn(this.value));
},
};
functorTrait[fantasyLand.map] = functorTrait.map;

export const applyTrait = {
ap(monadWithApplyOfAFunction) {
return monadWithApplyOfAFunction.map(fn => fn(this.value));
},
};
applyTrait[fantasyLand.ap] = applyTrait.ap;
28 changes: 3 additions & 25 deletions src/liftFN.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import fantasyLand from 'fantasy-land';
import { curry, head, slice, reduce, ap as apR, curryN, map, add, flip } from 'ramda';

import Identity from './internal/algebraicStructures/Identity';

const applicativeTrait = {
of(value) {
return { ...this, value };
},
};
applicativeTrait[fantasyLand.of] = applicativeTrait.of;

const functorTrait = {
map(fn) {
return this.of(fn(this.value));
},
};
functorTrait[fantasyLand.map] = functorTrait.map;

const applyTrait = {
ap(monadWithApplyOfAFunction) {
return monadWithApplyOfAFunction.map(fn => fn(this.value));
},
};
applyTrait[fantasyLand.ap] = applyTrait.ap;

const Monad = { ...applicativeTrait, ...functorTrait, ...applyTrait };
const m1 = Monad.of(1);
const m2 = Monad.of(2).map(add);
const m1 = Identity.of(1);
const m2 = Identity.of(2).map(add);


export const createAp = (ap1, ap2) => {
Expand Down
26 changes: 26 additions & 0 deletions test/liftFN.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { add, reduce } from 'ramda';

import RA from '../src/index';
import eq from './shared/eq';
import { createAp } from '../src/liftFN';
import Identity from '../src/internal/algebraicStructures/Identity';


const addN = (...args) => reduce(add, 0, args);
Expand Down Expand Up @@ -40,11 +42,35 @@ describe('liftFN', function() {

it('retain order of arguments', function() {
eq(RA.liftFN(3, add3)(Maybe.Some('a'), Maybe.Some('b'), Maybe.Some('c')), Maybe.Some('abc'));
eq(RA.liftFN(3, add3)(Identity.of('a'), Identity.of('b'), Identity.of('c')), Identity.of('abc'));
});

it('is curried', function() {
const f4 = RA.liftFN(4);
eq(typeof f4, 'function');
eq(f4(addN)(Maybe.Some(1), Maybe.Some(1), Maybe.Some(1), Maybe.Some(1)), Maybe.Some(4));
});

it('test old fantasyland spec compatibility', function() {
const m1 = Identity.of(1);
const m2 = Identity.of(2).map(add);

eq(createAp(m1, m2)(m1, m2), Identity.of(3));
});

it('test new fantasyland spec compatibility', function() {
const m1 = Identity.of(1);
const m2 = Identity.of(2).map(add);
const ap = createAp(m1, m2);

eq(ap(m1, m2), Identity.of(3));
});

it('test old fantasyland spec compatibility', function() {
const m1 = Maybe.Some(1);
const m2 = Maybe.Some(2).map(add);
const ap = createAp(m1, m2);

eq(ap(m1, m2), Maybe.Some(3));
});
});

0 comments on commit 92c52a4

Please sign in to comment.