diff --git a/src/function.js b/src/function.js index 979180b..c2e335c 100644 --- a/src/function.js +++ b/src/function.js @@ -1,6 +1,6 @@ import { curry, curryN } from './curry.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' */ @@ -14,7 +14,7 @@ export const id = x => x export const concat = dispatch(['fantasy-land/concat', 'concat'], (b, a) => { if (isIterable(a) || isIterable(b)) { - return iterconcat(iter(a), iter(b)) + return iterConcat(iter(a), iter(b)) } else { return a.concat(b) } diff --git a/test/units/index.js b/test/units/index.js index 47103c2..6ec5bd5 100644 --- a/test/units/index.js +++ b/test/units/index.js @@ -1,16 +1,62 @@ -import { it, assert } from 'folktest' +import { it, assert, assertEq } from 'folktest' import { dispatch } from '../../src/fantasy-land.js' -const obj = { - 'test': a => console.log('test', a) +const test = (re, str = '') => re instanceof RegExp ? re.test(str) : new RegExp(re).test(str) + +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 = [ - it('whatever', () => { - const f = dispatch(['fantasy-land/test', 'test'], (f, a) => { - console.log(f, a) +const wrong = msg => assert(false, msg) + +export const Dispatch = [ + 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, {}) }) ]