[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
1---@toc_entry Generating unit tests boilerplate 2---@tag gopher.nvim-gotests 3---@text gotests is utilizing the `gotests` tool to generate unit tests boilerplate. 4---@usage 5--- - Generate unit test for specific function/method: 6--- 1. Place your cursor on the desired function/method. 7--- 2. Run `:GoTestAdd` 8--- 9--- - Generate unit tests for *all* functions/methods in current file: 10--- - run `:GoTestsAll` 11--- 12--- - Generate unit tests *only* for *exported(public)* functions/methods: 13--- - run `:GoTestsExp` 14--- 15--- You can also specify the template to use for generating the tests. See |gopher.nvim-config| 16--- More details about templates can be found at: https://github.com/cweill/gotests 17--- 18 19---@tag gopher.nvim-gotests-named 20---@text 21--- You can enable named tests in the config if you prefer using named tests. 22--- But you must install `gotests@develop` because the stable version doesn't support this feature. 23--- 24--- >lua 25--- -- simply run go get in your shell: 26--- go install github.com/cweill/gotests/...@develop 27--- 28--- -- if you want to install it within neovim, you can use one of this: 29--- -- if you choose to install gotests this way i reocmmend adding it to your `build` section in your |lazy.nvim| 30--- 31--- vim.fn.jobstart("go install github.com/cweill/gotests/...@develop") 32--- 33--- -- or if you want to use mason: 34--- require("mason-tool-installer").setup { 35--- ensure_installed = { 36--- { "gotests", version = "develop" }, 37--- } 38--- } 39--- < 40 41local c = require "gopher.config" 42local ts_utils = require "gopher._utils.ts" 43local r = require "gopher._utils.runner" 44local u = require "gopher._utils" 45local log = require "gopher._utils.log" 46local gotests = {} 47 48---@param args table 49---@private 50local function add_test(args) 51 if c.gotests.named then 52 table.insert(args, "-named") 53 end 54 55 if c.gotests.template_dir then 56 table.insert(args, "-template_dir") 57 table.insert(args, c.gotests.template_dir) 58 end 59 60 if c.gotests.template ~= "default" then 61 table.insert(args, "-template") 62 table.insert(args, c.gotests.template) 63 end 64 65 table.insert(args, "-w") 66 table.insert(args, vim.fn.expand "%") 67 68 log.debug("generating tests with args: ", args) 69 70 local rs = r.sync { c.commands.gotests, unpack(args) } 71 if rs.code ~= 0 then 72 error("gotests failed: " .. rs.stderr) 73 end 74 75 u.notify "unit test(s) generated" 76end 77 78-- generate unit test for one function 79function gotests.func_test() 80 local bufnr = vim.api.nvim_get_current_buf() 81 local func = ts_utils.get_func_under_cursor(bufnr) 82 83 add_test { "-only", func.name } 84end 85 86-- generate unit tests for all functions in current file 87function gotests.all_tests() 88 add_test { "-all" } 89end 90 91-- generate unit tests for all exported functions 92function gotests.all_exported_tests() 93 add_test { "-exported" } 94end 95 96return gotests