Skip to content

Instantly share code, notes, and snippets.

@dnwe
Last active September 18, 2023 22:02
Show Gist options
  • Save dnwe/dba4ccb42b5ffeba4437a42038b685df to your computer and use it in GitHub Desktop.
Save dnwe/dba4ccb42b5ffeba4437a42038b685df to your computer and use it in GitHub Desktop.
Ruleguard
package example
import "testing"
func TestXYZ(t *testing.T) {
tests := []struct {
name string
want int
}{
{
name: "single golden path",
want: 42,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.want != 42 {
t.FailNow()
}
})
}
}
package gorules
import (
"github.com/quasilyte/go-ruleguard/dsl"
_ "github.com/quasilyte/go-ruleguard/dsl/types"
)
func tableTests(m dsl.Matcher) {
m.Match(
`for $_, $_ := range $tests { t.Run($_, $_) }`,
).
Where(m["tests"].Contains(`[]$_{$_}`)).
Report(`unnecessary table-driven test with only one entry`)
}
% ruleguard -c=0 -rules rules.go -debug-group tableTests -v example_test.go
example_test.go:15: [rules.go:10] rejected by m["tests"].Contains(`[]$_{$_}`)
$tests []struct{name string; want int}: tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment