Add tests for DPT18, DPT19.
This commit is contained in:
parent
28e90a35ff
commit
aa52933d15
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT18 } from "../src/DPT18"
|
||||
import { DPT18, DPT18Result } from "../src/DPT18"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
@ -10,4 +10,67 @@ describe("Test DPT018", (): void => {
|
||||
|
||||
let dpt = new DPT18();
|
||||
|
||||
it("Decode correct buffer", async function () {
|
||||
let buffer = Buffer.from([0x80 + 0x21])
|
||||
|
||||
const decoded = dpt.decoder(buffer)
|
||||
expect(decoded.activateLearn).is.equal(1);
|
||||
expect(decoded.sceneNumber).is.equal(0x21);
|
||||
});
|
||||
|
||||
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])
|
||||
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferBiggerSize)
|
||||
}
|
||||
expect(testFunction).to.throw(BufferLengthError);
|
||||
});
|
||||
|
||||
/* Encoder tests */
|
||||
it("encode valid", async function () {
|
||||
const value: DPT18Result = {
|
||||
activateLearn: 1,
|
||||
sceneNumber: 0x11
|
||||
}
|
||||
const buffer = Buffer.from([0x80 + 0x11])
|
||||
|
||||
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 = 0x80
|
||||
var testFunction_invalidActivateLearn = function () {
|
||||
let value: DPT18Result = {
|
||||
activateLearn: 2,
|
||||
sceneNumber: 0x11
|
||||
}
|
||||
const result = dpt.encoder(value)
|
||||
}
|
||||
var testFunction_invalidSceneNumber = function () {
|
||||
let value: DPT18Result = {
|
||||
activateLearn: 0,
|
||||
sceneNumber: 64
|
||||
}
|
||||
const result = dpt.encoder(value)
|
||||
}
|
||||
expect(testFunction_invalidActivateLearn).to.throw(InvalidValueError);
|
||||
expect(testFunction_invalidSceneNumber).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
});
|
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DPT19 } from "../src/DPT19"
|
||||
import { DPT19, DPT19Result } from "../src/DPT19"
|
||||
import { BufferLengthError } from "../src/errors/BufferLengthError";
|
||||
import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
@ -10,4 +10,161 @@ describe("Test DPT019", (): void => {
|
||||
|
||||
let dpt = new DPT19();
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
let value: DPT19Result = {
|
||||
dateTime: new Date(1921, 8, 22, 10, 11, 19, 0),
|
||||
dayOfWeek: undefined,
|
||||
f: false,
|
||||
wd: false,
|
||||
nwd: false,
|
||||
ny: false,
|
||||
nd: false,
|
||||
ndow: false,
|
||||
nt: false,
|
||||
sst: false,
|
||||
internalClock: false,
|
||||
reliability: false
|
||||
}
|
||||
let buffer = Buffer.from([21, 9, 22, 10, 11, 19, 0, 0])
|
||||
const decoded = dpt.decoder(buffer)
|
||||
expect(decoded.dateTime.valueOf()).is.equal(value.dateTime.valueOf());
|
||||
expect(decoded.dayOfWeek).is.equal(value.dayOfWeek);
|
||||
expect(decoded.f).is.equal(value.f);
|
||||
expect(decoded.wd).is.equal(value.wd);
|
||||
expect(decoded.nwd).is.equal(value.nwd);
|
||||
expect(decoded.ny).is.equal(value.ny);
|
||||
expect(decoded.nd).is.equal(value.nd);
|
||||
expect(decoded.ndow).is.equal(value.ndow);
|
||||
expect(decoded.nt).is.equal(value.nt);
|
||||
expect(decoded.sst).is.equal(value.sst);
|
||||
expect(decoded.internalClock).is.equal(value.internalClock);
|
||||
expect(decoded.reliability).is.equal(value.reliability);
|
||||
});
|
||||
|
||||
it("Decode buffer DayOfWeek == 7", async function () {
|
||||
let value: DPT19Result = {
|
||||
dateTime: new Date(2021, 8, 27, 10, 11, 19, 0),
|
||||
dayOfWeek: 0,
|
||||
f: false,
|
||||
wd: false,
|
||||
nwd: false,
|
||||
ny: false,
|
||||
nd: false,
|
||||
ndow: false,
|
||||
nt: false,
|
||||
sst: false,
|
||||
internalClock: false,
|
||||
reliability: false
|
||||
}
|
||||
let buffer = Buffer.from([121, 9, 27, 10 + (7 << 5), 11, 19, 0, 0])
|
||||
const decoded = dpt.decoder(buffer)
|
||||
expect(decoded.dateTime.valueOf()).is.equal(value.dateTime.valueOf());
|
||||
expect(decoded.dayOfWeek).is.equal(value.dayOfWeek);
|
||||
expect(decoded.f).is.equal(value.f);
|
||||
expect(decoded.wd).is.equal(value.wd);
|
||||
expect(decoded.nwd).is.equal(value.nwd);
|
||||
expect(decoded.ny).is.equal(value.ny);
|
||||
expect(decoded.nd).is.equal(value.nd);
|
||||
expect(decoded.ndow).is.equal(value.ndow);
|
||||
expect(decoded.nt).is.equal(value.nt);
|
||||
expect(decoded.sst).is.equal(value.sst);
|
||||
expect(decoded.internalClock).is.equal(value.internalClock);
|
||||
expect(decoded.reliability).is.equal(value.reliability);
|
||||
});
|
||||
|
||||
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([0, 0, 0, 0, 0, 0, 0, 0, 0, 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: DPT19Result = {
|
||||
dateTime: new Date(1921, 10, 22, 10, 11, 19, 0),
|
||||
dayOfWeek: undefined,
|
||||
f: false,
|
||||
wd: false,
|
||||
nwd: false,
|
||||
ny: false,
|
||||
nd: false,
|
||||
ndow: false,
|
||||
nt: false,
|
||||
sst: false,
|
||||
internalClock: false,
|
||||
reliability: false
|
||||
}
|
||||
let buffer = Buffer.from([21, 11, 22, 10, 11, 19, 0, 0])
|
||||
|
||||
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
|
||||
});
|
||||
|
||||
it("encode with dayOfWeek", async function () {
|
||||
let value: DPT19Result = {
|
||||
dateTime: new Date(1921, 10, 22, 10, 11, 19, 0),
|
||||
dayOfWeek: 7,
|
||||
f: false,
|
||||
wd: false,
|
||||
nwd: false,
|
||||
ny: false,
|
||||
nd: false,
|
||||
ndow: false,
|
||||
nt: false,
|
||||
sst: false,
|
||||
internalClock: false,
|
||||
reliability: false
|
||||
}
|
||||
let buffer = Buffer.from([21, 11, 22, 234, 11, 19, 0, 0])
|
||||
|
||||
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
|
||||
});
|
||||
|
||||
it("encode from Date", async function () {
|
||||
|
||||
let value = new Date(1921, 10, 22, 10, 11, 19, 0)
|
||||
let buffer = Buffer.from([21, 11, 22, 74, 11, 19, 0, 128])
|
||||
|
||||
// Note the conversion from date says that the clock is unreliable.
|
||||
// It also gets the day of week and populates it.
|
||||
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 () {
|
||||
var testFunction = function () {
|
||||
let value: DPT19Result = {
|
||||
dateTime: new Date(1921, 9, 22, 24, 11, 19, 0),
|
||||
dayOfWeek: 10,
|
||||
f: false,
|
||||
wd: false,
|
||||
nwd: false,
|
||||
ny: false,
|
||||
nd: false,
|
||||
ndow: false,
|
||||
nt: false,
|
||||
sst: false,
|
||||
internalClock: false,
|
||||
reliability: false
|
||||
}
|
||||
const result = dpt.encoder(value)
|
||||
}
|
||||
expect(testFunction).to.throw(RangeError);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user