11# gogentest
22
3+ ![ CI] ( https://github.com/YuminosukeSato/gogentest/workflows/CI/badge.svg )
4+ [ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
5+
36A Neovim plugin for generating Go test templates using LSP (gopls) to extract function signatures.
47
5- ## Features
8+ ## ✨ Features
69
7- - Automatically generates test templates for Go functions
8- - Uses LSP (gopls) to extract function signatures with full type information
9- - Falls back to Treesitter or minimal template when gopls is unavailable
10- - Generates Goland-compatible test templates with:
10+ - 🚀 Automatically generates test templates for Go functions
11+ - 🔍 Uses LSP (gopls) to extract function signatures with full type information
12+ - 🔄 Falls back to Treesitter or minimal template when gopls is unavailable
13+ - 📝 Generates Goland-compatible test templates with:
1114 - Proper args struct with typed fields
1215 - Table-driven test structure
1316 - Error handling with ` assert.ErrorAssertionFunc `
1417 - Appropriate want fields based on return types
1518
16- ## Requirements
19+ ## 📋 Requirements
1720
1821- Neovim 0.8+ (for LSP support)
1922- gopls (Go language server) - usually installed with Go development environment
2023- Optional: nvim-treesitter with Go parser for fallback support
2124
22- ## Installation
25+ ## 📦 Installation
2326
24- ### Using [ lazy.nvim] ( https://github.com/folke/lazy.nvim )
27+ ### Using [ lazy.nvim] ( https://github.com/folke/lazy.nvim ) (Recommended)
2528
2629``` lua
27- {
28- " your-username/gogentest" ,
29- ft = " go" ,
30- dependencies = { " neovim/nvim-lspconfig" }, -- for gopls
31- keys = {
32- { " <leader>tG" , function () require (" gogentest" ).generate () end , desc = " Generate Go Test" },
30+ return {
31+ {
32+ " YuminosukeSato/gogentest" ,
33+ ft = " go" ,
34+ dependencies = { " neovim/nvim-lspconfig" },
35+ keys = {
36+ { " <leader>tG" , function () require (" gogentest" ).generate () end , desc = " Generate Go Test" },
37+ },
38+ },
39+ }
40+ ```
41+
42+ ### LazyVim Full Configuration
43+
44+ If you're using LazyVim, create a file ` ~/.config/nvim/lua/plugins/gogentest.lua ` :
45+
46+ ``` lua
47+ return {
48+ {
49+ " YuminosukeSato/gogentest" ,
50+ ft = " go" ,
51+ dependencies = { " neovim/nvim-lspconfig" },
52+ keys = {
53+ { " <leader>tG" , function () require (" gogentest" ).generate () end , desc = " Generate Go Test" },
54+ },
55+ config = function ()
56+ -- Optional: Add any custom configuration here
57+ end ,
58+ },
59+
60+ -- Ensure gopls is properly configured
61+ {
62+ " neovim/nvim-lspconfig" ,
63+ opts = {
64+ servers = {
65+ gopls = {
66+ settings = {
67+ gopls = {
68+ analyses = {
69+ unusedparams = true ,
70+ },
71+ staticcheck = true ,
72+ },
73+ },
74+ },
75+ },
76+ },
3377 },
3478}
3579```
@@ -38,16 +82,22 @@ A Neovim plugin for generating Go test templates using LSP (gopls) to extract fu
3882
3983``` lua
4084use {
41- " your-username /gogentest" ,
85+ " YuminosukeSato /gogentest" ,
4286 ft = " go" ,
4387 requires = { " neovim/nvim-lspconfig" },
88+ config = function ()
89+ -- Optional keymapping
90+ vim .api .nvim_set_keymap (' n' , ' <leader>tG' ,
91+ ' :lua require("gogentest").generate()<CR>' ,
92+ { noremap = true , silent = true , desc = " Generate Go Test" })
93+ end ,
4494}
4595```
4696
47- ## Usage
97+ ## 🚀 Usage
4898
49991 . Place your cursor on a Go function you want to test
50- 2 . Run ` :GogentestGenerate ` or use your configured keybinding (e.g., ` <leader>tG ` )
100+ 2 . Run ` :GogentestGenerate ` or use your configured keybinding (default: ` <leader>tG ` )
511013 . The plugin will:
52102 - Create a ` *_test.go ` file if it doesn't exist
53103 - Generate a test template with proper types extracted from gopls
@@ -58,7 +108,13 @@ use {
58108Given this Go function:
59109``` go
60110func ProcessData (ctx context .Context , id int , data string ) (string , error ) {
61- // implementation
111+ if id <= 0 {
112+ return " " , errors.New (" invalid id" )
113+ }
114+ if data == " " {
115+ return " " , errors.New (" empty data" )
116+ }
117+ return fmt.Sprintf (" processed: %s (id: %d )" , data, id), nil
62118}
63119```
64120
@@ -100,11 +156,14 @@ func TestProcessData(t *testing.T) {
100156}
101157```
102158
103- ## Configuration
159+ ## ⚙️ Configuration
104160
105- You can add custom keymappings in your Neovim configuration:
161+ ### Custom Keymappings
162+
163+ You can customize keymappings in your Neovim configuration:
106164
107165``` lua
166+ -- In your init.lua or lua/config/keymaps.lua
108167vim .api .nvim_create_autocmd (" FileType" , {
109168 pattern = " go" ,
110169 callback = function ()
@@ -115,19 +174,90 @@ vim.api.nvim_create_autocmd("FileType", {
115174})
116175```
117176
118- ## How it works
177+ ### LazyVim Keymaps
178+
179+ If you prefer to configure keymaps separately in LazyVim:
180+
181+ ``` lua
182+ -- In ~/.config/nvim/lua/config/keymaps.lua
183+ local function map (mode , lhs , rhs , opts )
184+ vim .keymap .set (mode , lhs , rhs , opts )
185+ end
186+
187+ -- Go test generation
188+ map (" n" , " <leader>tg" , function () require (" gogentest" ).generate () end ,
189+ { desc = " Generate Go test" })
190+ ```
191+
192+ ## 🔧 How it works
119193
1201941 . ** LSP First** : The plugin tries to get function signature from gopls using ` textDocument/signatureHelp `
1211952 . ** Parse Signature** : Extracts function name, parameter names/types, and return types
1221963 . ** Generate Template** : Creates a table-driven test with proper type information
1231974 . ** Fallback** : If gopls is unavailable, falls back to Treesitter (function name only) or minimal template
124198
125- ## Troubleshooting
199+ ## 🐛 Troubleshooting
200+
201+ ### "gopls unavailable" message
202+ Ensure gopls is installed and running:
203+ ``` bash
204+ go install golang.org/x/tools/gopls@latest
205+ ```
206+
207+ Then restart Neovim or run ` :LspRestart ` .
208+
209+ ### "function not detected"
210+ Make sure your cursor is on or inside a Go function declaration. The plugin looks for function signatures at the cursor position.
211+
212+ ### Type information missing
213+ Check that:
214+ - gopls is properly configured
215+ - The Go file has no syntax errors
216+ - Your Go module is properly initialized (` go mod init ` )
217+
218+ ### LazyVim specific issues
219+ If the plugin isn't loading:
220+ 1 . Run ` :Lazy sync ` to ensure it's installed
221+ 2 . Check ` :Lazy ` to see if the plugin is loaded
222+ 3 . Verify the file type is detected as "go" with ` :set ft? `
223+
224+ ## 👨💻 Development
225+
226+ ### Running Tests
227+ ``` bash
228+ make test
229+ ```
230+
231+ ### Linting
232+ ``` bash
233+ make lint
234+ ```
235+
236+ ### Format Code
237+ ``` bash
238+ make format
239+ ```
240+
241+ ### Check All
242+ ``` bash
243+ make check
244+ ```
245+
246+ ## 📄 License
247+
248+ MIT License - see [ LICENSE] ( LICENSE ) for details.
249+
250+ ## 🤝 Contributing
251+
252+ Contributions are welcome! Please feel free to submit a Pull Request.
126253
127- - ** "gopls unavailable" message** : Ensure gopls is installed and running for your Go files
128- - ** "function not detected"** : Make sure your cursor is on or inside a Go function declaration
129- - ** Type information missing** : Check that gopls is properly configured and the file has no syntax errors
254+ 1 . Fork the repository
255+ 2 . Create your feature branch (` git checkout -b feature/amazing-feature ` )
256+ 3 . Commit your changes (` git commit -m 'Add some amazing feature' ` )
257+ 4 . Push to the branch (` git push origin feature/amazing-feature ` )
258+ 5 . Open a Pull Request
130259
131- ## License
260+ ## 🙏 Acknowledgments
132261
133- MIT
262+ - Built with [ plenary.nvim] ( https://github.com/nvim-lua/plenary.nvim ) for testing
263+ - Inspired by various Go test generation tools
0 commit comments