mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* Add support for scopes with get and set methods * Fix build for node v12 * Fixup cli and parser tests * Add tests for simplify and evaluate * Add example for a custom scope object * Function calls need child scopes * Transitionary step: Separate Safe and Scope Property calls * Renamed identifiers in FunctionNode * Evaluate with ObjectScopeWrapper * Simplify tests passing * Assume all scopes are map-like. Except parser * Remove isMapLike check in customs.*SafeProperty() methods * Change MapLike to Map * Move keywords from an Object to a Set * Move ScopeProperty functions in to scope.js * Removed deprecation warning * Rename scope.js to map.js * Rename ScopeProperty to MapProperty * Add tests and docs for map.js * Put back the micro-optimization of function calls * Use Map in the parser * Called scope methods directly in cli.js * Coercing of scope into a Map is done in Node, not evaluate * Move createSubScope to its own file * Fixup following self-review * Add scope docs * Final self-review changes * Address reviewer comments * Remove MapProperty witness marks * Converted broken benchmark possibly lost in a rebase * Use bare map as scope in benchmark Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
157 lines
4.9 KiB
JavaScript
157 lines
4.9 KiB
JavaScript
// test parser
|
|
|
|
import assert from 'assert'
|
|
|
|
import approx from '../../../tools/approx.js'
|
|
import math from '../../../src/defaultInstance.js'
|
|
const Parser = math.Parser
|
|
|
|
describe('parser', function () {
|
|
it('should create a parser', function () {
|
|
const parser = new Parser()
|
|
assert.ok(parser instanceof Parser)
|
|
})
|
|
|
|
it('should have a property isParser', function () {
|
|
const a = new Parser()
|
|
assert.strictEqual(a.isParser, true)
|
|
})
|
|
|
|
it('should have a property type', function () {
|
|
const a = new Parser()
|
|
assert.strictEqual(a.type, 'Parser')
|
|
})
|
|
|
|
it('should evaluate an expression', function () {
|
|
const parser = new Parser()
|
|
|
|
const result = parser.evaluate('2 + 3')
|
|
assert.strictEqual(result, 5)
|
|
})
|
|
|
|
it('should get variables from the parsers namespace ', function () {
|
|
const parser = new Parser()
|
|
|
|
parser.evaluate('a = 3')
|
|
parser.evaluate('b = a + 2')
|
|
assert.strictEqual(parser.evaluate('a'), 3)
|
|
assert.strictEqual(parser.evaluate('b'), 5)
|
|
assert.strictEqual(parser.get('a'), 3)
|
|
assert.strictEqual(parser.get('b'), 5)
|
|
})
|
|
|
|
it('should get all variables from the parsers namespace ', function () {
|
|
const parser = new Parser()
|
|
|
|
parser.evaluate('a = 3')
|
|
parser.evaluate('b = a + 2')
|
|
assert.deepStrictEqual(parser.getAll(), { a: 3, b: 5 })
|
|
|
|
parser.remove('a')
|
|
assert.deepStrictEqual(parser.getAll(), { b: 5 })
|
|
})
|
|
|
|
it('should return undefined when getting a non existing variable', function () {
|
|
const parser = new Parser()
|
|
|
|
assert.strictEqual(parser.get('non_existing_variable'), undefined)
|
|
})
|
|
|
|
it('should set variables in the parsers namespace ', function () {
|
|
const parser = new Parser()
|
|
|
|
assert.strictEqual(parser.set('a', 3), 3)
|
|
assert.strictEqual(parser.evaluate('a'), 3)
|
|
assert.strictEqual(parser.evaluate('a + 2'), 5)
|
|
|
|
// adjust variable
|
|
assert.strictEqual(parser.evaluate('a = a + 2'), 5)
|
|
assert.strictEqual(parser.evaluate('a'), 5)
|
|
assert.strictEqual(parser.get('a'), 5)
|
|
|
|
assert.strictEqual(parser.set('a', parser.get('a') - 4), 1)
|
|
assert.strictEqual(parser.evaluate('a'), 1)
|
|
})
|
|
|
|
it('should remove a variable from the parsers namespace ', function () {
|
|
const parser = new Parser()
|
|
|
|
assert.strictEqual(parser.set('qq', 3), 3)
|
|
assert.strictEqual(parser.evaluate('qq'), 3)
|
|
assert.strictEqual(parser.get('qq'), 3)
|
|
|
|
parser.remove('qq')
|
|
assert.strictEqual(parser.get('qq'), undefined)
|
|
assert.throws(function () { parser.evaluate('qq') })
|
|
|
|
assert.strictEqual(parser.evaluate('ww = 5'), 5)
|
|
assert.strictEqual(parser.get('ww'), 5)
|
|
parser.remove('ww')
|
|
assert.strictEqual(parser.get('ww'), undefined)
|
|
assert.throws(function () { parser.evaluate('ww') })
|
|
})
|
|
|
|
it('should clear the parsers namespace ', function () {
|
|
const parser = new Parser()
|
|
|
|
assert.strictEqual(parser.evaluate('xx = yy = zz = 5'), 5)
|
|
|
|
assert.strictEqual(parser.set('pi', 'oops'), 'oops')
|
|
|
|
assert.strictEqual(parser.get('xx'), 5)
|
|
assert.strictEqual(parser.get('yy'), 5)
|
|
assert.strictEqual(parser.get('zz'), 5)
|
|
assert.strictEqual(parser.get('pi'), 'oops')
|
|
|
|
assert.strictEqual(parser.evaluate('xx'), 5)
|
|
assert.strictEqual(parser.evaluate('yy'), 5)
|
|
assert.strictEqual(parser.evaluate('zz'), 5)
|
|
assert.strictEqual(parser.evaluate('pi'), 'oops')
|
|
|
|
parser.clear()
|
|
|
|
assert.strictEqual(parser.get('xx'), undefined)
|
|
assert.strictEqual(parser.get('yy'), undefined)
|
|
assert.strictEqual(parser.get('zz'), undefined)
|
|
approx.equal(parser.get('pi'), undefined)
|
|
|
|
assert.throws(function () { parser.evaluate('xx') })
|
|
assert.throws(function () { parser.evaluate('yy') })
|
|
assert.throws(function () { parser.evaluate('zz') })
|
|
assert.strictEqual(parser.evaluate('pi'), Math.PI)
|
|
})
|
|
|
|
describe('security', function () {
|
|
it('should return undefined when accessing what appears to be inherited properties', function () {
|
|
try {
|
|
const parser = new Parser()
|
|
|
|
Object.prototype.foo = 'bar' // eslint-disable-line no-extend-native
|
|
|
|
parser.clear()
|
|
assert.strictEqual(parser.get('foo'), undefined)
|
|
// No longer uses a Object scope, so this now works!
|
|
// assert.throws(function () { parser.get('foo') }, /No access/)
|
|
} finally {
|
|
delete Object.prototype.foo
|
|
}
|
|
})
|
|
|
|
it('should throw an error when assigning an inherited property', function () {
|
|
try {
|
|
const parser = new Parser()
|
|
// We can safely set within the parser
|
|
assert.strictEqual(parser.set('toString', null), null)
|
|
// But getting it out via getAll() will throw.
|
|
assert.throws(function () { parser.getAll() }, /No access/)
|
|
} finally {
|
|
delete Object.prototype.foo
|
|
}
|
|
})
|
|
})
|
|
|
|
it('should throw an exception when creating a parser without new', function () {
|
|
assert.throws(function () { Parser() }, /Constructor must be called with the new operator/)
|
|
})
|
|
})
|