[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
at main 2.1 kB view raw
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. 16--- See |gopher.nvim-config|. 17--- More details about templates: https://github.com/cweill/gotests 18--- 19--- If you prefer named tests, you can enable them in |gopher.nvim-config|. 20 21local c = require "gopher.config" 22local ts_utils = require "gopher._utils.ts" 23local r = require "gopher._utils.runner" 24local u = require "gopher._utils" 25local log = require "gopher._utils.log" 26local gotests = {} 27 28---@param args table 29---@dochide 30local function add_test(args) 31 if c.gotests.named then 32 table.insert(args, "-named") 33 end 34 35 if c.gotests.template_dir then 36 table.insert(args, "-template_dir") 37 table.insert(args, c.gotests.template_dir) 38 end 39 40 if c.gotests.template ~= "default" then 41 table.insert(args, "-template") 42 table.insert(args, c.gotests.template) 43 end 44 45 table.insert(args, "-w") 46 table.insert(args, vim.fn.expand "%") 47 48 log.debug("generating tests with args: ", args) 49 50 local rs = r.sync { c.commands.gotests, unpack(args) } 51 if rs.code ~= 0 then 52 error("gotests failed: " .. rs.stderr) 53 end 54 55 u.notify "unit test(s) generated" 56end 57 58-- generate unit test for one function 59function gotests.func_test() 60 local bufnr = vim.api.nvim_get_current_buf() 61 local func = ts_utils.get_func_under_cursor(bufnr) 62 63 add_test { "-only", func.name } 64end 65 66-- generate unit tests for all functions in current file 67function gotests.all_tests() 68 add_test { "-all" } 69end 70 71-- generate unit tests for all exported functions 72function gotests.all_exported_tests() 73 add_test { "-exported" } 74end 75 76return gotests