From 51c0420ceea6bb292857a8835bb1fc6ff759143d Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Sat, 29 Jul 2023 10:26:22 -0300 Subject: [PATCH 1/2] feat: Get and run the specific test based on the model name field in the golang test table --- README.md | 2 +- after/ftplugin/go.lua | 7 ++++++ lua/go/test.lua | 57 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41aab48..09e5707 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Neovim (from v0.5) embeds a built-in Language Server Protocol (LSP) client. And - Auto format with `:GoFormat` (via `goimports`, `gofmt`, `gofumpt` and `lsp`) when saving. - Run linters with `:GoLint` (via `revive`) automatically. -- Quickly test with `:GoTest`, `:GoTestFunc`, `:GoTestFile` and `:GoTestAll`. Generate test with `:GoAddTest`. +- Quickly test with `:GoTest`, `:GoTestFunc`, `:GoTestName`, `:GoTestFile` and `:GoTestAll`. Generate test with `:GoAddTest`. - Import packages with `:GoGet` and `:GoImport`. - Modify struct tags with `:GoAddTags`, `:GoRemoveTags`, `:GoClearTags`, `:GoAddTagOptions`, `:GoRemoveTagOptions` and `:GoClearTagOptions`. - Generates JSON models with `:GoQuickType` (via `quicktype`). diff --git a/after/ftplugin/go.lua b/after/ftplugin/go.lua index d39fdaf..21d8a69 100644 --- a/after/ftplugin/go.lua +++ b/after/ftplugin/go.lua @@ -63,6 +63,13 @@ vim.api.nvim_create_user_command( nargs = 0, } ) +vim.api.nvim_create_user_command( + 'GoTestName', + 'lua require("go.test").test_name()', + { + nargs = 0, + } +) vim.api.nvim_create_user_command( 'GoTestFile', 'lua require("go.test").test_file()', diff --git a/lua/go/test.lua b/lua/go/test.lua index 1d45229..3e1de70 100644 --- a/lua/go/test.lua +++ b/lua/go/test.lua @@ -173,6 +173,63 @@ function M.test_func(opt) do_test(prefix, build_args(cmd)) end +local function replace_spaces_with_underscore(str) + return str:gsub("%s", "_") +end + +local function process_line_test_name() + local line = vim.api.nvim_get_current_line() + local start_idx, end_idx = string.find(line, "%b\"\"") + + if start_idx and end_idx then + local value_inside_parentheses = line:sub(start_idx + 1, end_idx - 1) + return replace_spaces_with_underscore(value_inside_parentheses) + end +end + +function M.test_name(opt) + if not util.binary_exists('go') then + return + end + + local prefix = 'GoTestName' + local func_name = '' + -- this is not available now actually + if opt and opt.func then + func_name = opt.func + else + local line = vim.fn.search([[func \(Test\|Example\)]], 'bcnW') + if line == 0 then + output.show_error( + prefix, + string.format('Test func not found: %s', func_name) + ) + return + end + local cur_line = vim.fn.getline(line) + func_name = split_file_name(cur_line) + end + if not valid_func_name(func_name) then + output.show_error( + 'GoTestFunc', + string.format('Invalid test func: %s', func_name) + ) + return + end + + local name_test = process_line_test_name() + + -- go test -run TestExample/Lorem_ipsum_dolor_sit_amet,_consectetur_adipiscing_elit + -- name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", + local cmd = { + 'go', + 'test', + '-run', + vim.fn.shellescape(string.format('^%s/%s$', func_name, name_test)), + } + do_test(prefix, build_args(cmd)) +end + function M.test_file() if not util.binary_exists('go') then return From 79aa8735172930b4d6f37fe438a9f0ac726e8251 Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Fri, 13 Oct 2023 09:01:57 -0300 Subject: [PATCH 2/2] chore: format code lua --- lua/go/test.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lua/go/test.lua b/lua/go/test.lua index 3e1de70..81ea8e2 100644 --- a/lua/go/test.lua +++ b/lua/go/test.lua @@ -174,12 +174,12 @@ function M.test_func(opt) end local function replace_spaces_with_underscore(str) - return str:gsub("%s", "_") + return str:gsub('%s', '_') end local function process_line_test_name() local line = vim.api.nvim_get_current_line() - local start_idx, end_idx = string.find(line, "%b\"\"") + local start_idx, end_idx = string.find(line, '%b""') if start_idx and end_idx then local value_inside_parentheses = line:sub(start_idx + 1, end_idx - 1) @@ -201,8 +201,8 @@ function M.test_name(opt) local line = vim.fn.search([[func \(Test\|Example\)]], 'bcnW') if line == 0 then output.show_error( - prefix, - string.format('Test func not found: %s', func_name) + prefix, + string.format('Test func not found: %s', func_name) ) return end @@ -211,16 +211,14 @@ function M.test_name(opt) end if not valid_func_name(func_name) then output.show_error( - 'GoTestFunc', - string.format('Invalid test func: %s', func_name) + 'GoTestFunc', + string.format('Invalid test func: %s', func_name) ) return end local name_test = process_line_test_name() - -- go test -run TestExample/Lorem_ipsum_dolor_sit_amet,_consectetur_adipiscing_elit - -- name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit", local cmd = { 'go', 'test',