Allow test nearest function

pull/27/head
ray-x 3 years ago
parent 2cf2a1b36c
commit 200dcd22e4

@ -146,6 +146,9 @@ Support table based unit test auto generate, parse current function/method name
| command | Description |
| ----------- | ----------- |
| GoTestFunc | run test for current func |
| GoTestFunc yourtag | run test for current func with `-tags yourtag` option |
| GoTestFile | run test for current file folder |
| GoTestFile yourtag | run test for current folder with `-tags yourtag` option |
| GoAddTest | |
| GoAddExpTest | Add tests for exported funcs|
| GoAddAllTest | Add tests for all funcs |
@ -197,6 +200,7 @@ e.g:
| ----------- | ----------- |
| GoDebug | start debug session |
| GoDebug test | start debug session for go test file |
| GoDebug nearest | start debug session for nearest go test function |
| GoBreakToggle | |
| GoBreakCondition | conditional break |
| GoDbgStop | Stop debug session|

@ -64,7 +64,11 @@ function go.setup(cfg)
-- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]])
vim.cmd([[command! GoLint :setl makeprg=golangci-lint\ run\ --out-format\ tab | :GoMake]])
vim.cmd([[command! GoTestFunc lua require('go.gotest').test_fun()]])
-- e.g. GoTestFunc unit
vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_fun(<f-args>)]])
-- e.g. GoTestFile unit
vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]])
vim.cmd([[command! GoAddTest lua require("go.gotests").fun_test()]])
vim.cmd([[command! GoAddExpTest lua require("go.gotests").exported_test()]])
vim.cmd([[command! GoAddAllTest lua require("go.gotests").all_test()]])

@ -111,6 +111,18 @@ M.run = function(...)
request = "launch",
dlvToolPath = vim.fn.exepath("dlv")
}
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if ns == nil or ns == {} then
log('ts not not found, debug while file')
if mode == 'nearest' then
mode = 'test'
end
end
if mode == 'test' then
dap_cfg.name = dap_cfg.name .. ' test'
dap_cfg.mode = "test"
@ -119,6 +131,14 @@ M.run = function(...)
dap_cfg.program = "./${relativeFileDirname}"
dap.configurations.go = {dap_cfg}
dap.continue()
elseif mode == 'nearest' then
dap_cfg.name = dap_cfg.name .. ' test_nearest'
dap_cfg.mode = "test"
dap_cfg.program = "./${relativeFileDirname}"
dap_cfg.args = {'-test.run', '^' .. ns.name}
log(dap_cfg)
dap.configurations.go = {dap_cfg}
dap.continue()
else
dap_cfg.program = "${file}"
dap_cfg.args = args

@ -11,11 +11,33 @@ M.test_fun = function(args)
return
end
local tag = ''
utils.log(args)
if args ~= nil then
tag = [[-tags\ ]] .. args .. [[\ ]]
end
utils.log("parnode" .. vim.inspect(ns))
local cmd = [[setl makeprg=go\ test\ -v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath
local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath
.. [[ | lua require"go.asyncmake".make()]]
utils.log("test cmd", cmd)
vim.cmd(cmd)
end
M.test_file = function(args)
local workfolder = vim.lsp.buf.list_workspace_folders()[1]
local tag = ''
utils.log(args)
if args ~= nil then
tag = [[-tags\ ]] .. args .. [[\ ]]
end
local fpath = vim.fn.expand("%:p:h") .. '/..'
-- local fpath = './' .. vim.fn.expand('%:h') .. '/...'
utils.log("fpath" .. fpath)
local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ]] .. fpath
.. [[| lua require"go.asyncmake".make()]]
utils.log("test cmd", cmd)
vim.cmd(cmd)
end
return M

Loading…
Cancel
Save