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

66 lines
1.8 KiB
TypeScript

import { expect } from "chai";
import { DPT8 } from "../src/DPT8"
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
let value: number = 256
let overflowValue: number = 40000
let bufferPositive = Buffer.from([1, 0])
let bufferNegative = Buffer.from([255, 0])
let bufferEmpty = Buffer.from([])
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
describe("Test DPT008", (): void => {
let dpt = new DPT8();
it("Decode buffer acceptable", async function () {
expect(dpt.decoder(bufferPositive)).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(value), bufferPositive)).is.true;
expect(compareBuffers(dpt.encoder(-value), bufferNegative)).is.true;
});
it("encode invalid", async function () {
var testFunction1 = function () {
const value = dpt.encoder(-overflowValue)
}
var testFunction2 = function () {
const value = dpt.encoder(overflowValue)
}
expect(testFunction1).to.throw(InvalidValueError);
expect(testFunction2).to.throw(InvalidValueError);
});
it("encode undefined", async function () {
var testFunction = function () {
const value = dpt.encoder(undefined)
}
expect(testFunction).to.throw(InvalidValueError);
});
});