2022-03-09 22:38:02 +01:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
|
|
|
import { DPT21 } from "../src/DPT21"
|
|
|
|
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
|
|
|
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
|
|
|
|
|
|
|
import { compareBuffers } from "./util"
|
|
|
|
|
2022-03-11 12:09:49 +01:00
|
|
|
describe("Test DPT021", (): void => {
|
2022-03-09 22:38:02 +01:00
|
|
|
|
|
|
|
let dpt = new DPT21();
|
|
|
|
|
2022-03-11 15:36:16 +01:00
|
|
|
it("Decode buffer acceptable", async function () {
|
|
|
|
let value = [false, false, false, true, false, true, false, false]
|
|
|
|
let buffer = Buffer.from([0x14])
|
|
|
|
const decoded = dpt.decoder(buffer)
|
2022-03-12 15:56:39 +01:00
|
|
|
|
|
|
|
expect(decoded.length).is.equal(value.length)
|
|
|
|
for (let i = 0; i < decoded.length; i++)
|
|
|
|
expect(decoded[i]).is.equal(value[i]);
|
2022-03-11 15:36:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Decode empty buffer", async function () {
|
|
|
|
let bufferEmpty = Buffer.from([])
|
|
|
|
var testFunction = function () {
|
|
|
|
const value = dpt.decoder(bufferEmpty)
|
|
|
|
}
|
|
|
|
expect(testFunction).to.throw(BufferLengthError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Decode oversized buffer", async function () {
|
|
|
|
let bufferBiggerSize = Buffer.from([0x20, 0x15, 0, 0, 0, 0, 0, 0, 0])
|
|
|
|
|
|
|
|
var testFunction = function () {
|
|
|
|
const value = dpt.decoder(bufferBiggerSize)
|
|
|
|
}
|
|
|
|
expect(testFunction).to.throw(BufferLengthError);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Encoder tests */
|
|
|
|
it("encode valid", async function () {
|
|
|
|
let value: boolean[] = [false, false, false, true, false, true, false, true]
|
|
|
|
let buffer = Buffer.from([0x15])
|
|
|
|
|
|
|
|
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("encode undefined", async function () {
|
|
|
|
var testFunction = function () {
|
|
|
|
const value = dpt.encoder(undefined)
|
|
|
|
}
|
|
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("encode invalid", async function () {
|
|
|
|
var testFunction = function () {
|
|
|
|
const value = dpt.encoder([true, true, true, true, false, false, false, false, true])
|
|
|
|
}
|
|
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
|
|
});
|
|
|
|
|
2022-03-09 22:38:02 +01:00
|
|
|
});
|