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

59 lines
1.9 KiB
TypeScript

import { expect } from "chai";
import { DPT16 } from "../src/DPT16"
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
describe("Test DPT016", (): void => {
let dpt = new DPT16();
it("Decode buffer acceptable", async function () {
let str = 'A brown fox ju'
let buffer = Buffer.from([0x41, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75])
const decoded = dpt.decoder(buffer)
expect(decoded).is.equal(str);
});
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([0xff, 0xff, 0x63, 0xc0, 0x00, 0xff, 0xff, 0x63, 0xc0, 0x00, 0xff, 0xff, 0x63, 0xc0, 0x00])
var testFunction = function () {
const value = dpt.decoder(bufferBiggerSize)
}
expect(testFunction).to.throw(BufferLengthError);
});
/* Encoder tests */
it("encode valid", async function () {
let str = 'A brown fox ju'
let buffer = Buffer.from([0x41, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75])
expect(compareBuffers(dpt.encoder(str), buffer)).is.true;
});
it("encode undefined", async function () {
var testFunction = function () {
const value = dpt.encoder(undefined)
}
expect(testFunction).to.throw(InvalidValueError);
});
it("encode longer text", async function () {
const str = "A brown fox jumps over the lazy dog."
let buffer = Buffer.from([0x41, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75])
expect(compareBuffers(dpt.encoder(str), buffer)).is.true;
});
});