dlms-protocol
Version:
DLMS Protocol parser for JavaScript
76 lines (61 loc) • 2.96 kB
JavaScript
import DlmsUint8 from "../src/typesDLMS/DlmsUint8";
import DlmsDataType from "../src/enum/DlmsDataType";
describe("DlmsUint8 class", () => {
test("should initialize with default values when no data is provided", () => {
const dlmsUint8 = new DlmsUint8();
expect(dlmsUint8.tag).toBe(DlmsDataType.UINT8);
expect(dlmsUint8.totalBytes).toBe(0);
expect(dlmsUint8.rawValue).toEqual([]);
expect(dlmsUint8.bodyBytes).toEqual([]);
expect(dlmsUint8.value).toBe(0);
});
test("should correctly initialize from a byte array", () => {
const data = [DlmsDataType.UINT8, 0x7F]; // UINT8 tag and value (127)
const dlmsUint8 = new DlmsUint8(data);
expect(dlmsUint8.tag).toBe(DlmsDataType.UINT8);
expect(dlmsUint8.totalBytes).toBe(2);
expect(dlmsUint8.rawValue).toEqual([DlmsDataType.UINT8, 0x7F]);
expect(dlmsUint8.bodyBytes).toEqual([0x7F]);
expect(dlmsUint8.value).toBe(127);
});
test("should throw an error if tag does not match when inserting data", () => {
const data = [0xFF, 0x7F]; // Invalid tag
const dlmsUint8 = new DlmsUint8();
expect(() => dlmsUint8.insertData(data)).toThrow("Tag mismatch.");
});
test("should correctly initialize from a numeric value", () => {
const dlmsUint8 = new DlmsUint8(200);
expect(dlmsUint8.tag).toBe(DlmsDataType.UINT8);
expect(dlmsUint8.totalBytes).toBe(2);
expect(dlmsUint8.rawValue).toEqual([DlmsDataType.UINT8, 0xC8]); // 200 in hexadecimal
expect(dlmsUint8.bodyBytes).toEqual([0xC8]);
expect(dlmsUint8.value).toBe(200);
});
test("should return the correct standard length", () => {
const dlmsUint8 = new DlmsUint8();
expect(dlmsUint8.getStandardLength()).toBe(2);
});
test("should return the correct value with getValue()", () => {
const data = [DlmsDataType.UINT8, 0x05];
const dlmsUint8 = new DlmsUint8(data);
expect(dlmsUint8.getValue()).toBe(5);
});
test("should correctly insert data from a byte array", () => {
const dlmsUint8 = new DlmsUint8();
dlmsUint8.insertData([DlmsDataType.UINT8, 0x0A]);
expect(dlmsUint8.tag).toBe(DlmsDataType.UINT8);
expect(dlmsUint8.totalBytes).toBe(2);
expect(dlmsUint8.rawValue).toEqual([DlmsDataType.UINT8, 0x0A]);
expect(dlmsUint8.bodyBytes).toEqual([0x0A]);
expect(dlmsUint8.value).toBe(10);
});
test("should correctly insert data from a numeric value", () => {
const dlmsUint8 = new DlmsUint8();
dlmsUint8.insertDataFromValue(45);
expect(dlmsUint8.tag).toBe(DlmsDataType.UINT8);
expect(dlmsUint8.totalBytes).toBe(2);
expect(dlmsUint8.rawValue).toEqual([DlmsDataType.UINT8, 0x2D]); // 45 in hexadecimal
expect(dlmsUint8.bodyBytes).toEqual([0x2D]);
expect(dlmsUint8.value).toBe(45);
});
});