55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { expect } from "chai";
|
|
|
|
import { DPT14 } from "../src/DPT14"
|
|
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
|
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
|
|
|
import { compareBuffers } from "./util"
|
|
|
|
describe("Test DPT014", (): void => {
|
|
|
|
let dpt = new DPT14();
|
|
|
|
it("Decode buffer acceptable", async function () {
|
|
let value = 3.123863134302025e-17
|
|
let buffer = Buffer.from([0x24, 0x10, 0x10, 0x11])
|
|
const decoded = dpt.decoder(buffer)
|
|
expect(decoded).is.equal(value);
|
|
});
|
|
|
|
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])
|
|
|
|
var testFunction = function () {
|
|
const value = dpt.decoder(bufferBiggerSize)
|
|
}
|
|
expect(testFunction).to.throw(BufferLengthError);
|
|
});
|
|
|
|
/* Encoder tests */
|
|
it("encode valid", async function () {
|
|
let value = 0x00f040cc
|
|
let buffer = Buffer.from([75, 112, 64, 204])
|
|
|
|
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
|
|
});
|
|
|
|
it("encode undefined", async function () {
|
|
var testFunctionUndefined = function () {
|
|
const value = dpt.encoder(undefined)
|
|
}
|
|
var testFunctionNull = function () {
|
|
const value = dpt.encoder(null)
|
|
}
|
|
expect(testFunctionNull).to.throw(InvalidValueError);
|
|
expect(testFunctionUndefined).to.throw(InvalidValueError);
|
|
});
|
|
}); |