Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example in IO docs #234

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#220 fix for *.isInstance methods
  • Loading branch information
wookieb authored and ulfryk committed Jan 21, 2020
commit d02ea78a369eefafc4cb325c71c0c7515bc764b9
4 changes: 2 additions & 2 deletions src/monet.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

function isInstance(typeName) {
return function (target) {
return target[TYPE_KEY] || target.constructor[TYPE_KEY] === LIB_NAME + '/' + typeName
return (target[TYPE_KEY] || target.constructor[TYPE_KEY]) === LIB_NAME + '/' + typeName
}
}

Expand Down Expand Up @@ -1003,7 +1003,7 @@
throw new Error('IO requires a function.')
}
this.effectFn = effectFn
setType(Validation, TYPES_NAMES.IO)
setType(this, TYPES_NAMES.IO)
},
map: function (fn) {
var self = this
Expand Down
18 changes: 18 additions & 0 deletions test/either-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ describe('An Either', function () {
})
})

describe('Either.isInstance', function () {
it('will return true only for Either instances', function () {
expect(Either.isInstance(Either.Left([]))).toBeTruthy();
expect(Either.isInstance(Either.Right([]))).toBeTruthy();
})
it('will return false for other monads', function () {
expect(Either.isInstance(Monet.Maybe.Some({}))).toBeFalsy();
expect(Either.isInstance(Monet.Maybe.None())).toBeFalsy();
expect(Either.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(Either.isInstance({})).toBeFalsy();
expect(Either.isInstance(true)).toBeFalsy();
expect(Either.isInstance(false)).toBeFalsy();
expect(Either.isInstance('foo')).toBeFalsy();
})
})

// TODO: Provide additional test suite for `monet-pimp`
xdescribe('will pimp an object', function () {
it('with right', function () {
Expand Down
18 changes: 18 additions & 0 deletions test/free-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ describe('A Free monad', function () {
expect(g(1).run()).toBe(limit)
})

describe('Free.isInstance', function () {
it('will return true only for Free instances', function () {
expect(Free.isInstance(Free.Return([]))).toBeTruthy();
expect(Free.isInstance(Free.Suspend([]))).toBeTruthy();
})
it('will return false for other monads', function () {
expect(Free.isInstance(Monet.Maybe.Some({}))).toBeFalsy();
expect(Free.isInstance(Monet.Maybe.None())).toBeFalsy();
expect(Free.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(Free.isInstance({})).toBeFalsy();
expect(Free.isInstance(true)).toBeFalsy();
expect(Free.isInstance(false)).toBeFalsy();
expect(Free.isInstance('foo')).toBeFalsy();
})
})

})
26 changes: 23 additions & 3 deletions test/io-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,28 @@ describe('An IO monad', function () {
})
expect(effect.ap(ioWithFn).run()).toBe('cool effect')
})
it('and is compatible with Fantasy Land', function () {
expect(effect.ap).toBe(effect['fantasy-land/ap'])
})
it('and is compatible with Fantasy Land', function () {
expect(effect.ap).toBe(effect['fantasy-land/ap'])
})
})

describe('IO.isInstance', function () {
it('will return true only for IO instances', function () {
var instance = IO(function () {
return 'foo'
})
expect(IO.isInstance(instance)).toBeTruthy();
})
it('will return false for other monads', function () {
expect(IO.isInstance(Monet.Maybe.Some({}))).toBeFalsy();
expect(IO.isInstance(Monet.Maybe.None())).toBeFalsy();
expect(IO.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(IO.isInstance({})).toBeFalsy();
expect(IO.isInstance(true)).toBeFalsy();
expect(IO.isInstance(false)).toBeFalsy();
expect(IO.isInstance('foo')).toBeFalsy();
})
})
})
17 changes: 17 additions & 0 deletions test/list-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,21 @@ describe('An immutable list', function () {
expect(list.equals).toBe(list['fantasy-land/equals'])
})

