Last active
September 18, 2023 22:02
-
-
Save dnwe/dba4ccb42b5ffeba4437a42038b685df to your computer and use it in GitHub Desktop.
Ruleguard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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