-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
doc.test.js
429 lines (415 loc) · 11.6 KB
/
doc.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
const assert = require('assert')
const path = require('path')
const approx = require('../../tools/approx.js')
const docgenerator = require('../../tools/docgenerator.js')
const math = require('../..')
const debug = process.argv.includes('--debug-docs')
function extractExpectation (comment, optional = false) {
if (comment === '') return undefined
const returnsParts = comment.split('eturns').map(s => s.trim())
if (returnsParts.length > 1) return extractValue(returnsParts[1])
const outputsParts = comment.split('utputs')
if (outputsParts.length > 1) {
let output = outputsParts[1]
if (output[0] === ':') output = output.substring(1)
return extractValue(output.trim())
}
// None of the usual flags; if we need a value,
// assume the whole comment is the desired value. Otherwise return undefined
if (optional) return undefined
return extractValue(comment)
}
function extractValue (spec) {
// First check for a leading keyword:
const words = spec.split(' ')
// If the last word end in 'i' and the value is not labeled as complex,
// label it for the comment writer:
if (words[words.length - 1].substr(-1) === 'i' && words[0] !== 'Complex') {
words.unshift('Complex')
}
// Collapse 'Dense Matrix' into 'DenseMatrix'
if (words[0] === 'Dense' && words[1] === 'Matrix') {
words.shift()
words[0] = 'DenseMatrix'
}
const keywords = {
number: 'Number(_)',
BigNumber: 'math.bignumber(_)',
Fraction: 'math.fraction(_)',
Complex: "math.complex('_')",
Unit: "math.unit('_')",
Array: '_',
Matrix: 'math.matrix(_)',
DenseMatrix: "math.matrix(_, 'dense')",
string: '_',
Node: 'math.parse(_)',
throws: "'_'"
}
if (words[0] in keywords) {
const template = keywords[words[0]]
const spot = template.indexOf('_')
let filler = words.slice(1).join(' ')
if (words[0] === 'Complex') { // a bit of a hack here :(
filler = words.slice(1).join('')
}
spec = template.substring(0, spot) + filler + template.substr(spot + 1)
}
if (spec.substring(0, 7) === 'matrix(') {
spec = 'math.' + spec // More hackery :(
}
let value
try {
value = eval(spec) // eslint-disable-line no-eval
} catch (err) {
if (spec[0] === '[') {
// maybe it was an array with mathjs expressions in it
try {
value = math.evaluate(spec).toArray()
} catch (newError) {
value = spec
}
} else if (err instanceof SyntaxError || err instanceof ReferenceError) {
value = spec
} else {
throw err
}
}
if (words[0] === 'Unit') { // more hackishness here :(
value.fixPrefix = true
}
if (words[0] === 'Node') { // and even more :(
delete value.comment
}
return value
}
const knownProblems = new Set([
'isZero', 'isPositive', 'isNumeric', 'isNegative', 'isNaN',
'isInteger', 'hasNumericValue', 'clone', 'print', 'hex', 'format', 'to', 'sin',
'cos', 'atan2', 'atan', 'asin', 'asec', 'acsc', 'acoth', 'acot', 'max',
'setUnion', 'unequal', 'equal', 'deepEqual', 'compareNatural', 'randomInt',
'random', 'pickRandom', 'kldivergence', 'xor', 'or', 'not', 'and', 'distance',
'parser', 'compile', 're', 'im', 'rightLogShift', 'rightArithShift',
'leftShift', 'bitNot', 'apply', 'subset', 'squeeze', 'rotationMatrix',
'rotate', 'reshape', 'partitionSelect', 'matrixFromRows', 'matrixFromFunction',
'matrixFromColumns', 'getMatrixDataType', 'forEach', 'eigs', 'diff',
'ctranspose', 'concat', 'sqrtm', 'subtract', 'nthRoots', 'nthRoot', 'multiply',
'mod', 'invmod', 'floor', 'fix', 'expm1', 'exp', 'dotPow', 'dotMultiply',
'dotDivide', 'divide', 'ceil', 'cbrt', 'add', 'usolveAll', 'usolve', 'slu',
'rationalize', 'qr', 'lusolve', 'lup', 'lsolveAll', 'lsolve', 'derivative',
'symbolicEqual', 'map', 'schur', 'sylvester', 'freqz', 'round'
])
let issueCount = 0
function maybeCheckExpectation (name, expected, expectedFrom, got, gotFrom) {
if (knownProblems.has(name)) {
try {
checkExpectation(expected, got)
} catch (err) {
issueCount++
if (debug) {
console.log(
`PLEASE RESOLVE: '${gotFrom}' was supposed to '${expectedFrom}'`)
console.log(' but', err.toString())
}
}
} else {
checkExpectation(expected, got)
}
}
function checkExpectation (want, got) {
if (Array.isArray(want)) {
if (!Array.isArray(got)) {
want = math.matrix(want)
}
return approx.deepEqual(got, want, 1e-9)
}
if (want instanceof math.Unit && got instanceof math.Unit) {
return approx.deepEqual(got, want, 1e-9)
}
if (want instanceof math.Complex && got instanceof math.Complex) {
return approx.deepEqual(got, want, 1e-9)
}
if (typeof want === 'number' && typeof got === 'number' && want !== got) {
issueCount++
if (debug) {
console.log(` Note: return value ${got} not exactly as expected: ${want}`)
}
return approx.equal(got, want, 1e-9)
}
if (typeof want !== 'undefined') {
return approx.deepEqual(got, want)
} else {
// don't check if we don't know what the result is supposed to be
}
}
const OKundocumented = new Set([
'addScalar', 'subtractScalar', 'divideScalar', 'multiplyScalar', 'equalScalar',
'docs', 'FibonacciHeap',
'IndexError', 'DimensionError', 'ArgumentsError'
])
const knownUndocumented = new Set([
'all',
'isNumber',
'isComplex',
'isBigNumber',
'isFraction',
'isUnit',
'isString',
'isArray',
'isMatrix',
'isCollection',
'isDenseMatrix',
'isSparseMatrix',
'isRange',
'isIndex',
'isBoolean',
'isResultSet',
'isHelp',
'isFunction',
'isDate',
'isRegExp',
'isObject',
'isNull',
'isUndefined',
'isAccessorNode',
'isArrayNode',
'isAssignmentNode',
'isBlockNode',
'isConditionalNode',
'isConstantNode',
'isFunctionAssignmentNode',
'isFunctionNode',
'isIndexNode',
'isNode',
'isObjectNode',
'isOperatorNode',
'isParenthesisNode',
'isRangeNode',
'isRelationalNode',
'isSymbolNode',
'isChain',
'on',
'off',
'once',
'emit',
'config',
'expression',
'import',
'create',
'factory',
'AccessorNode',
'ArrayNode',
'AssignmentNode',
'atomicMass',
'avogadro',
'BigNumber',
'bignumber',
'BlockNode',
'bohrMagneton',
'bohrRadius',
'boltzmann',
'boolean',
'chain',
'Chain',
'classicalElectronRadius',
'complex',
'Complex',
'ConditionalNode',
'conductanceQuantum',
'ConstantNode',
'coulomb',
'createUnit',
'DenseMatrix',
'deuteronMass',
'e',
'efimovFactor',
'electricConstant',
'electronMass',
'elementaryCharge',
'false',
'faraday',
'fermiCoupling',
'fineStructure',
'firstRadiation',
'fraction',
'Fraction',
'FunctionAssignmentNode',
'FunctionNode',
'gasConstant',
'gravitationConstant',
'gravity',
'hartreeEnergy',
'Help',
'i',
'ImmutableDenseMatrix',
'index',
'Index',
'IndexNode',
'Infinity',
'inverseConductanceQuantum',
'klitzing',
'LN10',
'LN2',
'LOG10E',
'LOG2E',
'loschmidt',
'magneticConstant',
'magneticFluxQuantum',
'matrix',
'Matrix',
'molarMass',
'molarMassC12',
'molarPlanckConstant',
'molarVolume',
'NaN',
'neutronMass',
'Node',
'nuclearMagneton',
'null',
'number',
'ObjectNode',
'OperatorNode',
'ParenthesisNode',
'parse',
'Parser',
'phi',
'pi',
'planckCharge',
'planckConstant',
'planckLength',
'planckMass',
'planckTemperature',
'planckTime',
'protonMass',
'quantumOfCirculation',
'Range',
'RangeNode',
'reducedPlanckConstant',
'RelationalNode',
'replacer',
'ResultSet',
'reviver',
'rydberg',
'SQRT1_2',
'SQRT2',
'sackurTetrode',
'secondRadiation',
'Spa',
'sparse',
'SparseMatrix',
'speedOfLight',
'splitUnit',
'stefanBoltzmann',
'string',
'SymbolNode',
'tau',
'thomsonCrossSection',
'true',
'typed',
'Unit',
'unit',
'E',
'PI',
'vacuumImpedance',
'version',
'weakMixingAngle',
'wienDisplacement'
])
describe('Testing examples from (jsdoc) comments', function () {
const allNames = Object.keys(math)
const srcPath = path.resolve(__dirname, '../../src') + '/'
const allDocs = docgenerator.collectDocs(allNames, srcPath)
it("should cover all names (but doesn't yet)", function () {
const documented = new Set(Object.keys(allDocs))
const badUndocumented = allNames.filter(name => {
return !(documented.has(name) ||
OKundocumented.has(name) ||
knownUndocumented.has(name) ||
name.substr(0, 1) === '_' ||
name.substr(-12) === 'Dependencies' ||
name.substr(0, 6) === 'create'
)
})
assert.deepEqual(badUndocumented, [])
})
const byCategory = {}
for (const fun of Object.values(allDocs)) {
if (!(fun.category in byCategory)) {
byCategory[fun.category] = []
}
byCategory[fun.category].push(fun.doc)
}
for (const category in byCategory) {
describe('category: ' + category, function () {
for (const doc of byCategory[category]) {
it('satisfies ' + doc.name, function () {
if (debug) {
console.log(` Testing ${doc.name} ...`) // can remove once no known failures; for now it clarifies "PLEASE RESOLVE"
}
const lines = doc.examples
lines.push('//') // modifies doc but OK for test
let accumulation = ''
let expectation
let expectationFrom = ''
for (const line of lines) {
if (line.includes('//')) {
let parts = line.split('//')
if (parts[0] && !parts[0].trim()) {
// Indented comment, unusual in examples
// assume this is a comment within some code to evaluate
// i.e., ignore it
continue
}
// Comment specifying a future value or the return of prior code
parts = parts.map(s => s.trim())
if (parts[0] !== '') {
if (accumulation) { accumulation += '\n' }
accumulation += parts[0]
}
if (accumulation !== '' && expectation === undefined) {
expectationFrom = parts[1]
expectation = extractExpectation(expectationFrom)
parts[1] = ''
}
if (accumulation && !accumulation.includes('console.log(')) {
// note: we ignore examples that contain a console.log to keep the output of the tests clean
let value
try {
value = eval(accumulation) // eslint-disable-line no-eval
} catch (err) {
value = err.toString()
}
maybeCheckExpectation(
doc.name, expectation, expectationFrom, value, accumulation)
accumulation = ''
}
expectationFrom = parts[1]
expectation = extractExpectation(expectationFrom, 'requireSignal')
} else {
if (line !== '') {
if (accumulation) { accumulation += '\n' }
accumulation += line
}
}
}
})
}
})
}
after(function () {
if (debug) {
if (knownProblems.size > 0) {
console.log(`\nWARNING: ${knownProblems.size} known errors converted ` +
'to PLEASE RESOLVE warnings.')
}
if (knownUndocumented.size > 0) {
console.log(`\nWARNING: ${knownUndocumented.size} symbols in math are known to ` +
'be undocumented; PLEASE EXTEND the documentation.')
}
}
if (issueCount > 0) {
console.log(`\nWARNING: ${issueCount} issues found in the JSDoc comments.` + (!debug
? ' Run the tests again with "npm run test:node -- --debug-docs" to see detailed information'
: ''))
}
})
})