From 9533dce257e7a79cc35cc239b2fc0f94d833847b Mon Sep 17 00:00:00 2001 From: Tyler Miller Date: Sun, 22 Sep 2024 00:24:53 -0700 Subject: [PATCH] test: add tests --- perf/fs_stat.lua | 22 +-- tests/min_init.lua | 64 +++---- tests/sos/command/resolve_bufspec_spec.lua | 206 ++++++++++++++++++++- tests/suspend_spec.lua | 6 +- 4 files changed, 241 insertions(+), 57 deletions(-) diff --git a/perf/fs_stat.lua b/perf/fs_stat.lua index 67880aa..a1cb8fe 100644 --- a/perf/fs_stat.lua +++ b/perf/fs_stat.lua @@ -1,29 +1,29 @@ -local loop = vim.loop -local tmpdir = assert(loop.fs_mkdtemp(loop.os_tmpdir() .. '/XXXXXX')) +local uv = vim.uv or vim.loop +local tmpdir = assert(uv.fs_mkdtemp(uv.os_tmpdir() .. '/XXXXXX')) local nfiles = 1e3 -- Create files for i = 1, nfiles, 1 do local fd = assert( - loop.fs_open( + uv.fs_open( tmpdir .. '/' .. tostring(i), - loop.constants.O_CREAT + loop.constants.O_EXCL, + uv.constants.O_CREAT + uv.constants.O_EXCL, 511 ) ) - assert(loop.fs_close(fd)) + assert(uv.fs_close(fd)) end -local t = loop.hrtime() +local t = uv.hrtime() for i = 1, nfiles, 1 do - local _stat = assert(loop.fs_stat(tmpdir .. '/' .. tostring(i))) + local _stat = assert(uv.fs_stat(tmpdir .. '/' .. tostring(i))) end print( 'time to stat ' .. tostring(nfiles) .. ' files (sync):', - (loop.hrtime() - t) / 1e6, + (uv.hrtime() - t) / 1e6, 'ms' ) @@ -34,7 +34,7 @@ local function proc_stat(err, stat) if cnt == nfiles then print( 'time to stat ' .. tostring(nfiles) .. ' files (async):', - (loop.hrtime() - t) / 1e6, + (uv.hrtime() - t) / 1e6, 'ms' ) vim.schedule(function() vim.fn.delete(tmpdir, 'rf') end) @@ -42,8 +42,8 @@ local function proc_stat(err, stat) assert(stat, err) end -t = loop.hrtime() +t = uv.hrtime() for i = 1, nfiles, 1 do - loop.fs_stat(tmpdir .. '/' .. tostring(i), proc_stat) + uv.fs_stat(tmpdir .. '/' .. tostring(i), proc_stat) end diff --git a/tests/min_init.lua b/tests/min_init.lua index c31ab17..be0f83e 100644 --- a/tests/min_init.lua +++ b/tests/min_init.lua @@ -10,49 +10,42 @@ xpcall(function() vim.o.shortmess = 'FWI' vim.o.showmode = false vim.o.showcmd = false - vim.opt.rtp:append(vim.fn.expand ':h:h') - -- vim.cmd "runtime! plugin/**/*.vim" - -- vim.cmd "runtime! plugin/**/*.lua" local plenary = vim.env.PLENARY + or (function() + for _, val in ipairs { 'PATH', 'DIR' } do + local v = vim.env['PLENARY' .. val] or vim.env['PLENARY_' .. val] + if v then return v end + end - if not plenary then - for _, val in ipairs { 'PATH', 'DIR' } do - plenary = vim.env['PLENARY' .. val] or vim.env['PLENARY_' .. val] - if plenary then break end - end - end + local searchpath = { + vim.fn.stdpath 'data', + vim.fn.stdpath 'config', + vim.fn.expand '~' .. '/.luarocks', + vim.fn.stdpath 'data_dirs', + } - if not plenary then - local searchpath = {} - table.insert(searchpath, vim.fn.stdpath 'data') - table.insert(searchpath, vim.fn.stdpath 'config') - table.insert(searchpath, vim.fn.expand '~' .. '/.luarocks') - table.insert(searchpath, vim.fn.stdpath 'data_dirs') - searchpath = vim.tbl_filter( - function(v) return vim.fn.getftype(v) == 'dir' end, - vim.tbl_flatten(searchpath) - ) + searchpath = vim.tbl_filter( + function(v) return vim.fn.getftype(v) == 'dir' end, + vim.tbl_flatten(searchpath) + ) - for _, dir in ipairs(searchpath) do - local paths = vim.fs.find( - 'plenary.nvim', - { path = dir, limit = math.huge, type = 'directory' } - ) or {} + for _, dir in ipairs(searchpath) do + local paths = vim.fs.find( + 'plenary.nvim', + { path = dir, limit = math.huge, type = 'directory' } + ) or {} - for _, path in ipairs(paths) do - if - vim.fn.filereadable(path .. '/plugin/plenary.vim') == 1 - or vim.fn.filereadable(path .. '/plugin/plenary.lua') == 1 - then - plenary = path - break + for _, path in ipairs(paths) do + if + vim.fn.filereadable(path .. '/plugin/plenary.vim') == 1 + or vim.fn.filereadable(path .. '/plugin/plenary.lua') == 1 + then + return path + end end end - - if plenary then break end - end - end + end)() assert( plenary, @@ -60,6 +53,7 @@ xpcall(function() ) vim.opt.rtp:append(plenary) + vim.opt.rtp:prepend '.' for _, ext in ipairs { 'vim', 'lua' } do vim.cmd('runtime! plugin/plenary.' .. ext) diff --git a/tests/sos/command/resolve_bufspec_spec.lua b/tests/sos/command/resolve_bufspec_spec.lua index 5fc8f00..a7c184a 100644 --- a/tests/sos/command/resolve_bufspec_spec.lua +++ b/tests/sos/command/resolve_bufspec_spec.lua @@ -1,4 +1,6 @@ +local action = require 'sos._test.action' local util = require 'sos._test.util' +local api = vim.api describe('command', function() -- (setup or before)(function() util.await_vim_enter() end) @@ -19,18 +21,206 @@ describe('command', function() 'accepts $ as bufname literally (or pattern if no such buffer)', function() end ) +describe('resolve_bufspec()', function() + local ret, emsg + api.nvim_create_user_command('SomeCmd', function(info) + ret = require('sos.commands').resolve_bufspec(info) + emsg = vim.v.errmsg + vim.v.errmsg = '' + end, { + nargs = '?', + count = -1, + addr = 'buffers', + complete = 'buffer', + force = true, + }) + + before_each(function() + ret, emsg, vim.o.write = nil, nil, false + vim.cmd 'silent! %bw!' + vim.v.errmsg = '' + end) + + local function assert_success(buf) + assert.equals('', emsg) + assert.equals(buf, ret) + end + + local function assert_error() + assert.is_string(emsg) + assert.does_not_equal('', emsg) + assert.is_nil(ret) + end + + local function clear_results() + ret, emsg, vim.v.errmsg = nil, nil, '' + end + + describe('command argument', function() + it('accepts relative bufame', function() + local path = 'dir/somefile' + local buf = util.silent_edit(path) + util.silent_edit(path .. 'a') + util.silent_edit(path .. '/a') + + action.cmd('SomeCmd ' .. path) + assert_success(buf) end) - describe('range', function() - pending('is always rejected if it contains 2 parts', function() end) - pending('accepts bufnr', function() end) - pending('rejects 0 bufnr', function() end) - pending('rejects negative bufnr', function() end) - pending('rejects non-integer', function() end) + it('accepts relative bufame', function() + local path = 'dir/somefile' + local buf = util.silent_edit(path) + util.silent_edit(path .. '/file') + util.silent_edit(path .. 'abc') + + action.cmd('SomeCmd ' .. path) + assert_success(buf) + end) + + it('accepts absolute bufame', function() + local path = util.tmpfile() + local buf = util.silent_edit(path) + util.silent_edit(path .. '/file') + util.silent_edit(path .. 'abc') + + action.cmd('SomeCmd ' .. path) + assert_success(buf) + end) + + pending('accepts bufame pattern', function() end) + pending('accepts bufnr', function() end) + pending('accepts bufnr via arg', function() end) + + it('rejects 0 bufnr and prints error message', function() + util.silent_edit '0' + action.cmd 'SomeCmd 0' + assert_error() + end) + + it('accepts negative integer as bufname', function() + -- no match + action.cmd 'SomeCmd -1' + assert_error() + + clear_results() + local buf = util.silent_edit '-1' + util.silent_edit '-1abc' + util.silent_edit '-1/file' + action.cmd 'SomeCmd -1' + assert_success(buf) end) - describe('argument+range', function() - pending('is accepted, but only argument is used', function() end) + it('accepts % as current buffer', function() + assert.equals('%', vim.fn.bufname((util.silent_edit '%'))) + local buf = util.silent_edit 'current_buffer' + action.cmd 'SomeCmd %' + assert_success(buf) + end) + + it('accepts # as alternate buffer', function() + local buf = util.silent_edit 'alt_buffer' + assert.equals('#', vim.fn.bufname((util.silent_edit '#'))) + action.cmd 'SomeCmd #' + assert_success(buf) + end) + + it( + 'accepts $ ($ by itself) literally (non-pattern/non-special char) and as bufname', + function() + -- exact match + local buf = util.silent_edit '$' + assert.equals('$', vim.fn.bufname(buf)) + util.silent_edit 'ab$' + util.silent_edit '$ab' + util.silent_edit 'ab$ab' + action.cmd 'SomeCmd $' + assert_success(buf) + + api.nvim_buf_delete(buf, { force = true }) + clear_results() + action.cmd 'SomeCmd $' + -- error (too many matches) + assert_error() + + vim.cmd 'silent! %bw!' + clear_results() + util.silent_edit 'file' + action.cmd 'SomeCmd $' + -- error (no match) + assert_error() + + vim.cmd 'silent! %bw!' + clear_results() + buf = util.silent_edit 'f$ile' + util.silent_edit 'file1' + util.silent_edit 'file2' + action.cmd 'silent enew' + action.cmd 'SomeCmd $' + assert_success(buf) + end + ) + end) + + describe('command range', function() + pending('is rejected if it contains 2 parts', function() end) + pending('accepts bufnr', function() end) + pending('rejects 0 bufnr', function() end) + pending('rejects negative bufnr', function() end) + pending('rejects non-integer', function() end) + end) + + describe('command argument with', function() + describe('1-part range', function() + it('is accepted, but only argument is used and resolved', function() + local buf1 = util.silent_edit 'f1' + local buf2 = util.silent_edit 'f2' + util.silent_edit 'f3' + util.silent_edit 'f4' + + action.cmd(('%dSomeCmd %d'):format(buf1, buf2)) + assert_success(buf2) + end) + + it("is rejected if arg isn't a positive integer", function() + local buf1 = util.silent_edit 'f1' + util.silent_edit 'f2' + util.silent_edit 'f3' + util.silent_edit 'f4' + + action.cmd(('%dSomeCmd f2'):format(buf1)) + assert_error() + clear_results() + action.cmd(('%dSomeCmd 0'):format(buf1)) + assert_error() + clear_results() + action.cmd(('%dSomeCmd -'):format(buf1)) + end) + end) + + describe('2-part range', function() + it('is rejected and prints error message', function() + action.cmd( + ('%d,%dSomeCmd %d'):format( + util.silent_edit 'f1', + util.silent_edit 'f2', + util.silent_edit 'f3' + ) + ) + + assert_error() + + clear_results() + + action.cmd( + ('%d,%dSomeCmd %s'):format( + util.silent_edit 'f1', + util.silent_edit 'f2', + util.silent_edit 'f3' and 'f3' + ) + ) + + assert_error() + end) end) end) end) diff --git a/tests/suspend_spec.lua b/tests/suspend_spec.lua index 6dbab26..b55b467 100644 --- a/tests/suspend_spec.lua +++ b/tests/suspend_spec.lua @@ -1,4 +1,4 @@ -local api = vim.api +local api, uv = vim.api, vim.uv or vim.loop local util = require 'sos._test.util' local got_VimSuspend_after_resuming @@ -70,7 +70,7 @@ describe('neovim by default', function() nvim:buf_set_lines(0, 0, -1, true, { 'x' }) nvim:suspend() util.wait(500) - assert(vim.loop.fs_stat(tmp) == nil, 'expected file not to be saved') + assert(uv.fs_stat(tmp) == nil, 'expected file not to be saved') end) it( @@ -84,7 +84,7 @@ describe('neovim by default', function() nvim:buf_set_lines(0, 0, -1, true, { 'x' }) nvim:suspend() util.wait(250) - local stat = assert(vim.loop.fs_stat(tmp)) + local stat = assert(uv.fs_stat(tmp)) assert(stat.type == 'file', "dirent exists but isn't a regular file") end )