2022-03-25 07:49:03 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2022-03-25 20:09:50 +00:00
|
|
|
const defNumber = what => {
|
|
|
|
slate.addImps([args => typeof args[0] === 'number',
|
|
|
|
n => 'I am not number ' + (what+n)],
|
|
|
|
defNumber, what+1)
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:49:03 +00:00
|
|
|
it('extends an empty tf', () => {
|
2022-03-25 20:09:50 +00:00
|
|
|
defNumber(2)
|
|
|
|
assert.strictEqual(slate(0, 'was here'), 'I am not number 2')
|
|
|
|
assert.throws(() => slate('Kilroy', 'was here'), TypeError)
|
2022-03-25 07:49:03 +00:00
|
|
|
slate.addImps([
|
|
|
|
[args => typeof args[0] === 'string', s => s + ' wuz here'],
|
2022-03-25 20:09:50 +00:00
|
|
|
[args => typeof args[0] === 'boolean', () => 'Maybe I am a number']
|
2022-03-25 07:49:03 +00:00
|
|
|
])
|
|
|
|
assert.strictEqual(slate('Kilroy', 'was here'), 'Kilroy wuz here')
|
2022-03-25 20:09:50 +00:00
|
|
|
assert.strictEqual(slate(true), 'Maybe I am a number')
|
2022-03-25 07:49:03 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-25 20:09:50 +00:00
|
|
|
|
|
|
|
it('can invalidate and lazily reload implementations', () => {
|
|
|
|
slate.invalidate(defNumber)
|
|
|
|
assert.strictEqual(slate.imps.length, 3) // reloader plus two non-number
|
|
|
|
assert.strictEqual(slate(1), 'I am not number 4') // 'what' is now 3!!
|
|
|
|
assert.strictEqual(slate('Yorlik', 7), 'Yorlik wuz here')
|
|
|
|
assert.strictEqual(slate(false), 'Maybe I am a number')
|
|
|
|
assert.strictEqual(slate.imps.length, 3) // reloader gone
|
|
|
|
})
|
2022-03-25 07:49:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|