kojima/tests/units/result.js
2025-03-26 21:28:33 -05:00

37 lines
759 B
JavaScript

import { it, assertEq } from 'folktest'
import { Ok, Err } from '../../src/result.js'
import { prove } from './monad.js'
const id = x => () => x
export const Tests = [
it('should pass monadic laws', () => {
prove(
Ok,
Ok(1),
1,
x => Ok(x + 1),
x => Ok(x * 2)
)
const e = new Error()
prove(
Err,
Err(e),
e,
x => Err(new Error()),
x => Err(new Error())
)
}),
it('should not bind err', () => {
const e = new Error()
const result = Ok(1)
.bind(id(Err(e)))
.bind(id(Ok(1)))
.map(x => x + 1)
assertEq(result, Err(e))
}),
]