describe('List.isInstance', function () {
it('will return true only for List instances', function () {
expect(List.isInstance(List.fromArray([]))).toBeTruthy();
})
it('will return false for other monads', function () {
expect(List.isInstance(Monet.Maybe.Some({}))).toBeFalsy();
expect(List.isInstance(Monet.Maybe.None())).toBeFalsy();
expect(List.isInstance(Monet.Validation.Success([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(List.isInstance({})).toBeFalsy();
expect(List.isInstance(true)).toBeFalsy();
expect(List.isInstance(false)).toBeFalsy();
expect(List.isInstance('foo')).toBeFalsy();
})
})

})
30 changes: 26 additions & 4 deletions test/maybe-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe('A Maybe', function () {
expect(someThree.every(predicateAll)).toBe(true)
expect(someThree.every(predicateSome)).toBe(false)
expect(someThree.every(predicateNone)).toBe(false)

expect(someThree.every(predicateAll)).toBe(someThree.forall(predicateAll))
expect(someThree.every(predicateSome)).toBe(someThree.forall(predicateSome))
expect(someThree.every(predicateNone)).toBe(someThree.forall(predicateNone))
Expand All @@ -334,7 +334,7 @@ describe('A Maybe', function () {
expect(aNothing.every(predicateAll)).toBe(true)
expect(aNothing.every(predicateSome)).toBe(true)
expect(aNothing.every(predicateNone)).toBe(true)

expect(aNothing.every(predicateAll)).toBe(aNothing.forall(predicateAll))
expect(aNothing.every(predicateSome)).toBe(aNothing.forall(predicateSome))
expect(aNothing.every(predicateNone)).toBe(aNothing.forall(predicateNone))
Expand Down Expand Up @@ -532,7 +532,9 @@ describe('A Maybe', function () {
expect(Maybe.fromEmpty([])).toBeNoneMaybe()
})
it('an empty object', function () {
function foo() {}
function foo() {
}

foo.prototype.bar = 42
expect(Maybe.fromEmpty(new foo())).toBeNoneMaybe()
})
Expand All @@ -550,7 +552,10 @@ describe('A Maybe', function () {
expect(Maybe.fromEmpty([1])).toBeSomeMaybe()
})
it('any non-empty objects', function () {
function foo() { this.value = 42 }
function foo() {
this.value = 42
}

expect(Maybe.fromEmpty(new foo())).toBeSomeMaybe()
expect(Maybe.fromEmpty({foo: 42})).toBeSomeMaybe()
})
Expand Down Expand Up @@ -602,5 +607,22 @@ describe('A Maybe', function () {
})
})

describe('Maybe.isInstance', function () {
it('will return true only for Maybe instances', function () {
expect(Maybe.isInstance(Some('hi'))).toBeTruthy();
expect(Maybe.isInstance(None())).toBeTruthy();
})
it('will return false for other monads', function () {
expect(Maybe.isInstance(Monet.Validation.Success({}))).toBeFalsy();
expect(Maybe.isInstance(Monet.Validation.Fail({}))).toBeFalsy();
expect(Maybe.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(Maybe.isInstance({})).toBeFalsy();
expect(Maybe.isInstance(true)).toBeFalsy();
expect(Maybe.isInstance(false)).toBeFalsy();
expect(Maybe.isInstance('foo')).toBeFalsy();
})
})

})
17 changes: 17 additions & 0 deletions test/nel-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ describe('A Non-Empty immutable list', function () {
expect(NEL(1, NEL(false, Nil)).inspect()).toBe('NEL(1, false)')
})

describe('NEL.isInstance', function () {
it('will return true only for NEL instances', function () {
expect(NEL.isInstance(NEL(1))).toBeTruthy();
})
it('will return false for other monads', function () {
expect(NEL.isInstance(Monet.Validation.Success({}))).toBeFalsy();
expect(NEL.isInstance(Monet.Validation.Fail({}))).toBeFalsy();
expect(NEL.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(NEL.isInstance({})).toBeFalsy();
expect(NEL.isInstance(true)).toBeFalsy();
expect(NEL.isInstance(false)).toBeFalsy();
expect(NEL.isInstance('foo')).toBeFalsy();
})
})

describe('can be described with collection predicates', function () {
var predicateAll = function (e) {
return e < 5
Expand Down
20 changes: 20 additions & 0 deletions test/reader-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,24 @@ describe('A Reader Monad', function () {

})

describe('Reader.isInstance', function () {
it('will return true only for Reader instances', function () {
var instance = Reader(function (x) {
return x
})
expect(Reader.isInstance(instance)).toBeTruthy();
})
it('will return false for other monads', function () {
expect(Reader.isInstance(Monet.Validation.Success({}))).toBeFalsy();
expect(Reader.isInstance(Monet.Validation.Fail({}))).toBeFalsy();
expect(Reader.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(Reader.isInstance({})).toBeFalsy();
expect(Reader.isInstance(true)).toBeFalsy();
expect(Reader.isInstance(false)).toBeFalsy();
expect(Reader.isInstance('foo')).toBeFalsy();
})
})

})
18 changes: 18 additions & 0 deletions test/validation-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ describe('A Validation', function () {
})
})

describe('Validation.isInstance', function () {
it('will return true only for Validation instances', function () {
expect(Validation.isInstance(Validation.Success('hi'))).toBeTruthy();
expect(Validation.isInstance(Validation.Fail({}))).toBeTruthy();
})
it('will return false for other monads', function () {
expect(Validation.isInstance(Monet.Maybe.Some({}))).toBeFalsy();
expect(Validation.isInstance(Monet.Maybe.None())).toBeFalsy();
expect(Validation.isInstance(Monet.List.fromArray([]))).toBeFalsy();
})
it('will return false on non-monads', function () {
expect(Validation.isInstance({})).toBeFalsy();
expect(Validation.isInstance(true)).toBeFalsy();
expect(Validation.isInstance(false)).toBeFalsy();
expect(Validation.isInstance('foo')).toBeFalsy();
})
})

// TODO: Provide additional test suite for `monet-pimp`
xdescribe('will pimp an object', function () {
it('with success', function () {
Expand Down