Mercurial > prosody-hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 14213:75d09be04f13 | 14217:7eb0e47418ab |
|---|---|
| 98 test(31536000, "365 days"); | 98 test(31536000, "365 days"); |
| 99 test(31556952, "1 year"); | 99 test(31556952, "1 year"); |
| 100 return assert.is_nil(human_io.parse_duration_lax("two weeks"), "\"2 weeks\" -> nil"); | 100 return assert.is_nil(human_io.parse_duration_lax("two weeks"), "\"2 weeks\" -> nil"); |
| 101 end); | 101 end); |
| 102 end); | 102 end); |
| 103 | |
| 104 describe("simple_version_compare", function () | |
| 105 local version_compare = human_io.simple_version_compare; | |
| 106 | |
| 107 local function cmp_version(a, b, exp) | |
| 108 assert.equal(exp, version_compare(a, b), ("%q < %q"):format(a, b)); | |
| 109 end | |
| 110 | |
| 111 it("orders versions with differing major numbers", function () | |
| 112 cmp_version("1.0", "2.0", true); | |
| 113 cmp_version("2.0", "1.0", false); | |
| 114 end); | |
| 115 | |
| 116 it("orders versions with differing minor numbers", function () | |
| 117 cmp_version("1.0", "1.1", true); | |
| 118 cmp_version("1.1", "1.0", false); | |
| 119 end); | |
| 120 | |
| 121 it("treats a shorter version as less than a longer one sharing the same prefix", function () | |
| 122 cmp_version("1.0", "1.0.1", true); | |
| 123 cmp_version("1.0.1", "1.0", false); | |
| 124 | |
| 125 cmp_version("1", "1.0", true); | |
| 126 cmp_version("1", "1.1", true); | |
| 127 | |
| 128 cmp_version("1.0.0", "1.0.0.1", true); | |
| 129 end); | |
| 130 | |
| 131 it("compares components left-to-right regardless of how many components there are", function () | |
| 132 cmp_version("1.0.1", "1.1", true); | |
| 133 cmp_version("1.1", "1.0.1", false); | |
| 134 | |
| 135 cmp_version("1.0.1", "1.1.0", true); | |
| 136 cmp_version("1.1.0", "1.0.1", false); | |
| 137 | |
| 138 cmp_version("1.0.0.1", "1.0.1", true); | |
| 139 end); | |
| 140 | |
| 141 it("never considers a version less than itself", function () | |
| 142 cmp_version("0", "0", false); | |
| 143 cmp_version("1.0", "1.0", false); | |
| 144 cmp_version("1.2.3", "1.2.3", false); | |
| 145 cmp_version("foo", "foo", false); | |
| 146 end); | |
| 147 | |
| 148 it("compares components numerically", function () | |
| 149 cmp_version("1.9", "1.10", true); | |
| 150 cmp_version("1.10", "1.9", false); | |
| 151 | |
| 152 cmp_version("1.9.9", "1.10.0", true); | |
| 153 | |
| 154 cmp_version("2.0", "10.0", true); | |
| 155 cmp_version("10.0", "2.0", false); | |
| 156 | |
| 157 cmp_version("1.2", "1.10", true); | |
| 158 | |
| 159 cmp_version("1.99", "1.100", true); | |
| 160 cmp_version("1.100", "1.99", false); | |
| 161 end); | |
| 162 | |
| 163 it("handles leading zeros in numeric components", function () | |
| 164 cmp_version("1.02", "1.10", true); | |
| 165 cmp_version("1.10", "1.02", false); | |
| 166 end); | |
| 167 | |
| 168 it("treats an empty or non-numeric string as a version with no components", function () | |
| 169 cmp_version("", "1.0", true); | |
| 170 cmp_version("1.0", "", false); | |
| 171 cmp_version("foo", "1.0", true); | |
| 172 cmp_version("1.0", "foo", false); | |
| 173 cmp_version("", "", false); | |
| 174 end); | |
| 175 end); | |
| 103 end); | 176 end); |
| 104 | 177 |
| 105 | 178 |
| 106 | 179 |
