UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

82 lines (65 loc) 3.62 kB
import DlmsOctetString from "../src/typesDLMS/DlmsOctetString"; import DlmsDataType from "../src/enum/DlmsDataType"; describe("DlmsOctetString class", () => { test("should initialize with default values when no data is provided", () => { const dlmsOctetString = new DlmsOctetString(); expect(dlmsOctetString.tag).toBe(DlmsDataType.OCTET_STRING); expect(dlmsOctetString.totalBytes).toBe(0); expect(dlmsOctetString.rawValue).toEqual([]); expect(dlmsOctetString.bodyBytes).toEqual([]); expect(dlmsOctetString.value).toBe(''); expect(dlmsOctetString.len).toBe(0); }); test("should correctly initialize from a string", () => { const data = "48656C6C6F"; // "Hello" in hex const dlmsOctetString = new DlmsOctetString(data); expect(dlmsOctetString.tag).toBe(DlmsDataType.OCTET_STRING); expect(dlmsOctetString.totalBytes).toBe(7); // Tag (1) + Len (1) + Body (5) expect(dlmsOctetString.rawValue).toEqual([DlmsDataType.OCTET_STRING, 5, 0x48, 0x65, 0x6C, 0x6C, 0x6F]); expect(dlmsOctetString.bodyBytes).toEqual([0x48, 0x65, 0x6C, 0x6C, 0x6F]); expect(dlmsOctetString.value).toBe("48656C6C6F"); expect(dlmsOctetString.len).toBe(5); }); test("should correctly initialize from a byte array without insertion", () => { const data = [DlmsDataType.OCTET_STRING, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; const dlmsOctetString = new DlmsOctetString(data); expect(dlmsOctetString.len).toBe(5); expect(dlmsOctetString.bodyBytes).toEqual([0x48, 0x65, 0x6C, 0x6C, 0x6F]); expect(dlmsOctetString.value).toBe("Hello"); expect(dlmsOctetString.rawValue).toEqual(data); expect(dlmsOctetString.totalBytes).toBe(7); }); test("should throw an error if tag does not match when inserting data", () => { const data = [0xFF, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; // Invalid tag const dlmsOctetString = new DlmsOctetString(); expect(() => dlmsOctetString.insertData(data)).toThrow("Tag mismatch."); }); test("should correctly initialize from a byte array with insertion", () => { const data = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in ASCII bytes const dlmsOctetString = new DlmsOctetString(data, true); expect(dlmsOctetString.len).toBe(5); expect(dlmsOctetString.bodyBytes).toEqual(data); expect(dlmsOctetString.value).toBe("Hello"); expect(dlmsOctetString.rawValue).toEqual([DlmsDataType.OCTET_STRING, 5, ...data]); expect(dlmsOctetString.totalBytes).toBe(7); }); test("should return the correct standard length", () => { const dlmsOctetString = new DlmsOctetString(); expect(dlmsOctetString.getStandardLength()).toBe(2); }); test("should return the correct value with getValue()", () => { const data = [DlmsDataType.OCTET_STRING, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; const dlmsOctetString = new DlmsOctetString(data); expect(dlmsOctetString.getValue()).toBe("Hello"); }); test("should correctly convert bytes to a string", () => { const dlmsOctetString = new DlmsOctetString(); const bytes = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; expect(dlmsOctetString._bytesToString(bytes)).toBe("Hello"); }); test("should correctly convert bytes to a hexadecimal string", () => { const dlmsOctetString = new DlmsOctetString(); const bytes = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; expect(dlmsOctetString._bytesToHex(bytes)).toBe("48656c6c6f"); }); });