local pkg = require("assay.pkg")
local function check(cond, msg)
if not cond then error(msg, 2) end
end
local function check_parse(input, expected)
local got = pkg.version.parse(input)
check(#got == #expected,
("parse(%q): #got=%d #expected=%d"):format(input, #got, #expected))
for i, n in ipairs(expected) do
check(got[i] == n,
("parse(%q)[%d]: got=%s want=%s"):format(input, i, tostring(got[i]), tostring(n)))
end
end
check_parse("1.2.3", {1,2,3})
check_parse("v1.2.3", {1,2,3})
check_parse("0.10.0", {0,10,0})
check_parse("2024.10.1", {2024,10,1})
check_parse("2024.9.0", {2024,9,0})
check(pkg.version.cmp("1.2.3", "1.2.3") == 0, "equal")
check(pkg.version.cmp("1.2.3", "1.2.4") == -1, "patch <")
check(pkg.version.cmp("1.2.4", "1.2.3") == 1, "patch >")
check(pkg.version.cmp("0.9.9", "0.10.0") == -1, "two-digit minor not lex")
check(pkg.version.cmp("v1.2.3", "1.2.3") == 0, "v-prefix equal")
check(pkg.version.cmp("2024.9.0", "2024.10.1") == -1, "calver same year")
check(pkg.version.cmp("2023.12.31", "2024.1.1") == -1, "calver year boundary")
check(pkg.version.cmp("1.2", "1.2.0") == 0, "1.2 == 1.2.0")
check(pkg.version.cmp("1.2", "1.2.1") == -1, "1.2 < 1.2.1")
print("version_compare.lua OK")