dlms-protocol
Version:
DLMS Protocol parser for JavaScript
65 lines (52 loc) • 2.77 kB
JavaScript
import DlmsString from "../src/typesDLMS/DlmsString";
import DlmsDataType from "../src/enum/DlmsDataType";
describe("DlmsString class", () => {
test("should initialize with default values when no data is provided", () => {
const dlmsString = new DlmsString();
expect(dlmsString.tag).toBe(DlmsDataType.STRING);
expect(dlmsString.totalBytes).toBe(0);
expect(dlmsString.rawValue).toEqual([]);
expect(dlmsString.bodyBytes).toEqual([]);
expect(dlmsString.value).toBe('');
expect(dlmsString.size).toBe(0);
});
test("should correctly initialize from a byte array", () => {
const data = [DlmsDataType.STRING, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in bytes
const dlmsString = new DlmsString(data);
expect(dlmsString.tag).toBe(DlmsDataType.STRING);
expect(dlmsString.size).toBe(5); // Length of the string
expect(dlmsString.bodyBytes).toEqual([0x48, 0x65, 0x6C, 0x6C, 0x6F]);
expect(dlmsString.value).toBe("Hello");
expect(dlmsString.rawValue).toEqual(data);
expect(dlmsString.totalBytes).toBe(7); // Tag (1) + Size (1) + Body (5)
});
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 dlmsString = new DlmsString();
expect(() => dlmsString.insertData(data)).toThrow("Tag mismatch.");
});
test("should correctly handle an empty string", () => {
const data = [DlmsDataType.STRING, 0x00]; // Empty string
const dlmsString = new DlmsString(data);
expect(dlmsString.tag).toBe(DlmsDataType.STRING);
expect(dlmsString.size).toBe(0);
expect(dlmsString.bodyBytes).toEqual([]);
expect(dlmsString.value).toBe('');
expect(dlmsString.rawValue).toEqual([DlmsDataType.STRING, 0x00]);
expect(dlmsString.totalBytes).toBe(2); // Tag (1) + Size (1)
});
test("should correctly convert bytes to string using _bytesToString", () => {
const dlmsString = new DlmsString();
const bytes = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in bytes
expect(dlmsString._bytesToString(bytes)).toBe("Hello");
});
test("should throw an error if getStandardLength() is called", () => {
const dlmsString = new DlmsString();
expect(() => dlmsString.getStandardLength()).toThrow("Method not implemented.");
});
test("should return the correct value with getValue()", () => {
const data = [DlmsDataType.STRING, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in bytes
const dlmsString = new DlmsString(data);
expect(dlmsString.getValue()).toBe("Hello");
});
});