Skip to content

Commit

Permalink
unit test for FunctionDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Metelkin committed Jan 23, 2024
1 parent 25780dd commit 3958eb8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/container/container-actions --config=./test/.mocharc.json",
"test:dev": "mocha test/define-function/bind --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
22 changes: 19 additions & 3 deletions test/core/function-def.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ describe('Unit test for FunctionDef', () => {
});

expect(simple._container.logger).property('hasErrors').false;

simple._container.logger.resetErrors();
});

it('Error: wrong input 1.', () => {
it('defineFunction without math is ok.', () => {
let simple = new p.classes.FunctionDef({
id: 'ud1',
arguments: ['x']
});
expect(simple._container.logger).to.has.property('hasErrors', false);
});

it('Error: wrong input 1 (bad arguments).', () => {
let simple1 = new p.classes.FunctionDef({
id: 'u1',
arguments: 'xxx'
Expand All @@ -41,13 +48,22 @@ describe('Unit test for FunctionDef', () => {
simple1._container.logger.resetErrors();
});

it('Error: wrong input 2.', () => {
it('Error: wrong input 2 (no id).', () => {
let simple2 = new p.classes.FunctionDef({
arguments: ['xxx']
});
expect(simple2._container.logger).to.has.property('hasErrors', true);
simple2._container.logger.resetErrors();
});

it('Error: error input 3 (math without arguments).', () => {
let simple3 = new p.classes.FunctionDef({
id: 'u3',
math: '1*1'
});
expect(simple3._container.logger).to.has.property('hasErrors', true);
simple3._container.logger.resetErrors();
});
});


Expand Down
70 changes: 70 additions & 0 deletions test/define-function/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* global describe, it*/
const { expect } = require('chai');
const { Container } = require('../../src');

let qArr1 = [
// #defineFunction
{ id: 'pow7', action: 'defineFunction', arguments: ['x'], math: 'pow(x, 7)' },
{ id: 'f3', action: 'defineFunction', arguments: ['x1', 'x2', 'x3'], math: 'sqrt(x1^2 + x2^2 + x3^2)' },
{ id: 'f4', action: 'defineFunction', arguments: [], math: '15*2' },
{ id: 'f5', action: 'defineFunction', arguments: ['x', 'y'], math: 'f4(x)^2' },
// @Record
{ id: 'rec1', class: 'Record', assignments: {ode_: '1.1 * pow7(2) * 2.2'} },
{ id: 'rec2', class: 'Record', assignments: {ode_: '1.1 * sin(2) * 2.2'} }
];
let qArr2 = [
{ id: 'f7', action: 'defineFunction', arguments: ['x', 'y'], math: 'sss(x)^2' },
];
describe('Proper binding of functionDefinition', () => {
var c1;
it('Load proper elements', () => {
c1 = new Container();
c1.loadMany(qArr1);
c1.knitMany();

expect(c1.defaultLogs).lengthOf(1);
c1.defaultLogs.length = 0;
});

it('ref to wrong function', () => {
c1.load({
id: 'f7', action: 'defineFunction', arguments: ['x', 'y'], math: 'sss(x)^2'
});
c1.knitMany();

expect(c1.defaultLogs).lengthOf(1);
c1.defaultLogs.length = 0;
});

it('lost argument inside math', () => {
c1.load({
id: 'f12', action: 'defineFunction', arguments: ['x', 'y'], math: 'f5()*pow(x,y)'
});
c1.knitMany();

expect(c1.defaultLogs).lengthOf(2);
c1.defaultLogs.length = 0;
});

it('lost argument inside ode_', () => {
c1.load({
id: 'rec3', class: 'Record', assignments: { ode_: 'f5()*pow(rec1, rec2)'}
});
c1.knitMany();

expect(c1.defaultLogs).lengthOf(3);
c1.defaultLogs.length = 0;
});

it('circular function', () => {
c1.load({
id: 'f6', action: 'defineFunction', arguments: ['x'], math: 'f6(x)^2'
});
c1.checkCircFunctionDef();

expect(c1.defaultLogs).lengthOf(1);
c1.defaultLogs.length = 0;
});

//it('Debugging', () => console.log(c1.hetaErrors()));
});

0 comments on commit 3958eb8

Please sign in to comment.