util.test.js 3.58 KB
import expect from 'expect.js';
import muk from 'muk';
import {
    tenToHex,
    hexToTen,
    toHex,
    log,
    returnProc,
    spliteRet,
    locateOrder
}
from '../../src/lib/util';
import interfaces from '../../src/lib/interface';

describe('util.js', function() {
    let util;
    before(function() {
        util = muk('../../src/lib/util', {
            'log4js': {
                loadAppender: function() {},
                addAppender: function() {},
                appenders: {
                    file: function() {}
                },
                getLogger: function() {
                    return {
                        error: function() {},
                        info: function() {},
                        setLevel: function() {}
                    }
                }
            }
        });
    });

    describe('tenToHex', function() {
        it('tenToHex input 12 expect c', function() {
            expect(tenToHex(12)).to.be('c');
        });

        it('tenToHex input -12 expect fffffff4', function() {
            expect(tenToHex(-12)).to.be('fffffff4');
        });

        it('tenToHex input 0 expect 0', function() {
            expect(tenToHex(0)).to.be('0');
        });
    });

    describe('hexToTen', function() {
        it('hexToTen input FFD3 ,true expect -45', function() {
            expect(hexToTen('FFD3', true)).to.be(-45);
        });
        it('hexToTen input FFD3 expect 65491', function() {
            expect(hexToTen('FFD3')).to.be(65491);
        })
    });

    describe('toHex', function() {
        it('toHex input new Buffer("0C0D","hex") expect ["c","d"]', function() {
            let input = new Buffer('0C0D', 'hex');
            expect(toHex(input)).to.eql(['c', 'd']);
        });
    });

    describe('returnProc', function() {
        it('returnProc input null expect false', function() {
            expect(returnProc()).to.be(false);
        });

        it('returnProc input [1,2] 1.1.1.1 {"1":1} expect {}', function() {
            expect(returnProc([1, 2], "1.1.1.1", {
                "1": 1
            })).to.eql({});
        });

        it('returnProc input ["0", "1"], "1.1.1.1", {"01": {"message": "test","hex": "01"}}', function() {
            let ret = util.returnProc(['0', '1'], '1.1.1.1', {
                '01': {
                    'message': 'test'
                }
            });

            expect(ret).to.eql({
                'message': 'test',
                'hex': '01'
            });
        });
    });

    describe('spliteRet', function() {
        let mq = ['f5', 'f2', 'f3'];
        let data = ['f5', '1', '3', 'f2', '2', '3', '4', 'f3', '0', '12', '2'];
        it("expect ['f5','1','3'] ['f2','2','3','4'],['f3','0','12','2']", function() {
            expect(spliteRet(mq, data)).to.eql({
                rets: [
                    ['f5', '1', '3'],
                    ['f2', '2', '3', '4'],
                    ['f3', '0', '12', '2']
                ],
                mq: []
            });
        });

        let mq2 = ['f1'];
        let data2 = ['f1', '1', '2', 'f1', '1', '4'];
        it("expect ['f1', '1', '2' ],['f1', '1', '4']", function() {
            expect(spliteRet(mq2, data2)).to.eql({
                rets: [
                    ['f1', '1', '2'],
                    ['f1', '1', '4']
                ],
                mq: []
            });
        })
    });

    describe('locateOrder', function() {
        it('input ["a0", "00", "00"] expect {key:"openUhf",val:interfaces.openUhf}', function() {
            expect(locateOrder(interfaces, ['a0', '00', '00']).name).to.be('openUhf');
        })
    })
});