mathjs/test/generated-code-tests/entry/mainNumber.test.js
Jos de Jong 6f00715754
Specify import require paths (continuation of #1941) (#1962)
* Add `.js` extension to source file imports

* Specify package `exports` in `package.json`

Specify package type as `commonjs` (It's good to be specific)

* Move all compiled scripts into `lib` directory

Remove ./number.js (You can use the compiled ones in `./lib/*`)

Tell node that the `esm` directory is type `module` and enable tree shaking.

Remove unused files from packages `files` property

* Allow importing of package.json

* Make library ESM first

* - Fix merge conflicts
- Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs`
- Refactor unit tests to be able to run with plain nodejs (no transpiling)
- Fix browser examples

* Fix browser and browserstack tests

* Fix running unit tests on Node 10 (which has no support for modules)

* Fix node.js examples (those are still commonjs)

* Remove the need for `browserBundle.cjs`

* Generate minified bundle only

* [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.**
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Cleanup console.log

* Add integration tests to test the entry points (commonjs/esm, full/number only)

* Create backward compatibility error messages in the files moved/removed since v8

* Describe breaking changes in HISTORY.md

* Bump karma from 5.2.1 to 5.2.2 (#1965)

Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-09-20 18:01:29 +02:00

164 lines
5.2 KiB
JavaScript

import assert from 'assert'
import * as mainNumber from '../../../src/entry/mainNumber.js'
import { createSnapshotFromFactories, validateBundle, validateTypeOf } from '../../../src/utils/snapshot.js'
import * as factoriesNumber from '../../../src/factoriesNumber.js'
const { create, all, add, isObject, isNumber, pi, sqrt, evaluate, chain, Range, reviver, derivative, simplify, addDependencies } = mainNumber
const {
expectedInstanceStructure,
expectedES6Structure
} = createSnapshotFromFactories(factoriesNumber)
describe('mainNumber', function () {
it('should export functions', () => {
assert.strictEqual(add(2, 3), 5)
assert.strictEqual(sqrt(4), 2)
})
it('should export all functions and constants', function () {
// snapshot testing
validateBundle(expectedES6Structure, mainNumber)
})
it('new instance should have all expected functions', function () {
// snapshot testing
const newMathInstance = create(all)
validateBundle(expectedInstanceStructure, newMathInstance)
})
it('new instance should import some factory functions via import', function () {
const newMathInstance = create()
newMathInstance.import({
addDependencies
})
assert.strictEqual(newMathInstance.add(2, 3), 5)
})
it('new instance should import all factory functions via import', function () {
// snapshot testing
const newMathInstance = create()
newMathInstance.import(all)
validateBundle(expectedInstanceStructure, newMathInstance)
})
it('new instance should import some factory functions via import', function () {
const newMathInstance = create()
newMathInstance.import({
addDependencies
}, { silent: true })
assert.strictEqual(newMathInstance.add(2, 3), 5)
})
it('evaluate should contain all functions from mathWithTransform', function () {
// snapshot testing
const mathWithTransform = expectedInstanceStructure.expression.mathWithTransform
Object.keys(mathWithTransform).forEach(key => {
if (key === 'not') {
// operator, special case
assert.strictEqual(evaluate('not true'), false)
} else if (key === 'apply') {
// TODO: special case, apply is not yet working in the expression parser due to security constraints
} else {
try {
assert.strictEqual(validateTypeOf(evaluate(key)), mathWithTransform[key], `Compare type of "${key}"`)
} catch (err) {
console.error(err.toString())
assert.ok(false, `Missing or wrong type of entry in mathWithTransform: "${key}"`)
}
}
})
})
it('evaluate should not contain classes', function () {
assert.throws(() => { evaluate('Complex') }, /Undefined symbol Complex/)
assert.throws(() => { evaluate('SymbolNode') }, /Undefined symbol SymbolNode/)
})
it('should export constants', () => {
assert.strictEqual(pi, Math.PI)
})
it('should export type checking functions', () => {
assert.strictEqual(isObject({}), true)
assert.strictEqual(isObject(null), false)
assert.strictEqual(isNumber('23'), false)
})
it('should export evaluate having functions and constants', () => {
assert.strictEqual(evaluate('sqrt(4)'), 2)
assert.strictEqual(evaluate('pi'), Math.PI)
// TODO: should loop over all functions and constants
assert.strictEqual(typeof evaluate('help'), 'function')
assert.strictEqual(typeof evaluate('parse'), 'function')
assert.strictEqual(typeof evaluate('compile'), 'function')
assert.strictEqual(typeof evaluate('evaluate'), 'function')
assert.strictEqual(typeof evaluate('chain'), 'function')
assert.strictEqual(typeof evaluate('simplify'), 'function')
assert.strictEqual(typeof evaluate('derivative'), 'function')
assert.strictEqual(typeof evaluate('rationalize'), 'function')
})
it('should export chain with all functions', () => {
assert.strictEqual(chain(2).add(3).done(), 5)
})
it('derivative should work', () => {
assert.strictEqual(derivative('2x', 'x').toString(), '2')
})
it('simplify should work', () => {
assert.strictEqual(simplify('2x + 3x').toString(), '5 * x')
})
it('should export evaluate having help and embedded docs', () => {
const h = evaluate('help(simplify)')
assert(h.toString().indexOf('Name: simplify') >= 0, true)
})
it('should get/set scope variables', () => {
const math = create(all)
const evaluate = math.evaluate
assert.strictEqual(evaluate('b + 2', { b: 3 }), 5)
const scope = {}
assert.strictEqual(evaluate('b = 2', scope), 2)
assert.deepStrictEqual(scope, { b: 2 })
})
it('doe not support assignement and access right now', () => {
// TODO: implement support for subset in number implementation
assert.throws(function () {
evaluate('A[2]', { A: [10, 20, 30] })
}, /No "index" implementation available/)
assert.throws(function () {
const scope = { A: [10, 20, 30] }
evaluate('A[2] = 200', scope)
}, /No "index" implementation available/)
})
it('should export reviver', () => {
const json = '{"mathjs":"Range","start":2,"end":10}'
const r = new Range(2, 10)
const obj = JSON.parse(json, reviver)
assert(obj instanceof Range)
assert.deepStrictEqual(obj, r)
})
// TODO: test export of errors
// TODO: test export of classes
})