Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add diagnostics for table tests. #75

Open
arnevm123 opened this issue Feb 7, 2024 · 2 comments
Open

Feature request: Add diagnostics for table tests. #75

arnevm123 opened this issue Feb 7, 2024 · 2 comments

Comments

@arnevm123
Copy link

arnevm123 commented Feb 7, 2024

Hi,
Currently I have support for table tests enabled with experimental = { test_table = true } but diagnostics are only shown at the assertion, while the symbols are shown at the table-level.
It would be nice to also have the diagnostic at the test-table level.
I had a look in the code and it seems that this should be possible within marshal_gotest_output (lua/neotest-go/output.lua:26).

We get the exact test name + file so we should be able to get the test with some Treesitter magic.
If anyone could help, or has a more deep understanding of the plugin and can quickly implement this, feel free.
If not, I'll try to see if I can implement this if I have some more free time.

@sergii4
Copy link
Collaborator

sergii4 commented Feb 7, 2024

Hi @arnevm123 could you please provide some screenshots? It would help understand what do you want to achive

@arnevm123
Copy link
Author

arnevm123 commented Feb 7, 2024

So basically I would want my diagnostic message on the line of the failure. The blue arrow should show this 😄
image

my config:

"nvim-neotest/neotest",
dependencies = {
	"nvim-lua/plenary.nvim",
	"nvim-treesitter/nvim-treesitter",
	"nvim-neotest/neotest-plenary",
	"nvim-neotest/neotest-go",
},
config = function()
	local neotest_ns = vim.api.nvim_create_namespace("neotest")
	vim.diagnostic.config({
		virtual_text = {
			format = function(diagnostic)
				local message =
					diagnostic.message:gsub("\n", " "):gsub("\t", " "):gsub("%s+", " "):gsub("^%s+", "")
				return message
			end,
		},
	}, neotest_ns)
	require("neotest").setup({
		diagnostic = {
			enabled = true,
			severity = 4,
		},
		adapters = {
			require("neotest-go")({
				experimental = { test_table = true },
			}),
		},
	})
end,

main.go:

package main

import "fmt"

func AddNoZero(a, b int) (int, error) {
	if a == 0 || b == 0 {
		return 0, fmt.Errorf("cannot add zero")
	}
	return a + b, nil
}

main_test.go

package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestAddNoZero(t *testing.T) {
	type args struct {
		a int
		b int
	}
	tests := []struct {
		name      string
		args      args
		want      int
		assertion assert.ErrorAssertionFunc
	}{
		{
			name: "correct test",
			args: args{
				a: 2,
				b: 5,
			},
			want:      7,
			assertion: assert.NoError,
		},
		{
			name: "another correct test",
			args: args{
				a: 8,
				b: 5,
			},
			want:      13,
			assertion: assert.NoError,
		},
		{
			name: "failing test",
			args: args{
				a: 7,
				b: 5,
			},
			want:      9,
			assertion: assert.NoError,
		},
		{
			name: "final correct test",
			args: args{
				a: 7,
				b: 5,
			},
			want:      12,
			assertion: assert.NoError,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := AddNoZero(tt.args.a, tt.args.b)
			tt.assertion(t, err)
			assert.Equal(t, tt.want, got)
		})
	}
}

folke pushed a commit to LazyVim/LazyVim that referenced this issue Jun 23, 2024
## What is this PR for?

This PR switches
[nvim-neotest/neotest-go](https://github.com/nvim-neotest/neotest-go)
for
[fredrikaverpil/neotest-golang](https://github.com/fredrikaverpil/neotest-golang).

## Does this PR fix an existing issue?

Neotest-go comes with some problems which are mitigated in
neotest-golang. A full description/background is available in the
project README, but here are some highlights:

### Neotest-go issues mitigated in neotest-golang

- Test Output in JSON, making it difficult to read:
[neotest-go#52](nvim-neotest/neotest-go#52)
- "Run nearest" runs all tests:
[neotest-go#83](nvim-neotest/neotest-go#83)
- Running test suite doesn't work:
[neotest-go#89](nvim-neotest/neotest-go#89)
- Diagnostics for table tests on the line of failure:
[neotest-go#75](nvim-neotest/neotest-go#75)
- Support for Nested Subtests:
[neotest-go#74](nvim-neotest/neotest-go#74)
- DAP support:
[neotest-go#12](nvim-neotest/neotest-go#12)

### Features

- Supports all [Neotest
usage](https://github.com/nvim-neotest/neotest#usage).
- Integrates with [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
for debugging of tests using delve.
- Inline diagnostics.
- Works great with
[andythigpen/nvim-coverage](https://github.com/andythigpen/nvim-coverage)
for displaying coverage in the sign column (per-Go package, or per-test
basis).
- Monorepo support (detect, run and debug tests in sub-projects).
- Supports table tests (relies on treesitter AST detection).
- Supports nested test functions.

## Notes

- I'm the author of
[fredrikaverpil/neotest-golang](https://github.com/fredrikaverpil/neotest-golang).


## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants