feat(gotests): add generate one test
docs(gotests): add generate one test
This commit is contained in:
parent
507ccb6347
commit
8b3c68e38a
5 changed files with 74 additions and 2 deletions
12
README.md
12
README.md
|
|
@ -3,7 +3,6 @@
|
||||||
Minimalistic plugin for Go development in Neovim written in Lua.
|
Minimalistic plugin for Go development in Neovim written in Lua.
|
||||||
|
|
||||||
It's not an LSP tool, the main goal of this plugin add go tooling support in neovim.
|
It's not an LSP tool, the main goal of this plugin add go tooling support in neovim.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18)
|
Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18)
|
||||||
|
|
@ -32,6 +31,7 @@ This will install next tools:
|
||||||
|
|
||||||
- [gomodifytags](https://github.com/fatih/gomodifytags)
|
- [gomodifytags](https://github.com/fatih/gomodifytags)
|
||||||
- [impl](https://github.com/josharian/impl)
|
- [impl](https://github.com/josharian/impl)
|
||||||
|
- [gotests](https://github.com/cweill/gotests)
|
||||||
|
|
||||||
2. Modify struct tags:
|
2. Modify struct tags:
|
||||||
By default be added/removed `json` tag, if not set.
|
By default be added/removed `json` tag, if not set.
|
||||||
|
|
@ -78,7 +78,15 @@ Example of usage:
|
||||||
:GoImpl io.Reader
|
:GoImpl io.Reader
|
||||||
```
|
```
|
||||||
|
|
||||||
5. Run `go generate` command
|
6. Generate tests with [gotests](https://github.com/cweill/gotests)
|
||||||
|
|
||||||
|
Generate one test for spesific function/method
|
||||||
|
|
||||||
|
```vim
|
||||||
|
:GoTestAdd
|
||||||
|
```
|
||||||
|
|
||||||
|
7. Run `go generate` command
|
||||||
|
|
||||||
```vim
|
```vim
|
||||||
" Run `go generate` in cwd path
|
" Run `go generate` in cwd path
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ local M = {
|
||||||
querys = {
|
querys = {
|
||||||
struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
|
struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
|
||||||
em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]],
|
em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]],
|
||||||
|
method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]],
|
||||||
|
func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,4 +33,15 @@ function M.get_struct_node_at_pos(row, col, bufnr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.get_func_method_node_at_pos(row, col, bufnr)
|
||||||
|
local query = M.querys.func .. " " .. M.querys.method_name
|
||||||
|
local bufn = bufnr or vim.api.nvim_get_current_buf()
|
||||||
|
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
|
||||||
|
if ns == nil then
|
||||||
|
print "func not found"
|
||||||
|
else
|
||||||
|
return ns[#ns]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
48
lua/gopher/gotests.lua
Normal file
48
lua/gopher/gotests.lua
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
local Job = require "plenary.job"
|
||||||
|
local ts_utils = require "gopher._utils.ts"
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
---@param cmd_args table
|
||||||
|
local function run(cmd_args)
|
||||||
|
Job
|
||||||
|
:new({
|
||||||
|
command = "gotests",
|
||||||
|
args = cmd_args,
|
||||||
|
on_exit = function(_, retval)
|
||||||
|
if retval ~= 0 then
|
||||||
|
print("command exited with code " .. retval)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
print "unit test(s) generated"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
:start()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param args table
|
||||||
|
local function add_test(args)
|
||||||
|
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
|
||||||
|
table.insert(args, "-w")
|
||||||
|
table.insert(args, fpath)
|
||||||
|
run(args)
|
||||||
|
end
|
||||||
|
|
||||||
|
---generate unit test for one function
|
||||||
|
---@param parallel boolean
|
||||||
|
function M.func_test(parallel)
|
||||||
|
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
|
||||||
|
if ns == nil or ns.name == nil then
|
||||||
|
print "cursor on func/method and execute the command again"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cmd_args = { "-only", ns.name }
|
||||||
|
if parallel then
|
||||||
|
table.insert(cmd_args, "-parallel")
|
||||||
|
end
|
||||||
|
|
||||||
|
add_test(cmd_args)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
local tags = require "gopher.struct_tags"
|
local tags = require "gopher.struct_tags"
|
||||||
|
local gotests = require "gopher.gotests"
|
||||||
local gopher = {}
|
local gopher = {}
|
||||||
|
|
||||||
gopher.install_deps = require "gopher.installer"
|
gopher.install_deps = require "gopher.installer"
|
||||||
|
|
@ -8,5 +9,6 @@ gopher.mod = require "gopher.gomod"
|
||||||
gopher.get = require "gopher.goget"
|
gopher.get = require "gopher.goget"
|
||||||
gopher.impl = require "gopher.impl"
|
gopher.impl = require "gopher.impl"
|
||||||
gopher.generate = require "gopher.gogenerate"
|
gopher.generate = require "gopher.gogenerate"
|
||||||
|
gopher.test_add = gotests.one_test
|
||||||
|
|
||||||
return gopher
|
return gopher
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>)
|
command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>)
|
||||||
command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>)
|
command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>)
|
||||||
|
command! -nargs=* GoTestAdd :lua require"gopher".test_add(<f-args>)
|
||||||
command! -nargs=* GoMod :lua require"gopher".mod(<f-args>)
|
command! -nargs=* GoMod :lua require"gopher".mod(<f-args>)
|
||||||
command! -nargs=* GoGet :lua require"gopher".get(<f-args>)
|
command! -nargs=* GoGet :lua require"gopher".get(<f-args>)
|
||||||
command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>)
|
command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue