feat: Add all remaining features of original Picomath PoC (#5)

Namely, a README describing the proof-of-concept,
  a custom selective loader, and some additional tests.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #5
This commit is contained in:
Glen Whitney 2022-07-19 19:37:52 +00:00
parent 84a8b9d5c4
commit a16d6a5ce3
4 changed files with 67 additions and 1 deletions

View file

@ -0,0 +1,13 @@
import assert from 'assert'
import PocomathInstance from '../PocomathInstance.mjs'
describe('PocomathInstance', () => {
it('creates an instance that can define typed-functions', () => {
const pi = new PocomathInstance('dummy')
pi.install({'add': {'any,any': [[], (a,b) => a+b]}})
assert.strictEqual(pi.add(2,2), 4)
assert.strictEqual(pi.add('Kilroy', 17), 'Kilroy17')
assert.strictEqual(pi.add(1, undefined), NaN)
assert.throws(() => pi.add(1), TypeError)
})
})

View file

@ -3,9 +3,11 @@ import math from '../pocomath.mjs'
import typed from 'typed-function'
import PocomathInstance from '../PocomathInstance.mjs'
import * as numbers from '../number/all.mjs'
import * as numberAdd from '../number/add.mjs'
import * as complex from '../complex/all.mjs'
import * as complexAdd from '../complex/add.mjs'
import * as complexNegate from '../complex/negate.mjs'
import extendToComplex from '../complex/extendToComplex.mjs'
const bw = new PocomathInstance('backwards')
describe('A custom instance', () => {
@ -38,4 +40,18 @@ describe('A custom instance', () => {
assert.deepStrictEqual(
pm.subtract({re:5, im:0}, {re:10, im:1}), {re:-5, im: -1})
})
it("can selectively import in cute ways", async function () {
const cherry = new PocomathInstance('cherry')
cherry.install(numberAdd)
await extendToComplex(cherry)
// Now we have an instance that supports addition for number and complex
// and little else:
assert.strictEqual(cherry.add(3, 4, 2), 9)
assert.deepStrictEqual(
cherry.add(cherry.complex(3, 3), 4, cherry.complex(2, 2)),
math.complex(9,5))
assert.strictEqual('subtract' in cherry, false)
assert.strictEqual('negate' in cherry, false)
})
})