nvim-neotest / neotest-go

MIT License
136 stars 43 forks source link

Feature request: Add diagnostics for table tests. #75

Open arnevm123 opened 9 months ago

arnevm123 commented 9 months ago

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 commented 9 months ago

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

arnevm123 commented 9 months ago

So basically I would want my diagnostic message on the line of the failure. The blue arrow should show this :smile: 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)
        })
    }
}