dptlib/tests/DPT009.test.ts
2022-03-11 12:09:49 +01:00

62 lines
1.8 KiB
TypeScript

import { expect } from "chai";
import { DPT9 } from "../src/DPT9"
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
let value: number = 21.08
let valuemin: number = 1.0e-15
let bufferValue = Buffer.from([0x0C, 0x1E])
let bufferZero = Buffer.from([0x00, 0x00])
let bufferNegative = Buffer.from([0x8b, 0xe2])
let bufferEmpty = Buffer.from([])
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
describe("Test DPT009", (): void => {
let dpt = new DPT9();
it("Decode buffer acceptable", async function () {
expect(dpt.decoder(bufferZero)).is.equal(0);
expect(dpt.decoder(bufferValue)).is.equal(value);
expect(dpt.decoder(bufferNegative)).is.equal(-value);
});
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);
});
/* Encoder tests */
it("encode valid", async function () {
expect(compareBuffers(dpt.encoder(0), bufferZero)).is.true;
expect(compareBuffers(dpt.encoder(value), bufferValue)).is.true;
expect(compareBuffers(dpt.encoder(-value), bufferNegative)).is.true;
});
it("encode undefined", async function () {
var testFunction = function () {
const value = dpt.encoder(undefined)
}
expect(testFunction).to.throw(InvalidValueError);
});
it("encode infinite", async function () {
var testFunction = function () {
const value = dpt.encoder(Infinity)
}
expect(testFunction).to.throw(InvalidValueError);
});
});