From 75a2cc5f784406b6e74bb5057f8a91597b1411cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cornelius=20K=C3=B6pp?= Date: Sun, 29 Dec 2024 21:26:04 +0100 Subject: [PATCH] Refactor/Test/Fix `uctVersionToStr()` --- src/ConfigTransfer.script.js | 6 ++-- test/ConfigTransfer.test.js | 68 ++++++++++++++++++++++++++++++++++++ test/js_suffix.txt | 3 +- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/ConfigTransfer.script.js b/src/ConfigTransfer.script.js index b9cfef1..07b0958 100644 --- a/src/ConfigTransfer.script.js +++ b/src/ConfigTransfer.script.js @@ -500,11 +500,11 @@ function uctExportModuleChannelToString(device, module, channel, keyFormat, mult function uctVersionToStr(ver) { if (typeof ver == 'number' && (ver % 1) == 0) { - var minor = ver % 16; - var major = (ver - minor) / 16; + var minor = ver & 0x0f; + var major = ver >> 4; return 'v' + major + '.' + minor; } else if (typeof ver == 'string') { - return isNaN(ver) ? ('"' + ver + '"') : uctVersionToStr(parseInt(ver)); + return (isNaN(ver) || isNaN(parseInt(ver))) ? ('"' + ver + '"') : uctVersionToStr(parseInt(ver)); } else { return "" + ver; } diff --git a/test/ConfigTransfer.test.js b/test/ConfigTransfer.test.js index 82d66b5..e0f0d5c 100644 --- a/test/ConfigTransfer.test.js +++ b/test/ConfigTransfer.test.js @@ -1099,6 +1099,74 @@ describe('Helper', () => { // TODO handling of non-strings? }); + + describe("uctVersionToStr", () => { + const uctVersionToStr = cts.uctVersionToStr; + it("accepts empty string", () => { + expect(uctVersionToStr("")).toBe('""'); + }); + it("converts integer to vN.M", () => { + expect(uctVersionToStr(0)).toBe('v0.0'); + expect(uctVersionToStr(0x0)).toBe('v0.0'); + expect(uctVersionToStr(1)).toBe('v0.1'); + expect(uctVersionToStr(0x1)).toBe('v0.1'); + expect(uctVersionToStr(3)).toBe('v0.3'); + expect(uctVersionToStr(0x3)).toBe('v0.3'); + expect(uctVersionToStr(15)).toBe('v0.15'); + expect(uctVersionToStr(0x0f)).toBe('v0.15'); + expect(uctVersionToStr(16)).toBe('v1.0'); + expect(uctVersionToStr(0x10)).toBe('v1.0'); + expect(uctVersionToStr(20)).toBe('v1.4'); + expect(uctVersionToStr(0x14)).toBe('v1.4'); + expect(uctVersionToStr(64)).toBe('v4.0'); + expect(uctVersionToStr(0x40)).toBe('v4.0'); + expect(uctVersionToStr(255)).toBe('v15.15'); + expect(uctVersionToStr(0xff)).toBe('v15.15'); + expect(uctVersionToStr(0x80)).toBe('v8.0'); + }); + it("converts decimal integer string to vN.M", () => { + expect(uctVersionToStr("0")).toBe('v0.0'); + expect(uctVersionToStr("1")).toBe('v0.1'); + expect(uctVersionToStr("3")).toBe('v0.3'); + expect(uctVersionToStr("15")).toBe('v0.15'); + expect(uctVersionToStr("16")).toBe('v1.0'); + expect(uctVersionToStr("20")).toBe('v1.4'); + expect(uctVersionToStr("64")).toBe('v4.0'); + expect(uctVersionToStr("255")).toBe('v15.15'); + }); + it("converts hex integer string to vN.M", () => { + expect(uctVersionToStr("0x0")).toBe('v0.0'); + expect(uctVersionToStr("0x1")).toBe('v0.1'); + expect(uctVersionToStr("0x3")).toBe('v0.3'); + expect(uctVersionToStr("0x0F")).toBe('v0.15'); + expect(uctVersionToStr("0x10")).toBe('v1.0'); + expect(uctVersionToStr("0x14")).toBe('v1.4'); + expect(uctVersionToStr("0x40")).toBe('v4.0'); + expect(uctVersionToStr("0xFF")).toBe('v15.15'); + expect(uctVersionToStr("0x80")).toBe('v8.0'); + }); + it("converts lowercase hex integer string to vN.M", () => { + expect(uctVersionToStr("0xab")).toBe('v10.11'); + expect(uctVersionToStr("0x0f")).toBe('v0.15'); + expect(uctVersionToStr("0xff")).toBe('v15.15'); + }); + it("encloses '*' and '-'", () => { + expect(uctVersionToStr('*')).toBe('"*"'); + expect(uctVersionToStr('-')).toBe('"-"'); + }); + it("encloses strings", () => { + expect(uctVersionToStr('X')).toBe('"X"'); + expect(uctVersionToStr('other_string')).toBe('"other_string"'); + expect(uctVersionToStr('v1.0')).toBe('"v1.0"'); + }); + // TODO check fail as unexpected + it("conserves non-integer numbers", () => { + expect(uctVersionToStr(1.5)).toBe('1.5'); + expect(uctVersionToStr(0.7)).toBe('0.7'); + }); + // TODO expected for null, undefined or other? + }); + }); diff --git a/test/js_suffix.txt b/test/js_suffix.txt index c0e59c2..497731a 100644 --- a/test/js_suffix.txt +++ b/test/js_suffix.txt @@ -23,7 +23,8 @@ module.exports = { uctUnserializeParamValue, uctCreateHeader, uctHexNumberStr, - uctParseHeader, + uctParseHeader, + uctVersionToStr, uctBtnExport, uctBtnImport,