[mirror] Make your go dev experience better
github.com/olexsmir/gopher.nvim
neovim
golang
1local Job = require "plenary.job"
2local ts_utils = require "gopher._utils.ts"
3local c = require("gopher.config").config.commands
4local u = require "gopher._utils"
5local M = {}
6
7---@param cmd_args table
8local function run(cmd_args)
9 Job:new({
10 command = c.gotests,
11 args = cmd_args,
12 on_exit = function(_, retval)
13 if retval ~= 0 then
14 u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
15 return
16 end
17
18 u.notify("unit test(s) generated", "info")
19 end,
20 }):start()
21end
22
23---@param args table
24local function add_test(args)
25 local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
26 table.insert(args, "-w")
27 table.insert(args, fpath)
28 run(args)
29end
30
31---generate unit test for one function
32---@param parallel boolean
33function M.func_test(parallel)
34 local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
35 if ns == nil or ns.name == nil then
36 u.notify("cursor on func/method and execute the command again", "info")
37 return
38 end
39
40 local cmd_args = { "-only", ns.name }
41 if parallel then
42 table.insert(cmd_args, "-parallel")
43 end
44
45 add_test(cmd_args)
46end
47
48---generate unit tests for all functions in current file
49---@param parallel boolean
50function M.all_tests(parallel)
51 local cmd_args = { "-all" }
52 if parallel then
53 table.insert(cmd_args, "-parallel")
54 end
55
56 add_test(cmd_args)
57end
58
59---generate unit tests for all exported functions
60---@param parallel boolean
61function M.all_exported_tests(parallel)
62 local cmd_args = {}
63 if parallel then
64 table.insert(cmd_args, "-parallel")
65 end
66
67 table.insert(cmd_args, "-exported")
68 add_test(cmd_args)
69end
70
71return M