Mercurial > prosody-hg
diff spec/util_human_io_spec.lua @ 14217:7eb0e47418ab 13.0
util.human.io: Add simple version comparison
This is a small helper function that compares version string (it can be passed
to table.sort() as a comparator).
Full version sorts (e.g. semver, and natural/alphanumeric sort in general) are
more complex, and I would like to eventually support those. But this is a
stable branch and we don't need it for anything just yet.
Regarding justification for the stable branch:
The function is being committed with extensive tests, and isn't currently used
anywhere. The plan is to use it in an upcoming update of how TLS profiles are
configured, but the inputs in that case will be fixed (meaning that the risk
of breakage is lower than if we were running it against arbitrary data).
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 12 Jun 2026 11:38:39 +0100 |
| parents | 82513890a1d8 |
| children |
line wrap: on
line diff
--- a/spec/util_human_io_spec.lua Sun May 31 11:51:22 2026 +0200 +++ b/spec/util_human_io_spec.lua Fri Jun 12 11:38:39 2026 +0100 @@ -100,6 +100,79 @@ return assert.is_nil(human_io.parse_duration_lax("two weeks"), "\"2 weeks\" -> nil"); end); end); + + describe("simple_version_compare", function () + local version_compare = human_io.simple_version_compare; + + local function cmp_version(a, b, exp) + assert.equal(exp, version_compare(a, b), ("%q < %q"):format(a, b)); + end + + it("orders versions with differing major numbers", function () + cmp_version("1.0", "2.0", true); + cmp_version("2.0", "1.0", false); + end); + + it("orders versions with differing minor numbers", function () + cmp_version("1.0", "1.1", true); + cmp_version("1.1", "1.0", false); + end); + + it("treats a shorter version as less than a longer one sharing the same prefix", function () + cmp_version("1.0", "1.0.1", true); + cmp_version("1.0.1", "1.0", false); + + cmp_version("1", "1.0", true); + cmp_version("1", "1.1", true); + + cmp_version("1.0.0", "1.0.0.1", true); + end); + + it("compares components left-to-right regardless of how many components there are", function () + cmp_version("1.0.1", "1.1", true); + cmp_version("1.1", "1.0.1", false); + + cmp_version("1.0.1", "1.1.0", true); + cmp_version("1.1.0", "1.0.1", false); + + cmp_version("1.0.0.1", "1.0.1", true); + end); + + it("never considers a version less than itself", function () + cmp_version("0", "0", false); + cmp_version("1.0", "1.0", false); + cmp_version("1.2.3", "1.2.3", false); + cmp_version("foo", "foo", false); + end); + + it("compares components numerically", function () + cmp_version("1.9", "1.10", true); + cmp_version("1.10", "1.9", false); + + cmp_version("1.9.9", "1.10.0", true); + + cmp_version("2.0", "10.0", true); + cmp_version("10.0", "2.0", false); + + cmp_version("1.2", "1.10", true); + + cmp_version("1.99", "1.100", true); + cmp_version("1.100", "1.99", false); + end); + + it("handles leading zeros in numeric components", function () + cmp_version("1.02", "1.10", true); + cmp_version("1.10", "1.02", false); + end); + + it("treats an empty or non-numeric string as a version with no components", function () + cmp_version("", "1.0", true); + cmp_version("1.0", "", false); + cmp_version("foo", "1.0", true); + cmp_version("1.0", "foo", false); + cmp_version("", "", false); + end); + end); end);
