Skip to content

Commit

Permalink
Add Alt, Plus, Alterative. Fix FL laws tests O.o
Browse files Browse the repository at this point in the history
  • Loading branch information
briancavalier committed Nov 19, 2016
1 parent 458d790 commit 687f9ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"uglify-js": "^2.7.3"
},
"dependencies": {
"fantasy-land": "^2.0.0"
"fantasy-land": "^2.1.0"
}
}
8 changes: 8 additions & 0 deletions src/Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class Core {
return this.concat(p)
}

[fl.alt] (p) {
return this.or(p)
}

static [fl.zero] () {
return never()
}

// @deprecated The name concat is deprecated, use or() instead.
concat (b) {
return this.or(b)
Expand Down
78 changes: 46 additions & 32 deletions test/fantasyland-laws-test.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,106 @@
import { describe, it } from 'mocha'
import assert from 'assert'
import { fulfill, Promise } from '../src/main'
import { isNever } from '../src/inspect'
import { assertSame } from './lib/test-util'
import * as Functor from 'fantasy-land/laws/functor'
import * as Chain from 'fantasy-land/laws/chain'
import * as Apply from 'fantasy-land/laws/apply'
import * as Applicative from 'fantasy-land/laws/applicative'
import * as Semigroup from 'fantasy-land/laws/semigroup'
import * as Monoid from 'fantasy-land/laws/monoid'
import fl from 'fantasy-land'
import * as Alt from 'fantasy-land/laws/alt'
import * as Plus from 'fantasy-land/laws/plus'
import * as Alternative from 'fantasy-land/laws/alternative'

const id = x => x
const assertIsNever = x => assert(isNever(x))

describe('fantasyland laws', () => {
describe('functor', () => {
it('should satisfy identity', () => {
return Functor.identity(fulfill, assertSame, {})
return Functor.identity(fulfill)(assertSame)({})
})

it('should satisfy composition', () => {
const f = x => x + 'f'
const g = x => x + 'g'
return Functor.composition(fulfill, assertSame, f, g, 'x')
})

it('should be covered', () => {
return fulfill()[fl.map](id)
return Functor.composition(fulfill)(assertSame)(f)(g)('x')
})
})

describe('apply', () => {
it('should satisfy composition', () => {
return Apply.composition(fulfill, assertSame, {})
})

it('should be covered', () => {
return fulfill()[fl.ap](fulfill(id))
return Apply.composition(Promise)(assertSame)({})
})
})

describe('applicative', () => {
it('should satisfy identity', () => {
return Applicative.identity(Promise, assertSame, {})
return Applicative.identity(Promise)(assertSame)({})
})

it('should satisfy homomorphism', () => {
return Applicative.homomorphism(Promise, assertSame, {})
return Applicative.homomorphism(Promise)(assertSame)({})
})

it('should satisfy interchange', () => {
return Applicative.interchange(Promise, assertSame, {})
return Applicative.interchange(Promise)(assertSame)({})
})
})

it('should be covered', () => {
return Promise[fl.of](undefined)
describe('chain', () => {
it('should satisfy associativity', () => {
return Chain.associativity(Promise)(assertSame)({})
})
})

describe('chain', () => {
describe('semigroup', () => {
it('should satisfy associativity', () => {
return Chain.associativity(fulfill, assertSame, {})
return Semigroup.associativity(fulfill)(assertSame)({})
})
})

describe('monoid', () => {
it('should satisfy rightIdentity', () => {
return Monoid.rightIdentity(Promise)(assertSame)({})
})

it('should be covered', () => {
return fulfill()[fl.chain](fulfill)
it('should satisfy leftIdentity', () => {
return Monoid.leftIdentity(Promise)(assertSame)({})
})
})

describe('semigroup', () => {
describe('alt', () => {
it('should satisfy associativity', () => {
return Semigroup.associativity(fulfill, assertSame, {})
return Alt.associativity(assertSame)(fulfill(1))(fulfill(2))(fulfill(3))
})

it('should be covered', () => {
return fulfill()[fl.concat](fulfill())
it('should satisfy distributivity', () => {
return Alt.distributivity(assertSame)(fulfill(1))(fulfill(-1))(x => x + 1)
})
})

describe('monoid', () => {
describe('plus', () => {
it('should satisfy leftIdentity', () => {
return Plus.leftIdentity(Promise)(assertSame)(fulfill({}))
})

it('should satisfy rightIdentity', () => {
return Monoid.rightIdentity(Promise, assertSame, {})
return Plus.rightIdentity(Promise)(assertSame)(fulfill({}))
})

it('should satisfy leftIdentity', () => {
return Monoid.leftIdentity(Promise, assertSame, {})
it('should satisfy annihilation', () => {
return Plus.annihilation(Promise)(assertIsNever)(x => x + 1)
})
})

describe('alternative', () => {
it('should satisfy distributivity', () => {
return Alternative.distributivity(p => p.then(x => assert.strictEqual(1, x)))(fulfill(0))(fulfill(x => x + 1))(fulfill(x => x - 1))
})

it('should be covered', () => {
return Promise[fl.empty]().concat(fulfill())
it('should satisfy annihilation', () => {
return Plus.annihilation(Promise)(assertIsNever)(fulfill({}))
})
})
})

0 comments on commit 687f9ef

Please sign in to comment.