picomath/test/_poortf.js
Glen Whitney 536656bfe8 feat: Initial core of picomath
Implements a totally simplistic "poortf" mutable typed function and
  a picomath instance generator, as well as the very beginnings of a
  number type and one generic function and a default full picomath instance.

  Also provides some tests which serve as usage examples.
2022-03-25 00:49:03 -07:00

42 lines
1.3 KiB
JavaScript

import assert from 'assert'
import poortf from '../poortf.js'
describe('poortf', () => {
const slate = poortf('slate')
it('creates an empty tf', () => {
assert.throws(() => slate('empty'), TypeError)
assert.throws(() => slate('empty'), /slate.*empty/)
})
const add = poortf('add', [
[args => Array.from(args).every(a => typeof a === 'number'),
(a, b) => a+b],
[args => Array.from(args).every(a => typeof a === 'boolean'),
(p, q) => p || q]
])
it('creates a tf with initial behaviors', () => {
assert.strictEqual(add(2,2), 4)
assert.strictEqual(add(true, false), true)
assert.throws(() => add('kilroy'), TypeError)
})
it('extends an empty tf', () => {
slate.addImps([
[args => typeof args[0] === 'string', s => s + ' wuz here'],
[args => typeof args[0] === 'number', () => 'I am not a number']
])
assert.strictEqual(slate('Kilroy', 'was here'), 'Kilroy wuz here')
assert.strictEqual(slate(2, 'was here'), 'I am not a number')
assert.throws(() => slate(['Ha!']), TypeError)
})
it('extends a tf with other behaviors', () => {
add.addImps([args => typeof args[0] === 'string', (s,x) => s + x]),
assert.strictEqual(add('Kilroy', 23), 'Kilroy23')
assert.throws(() => add(['Ha!'], 'gotcha'), TypeError)
})
})