aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/version_spec.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-03-16 14:50:20 +0100
committerJustin M. Keyes <justinkz@gmail.com>2023-03-20 13:36:55 +0100
commit990c481551af2b346f315d75aa0815e9b65051f3 (patch)
treec7d99c099853be3fd886b153a2ebde1e5307fe15 /test/functional/lua/version_spec.lua
parentd8f9dcd5deb72f1fbc706d7b3bb5b823c7998e38 (diff)
downloadrneovim-990c481551af2b346f315d75aa0815e9b65051f3.tar.gz
rneovim-990c481551af2b346f315d75aa0815e9b65051f3.tar.bz2
rneovim-990c481551af2b346f315d75aa0815e9b65051f3.zip
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>
Diffstat (limited to 'test/functional/lua/version_spec.lua')
-rw-r--r--test/functional/lua/version_spec.lua120
1 files changed, 109 insertions, 11 deletions
diff --git a/test/functional/lua/version_spec.lua b/test/functional/lua/version_spec.lua
index b68727ca77..2901646e66 100644
--- a/test/functional/lua/version_spec.lua
+++ b/test/functional/lua/version_spec.lua
@@ -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 = {
{
@@ -206,16 +304,6 @@ describe('version', function()
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' },
@@ -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(