dlms-protocol
Version:
DLMS Protocol parser for JavaScript
90 lines (71 loc) • 3.52 kB
JavaScript
import DlmsUint16 from "../src/typesDLMS/DlmsUint16";
import DlmsDataType from "../src/enum/DlmsDataType";
describe("DlmsUint16 class", () => {
test("should initialize with default values when no data is provided", () => {
const dlmsUint16 = new DlmsUint16();
expect(dlmsUint16.tag).toBe(DlmsDataType.UINT16);
expect(dlmsUint16.totalBytes).toBe(0);
expect(dlmsUint16.rawValue).toEqual([]);
expect(dlmsUint16.bodyBytes).toEqual([]);
expect(dlmsUint16.value).toBe(0);
});
test("should correctly initialize from a byte array", () => {
const data = [DlmsDataType.UINT16, 0x12, 0x34]; // UINT16 tag and value (0x1234)
const dlmsUint16 = new DlmsUint16(data);
expect(dlmsUint16.tag).toBe(DlmsDataType.UINT16);
expect(dlmsUint16.totalBytes).toBe(3);
expect(dlmsUint16.rawValue).toEqual([DlmsDataType.UINT16, 0x12, 0x34]);
expect(dlmsUint16.bodyBytes).toEqual([0x12, 0x34]);
expect(dlmsUint16.value).toBe(0x1234);
});
test("should throw an error if tag does not match when inserting data", () => {
const data = [0xFF, 0x12, 0x34]; // Invalid tag
const dlmsUint16 = new DlmsUint16();
expect(() => dlmsUint16.insertData(data)).toThrow("Tag mismatch.");
});
test("should correctly initialize from a numeric value", () => {
const dlmsUint16 = new DlmsUint16(4660); // 0x1234
expect(dlmsUint16.tag).toBe(DlmsDataType.UINT16);
expect(dlmsUint16.totalBytes).toBe(3);
expect(dlmsUint16.rawValue).toEqual([DlmsDataType.UINT16, 0x12, 0x34]);
expect(dlmsUint16.bodyBytes).toEqual([0x12, 0x34]);
expect(dlmsUint16.value).toBe(4660);
});
test("should return the correct standard length", () => {
const dlmsUint16 = new DlmsUint16();
expect(dlmsUint16.getStandardLength()).toBe(3);
});
test("should return the correct value with getValue()", () => {
const data = [DlmsDataType.UINT16, 0x12, 0x34];
const dlmsUint16 = new DlmsUint16(data);
expect(dlmsUint16.getValue()).toBe(0x1234);
});
test("should correctly insert data from a byte array", () => {
const dlmsUint16 = new DlmsUint16();
dlmsUint16.insertData([DlmsDataType.UINT16, 0xAB, 0xCD]);
expect(dlmsUint16.tag).toBe(DlmsDataType.UINT16);
expect(dlmsUint16.totalBytes).toBe(3);
expect(dlmsUint16.rawValue).toEqual([DlmsDataType.UINT16, 0xAB, 0xCD]);
expect(dlmsUint16.bodyBytes).toEqual([0xAB, 0xCD]);
expect(dlmsUint16.value).toBe(0xABCD);
});
test("should correctly insert data from a numeric value", () => {
const dlmsUint16 = new DlmsUint16();
dlmsUint16.insertDataFromValue(65535); // 0xFFFF
expect(dlmsUint16.tag).toBe(DlmsDataType.UINT16);
expect(dlmsUint16.totalBytes).toBe(3);
expect(dlmsUint16.rawValue).toEqual([DlmsDataType.UINT16, 0xFF, 0xFF]);
expect(dlmsUint16.bodyBytes).toEqual([0xFF, 0xFF]);
expect(dlmsUint16.value).toBe(65535);
});
test("should correctly convert bytes to Uint16", () => {
const dlmsUint16 = new DlmsUint16();
const bytes = [0x12, 0x34];
expect(dlmsUint16._toUint16(bytes)).toBe(0x1234);
});
test("should correctly convert Uint16 to bytes", () => {
const dlmsUint16 = new DlmsUint16();
const value = 0x1234;
expect(dlmsUint16._fromUint16(value)).toEqual([0x12, 0x34]);
});
});