@@ -2,6 +2,59 @@ import assert from 'assert'
22import math from '../../../../src/defaultInstance.js'
33import { embeddedDocs } from '../../../../src/expression/embeddedDocs/embeddedDocs.js'
44
5+ let mathDocs = math . create ( math . all )
6+ const originalConfig = mathDocs . config ( )
7+ // Add names to the skipDocs array if they are not meant to have embedded docs
8+ const skipDocs = new Set ( [ 'import' , 'addScalar' , 'divideScalar' , 'equalScalar' , 'multiplyScalar' ,
9+ 'subtractScalar' , 'apply' , 'replacer' , 'reviver' ] )
10+
11+ // Add names to skipExamples if their examples in the embedded docs contain acceptable errors
12+ const skipExamples = new Set ( [ ] )
13+
14+ const testDocs = new Set ( [
15+ ...Object . keys ( embeddedDocs ) ,
16+ ...Object . keys ( math . expression . mathWithTransform )
17+ ] . filter ( name => { return ! skipDocs . has ( name ) } ) )
18+
19+ const testExamples = new Set ( [ ...testDocs ] . filter ( name => {
20+ return ! skipExamples . has ( name )
21+ } ) )
22+
23+ function runExamplesInDocs ( name ) {
24+ mathDocs . config ( originalConfig )
25+ // every funciton should have doc.examples
26+ const examples = mathDocs . evaluate ( `help("${ name } ")` ) . doc . examples
27+ try {
28+ // validate if the examples run without errors
29+ mathDocs . evaluate ( examples )
30+ return
31+ } catch {
32+ }
33+ // if they still have errors try with a new math instance
34+ mathDocs = math . create ( math . all )
35+ mathDocs . evaluate ( examples )
36+ }
37+
38+ function hasValidSeeAlso ( name ) {
39+ let seeAlso = [ ]
40+ try {
41+ seeAlso = mathDocs . evaluate ( `help("${ name } ")` ) . doc . seealso
42+ } catch ( err ) {
43+ return
44+ }
45+ if ( seeAlso && seeAlso . length > 0 ) {
46+ seeAlso . forEach ( see => {
47+ if ( testDocs . has ( see ) ) {
48+ if ( see === name ) {
49+ throw new Error ( `See also name "${ see } " should not be the same as "${ name } " in docs for "${ name } ".` )
50+ }
51+ } else {
52+ throw new Error ( `See also with name "${ see } " is not a valid documentation name used in docs for "${ name } ".` )
53+ }
54+ } )
55+ }
56+ }
57+
558describe ( 'help' , function ( ) {
659 it ( 'should find documentation for a function by its name' , function ( ) {
760 const help = math . help ( 'sin' )
@@ -59,11 +112,33 @@ describe('help', function () {
59112 // assert.throws(function () {math.help(undefined)}, /No documentation found/);
60113 assert . throws ( function ( ) { math . help ( new Date ( ) ) } , / N o d o c u m e n t a t i o n f o u n d / )
61114 assert . throws ( function ( ) { math . help ( 'nonExistingFunction' ) } , / N o d o c u m e n t a t i o n f o u n d / )
62- assert . throws ( function ( ) { math . help ( 'parse ' ) } , / N o d o c u m e n t a t i o n f o u n d / )
115+ assert . throws ( function ( ) { math . help ( 'addScalar ' ) } , / N o d o c u m e n t a t i o n f o u n d / )
63116 } )
64117
65118 it ( 'should LaTeX help' , function ( ) {
66119 const expression = math . parse ( 'help(parse)' )
67120 assert . strictEqual ( expression . toTex ( ) , '\\mathrm{help}\\left( parse\\right)' )
68121 } )
122+
123+ for ( const name of testDocs ) {
124+ it ( `should find documentation for ${ name } ` , function ( ) {
125+ assert . doesNotThrow ( ( ) => mathDocs . help ( name ) . doc )
126+ } )
127+ }
128+
129+ for ( const name of testDocs ) {
130+ it ( `should find examples for ${ name } ` , function ( ) {
131+ assert . doesNotThrow ( ( ) => mathDocs . help ( name ) . doc . examples )
132+ } )
133+ }
134+ for ( const name of testExamples ) {
135+ it ( `should run examples for ${ name } without errors` , function ( ) {
136+ assert . doesNotThrow ( ( ) => runExamplesInDocs ( name ) )
137+ } )
138+ }
139+ for ( const name of testDocs ) {
140+ it ( `should have all valid See Also for ${ name } that are not ${ name } ` , function ( ) {
141+ assert . doesNotThrow ( ( ) => hasValidSeeAlso ( name ) )
142+ } )
143+ }
69144} )
0 commit comments