From f9fa0100baf076ab007e66e67bf44668fa711eeb Mon Sep 17 00:00:00 2001 From: Laur Ivan Date: Sat, 12 Mar 2022 23:02:28 +0100 Subject: [PATCH] Add Test for DPT12. --- tests/DPT012.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/DPT012.test.ts b/tests/DPT012.test.ts index c823114..821a3b4 100644 --- a/tests/DPT012.test.ts +++ b/tests/DPT012.test.ts @@ -9,5 +9,42 @@ import { compareBuffers } from "./util" describe("Test DPT012", (): void => { let dpt = new DPT12(); + it("Decode buffer acceptable", async function () { + let value = 0x24101011 + 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 = 0x15f040cc + let buffer = Buffer.from([0x15, 0xf0, 0x40, 0xcc]) + + 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); + }); }); \ No newline at end of file