dispatch unit tests
This commit is contained in:
parent
fbbd99765b
commit
b853fe8476
2 changed files with 56 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { curry, curryN } from './curry.js'
|
import { curry, curryN } from './curry.js'
|
||||||
import { dispatch } from './fantasy-land.js'
|
import { dispatch } from './fantasy-land.js'
|
||||||
import { head, concat as iterconcat, iter, tail } from './list.js'
|
import { head, isIterable, iterConcat, iter, tail } from './list.js'
|
||||||
|
|
||||||
/** @import { Fn, Morphism, InferredMorphism, Predicate } from './types.js' */
|
/** @import { Fn, Morphism, InferredMorphism, Predicate } from './types.js' */
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ export const id = x => x
|
||||||
export const concat = dispatch(['fantasy-land/concat', 'concat'],
|
export const concat = dispatch(['fantasy-land/concat', 'concat'],
|
||||||
(b, a) => {
|
(b, a) => {
|
||||||
if (isIterable(a) || isIterable(b)) {
|
if (isIterable(a) || isIterable(b)) {
|
||||||
return iterconcat(iter(a), iter(b))
|
return iterConcat(iter(a), iter(b))
|
||||||
} else {
|
} else {
|
||||||
return a.concat(b)
|
return a.concat(b)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,62 @@
|
||||||
import { it, assert } from 'folktest'
|
import { it, assert, assertEq } from 'folktest'
|
||||||
import { dispatch } from '../../src/fantasy-land.js'
|
import { dispatch } from '../../src/fantasy-land.js'
|
||||||
|
|
||||||
const obj = {
|
const test = (re, str = '') => re instanceof RegExp ? re.test(str) : new RegExp(re).test(str)
|
||||||
'test': a => console.log('test', a)
|
|
||||||
|
const assertErr = (f, err) => {
|
||||||
|
try {
|
||||||
|
f()
|
||||||
|
} catch (e) {
|
||||||
|
if (typeof err === 'string' || err instanceof RegExp) {
|
||||||
|
assert(test(err, e.message), `${err} did not match ${e.message}`)
|
||||||
|
} else if (typeof err === 'function') {
|
||||||
|
assert(e.constructor === err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const expected = err ? `"${err}" ` : ""
|
||||||
|
assert(false, `expecting error ${expected}but function did not throw`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Tests = [
|
const wrong = msg => assert(false, msg)
|
||||||
it('whatever', () => {
|
|
||||||
const f = dispatch(['fantasy-land/test', 'test'], (f, a) => {
|
export const Dispatch = [
|
||||||
console.log(f, a)
|
it('should dispatch to any listed method or fallback', () => {
|
||||||
|
const a1 = {
|
||||||
|
['fantasy-land/test']: x => assertEq(x, 1),
|
||||||
|
test: () => wrong(`a1['test'] should not have been called`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const f1 = dispatch(['fantasy-land/test', 'test'], (v, x) => {
|
||||||
|
wrong(`f1 fallback should not have been called for ${JSON.stringify(x)}`)
|
||||||
})
|
})
|
||||||
console.log(f(1, obj))
|
|
||||||
|
f1(1, a1)
|
||||||
|
|
||||||
|
delete a1['fantasy-land/test']
|
||||||
|
|
||||||
|
assertErr(() => {
|
||||||
|
f1(1, a1)
|
||||||
|
})
|
||||||
|
|
||||||
|
const f2 = dispatch(['test', 'fantasy-land/test'], (v, x) => {
|
||||||
|
wrong(`f2 fallback should not have been called for ${JSON.stringify(x)}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
const a2 = {
|
||||||
|
['fantasy-land/test']: () => wrong(`a2['fantasy-land/test'] should not have been called`),
|
||||||
|
test: x => assertEq(x, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
f2(2, a2)
|
||||||
|
|
||||||
|
const f3 = dispatch(['fantasy-land/test', 'test'], x => {
|
||||||
|
assertEq(x, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
f3(3, {})
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue