github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/dbc/analysis/analysistest/analysistest.go (about)

     1  package analysistest
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     8  	"github.com/blueinnovationsgroup/can-go/pkg/dbc/analysis"
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  type Case struct {
    13  	Name        string
    14  	Data        string
    15  	Diagnostics []*analysis.Diagnostic
    16  }
    17  
    18  func Run(t *testing.T, a *analysis.Analyzer, cs []*Case) {
    19  	t.Helper()
    20  	for _, c := range cs {
    21  		p := dbc.NewParser(c.Name, []byte(strings.TrimLeft(c.Data, "\n")))
    22  		assert.NilError(t, p.Parse())
    23  		pass := &analysis.Pass{
    24  			Analyzer: a,
    25  			File:     p.File(),
    26  		}
    27  		assert.NilError(t, a.Run(pass))
    28  		// allow omitting byte offsets and file names
    29  		for _, d := range c.Diagnostics {
    30  			d.Pos.Offset = 0
    31  			d.Pos.Filename = c.Name
    32  		}
    33  		for _, d := range pass.Diagnostics {
    34  			d.Pos.Offset = 0
    35  		}
    36  		assert.DeepEqual(t, c.Diagnostics, pass.Diagnostics)
    37  	}
    38  }