dlms-protocol
Version:
DLMS Protocol parser for JavaScript
90 lines (71 loc) • 3.74 kB
JavaScript
import DlmsUint32 from "../src/typesDLMS/DlmsUint32";
import DlmsDataType from "../src/enum/DlmsDataType";
describe("DlmsUint32 class", () => {
test("should initialize with default values when no data is provided", () => {
const dlmsUint32 = new DlmsUint32();
expect(dlmsUint32.tag).toBe(DlmsDataType.UINT32);
expect(dlmsUint32.totalBytes).toBe(0);
expect(dlmsUint32.rawValue).toEqual([]);
expect(dlmsUint32.bodyBytes).toEqual([]);
expect(dlmsUint32.value).toBe(0);
});
test("should correctly initialize from a byte array", () => {
const data = [DlmsDataType.UINT32, 0x12, 0x34, 0x56, 0x78]; // UINT32 tag and value (0x12345678)
const dlmsUint32 = new DlmsUint32(data);
expect(dlmsUint32.tag).toBe(DlmsDataType.UINT32);
expect(dlmsUint32.totalBytes).toBe(5);
expect(dlmsUint32.rawValue).toEqual([DlmsDataType.UINT32, 0x12, 0x34, 0x56, 0x78]);
expect(dlmsUint32.bodyBytes).toEqual([0x12, 0x34, 0x56, 0x78]);
expect(dlmsUint32.value).toBe(0x12345678);
});
test("should throw an error if tag does not match when inserting data", () => {
const data = [0xFF, 0x12, 0x34, 0x56, 0x78]; // Invalid tag
const dlmsUint32 = new DlmsUint32();
expect(() => dlmsUint32.insertData(data)).toThrow("Tag mismatch.");
});
test("should correctly initialize from a numeric value", () => {
const dlmsUint32 = new DlmsUint32(305419896); // 0x12345678
expect(dlmsUint32.tag).toBe(DlmsDataType.UINT32);
expect(dlmsUint32.totalBytes).toBe(5);
expect(dlmsUint32.rawValue).toEqual([DlmsDataType.UINT32, 0x12, 0x34, 0x56, 0x78]);
expect(dlmsUint32.bodyBytes).toEqual([0x12, 0x34, 0x56, 0x78]);
expect(dlmsUint32.value).toBe(305419896);
});
test("should return the correct standard length", () => {
const dlmsUint32 = new DlmsUint32();
expect(dlmsUint32.getStandardLength()).toBe(5);
});
test("should return the correct value with getValue()", () => {
const data = [DlmsDataType.UINT32, 0x12, 0x34, 0x56, 0x78];
const dlmsUint32 = new DlmsUint32(data);
expect(dlmsUint32.getValue()).toBe(0x12345678);
});
test("should correctly insert data from a byte array", () => {
const dlmsUint32 = new DlmsUint32();
dlmsUint32.insertData([DlmsDataType.UINT32, 0xAB, 0xCD, 0xEF, 0x01]);
expect(dlmsUint32.tag).toBe(DlmsDataType.UINT32);
expect(dlmsUint32.totalBytes).toBe(5);
expect(dlmsUint32.rawValue).toEqual([DlmsDataType.UINT32, 0xAB, 0xCD, 0xEF, 0x01]);
expect(dlmsUint32.bodyBytes).toEqual([0xAB, 0xCD, 0xEF, 0x01]);
expect(dlmsUint32.value).toBe(0xABCDEF01);
});
test("should correctly insert data from a numeric value", () => {
const dlmsUint32 = new DlmsUint32();
dlmsUint32.insertDataFromValue(4294967295); // 0xFFFFFFFF
expect(dlmsUint32.tag).toBe(DlmsDataType.UINT32);
expect(dlmsUint32.totalBytes).toBe(5);
expect(dlmsUint32.rawValue).toEqual([DlmsDataType.UINT32, 0xFF, 0xFF, 0xFF, 0xFF]);
expect(dlmsUint32.bodyBytes).toEqual([0xFF, 0xFF, 0xFF, 0xFF]);
expect(dlmsUint32.value).toBe(4294967295);
});
test("should correctly convert bytes to Uint32", () => {
const dlmsUint32 = new DlmsUint32();
const bytes = [0x12, 0x34, 0x56, 0x78];
expect(dlmsUint32._toUint32(bytes)).toBe(0x12345678);
});
test("should correctly convert Uint32 to bytes", () => {
const dlmsUint32 = new DlmsUint32();
const value = 0x12345678;
expect(dlmsUint32._fromUint32(value)).toEqual([0x12, 0x34, 0x56, 0x78]);
});
});