64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { expect } from "chai";
|
|
|
|
import { DPT11 } from "../src/DPT11"
|
|
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
|
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
|
|
|
import { compareBuffers } from "./util"
|
|
|
|
describe("Test DPT011", (): void => {
|
|
|
|
let dpt = new DPT11();
|
|
let value = new Date(2021, 11, 25)
|
|
let buffer = Buffer.from([25, 12, 21])
|
|
|
|
it("Decode buffer acceptable", async function () {
|
|
const decoded = dpt.decoder(buffer)
|
|
expect(decoded.toUTCString()).is.equal(value.toUTCString());
|
|
});
|
|
|
|
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 invalid buffer", async function () {
|
|
let bufferEmpty = Buffer.from([25, 13, 21])
|
|
var testFunction = function () {
|
|
const value = dpt.decoder(bufferEmpty)
|
|
}
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
});
|
|
|
|
it("Decode oversized buffer", async function () {
|
|
let bufferBiggerSize = Buffer.from([0x20, 0x15, 0, 0])
|
|
|
|
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), buffer)).is.true;
|
|
});
|
|
|
|
it("encode undefined", async function () {
|
|
var testFunction = function () {
|
|
const value = dpt.encoder(undefined)
|
|
}
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
});
|
|
|
|
it("encode invalid value", async function () {
|
|
const value = "invalid"
|
|
var testFunction = function () {
|
|
const result = dpt.encoder(value)
|
|
}
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
});
|
|
}); |