66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { expect } from "chai";
|
|
|
|
import { DPT6 } from "../src/DPT6"
|
|
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
|
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
|
|
|
import { compareBuffers } from "./util"
|
|
|
|
let value: number = 65
|
|
let overflowValue: number = 4000
|
|
|
|
let bufferPositive = Buffer.from([value])
|
|
let bufferNegative = Buffer.from([-value])
|
|
|
|
let bufferEmpty = Buffer.from([])
|
|
let bufferBiggerSize = Buffer.from([1, 0])
|
|
|
|
describe("Test DPT006", (): void => {
|
|
|
|
let dpt = new DPT6();
|
|
|
|
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);
|
|
});
|
|
}); |