select test from functions

pull/130/head
ray-x 2 years ago
parent 125d6fa3d3
commit fed6b24482

@ -11,11 +11,12 @@ local long_opts = {
compile = "c",
tags = "t",
bench = "b",
select = "s",
floaterm = "F",
}
local sep = require("go.utils").sep()
local short_opts = "vct:bF"
local short_opts = "vct:bsF"
local bench_opts = { "-benchmem", "-cpuprofile", "profile.out" }
M.efm = function()
@ -196,7 +197,7 @@ M.test_fun = function(...)
row, col = row, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if empty(ns) then
return false
return M.select_tests()
end
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
@ -220,6 +221,9 @@ M.test_fun = function(...)
table.insert(cmd, "test")
end
if optarg["s"] then
return M.select_tests()
end
if _GO_NVIM_CFG.verbose_tests then
table.insert(cmd, "-v")
end
@ -244,12 +248,12 @@ M.test_fun = function(...)
term({ cmd = cmd, autoclose = false })
return
end
vim.list_extend(cmd, reminder)
vim.cmd([[setl makeprg=]] .. test_runner .. [[\ test]])
-- set_efm()
utils.log("test cmd", cmd)
return require("go.asyncmake").make(unpack(cmd))
end
M.test_file = function(...)
@ -351,12 +355,11 @@ M.run_file = function()
end)
end
-- GUI to select test?
M.select_tests = function()
local bufnr = vim.api.nvim_get_current_buf()
local tree = vim.treesitter.get_parser(bufnr):parse()[1]
local query = vim.treesitter.parse_query("go", require("go.ts.textobjects").query_test_func)
local query = vim.treesitter.parse_query("go", require("go.ts.go").query_test_func)
local test_names = {}
for id, node, metadata in query:iter_captures(tree:root(), bufnr, 0, -1) do
local name = query.captures[id] -- name of the capture in the query
@ -372,37 +375,29 @@ M.select_tests = function()
vim.ui.select = require("guihua.gui").select
end
local title = "Possible Tests"
pickers.new({}, {
prompt_title = title,
finder = finders.new_table({
results = test_names,
entry_maker = function(entry)
return {
value = entry,
text = entry,
display = entry,
ordinal = entry,
}
end,
}),
previewer = false,
sorter = conf.generic_sorter({}),
attach_mappings = function(_)
actions.select_default:replace(function(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.schedule(function()
vim.lsp.buf.execute_command({
command = "gopls.run_tests",
arguments = { { URI = vim.uri_from_bufnr(0), Tests = { selection.value } } },
})
end)
actions.close(prompt_bufnr)
end)
return true
end,
}):find()
vim.defer_fn(function()
vim.ui.select = original_select
end, 500)
local function onselect(item, idx)
if not item then
return
end
local uri = vim.uri_from_bufnr(0)
log(uri, item, idx)
vim.schedule(function()
vim.lsp.buf.execute_command({
command = "gopls.run_tests",
arguments = { { URI = uri, Tests = { item } } },
})
end)
end
vim.ui.select(test_names, {
prompt = "select test to run:",
kind = "codelensaction",
}, onselect)
return test_names
end
return M

@ -33,3 +33,35 @@ func Test_branch(t *testing.T) {
})
}
}
func TestBranch(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want int
}{
// TODO: Add test cases.
{
name: "a10",
args: args{a: 10},
want: 10,
},
{
name: "b10",
args: args{b: 10},
want: 20,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := branch(tt.args.a, tt.args.b); got == tt.want {
t.Errorf("branch() = %v, want %v", got, tt.want)
}
})
}
}

@ -44,7 +44,7 @@ describe("should run test file", function()
vim.fn.setpos(".", { 0, 5, 11, 0 })
local cmd = require("go.gotest").test_file()
eq({ "go", "test", "-v", "-run", "Test_branch", "./lua/tests/fixtures/coverage/branch_test.go" }, cmd)
eq({ "go", "test", "-v", "-run", "Test_branch|TestBranch", "./lua/tests/fixtures/coverage/branch_test.go" }, cmd)
end)
end)
@ -70,3 +70,26 @@ describe("should run test package", function()
eq({ "go", "test", "-v", "./lua/tests/fixtures/coverage/..." }, cmd)
end)
end)
describe("should allow select test func", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])
local path = cur_dir .. "/lua/tests/fixtures/coverage/branch_test.go" -- %:p:h ? %:p
require("go").setup({
trace = true,
lsp_cfg = true,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
})
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos(".", { 0, 1, 1, 0 })
local cmd = require("go.gotest").select_tests()
eq({ "Test_branch", "TestBranch" }, cmd)
end)
end)

Loading…
Cancel
Save