From 81127b8a9b0fc348c02dae2f13a59816b701d73e Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Fri, 29 Jul 2022 19:43:24 -0700 Subject: [PATCH] test: Check for undefined types, as a means to detect typos in type names --- src/core/PocomathInstance.mjs | 13 ++++++++++++- test/_pocomath.mjs | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 405479f..3c7e429 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -16,7 +16,9 @@ export default class PocomathInstance { 'install', 'installType', 'name', - 'Types']) + 'Types', + 'undefinedTypes' + ]) constructor(name) { this.name = name @@ -25,6 +27,7 @@ export default class PocomathInstance { this._typed = typed.create() this._typed.clear() this.Types = {any: anySpec} // dummy entry to track the default 'any' type + this._usedTypes = new Set() // all types that have occurred in a signature this._doomed = new Set() // for detecting circular reference this._config = {predictable: false} const self = this @@ -226,6 +229,13 @@ export default class PocomathInstance { this._invalidateDependents(':' + type) } + /* Returns a list of all types that have been mentioned in the + * signatures of operations, but which have not actually been installed: + */ + undefinedTypes() { + return Array.from(this._usedTypes).filter(t => !(t in this.Types)) + } + /* Used internally by install, see the documentation there */ _installFunctions(functions) { for (const [name, spec] of Object.entries(functions)) { @@ -246,6 +256,7 @@ export default class PocomathInstance { this._addAffect(depname, name) } for (const type of typesOfSignature(signature)) { + this._usedTypes.add(type) this._addAffect(':' + type, name) } } diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index a361c31..8e88668 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -2,6 +2,14 @@ import assert from 'assert' import math from '../src/pocomath.mjs' describe('The default full pocomath instance "math"', () => { + it('has no undefined types', () => { + const undef = math.undefinedTypes() + if (undef.length) { + console.log('Probable typo: found undefined types', undef) + } + assert.strictEqual(undef.length, 0) + }) + it('can subtract numbers', () => { assert.strictEqual(math.subtract(12, 5), 7) }) @@ -35,6 +43,9 @@ describe('The default full pocomath instance "math"', () => { assert.deepStrictEqual(math.complex(2,3), norm13) assert.deepStrictEqual(math.complex(2), math.complex(2,0)) assert.deepStrictEqual(math.add(2, math.complex(0,3)), norm13) + assert.deepStrictEqual( + math.subtract(math.complex(1,1), math.complex(2,-2)), + math.complex(-1,3)) assert.deepStrictEqual( math.subtract(16, math.add(3, math.complex(0,4), 2)), math.complex(11, -4))