refactor(vim.version): use lazy.nvim semver module

Use semver code from https://github.com/folke/lazy.nvim
License: Apache License 2.0

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Justin M. Keyes
2023-03-16 14:50:20 +01:00
parent d8f9dcd5de
commit 990c481551
2 changed files with 300 additions and 11 deletions

View File

@@ -6,17 +6,115 @@ local matches = helpers.matches
local pcall_err = helpers.pcall_err
local version = require('vim.version')
local Semver = version.LazyM
local function quote_empty(s)
return tostring(s) == '' and '""' or tostring(s)
end
local function v(ver)
return Semver.parse(ver)
end
describe('version', function()
it('package', function()
clear()
eq({ major = 42, minor = 3, patch = 99 }, exec_lua("return vim.version.parse('v42.3.99')"))
end)
describe('semver version', function()
local tests = {
['v1.2.3'] = { major = 1, minor = 2, patch = 3 },
['v1.2'] = { major = 1, minor = 2, patch = 0 },
['v1.2.3-prerelease'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease' },
['v1.2-prerelease'] = { major = 1, minor = 2, patch = 0, prerelease = 'prerelease' },
['v1.2.3-prerelease+build'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease', build = "build" },
['1.2.3+build'] = { major = 1, minor = 2, patch = 3, build = 'build' },
}
for input, output in pairs(tests) do
it('parses ' .. input, function()
assert.same(output, v(input))
end)
end
end)
describe('semver range', function()
local tests = {
['1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 4 } },
['1.2'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
['=1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 4 } },
['>1.2.3'] = { from = { 1, 2, 4 } },
['>=1.2.3'] = { from = { 1, 2, 3 } },
['~1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 3, 0 } },
['^1.2.3'] = { from = { 1, 2, 3 }, to = { 2, 0, 0 } },
['^0.2.3'] = { from = { 0, 2, 3 }, to = { 0, 3, 0 } },
['^0.0.1'] = { from = { 0, 0, 1 }, to = { 0, 0, 2 } },
['^1.2'] = { from = { 1, 2, 0 }, to = { 2, 0, 0 } },
['~1.2'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
['~1'] = { from = { 1, 0, 0 }, to = { 2, 0, 0 } },
['^1'] = { from = { 1, 0, 0 }, to = { 2, 0, 0 } },
['1.*'] = { from = { 1, 0, 0 }, to = { 2, 0, 0 } },
['1'] = { from = { 1, 0, 0 }, to = { 2, 0, 0 } },
['1.x'] = { from = { 1, 0, 0 }, to = { 2, 0, 0 } },
['1.2.x'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
['1.2.*'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
['*'] = { from = { 0, 0, 0 } },
['1.2 - 2.3.0'] = { from = { 1, 2, 0 }, to = { 2, 3, 0 } },
['1.2.3 - 2.3.4'] = { from = { 1, 2, 3 }, to = { 2, 3, 4 } },
['1.2.3 - 2'] = { from = { 1, 2, 3 }, to = { 3, 0, 0 } },
}
for input, output in pairs(tests) do
output.from = v(output.from)
output.to = output.to and v(output.to)
local range = Semver.range(input)
it('parses ' .. input, function()
assert.same(output, range)
end)
it('[from] in range ' .. input, function()
assert(range:matches(output.from))
end)
it('[from-1] not in range ' .. input, function()
local lower = vim.deepcopy(range.from)
lower.major = lower.major - 1
assert(not range:matches(lower))
end)
it('[to] not in range ' .. input .. ' to:' .. tostring(range.to), function()
if range.to then
assert(not (range.to < range.to))
assert(not range:matches(range.to))
end
end)
end
it("handles prerelease", function()
assert(not Semver.range('1.2.3'):matches('1.2.3-alpha'))
assert(Semver.range('1.2.3-alpha'):matches('1.2.3-alpha'))
assert(not Semver.range('1.2.3-alpha'):matches('1.2.3-beta'))
end)
end)
describe('semver order', function()
it('is correct', function()
assert(v('v1.2.3') == v('1.2.3'))
assert(not (v('v1.2.3') < v('1.2.3')))
assert(v('v1.2.3') > v('1.2.3-prerelease'))
assert(v('v1.2.3-alpha') < v('1.2.3-beta'))
assert(v('v1.2.3-prerelease') < v('1.2.3'))
assert(v('v1.2.3') >= v('1.2.3'))
assert(v('v1.2.3') >= v('1.0.3'))
assert(v('v1.2.3') >= v('1.2.2'))
assert(v('v1.2.3') > v('1.2.2'))
assert(v('v1.2.3') > v('1.0.3'))
assert.same(Semver.last({ v('1.2.3'), v('2.0.0') }), v('2.0.0'))
assert.same(Semver.last({ v('2.0.0'), v('1.2.3') }), v('2.0.0'))
end)
end)
describe('cmp()', function()
local testcases = {
{
@@ -205,16 +303,6 @@ describe('version', function()
version = 'v1.2.3',
want = { major = 1, minor = 2, patch = 3 },
},
{
desc = 'valid version with leading "v" and whitespace',
version = ' v1.2.3',
want = { major = 1, minor = 2, patch = 3 },
},
{
desc = 'valid version with leading "v" and trailing whitespace',
version = 'v1.2.3 ',
want = { major = 1, minor = 2, patch = 3 },
},
{
desc = 'version with prerelease',
version = '1.2.3-alpha',
@@ -246,7 +334,7 @@ describe('version', function()
it(
string.format('for %q: version = %q', tc.desc, tc.version),
function()
eq(tc.want, version.parse(tc.version, { strict = true }))
eq(tc.want, Semver.parse(tc.version))
end
)
end
@@ -274,6 +362,16 @@ describe('version', function()
version = '1-1.0',
want = { major = 1, minor = 0, patch = 0, prerelease = '1.0' },
},
{
desc = 'valid version with leading "v" and trailing whitespace',
version = 'v1.2.3 ',
want = { major = 1, minor = 2, patch = 3 },
},
{
desc = 'valid version with leading "v" and whitespace',
version = ' v1.2.3',
want = { major = 1, minor = 2, patch = 3 },
},
}
for _, tc in ipairs(testcases) do
it(