pws-formula
Version:
This is a formula execute the string or buffer format formula libray to get values from string or buffer.
62 lines (58 loc) • 2.33 kB
JavaScript
/**
* Project: pws-formula
* Created Date: Friday, May 4th 2018, 11:09:37 am
* Author: Thomas.Li
* E-Mail: leeyinghui@hotmail.com
* Intro: test the interface of index.js
* -----
* Last Modified: Fri May 04 2018
* Modified By: Thomas.Li
* -----
* Copyright (c) 2018 pareact
* ------------------------------------
* Always bet on Javascript!
*/
const formula = require('../index')
const expect = require('chai').expect;
describe('buffer function test', function () {
let buf = Buffer.from('010304012345673e4d', 'hex');
let f1 = 'pif(hx(3,2)>100, ht(0,2), hb(1,1))';
let f2 = 'pif(hx(3,2)<100, ht(0,2), hb(1,1))';
let f3 = 'pif(hx(3,2)>100, ht(-3,2), hb(1,1))';
let f4 = 'pif(hx(3,2)<100, ht(0,2), hb(1,21))';
it('f1 get 103', function () {
expect(formula.getBuffValue(buf, [f1,])[0].data).to.equal(103);
});
it('f2 get 1', function () {
expect(formula.getBuffValue(buf, [f2,])[0].data).to.equal(1);
});
it('f3 get false (negative)', function () {
expect(formula.getBuffValue(buf, [f3,])[0].success).to.be.false;
});
it('f4 get false (21 not in 0-7)', function () {
expect(formula.getBuffValue(buf, [f4,])[0].success).to.be.false;
});
});
describe('str function test', function () {
let str1 = ':F0061A0102090463FFFFFF5100080000E30000000000017A014C0497';
let str2 = '~10012A0020680133B35A4300005F43CDCC6043CDCC5C4300005D4300005D4332322E330000604100-32.600C0400000B0409AD9CC439A9947420000009AD9CC43E77D';
let f1 = 'pif(ab(0,3)==0, ax(8,4), ad(8,4))';
let f2 = 'pif(ab(0,5)==0, ax(8,4), ad(8,4))';
let f3 = 'pif(ap(14)>200, ac(62,8), ad(80,5))';
let f4 = 'pif(ap(14)<200, ac(62,8), ad(80,5))';
it('f1 get 521', function () {
expect(formula.getStringValue(str1.slice(1), [f1,])[0].data).to.equal(521);
});
it('f2 get 209', function () {
expect(formula.getStringValue(str1.slice(1), [f2,])[0].data).to.equal(209);
});
it('f1 should get true', function () {
expect(formula.getStringValue(str1.slice(1), [f1,])[0].success).to.be.true;
});
it('f3 get 22.3', function () {
expect(formula.getStringValue(str2.slice(1), [f3,])[0].data).to.equal(22.3);
});
it('f4 get -32.6', function () {
expect(formula.getStringValue(str2.slice(1), [f4,])[0].data).to.equal(-32.6);
});
});