36 lines
710 B
JavaScript
36 lines
710 B
JavaScript
import { it, assert } from 'folktest'
|
|
import { Option, Result } from '../../src/index.js'
|
|
|
|
const leftIdentity = (M, a, f) => {
|
|
assert(M(a).chain(f).equals(f(a)))
|
|
}
|
|
|
|
const rightIdentity = (M, m) => {
|
|
assert(m.chain(M).equals(m))
|
|
}
|
|
|
|
const associativity = (m, f, g) => {
|
|
assert(m.chain(f).chain(g).equals(m.chain(x => f(x).chain(g))))
|
|
}
|
|
|
|
export const prove = (M, m, a, f, g) => {
|
|
leftIdentity(M, a, f)
|
|
rightIdentity(M, m)
|
|
associativity(m, f, g)
|
|
}
|
|
|
|
export const Tests = [
|
|
it('should adhere to monadic laws', () => {
|
|
[Option, Result].forEach(F => {
|
|
const Ctr = F.of
|
|
|
|
prove(
|
|
Ctr,
|
|
Ctr(1),
|
|
1,
|
|
x => Ctr(x + 1),
|
|
x => Ctr(x * 2)
|
|
)
|
|
})
|
|
})
|
|
]
|