Renamed tests so they're executed in order.
Add tests for 16,17.
This commit is contained in:
parent
a998ec05ee
commit
28e90a35ff
@ -19,7 +19,7 @@ export class DPT10 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): Date {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
const [dnh, minutes, seconds] = buffer;
|
||||
const dow = (dnh & 0b11100000) >> 5;
|
||||
|
@ -16,7 +16,7 @@ export class DPT11 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): Date {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
const day = buffer[0] & 31; //0b00011111);
|
||||
const month = buffer[1] & 15; //0b00001111);
|
||||
|
@ -17,7 +17,7 @@ export class DPT12 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
return buffer.readUInt32BE(0);
|
||||
};
|
||||
|
@ -17,7 +17,7 @@ export class DPT13 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
// In 4 bytes, there's no way to exceed the range
|
||||
return buffer.readInt32BE(0)
|
||||
|
@ -17,7 +17,7 @@ export class DPT14 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
return buffer.readFloatBE(0);
|
||||
};
|
||||
|
@ -46,7 +46,7 @@ export class DPT15 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): DPT15Result {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
const result: DPT15Result = {
|
||||
U: (buffer[0] & 0xf0) >> 4,
|
||||
|
@ -16,7 +16,7 @@ export class DPT16 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): string {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
|
||||
return buffer.toString('ascii');
|
||||
|
@ -16,7 +16,7 @@ export class DPT17 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
// Scene number between 0 and 63
|
||||
return buffer.readUInt8(0) & 0b00111111;
|
||||
|
@ -21,7 +21,7 @@ export class DPT18 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): DPT18Result {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
|
||||
return {
|
||||
|
@ -102,7 +102,7 @@ export class DPT19 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): DPT19Result {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
const byte8 = buffer.readUInt8(0)
|
||||
const byte7 = buffer.readUInt8(1)
|
||||
|
@ -29,7 +29,7 @@ export class DPT20 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
return buffer.readUInt8(0);
|
||||
};
|
||||
|
@ -28,7 +28,7 @@ export class DPT21 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): boolean[] {
|
||||
if (buffer.length !== 2)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected 2.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected 2.`);
|
||||
|
||||
const result = [
|
||||
!!(buffer[0] & 0x80),
|
||||
|
@ -17,7 +17,7 @@ export class DPT237 implements DPT {
|
||||
*/
|
||||
decoder(buffer: Buffer): number {
|
||||
if (buffer.length !== this.bufferLength)
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT8. Expected ${this.bufferLength}.`);
|
||||
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
||||
|
||||
return value;
|
||||
};
|
||||
|
@ -13,7 +13,7 @@ let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
|
||||
describe("Test DPT1", (): void => {
|
||||
describe("Test DPT001", (): void => {
|
||||
|
||||
let dpt = new DPT1();
|
||||
|
@ -15,7 +15,7 @@ let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
|
||||
describe("Test DPT2", (): void => {
|
||||
describe("Test DPT002", (): void => {
|
||||
|
||||
let dpt = new DPT2();
|
||||
|
@ -14,7 +14,7 @@ let bufferIncValue = Buffer.from([13])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT3", (): void => {
|
||||
describe("Test DPT003", (): void => {
|
||||
|
||||
let dpt = new DPT3();
|
||||
|
@ -11,7 +11,7 @@ let bufferA = Buffer.from([65])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT4", (): void => {
|
||||
describe("Test DPT004", (): void => {
|
||||
|
||||
let dpt = new DPT4();
|
||||
|
@ -12,7 +12,7 @@ let bufferMax = Buffer.from([254])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT5", (): void => {
|
||||
describe("Test DPT005", (): void => {
|
||||
|
||||
let dpt = new DPT5();
|
||||
|
@ -15,7 +15,7 @@ let bufferNegative = Buffer.from([-value])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0])
|
||||
|
||||
describe("Test DPT6", (): void => {
|
||||
describe("Test DPT006", (): void => {
|
||||
|
||||
let dpt = new DPT6();
|
||||
|
@ -12,7 +12,7 @@ let bufferMax = Buffer.from([255, 255])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([1, 0, 2, 4])
|
||||
|
||||
describe("Test DPT7", (): void => {
|
||||
describe("Test DPT007", (): void => {
|
||||
|
||||
let dpt = new DPT7();
|
||||
|
@ -15,7 +15,7 @@ let bufferNegative = Buffer.from([255, 0])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
describe("Test DPT8", (): void => {
|
||||
describe("Test DPT008", (): void => {
|
||||
|
||||
let dpt = new DPT8();
|
||||
|
@ -15,7 +15,7 @@ let bufferNegative = Buffer.from([0x8b, 0xe2])
|
||||
let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
describe("Test DPT9", (): void => {
|
||||
describe("Test DPT009", (): void => {
|
||||
|
||||
let dpt = new DPT9();
|
||||
|
@ -25,7 +25,7 @@ let bufferEmpty = Buffer.from([])
|
||||
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0])
|
||||
|
||||
|
||||
describe("Test DPT10", (): void => {
|
||||
describe("Test DPT010", (): void => {
|
||||
|
||||
let dpt = new DPT10();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT11", (): void => {
|
||||
describe("Test DPT011", (): void => {
|
||||
|
||||
let dpt = new DPT11();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT12", (): void => {
|
||||
describe("Test DPT012", (): void => {
|
||||
|
||||
let dpt = new DPT12();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT13", (): void => {
|
||||
describe("Test DPT013", (): void => {
|
||||
|
||||
let dpt = new DPT13();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT14", (): void => {
|
||||
describe("Test DPT014", (): void => {
|
||||
|
||||
let dpt = new DPT14();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT15", (): void => {
|
||||
describe("Test DPT015", (): void => {
|
||||
|
||||
let dpt = new DPT15();
|
||||
|
59
tests/DPT016.test.ts
Normal file
59
tests/DPT016.test.ts
Normal file
@ -0,0 +1,59 @@
|
||||
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;
|
||||
});
|
||||
|
||||
});
|
58
tests/DPT017.test.ts
Normal file
58
tests/DPT017.test.ts
Normal file
@ -0,0 +1,58 @@
|
||||
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 DPT017", (): void => {
|
||||
|
||||
let dpt = new DPT17();
|
||||
it("Decode buffer acceptable", async function () {
|
||||
let value = 0x24
|
||||
let buffer = Buffer.from([0x24])
|
||||
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])
|
||||
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferBiggerSize)
|
||||
}
|
||||
expect(testFunction).to.throw(BufferLengthError);
|
||||
});
|
||||
|
||||
/* Encoder tests */
|
||||
it("encode valid", async function () {
|
||||
let value = 0x15
|
||||
let buffer = Buffer.from([0x15])
|
||||
|
||||
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 = function () {
|
||||
const result = dpt.encoder(value)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT18", (): void => {
|
||||
describe("Test DPT018", (): void => {
|
||||
|
||||
let dpt = new DPT18();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT19", (): void => {
|
||||
describe("Test DPT019", (): void => {
|
||||
|
||||
let dpt = new DPT19();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT20", (): void => {
|
||||
describe("Test DPT020", (): void => {
|
||||
|
||||
let dpt = new DPT20();
|
||||
|
@ -6,7 +6,7 @@ import { InvalidValueError } from "../src/errors/InvalidValueError";
|
||||
|
||||
import { compareBuffers } from "./util"
|
||||
|
||||
describe("Test DPT21", (): void => {
|
||||
describe("Test DPT021", (): void => {
|
||||
|
||||
let dpt = new DPT21();
|
||||
|
@ -1,13 +0,0 @@
|
||||
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();
|
||||
|
||||
});
|
@ -1,13 +0,0 @@
|
||||
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();
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user