import { expect } from "chai"; import { DPT3 } from "../src/DPT3" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" let bufferDecZero = Buffer.from([0]) let bufferDecValue = Buffer.from([5]) let bufferIncZero = Buffer.from([8]) let bufferIncValue = Buffer.from([13]) let bufferEmpty = Buffer.from([]) let bufferBiggerSize = Buffer.from([1, 0]) describe("Test DPT003", (): void => { let dpt = new DPT3(); it("Decode buffer dec-zero", async function () { const value = dpt.decoder(bufferDecZero) expect(value.data).is.equal(0); expect(value.decr_incr).is.equal(0); }); it("Decode buffer dec-value", async function () { const value = dpt.decoder(bufferDecValue) expect(value.data).is.equal(5); expect(value.decr_incr).is.equal(0); }); it("Decode buffer inc-zero", async function () { const value = dpt.decoder(bufferIncZero) expect(value.data).is.equal(0); expect(value.decr_incr).is.equal(1); }); it("Decode buffer inc-value", async function () { const value = dpt.decoder(bufferIncValue) expect(value.data).is.equal(5); expect(value.decr_incr).is.equal(1); }); it("Decode empty buffer", async function () { var testFunction = function () { const value = dpt.decoder(bufferEmpty) } expect(testFunction).to.throw(BufferLengthError); }); it("Decode oversized buffer", async function () { var testFunction = function () { const value = dpt.decoder(bufferBiggerSize) } expect(testFunction).to.throw(BufferLengthError); }); /* Encode tests */ it("encode valid", async function () { expect(compareBuffers(dpt.encoder({ data: 0, decr_incr: 0 }), bufferDecZero)).is.true; expect(compareBuffers(dpt.encoder({ data: 5, decr_incr: 0 }), bufferDecValue)).is.true; expect(compareBuffers(dpt.encoder({ data: 0, decr_incr: 1 }), bufferIncZero)).is.true; expect(compareBuffers(dpt.encoder({ data: 5, decr_incr: 1 }), bufferIncValue)).is.true; }); it("encode invalid", async function () { var testFunction = function () { const value = dpt.encoder({ data: 0, decr_incr: 3 }) } expect(testFunction).to.throw(InvalidValueError); }); it("encode undefined", async function () { var testFunction = function () { const value = dpt.encoder(undefined) } expect(testFunction).to.throw(InvalidValueError); }); });