mathjs/test/expression/node/FunctionNode.test.js
2014-02-28 22:50:46 +01:00

74 lines
2.6 KiB
JavaScript

// test FunctionNode
var assert = require('assert'),
approx = require('../../../tools/approx'),
math = require('../../../index')(),
Node = require('../../../lib/expression/node/Node'),
ConstantNode = require('../../../lib/expression/node/ConstantNode'),
OperatorNode = require('../../../lib/expression/node/OperatorNode'),
FunctionNode = require('../../../lib/expression/node/FunctionNode'),
RangeNode = require('../../../lib/expression/node/RangeNode'),
SymbolNode = require('../../../lib/expression/node/SymbolNode');
describe('FunctionNode', function() {
it ('should create a FunctionNode', function () {
var n = new FunctionNode();
assert(n instanceof FunctionNode);
assert(n instanceof Node);
});
it ('should compile a FunctionNode', function () {
var a = new ConstantNode('number', '2');
var x = new SymbolNode('x');
var o = new OperatorNode('+', 'add', [a, x]);
var n = new FunctionNode('f', ['x'], o);
var expr = n.compile(math);
var scope = {};
var f = expr.eval(scope);
assert.equal(typeof scope.f, 'function');
assert.equal(scope.f(3), 5);
assert.equal(scope.f(5), 7);
assert.throws(function () {scope.f()}, SyntaxError);
assert.throws(function () {scope.f(2, 3)}, SyntaxError);
});
it ('should find a FunctionNode', function () {
var a = new ConstantNode('number', '2');
var x = new SymbolNode('x');
var o = new OperatorNode('+', 'add', [a, x]);
var n = new FunctionNode('f', ['x'], o);
assert.deepEqual(n.find({type: FunctionNode}), [n]);
assert.deepEqual(n.find({type: SymbolNode}), [x]);
assert.deepEqual(n.find({type: RangeNode}), []);
assert.deepEqual(n.find({type: ConstantNode}), [a]);
assert.deepEqual(n.find({type: ConstantNode, properties: {value: '2'}}), [a]);
assert.deepEqual(n.find({type: ConstantNode, properties: {value: '4'}}), []);
});
it ('should find a FunctionNode without expression', function () {
var e = new FunctionNode();
assert.deepEqual(e.find({type: FunctionNode}), [e]);
assert.deepEqual(e.find({type: SymbolNode}), []);
});
it ('should match a FunctionNode', function () {
var a = new FunctionNode();
assert.equal(a.match({type: FunctionNode}), true);
assert.equal(a.match({type: SymbolNode}), false);
});
it ('should stringify a FunctionNode', function () {
var a = new ConstantNode('number', '2');
var x = new SymbolNode('x');
var o = new OperatorNode('+', 'add', [a, x]);
var n = new FunctionNode('f', ['x'], o);
assert.equal(n.toString(), 'function f(x) = 2 + x');
});
});