Initial commit.
This commit is contained in:
67
tests/DPT1.test.ts
Normal file
67
tests/DPT1.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT1 } from "../src/DPT1"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferTrue = Buffer.from([1])
|
||||
let bufferFalse = Buffer.from([0])
|
||||
let bufferInvalid = Buffer.from([5])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
|
||||
describe("Test DPT1", (): void => {
|
||||
|
||||
let dpt = new DPT1();
|
||||
|
||||
it("Decode true", async function () {
|
||||
const value = dpt.decoder(bufferTrue)
|
||||
expect(value).is.equal(1);
|
||||
});
|
||||
|
||||
it("Decode false", async function () {
|
||||
const value = dpt.decoder(bufferFalse)
|
||||
expect(value).is.equal(0);
|
||||
});
|
||||
|
||||
it("Decode invalid value buffer", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferInvalid)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("encode true", async function () {
|
||||
const value = dpt.encoder(1)
|
||||
expect(compareBuffers(value, bufferTrue)).is.true;
|
||||
});
|
||||
|
||||
it("encode false", async function () {
|
||||
const value = dpt.encoder(0)
|
||||
expect(compareBuffers(value, bufferFalse)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(5)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
106
tests/DPT10.test.ts
Normal file
106
tests/DPT10.test.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT10 } from "../src/DPT10"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let date = new Date(Date.parse('04 Mar 2022 00:12:00 GMT'))
|
||||
let dayOfWeek = date.getDay()
|
||||
let hour = date.getHours()
|
||||
let minute = date.getMinutes()
|
||||
let second = date.getSeconds()
|
||||
let bufferValue = Buffer.from([(dayOfWeek << 5) + hour, minute, second])
|
||||
|
||||
let today = new Date()
|
||||
today.setDate(today.getDate() + dayOfWeek - today.getDay())
|
||||
today.setHours(hour)
|
||||
today.setMinutes(minute)
|
||||
today.setSeconds(second)
|
||||
today.setMilliseconds(0)
|
||||
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
|
||||
describe("Test DPT10", (): void => {
|
||||
|
||||
let dpt = new DPT10();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
expect(dpt.decoder(bufferValue).getTime()).is.equal(today.getTime());
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("Decode invalid buffers", async function () {
|
||||
let bufferErrorHour = Buffer.from([(dayOfWeek << 5) + 24, minute, second])
|
||||
let bufferErrorMinute = Buffer.from([(dayOfWeek << 5) + hour, 70, second])
|
||||
let bufferErrorSecond = Buffer.from([(dayOfWeek << 5) + hour, minute, 70])
|
||||
|
||||
var test_bufferErrorHour = function () {
|
||||
const value = dpt.decoder(bufferErrorHour)
|
||||
}
|
||||
var test_bufferErrorMinute = function () {
|
||||
const value = dpt.decoder(bufferErrorMinute)
|
||||
}
|
||||
var test_bufferErrorSecond = function () {
|
||||
const value = dpt.decoder(bufferErrorSecond)
|
||||
}
|
||||
|
||||
expect(test_bufferErrorHour).to.throw(InvalidValueError);
|
||||
expect(test_bufferErrorMinute).to.throw(InvalidValueError);
|
||||
expect(test_bufferErrorSecond).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
|
||||
/* Encoder tests */
|
||||
it("encode valid date", async function () {
|
||||
expect(compareBuffers(dpt.encoder(date), bufferValue)).is.true;
|
||||
});
|
||||
|
||||
it("encode valid number", async function () {
|
||||
expect(compareBuffers(dpt.encoder(date.getTime()), bufferValue)).is.true;
|
||||
});
|
||||
|
||||
it("encode valid string", async function () {
|
||||
|
||||
let todayTest = new Date()
|
||||
|
||||
todayTest.setHours(hour)
|
||||
todayTest.setMinutes(minute)
|
||||
todayTest.setSeconds(second)
|
||||
todayTest.setMilliseconds(0)
|
||||
let todayBuffer = dpt.encoder(todayTest)
|
||||
|
||||
expect(compareBuffers(dpt.encoder(`${hour}:${minute}:${second}`), todayBuffer)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid string", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder("10-10-10")
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
13
tests/DPT11.test.ts
Normal file
13
tests/DPT11.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
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 DPT11", (): void => {
|
||||
|
||||
let dpt = new DPT11();
|
||||
|
||||
});
|
||||
13
tests/DPT12.test.ts
Normal file
13
tests/DPT12.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT12 } from "../src/DPT12"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT12", (): void => {
|
||||
|
||||
let dpt = new DPT12();
|
||||
|
||||
});
|
||||
13
tests/DPT13.test.ts
Normal file
13
tests/DPT13.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT13 } from "../src/DPT13"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT13", (): void => {
|
||||
|
||||
let dpt = new DPT13();
|
||||
|
||||
});
|
||||
13
tests/DPT14.test.ts
Normal file
13
tests/DPT14.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
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 DPT14", (): void => {
|
||||
|
||||
let dpt = new DPT14();
|
||||
|
||||
});
|
||||
103
tests/DPT15.test.ts
Normal file
103
tests/DPT15.test.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT15, DPT15Result } from "../src/DPT15"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT15", (): void => {
|
||||
|
||||
let dpt = new DPT15();
|
||||
|
||||
let value: DPT15Result = {
|
||||
U: 0x03,
|
||||
V: 0x09,
|
||||
W: 0x04,
|
||||
X: 0x08,
|
||||
Y: 0x00,
|
||||
Z: 0x02,
|
||||
E: 0x01,
|
||||
P: 0x00,
|
||||
D: 0x01,
|
||||
C: 0x00,
|
||||
N: 0x05
|
||||
}
|
||||
|
||||
let bufferValid = Buffer.from([0x39, 0x48, 0x02, 0xa5])
|
||||
let bufferInvalid = Buffer.from([0x39, 0xc8, 0xa2, 0xa5])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0, 0x00])
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
const decoded = dpt.decoder(bufferValid)
|
||||
expect(decoded.U).is.equal(value.U);
|
||||
expect(decoded.V).is.equal(value.V);
|
||||
expect(decoded.W).is.equal(value.W);
|
||||
expect(decoded.X).is.equal(value.X);
|
||||
expect(decoded.Y).is.equal(value.Y);
|
||||
expect(decoded.Z).is.equal(value.Z);
|
||||
expect(decoded.E).is.equal(value.E);
|
||||
expect(decoded.P).is.equal(value.P);
|
||||
expect(decoded.D).is.equal(value.D);
|
||||
expect(decoded.C).is.equal(value.C);
|
||||
expect(decoded.N).is.equal(value.N);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("Decode invalid buffer", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferInvalid)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
/* Encoder */
|
||||
|
||||
it("encode valid", async function () {
|
||||
expect(compareBuffers(dpt.encoder(value), bufferValid)).is.true;
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction = function () {
|
||||
let invalidValue: DPT15Result = {
|
||||
U: 0x03,
|
||||
V: 0x0a, // invalid value as per spec
|
||||
W: 0x04,
|
||||
X: 0x08,
|
||||
Y: 0x00,
|
||||
Z: 0x02,
|
||||
E: 0x01,
|
||||
P: 0x00,
|
||||
D: 0x01,
|
||||
C: 0x00,
|
||||
N: 0x05
|
||||
}
|
||||
const result = dpt.encoder(invalidValue)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
});
|
||||
13
tests/DPT16.test.ts
Normal file
13
tests/DPT16.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
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 DPT16", (): void => {
|
||||
|
||||
let dpt = new DPT16();
|
||||
|
||||
});
|
||||
13
tests/DPT17.test.ts
Normal file
13
tests/DPT17.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT17 } from "../src/DPT17"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT17", (): void => {
|
||||
|
||||
let dpt = new DPT17();
|
||||
|
||||
});
|
||||
13
tests/DPT18.test.ts
Normal file
13
tests/DPT18.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT18 } from "../src/DPT18"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT18", (): void => {
|
||||
|
||||
let dpt = new DPT18();
|
||||
|
||||
});
|
||||
13
tests/DPT19.test.ts
Normal file
13
tests/DPT19.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT19 } from "../src/DPT19"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT19", (): void => {
|
||||
|
||||
let dpt = new DPT19();
|
||||
|
||||
});
|
||||
74
tests/DPT2.test.ts
Normal file
74
tests/DPT2.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT2 } from "../src/DPT2"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferZeroZero = Buffer.from([0])
|
||||
let bufferZeroOne = Buffer.from([1])
|
||||
let bufferOneZero = Buffer.from([2])
|
||||
let bufferOneOne = Buffer.from([3])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
|
||||
describe("Test DPT2", (): void => {
|
||||
|
||||
let dpt = new DPT2();
|
||||
|
||||
it("Decode buffer00", async function () {
|
||||
const value = dpt.decoder(bufferZeroZero)
|
||||
expect(value.data).is.false;
|
||||
expect(value.priority).is.false;
|
||||
});
|
||||
|
||||
it("Decode buffer01", async function () {
|
||||
const value = dpt.decoder(bufferZeroOne)
|
||||
expect(value.data).is.true;
|
||||
expect(value.priority).is.false;
|
||||
});
|
||||
|
||||
it("Decode buffer10", async function () {
|
||||
const value = dpt.decoder(bufferOneZero)
|
||||
expect(value.data).is.false;
|
||||
expect(value.priority).is.true;
|
||||
});
|
||||
|
||||
it("Decode buffer11", async function () {
|
||||
const value = dpt.decoder(bufferOneOne)
|
||||
expect(value.data).is.true;
|
||||
expect(value.priority).is.true;
|
||||
});
|
||||
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("encode true", async function () {
|
||||
expect(compareBuffers(dpt.encoder({ data: false, priority: false }), bufferZeroZero)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: false, priority: true }), bufferOneZero)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: true, priority: false }), bufferZeroOne)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: true, priority: true }), bufferOneOne)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
13
tests/DPT20.test.ts
Normal file
13
tests/DPT20.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT20 } from "../src/DPT20"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT20", (): void => {
|
||||
|
||||
let dpt = new DPT20();
|
||||
|
||||
});
|
||||
13
tests/DPT21.test.ts
Normal file
13
tests/DPT21.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT21 } from "../src/DPT21"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT21", (): void => {
|
||||
|
||||
let dpt = new DPT21();
|
||||
|
||||
});
|
||||
85
tests/DPT232.test.ts
Normal file
85
tests/DPT232.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT232, DPT232Result } from "../src/DPT232"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let value: DPT232Result = { red: 127, green: 200, blue: 20 }
|
||||
|
||||
let buffer = Buffer.from([127, 200, 20])
|
||||
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
describe("Test DPT232", (): void => {
|
||||
|
||||
let dpt = new DPT232();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
const v = dpt.decoder(buffer)
|
||||
expect(v.red).is.equal(value.red);
|
||||
expect(v.green).is.equal(value.green);
|
||||
expect(v.blue).is.equal(value.blue);
|
||||
});
|
||||
|
||||
|
||||
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(value), buffer)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunctionRedPlus = function () {
|
||||
const value = dpt.encoder({ red: 300, green: 0, blue: 0 })
|
||||
}
|
||||
var testFunctionRedMinus = function () {
|
||||
const value = dpt.encoder({ red: -2, green: 0, blue: 0 })
|
||||
}
|
||||
|
||||
var testFunctionGreenPlus = function () {
|
||||
const value = dpt.encoder({ green: 300, red: 0, blue: 0 })
|
||||
}
|
||||
var testFunctionGreenMinus = function () {
|
||||
const value = dpt.encoder({ green: -2, red: 0, blue: 0 })
|
||||
}
|
||||
|
||||
var testFunctionBluePlus = function () {
|
||||
const value = dpt.encoder({ blue: 300, green: 0, red: 0 })
|
||||
}
|
||||
var testFunctionBlueMinus = function () {
|
||||
const value = dpt.encoder({ blue: -2, green: 0, red: 0 })
|
||||
}
|
||||
|
||||
|
||||
expect(testFunctionRedPlus).to.throw(InvalidValueError);
|
||||
expect(testFunctionRedMinus).to.throw(InvalidValueError);
|
||||
expect(testFunctionGreenPlus).to.throw(InvalidValueError);
|
||||
expect(testFunctionGreenMinus).to.throw(InvalidValueError);
|
||||
expect(testFunctionBluePlus).to.throw(InvalidValueError);
|
||||
expect(testFunctionBlueMinus).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
13
tests/DPT237.test.ts
Normal file
13
tests/DPT237.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT237 } from "../src/DPT237"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT237", (): void => {
|
||||
|
||||
let dpt = new DPT237();
|
||||
|
||||
});
|
||||
55
tests/DPT238.test.ts
Normal file
55
tests/DPT238.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT238 } from "../src/DPT238"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferA = Buffer.from([65])
|
||||
let bufferMax = Buffer.from([254])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT238", (): void => {
|
||||
|
||||
let dpt = new DPT238();
|
||||
|
||||
it("Decode buffer common", async function () {
|
||||
const value = dpt.decoder(bufferA)
|
||||
expect(value).is.equal(65);
|
||||
});
|
||||
|
||||
it("Decode buffer max", async function () {
|
||||
const value = dpt.decoder(bufferMax)
|
||||
expect(value).is.equal(254);
|
||||
});
|
||||
|
||||
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(65), bufferA)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(254), bufferMax)).is.true;
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
82
tests/DPT3.test.ts
Normal file
82
tests/DPT3.test.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT3 } from "../src/DPT3"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferDecZero = Buffer.from([0])
|
||||
let bufferDecValue = Buffer.from([5])
|
||||
let bufferIncZero = Buffer.from([8])
|
||||
let bufferIncValue = Buffer.from([13])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT3", (): void => {
|
||||
|
||||
let dpt = new DPT3();
|
||||
|
||||
it("Decode buffer dec-zero", async function () {
|
||||
const value = dpt.decoder(bufferDecZero)
|
||||
expect(value.data).is.equal(0);
|
||||
expect(value.decr_incr).is.equal(0);
|
||||
});
|
||||
|
||||
it("Decode buffer dec-value", async function () {
|
||||
const value = dpt.decoder(bufferDecValue)
|
||||
expect(value.data).is.equal(5);
|
||||
expect(value.decr_incr).is.equal(0);
|
||||
});
|
||||
|
||||
it("Decode buffer inc-zero", async function () {
|
||||
const value = dpt.decoder(bufferIncZero)
|
||||
expect(value.data).is.equal(0);
|
||||
expect(value.decr_incr).is.equal(1);
|
||||
});
|
||||
|
||||
it("Decode buffer inc-value", async function () {
|
||||
const value = dpt.decoder(bufferIncValue)
|
||||
expect(value.data).is.equal(5);
|
||||
expect(value.decr_incr).is.equal(1);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
/* Encode tests */
|
||||
it("encode valid", async function () {
|
||||
expect(compareBuffers(dpt.encoder({ data: 0, decr_incr: 0 }), bufferDecZero)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: 5, decr_incr: 0 }), bufferDecValue)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: 0, decr_incr: 1 }), bufferIncZero)).is.true;
|
||||
expect(compareBuffers(dpt.encoder({ data: 5, decr_incr: 1 }), bufferIncValue)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder({ data: 0, decr_incr: 3 })
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
57
tests/DPT4.test.ts
Normal file
57
tests/DPT4.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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 DPT4", (): 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);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
55
tests/DPT5.test.ts
Normal file
55
tests/DPT5.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT5 } from "../src/DPT5"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferA = Buffer.from([65])
|
||||
let bufferMax = Buffer.from([254])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT5", (): void => {
|
||||
|
||||
let dpt = new DPT5();
|
||||
|
||||
it("Decode buffer common", async function () {
|
||||
const value = dpt.decoder(bufferA)
|
||||
expect(value).is.equal(65);
|
||||
});
|
||||
|
||||
it("Decode buffer max", async function () {
|
||||
const value = dpt.decoder(bufferMax)
|
||||
expect(value).is.equal(254);
|
||||
});
|
||||
|
||||
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(65), bufferA)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(254), bufferMax)).is.true;
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
66
tests/DPT6.test.ts
Normal file
66
tests/DPT6.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT6 } from "../src/DPT6"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let value: number = 65
|
||||
let overflowValue: number = 4000
|
||||
|
||||
let bufferPositive = Buffer.from([value])
|
||||
let bufferNegative = Buffer.from([-value])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT6", (): void => {
|
||||
|
||||
let dpt = new DPT6();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
expect(dpt.decoder(bufferPositive)).is.equal(value);
|
||||
expect(dpt.decoder(bufferNegative)).is.equal(-value);
|
||||
});
|
||||
|
||||
|
||||
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(value), bufferPositive)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(-value), bufferNegative)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction1 = function () {
|
||||
const value = dpt.encoder(-overflowValue)
|
||||
}
|
||||
var testFunction2 = function () {
|
||||
const value = dpt.encoder(overflowValue)
|
||||
}
|
||||
|
||||
expect(testFunction1).to.throw(InvalidValueError);
|
||||
expect(testFunction2).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
63
tests/DPT7.test.ts
Normal file
63
tests/DPT7.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT7 } from "../src/DPT7"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let bufferA = Buffer.from([1, 0])
|
||||
let bufferMax = Buffer.from([255, 255])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0, 2, 4])
|
||||
|
||||
describe("Test DPT7", (): void => {
|
||||
|
||||
let dpt = new DPT7();
|
||||
|
||||
it("Decode buffer common", async function () {
|
||||
const value = dpt.decoder(bufferA)
|
||||
expect(value).is.equal(256);
|
||||
});
|
||||
|
||||
it("Decode buffer max", async function () {
|
||||
const value = dpt.decoder(bufferMax)
|
||||
expect(value).is.equal(65535);
|
||||
});
|
||||
|
||||
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(256), bufferA)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(65535), bufferMax)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(-200)
|
||||
}
|
||||
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
66
tests/DPT8.test.ts
Normal file
66
tests/DPT8.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT8 } from "../src/DPT8"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let value: number = 256
|
||||
let overflowValue: number = 40000
|
||||
|
||||
let bufferPositive = Buffer.from([1, 0])
|
||||
let bufferNegative = Buffer.from([255, 0])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
describe("Test DPT8", (): void => {
|
||||
|
||||
let dpt = new DPT8();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
expect(dpt.decoder(bufferPositive)).is.equal(value);
|
||||
expect(dpt.decoder(bufferNegative)).is.equal(-value);
|
||||
});
|
||||
|
||||
|
||||
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(value), bufferPositive)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(-value), bufferNegative)).is.true;
|
||||
});
|
||||
|
||||
it("encode invalid", async function () {
|
||||
var testFunction1 = function () {
|
||||
const value = dpt.encoder(-overflowValue)
|
||||
}
|
||||
var testFunction2 = function () {
|
||||
const value = dpt.encoder(overflowValue)
|
||||
}
|
||||
|
||||
expect(testFunction1).to.throw(InvalidValueError);
|
||||
expect(testFunction2).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
62
tests/DPT9.test.ts
Normal file
62
tests/DPT9.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT9 } from "../src/DPT9"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
let value: number = 21.08
|
||||
let valuemin: number = 1.0e-15
|
||||
let bufferValue = Buffer.from([0x0C, 0x1E])
|
||||
let bufferZero = Buffer.from([0x00, 0x00])
|
||||
let bufferNegative = Buffer.from([0x8b, 0xe2])
|
||||
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
describe("Test DPT9", (): void => {
|
||||
|
||||
let dpt = new DPT9();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
expect(dpt.decoder(bufferZero)).is.equal(0);
|
||||
expect(dpt.decoder(bufferValue)).is.equal(value);
|
||||
expect(dpt.decoder(bufferNegative)).is.equal(-value);
|
||||
});
|
||||
|
||||
|
||||
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(0), bufferZero)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(value), bufferValue)).is.true;
|
||||
expect(compareBuffers(dpt.encoder(-value), bufferNegative)).is.true;
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
it("encode infinite", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(Infinity)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
||||
28
tests/util.ts
Normal file
28
tests/util.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
export function bufferToString(buffer: Buffer): string {
|
||||
let result = '['
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
if (i != 0)
|
||||
result += ', '
|
||||
result += `${buffer[i]}`
|
||||
}
|
||||
result += ']'
|
||||
return result
|
||||
}
|
||||
|
||||
export function compareBuffers(b1: Buffer, b2: Buffer): boolean {
|
||||
if (b1.length != b2.length)
|
||||
throw new Error(`Different buffer lengths: b1: ${b1.length}, b2: ${b2.length}`)
|
||||
|
||||
for (let i = 0; i < b1.length; i++) {
|
||||
const v1 = b1[i];
|
||||
const v2 = b2[i];
|
||||
|
||||
if (typeof v1 != typeof v2)
|
||||
throw new Error(`Different type values: ${typeof v1}, ${typeof v2} @${i} - ${bufferToString(b1)} ${bufferToString(b2)}`)
|
||||
if (v1 != v2)
|
||||
throw new Error(`Different values: ${v1}, ${v2} @${i} - ${bufferToString(b1)} ${bufferToString(b2)}`)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user