58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { expect } from "chai";
|
|
|
|
import { DPT4 } from "../src/DPT4"
|
|
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
|
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
|
|
|
import { compareBuffers } from "./util"
|
|
|
|
let bufferA = Buffer.from([65])
|
|
|
|
let bufferEmpty = Buffer.from([])
|
|
let bufferBiggerSize = Buffer.from([1, 0])
|
|
|
|
describe("Test DPT004", (): void => {
|
|
|
|
let dpt = new DPT4();
|
|
|
|
it("Decode buffer with letter [A]", async function () {
|
|
const value = dpt.decoder(bufferA)
|
|
expect(value).is.equal('A');
|
|
});
|
|
|
|
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('A'), bufferA)).is.true;
|
|
});
|
|
|
|
it("encode invalid", async function () {
|
|
var testFunction = function () {
|
|
const value = dpt.encoder("\u03c0")
|
|
}
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
});
|
|
|
|
it("encode undefined", async function () {
|
|
var testFunction = function () {
|
|
const value = dpt.encoder(undefined)
|
|
}
|
|
expect(testFunction).to.throw(InvalidValueError);
|
|
});
|
|
|
|
|
|
});
|