Skip to content

Commit

Permalink
Refactor/Test/Fix uctVersionToStr()
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelius-koepp committed Dec 29, 2024
1 parent f8d4370 commit 75a2cc5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ConfigTransfer.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
68 changes: 68 additions & 0 deletions test/ConfigTransfer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
});

});


Expand Down
3 changes: 2 additions & 1 deletion test/js_suffix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = {
uctUnserializeParamValue,
uctCreateHeader,
uctHexNumberStr,
uctParseHeader,
uctParseHeader,
uctVersionToStr,

uctBtnExport,
uctBtnImport,
Expand Down

0 comments on commit 75a2cc5

Please sign in to comment.