diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-03 16:41:46 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-03 16:41:46 +0300 |
| commit | 3ee19139a95441a3ce10690377de8f453c7aec3f (patch) | |
| tree | b627097b2b2863e493e9ef0f9f4a88057c96c228 /docs | |
| parent | f3a1a41d92651d6d19ee6b078c0ffdc825015bde (diff) | |
lsp: add 'Hexai: implement unit test' code action for Gov0.4.1
- Generate or append tests to _test.go
- Jump to generated test via showDocument (command + server-initiated)
- Document the feature in docs/go-unit-tests.md
- Prefix action titles with 'Hexai: '
- Bump version to 0.4.1
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/go-unit-tests.md | 27 | ||||
| -rw-r--r-- | docs/usage-examples.md | 1 |
2 files changed, 28 insertions, 0 deletions
diff --git a/docs/go-unit-tests.md b/docs/go-unit-tests.md new file mode 100644 index 0000000..1be9fd0 --- /dev/null +++ b/docs/go-unit-tests.md @@ -0,0 +1,27 @@ +# Go unit tests via code action + +Hexai can generate Go unit tests for the function at your cursor. + +- Scope: Available only for Go source files ending with `.go` (not `_test.go`). +- Trigger: Use your editor's code actions on the current selection/position and pick "Implement unit test". + +What happens + +- Function detection: Hexai finds the nearest `func` definition above the cursor and captures the function body by balancing braces. +- Test generation: + - If an LLM provider is configured, Hexai asks it to generate one or more `Test*` functions using the `testing` package. The provider must return only the test function code (no package/import lines). + - If no provider is configured or the request fails, Hexai inserts a small stub test `Test<Name>` with a TODO. +- File handling and navigation: + - If `<file>_test.go` exists, the test function is appended to the end of that file. + - If it does not exist, Hexai creates it, writing `package <pkg>` (inferred from the source file) and `import "testing"`, followed by the generated test function(s). + - After applying the edit, Hexai asks the editor to focus the test file and place the cursor at the start of the newly added test function. + +Notes and limitations + +- Imports on append: when appending to an existing test file, Hexai assumes `testing` is available. If not, add `import "testing"` to the test file and re-run `go test`. +- Method names: for methods with receivers, test names default to `TestMethod` (stub fallback). Future improvement may generate `TestType_Method` automatically. +- Formatting: run `go fmt ./...` or your editor's formatter to normalize whitespace if needed. + +Examples + +In Helix, position the cursor inside a function and invoke code actions; choose "Implement unit test". Hexai will create or update `<file>_test.go` accordingly. diff --git a/docs/usage-examples.md b/docs/usage-examples.md index 5a80b18..3e96ade 100644 --- a/docs/usage-examples.md +++ b/docs/usage-examples.md @@ -65,6 +65,7 @@ Operate on the current selection in Helix: - Rewrite selection: finds the first instruction inside the selection and rewrites accordingly. - Resolve diagnostics: gathers only diagnostics overlapping the selection and fixes them by editing the selected code; diagnostics outside the selection are not changed. +- Implement unit test (Go): when editing a `.go` file, adds a code action to generate a unit test for the function under the cursor. If `<file>_test.go` exists, appends a new `Test*`; otherwise creates the test file with `package` and `import "testing"`. Instruction sources (first match wins): |
