Files
neovim/test/functional/lua/version_spec.lua
Justin M. Keyes e31e49a8e3 refactor(vim.version): cleanup
- version.cmp(): assert valid version
- add test for loading vim.version (the other tests use shared.lua in
  the test runner)
- reduce test scopes, reword test descriptions
2023-03-06 14:51:56 +01:00

572 lines
14 KiB
Lua

local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local eq = helpers.eq
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local pcall_err = helpers.pcall_err
local version = require('vim.version')
local function quote_empty(s)
return tostring(s) == '' and '""' or tostring(s)
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('cmp()', function()
local testcases = {
{
desc = '(v1 < v2)',
v1 = 'v0.0.0',
v2 = 'v9.0.0',
want = -1,
},
{
desc = '(v1 < v2)',
v1 = 'v0.0.0',
v2 = 'v0.9.0',
want = -1,
},
{
desc = '(v1 < v2)',
v1 = 'v0.0.0',
v2 = 'v0.0.9',
want = -1,
},
{
desc = '(v1 == v2)',
v1 = 'v0.0.0',
v2 = 'v0.0.0',
want = 0,
},
{
desc = '(v1 > v2)',
v1 = 'v9.0.0',
v2 = 'v0.0.0',
want = 1,
},
{
desc = '(v1 > v2)',
v1 = 'v0.9.0',
v2 = 'v0.0.0',
want = 1,
},
{
desc = '(v1 > v2)',
v1 = 'v0.0.9',
v2 = 'v0.0.0',
want = 1,
},
{
desc = '(v1 < v2) when v1 has prerelease',
v1 = 'v1.0.0-alpha',
v2 = 'v1.0.0',
want = -1,
},
{
desc = '(v1 > v2) when v2 has prerelease',
v1 = '1.0.0',
v2 = '1.0.0-alpha',
want = 1,
},
{
desc = '(v1 > v2) when v1 has a higher number identifier',
v1 = '1.0.0-2',
v2 = '1.0.0-1',
want = 1,
},
{
desc = '(v1 < v2) when v2 has a higher number identifier',
v1 = '1.0.0-2',
v2 = '1.0.0-9',
want = -1,
},
{
desc = '(v1 < v2) when v2 has more identifiers',
v1 = '1.0.0-2',
v2 = '1.0.0-2.0',
want = -1,
},
{
desc = '(v1 > v2) when v1 has more identifiers',
v1 = '1.0.0-2.0',
v2 = '1.0.0-2',
want = 1,
},
{
desc = '(v1 == v2) when v2 has same numeric identifiers',
v1 = '1.0.0-2.0',
v2 = '1.0.0-2.0',
want = 0,
},
{
desc = '(v1 == v2) when v2 has same alphabet identifiers',
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha',
want = 0,
},
{
desc = '(v1 < v2) when v2 has an alphabet identifier with higher ASCII sort order',
v1 = '1.0.0-alpha',
v2 = '1.0.0-beta',
want = -1,
},
{
desc = '(v1 > v2) when v1 has an alphabet identifier with higher ASCII sort order',
v1 = '1.0.0-beta',
v2 = '1.0.0-alpha',
want = 1,
},
{
desc = '(v1 < v2) when v2 has prerelease and number identifer',
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha.1',
want = -1,
},
{
desc = '(v1 > v2) when v1 has prerelease and number identifer',
v1 = '1.0.0-alpha.1',
v2 = '1.0.0-alpha',
want = 1,
},
{
desc = '(v1 > v2) when v1 has an additional alphabet identifier',
v1 = '1.0.0-alpha.beta',
v2 = '1.0.0-alpha',
want = 1,
},
{
desc = '(v1 < v2) when v2 has an additional alphabet identifier',
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha.beta',
want = -1,
},
{
desc = '(v1 < v2) when v2 has an a first alphabet identifier with higher precedence',
v1 = '1.0.0-alpha.beta',
v2 = '1.0.0-beta',
want = -1,
},
{
desc = '(v1 > v2) when v1 has an a first alphabet identifier with higher precedence',
v1 = '1.0.0-beta',
v2 = '1.0.0-alpha.beta',
want = 1,
},
{
desc = '(v1 < v2) when v2 has an additional number identifer',
v1 = '1.0.0-beta',
v2 = '1.0.0-beta.2',
want = -1,
},
{
desc = '(v1 < v2) when v2 has same first alphabet identifier but has a higher number identifer',
v1 = '1.0.0-beta.2',
v2 = '1.0.0-beta.11',
want = -1,
},
{
desc = '(v1 < v2) when v2 has higher alphabet precedence',
v1 = '1.0.0-beta.11',
v2 = '1.0.0-rc.1',
want = -1,
},
}
for _, tc in ipairs(testcases) do
it(
string.format('%d %s (v1 = %s, v2 = %s)', tc.want, tc.desc, tc.v1, tc.v2),
function()
eq(tc.want, version.cmp(tc.v1, tc.v2, { strict = true }))
end
)
end
end)
describe('parse()', function()
describe('strict=true', function()
local testcases = {
{
desc = 'version without leading "v"',
version = '10.20.123',
want = {
major = 10,
minor = 20,
patch = 123,
prerelease = nil,
build = nil,
},
},
{
desc = 'valid version with leading "v"',
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',
want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha' },
},
{
desc = 'version with prerelease with additional identifiers',
version = '1.2.3-alpha.1',
want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha.1' },
},
{
desc = 'version with build',
version = '1.2.3+build.15',
want = { major = 1, minor = 2, patch = 3, build = 'build.15' },
},
{
desc = 'version with prerelease and build',
version = '1.2.3-rc1+build.15',
want = {
major = 1,
minor = 2,
patch = 3,
prerelease = 'rc1',
build = 'build.15',
},
},
}
for _, tc in ipairs(testcases) do
it(
string.format('for %q: version = %q', tc.desc, tc.version),
function()
eq(tc.want, version.parse(tc.version, { strict = true }))
end
)
end
end)
describe('strict=false', function()
local testcases = {
{
desc = 'version missing patch version',
version = '1.2',
want = { major = 1, minor = 2, patch = 0 },
},
{
desc = 'version missing minor and patch version',
version = '1',
want = { major = 1, minor = 0, patch = 0 },
},
{
desc = 'version missing patch version with prerelease',
version = '1.1-0',
want = { major = 1, minor = 1, patch = 0, prerelease = '0' },
},
{
desc = 'version missing minor and patch version with prerelease',
version = '1-1.0',
want = { major = 1, minor = 0, patch = 0, prerelease = '1.0' },
},
}
for _, tc in ipairs(testcases) do
it(
string.format('for %q: version = %q', tc.desc, tc.version),
function()
eq(tc.want, version.parse(tc.version, { strict = false }))
end
)
end
end)
describe('invalid semver', function()
local testcases = {
{ desc = 'a word', version = 'foo' },
{ desc = 'empty string', version = '' },
{ desc = 'trailing period character', version = '0.0.0.' },
{ desc = 'leading period character', version = '.0.0.0' },
{ desc = 'negative major version', version = '-1.0.0' },
{ desc = 'negative minor version', version = '0.-1.0' },
{ desc = 'negative patch version', version = '0.0.-1' },
{ desc = 'leading invalid string', version = 'foobar1.2.3' },
{ desc = 'trailing invalid string', version = '1.2.3foobar' },
{ desc = 'an invalid prerelease', version = '1.2.3-%?' },
{ desc = 'an invalid build', version = '1.2.3+%?' },
{ desc = 'build metadata before prerelease', version = '1.2.3+build.0-rc1' },
}
for _, tc in ipairs(testcases) do
it(string.format('(%s): %s', tc.desc, quote_empty(tc.version)), function()
eq(nil, version.parse(tc.version, { strict = true }))
end)
end
end)
describe('invalid shape', function()
local testcases = {
{ desc = 'no parameters' },
{ desc = 'nil', version = nil },
{ desc = 'number', version = 0 },
{ desc = 'float', version = 0.01 },
{ desc = 'table', version = {} },
}
for _, tc in ipairs(testcases) do
it(string.format('(%s): %s', tc.desc, tostring(tc.version)), function()
matches(string.format('invalid version: "%s"', tostring(tc.version)),
pcall_err(version.parse, tc.version, { strict = true }))
end)
end
end)
end)
describe('eq()', function()
local testcases = {
{
v1 = '1.0.0',
v2 = '1.0.0',
want = true,
},
{
v1 = '1.0.0',
v2 = 'v1.0.0',
want = true,
},
{
v1 = '1.0.0',
v2 = '1.0',
want = true,
},
{
v1 = '1.0.0',
v2 = '1',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = 'v1.0.0-alpha',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha+build.5',
want = true,
},
{
v1 = '1.0.0-alpha.1',
v2 = '1.0.0-alpha.1+build.5',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha.1',
want = false,
},
{
v1 = '1.0.0',
v2 = '2.0.0',
want = false,
},
}
for _, tc in ipairs(testcases) do
it(string.format('returns %s for %s = %s', tostring(tc.want), tc.v1, tc.v2), function()
eq(tc.want, version.eq(tc.v1, tc.v2))
end)
end
describe('fails', function()
local failtests = {
{
v1 = '',
v2 = '1.0.0',
err_version = '',
},
{
v1 = '1.0.0',
v2 = '',
err_version = '',
},
{
v1 = '',
v2 = '',
err_version = '',
},
{
v1 = '1.0.0',
v2 = 'foo',
err_version = 'foo',
},
}
for _, tc in ipairs(failtests) do
it(string.format('for %s = %s', quote_empty(tc.v1), quote_empty(tc.v2)), function()
matches(string.format('invalid version: "%s"', tc.err_version),
pcall_err(version.eq, tc.v1, tc.v2))
end)
end
end)
end)
describe('lt()', function()
local testcases = {
{
v1 = '1.0.0',
v2 = '1.0.1',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.1',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.1-beta',
want = true,
},
{
v1 = '1.0.1',
v2 = '1.0.0',
want = false,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha',
want = false,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha+build.5',
want = false,
},
{
v1 = '1.0.0-alpha+build.4',
v2 = '1.0.0-alpha+build.5',
want = false,
},
}
for _, tc in ipairs(testcases) do
it(string.format('returns %s for %s < %s', tostring(tc.want), tc.v1, tc.v2), function()
eq(tc.want, version.lt(tc.v1, tc.v2))
end)
end
describe('fails', function()
local failtests = {
{
v1 = '',
v2 = '1.0.0',
err_version = '',
},
{
v1 = '1.0.0',
v2 = '',
err_version = '',
},
{
v1 = '',
v2 = '',
err_version = '',
},
}
for _, tc in ipairs(failtests) do
it(string.format('for %s < %s', quote_empty(tc.v1), quote_empty(tc.v2)), function()
matches(string.format('invalid version: "%s"', tc.err_version),
pcall_err(version.lt, tc.v1, tc.v2))
end)
end
end)
end)
describe('gt()', function()
local testcases = {
{
v1 = '1.0.1',
v2 = '1.0.0',
want = true,
},
{
v1 = '1.0.1',
v2 = '1.0.1-alpha',
want = true,
},
{
v1 = '1.0.0',
v2 = '1.0.1',
want = false,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.1',
want = false,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.1-beta',
want = false,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha',
want = false,
},
{
v1 = '1.0.0-beta',
v2 = '1.0.0-alpha',
want = true,
},
{
v1 = '1.0.0-alpha',
v2 = '1.0.0-alpha+build.5',
want = false,
},
{
v1 = '1.0.0-alpha+build.4',
v2 = '1.0.0-alpha+build.5',
want = false,
},
}
for _, tc in ipairs(testcases) do
it(string.format('returns %s for %s > %s', tostring(tc.want), tc.v1, tc.v2), function()
eq(tc.want, version.gt(tc.v1, tc.v2))
end)
end
describe('fails', function()
local failtests = {
{
v1 = '',
v2 = '1.0.0',
err_version = '',
},
{
v1 = '1.0.0',
v2 = '',
err_version = '',
},
{
v1 = '',
v2 = '',
err_version = '',
},
}
for _, tc in ipairs(failtests) do
it(string.format('for %s < %s', quote_empty(tc.v1), quote_empty(tc.v2)), function()
matches(string.format('invalid version: "%s"', tc.err_version),
pcall_err(version.gt, tc.v1, tc.v2))
end)
end
end)
end)
end)