uci-utils-byte/test/byte.test.js

136 lines
3.7 KiB
JavaScript
Raw Normal View History

let date = new Date(Date.now())
console.log('run:', date.getMinutes(), ':', date.getSeconds())
import { expect } from 'chai'
import * as _ from '../src/byte'
describe('Byte Library - ', function () {
it('Should Convert Byte Format for Single Byte', function () {
let byte = '10000001'
let fmt = {}
let results = ['10000001', Buffer.from([0x81]), 129, '81', [1, 0, 0, 0, 0, 0, 0, 1],
[8, 1]
]
_.BYTE_FORMATS.forEach((ifmt, i) => {
byte = results[i]
fmt.in = ifmt
// console.log('input', ifmt, byte)
_.BYTE_FORMATS.forEach((ofmt, o) => {
fmt.out = ofmt
// console.log(fmt.out, _.byteFormat(byte, fmt))
expect(_.byteFormat(byte, fmt)).deep.equal(results[o])
})
})
})
it('Should Convert from Any Format to Any Other', function () {
let bytes
let fmt = {}
let results = [
['10000001', '01010111'],
Buffer.from([0x81, 0x57]), [129, 87],
['81', '57'],
[
[1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 1, 0, 1, 1, 1]
],
[1, 8, 9, 10, 11, 13, 15]
]
_.BYTE_FORMATS.forEach((ifmt, i) => {
bytes = results[i]
fmt.in = ifmt
// console.log('input', ifmt, bytes)
_.BYTE_FORMATS.forEach((ofmt, o) => {
fmt.out = ofmt
// console.log('=',i, fmt.in, bytes, o, fmt.out, _.byteFormat(bytes, fmt))
expect(_.byteFormat(bytes, fmt)).deep.equal(results[o])
})
})
})
it('It should deal with missing byte when converting from PLC', function () {
let fmt={}
// check for missing place
fmt.in = 'PLC'
fmt.out = 'DEC'
let bytes = [1, 8, 17]
expect(_.byteFormat(bytes, fmt)).deep.equal([129, 0, 1])
})
it('PLC should return as with Big Endian when set in environment', function () {
// check Big endian
let fmt={}
process.env.UCI_ENDIAN='big'
fmt.in = 'PLC'
fmt.out = 'DEC'
let bytes = [1, 8, 17]
expect(_.byteFormat(bytes, fmt)).deep.equal([1, 0, 129])
})
})
describe('Byte Class - ', function () {
let byte = new _.Byte(20, 'HEX')
let byte2 = new _.Byte(20)
it('Should set the default format to DEC', function () {
expect(byte2.fmt).to.equal('DEC')
})
it('Should output an alternative format', function () {
expect(byte.toFmt('DEC')).to.equal(32)
expect(byte.toFmt('STR')).to.equal('00100000')
})
it('Verify getters and setters ', function () {
// value getter and setter
expect(byte.value, 'value getter failed').to.equal(20)
byte.value = 40
expect(byte.value, 'value setter failed').to.equal(40)
expect(byte.prev, 'previous value not set').to.equal(20)
// format getter/setter
expect(byte.fmt, 'format getter failed').to.equal('HEX')
byte.fmt = 'STR'
expect(byte.fmt, 'format setter failed').to.equal('STR')
expect(byte.value, 'format getter value change failed').to.equal('01000000')
})
it('should update value with format', function () {
// change the value with same format
// change value and set format
byte.reset(32)
expect(byte.value, 'value reset failed').to.equal(32)
expect(byte.value, 'previous value reset failed').to.equal(32)
expect(byte.fmt, 'default format reset failed').to.equal('DEC')
byte.reset(20, 'HEX')
expect(byte.value, 'value reset failed').to.equal(20)
expect(byte.fmt, 'format reset failed').to.equal('HEX')
byte.reset('10000000', 'STR')
expect(byte.toFmt('DEC'), 'string reset and convert failed').to.equal(128)
})
it('!!!-should find bit changes and state of those changes between previous and current', function () {
// change the value with same format
// change value and set format
expect(true, 'value reset failed').to.equal(true)
})
})