Skip to content

Commit 0643590

Browse files
authored
Add tests for config and analyzer (#170)
1 parent 9885e30 commit 0643590

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

config_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package wsl
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCheckSet(t *testing.T) {
11+
for _, tc := range []struct {
12+
defaultName string
13+
enable []string
14+
disable []string
15+
expectedChecks CheckSet
16+
expectedErrContains string
17+
}{
18+
{
19+
defaultName: "",
20+
expectedChecks: DefaultChecks(),
21+
},
22+
{
23+
defaultName: "all",
24+
expectedChecks: AllChecks(),
25+
},
26+
{
27+
defaultName: "none",
28+
expectedChecks: NoChecks(),
29+
},
30+
{
31+
defaultName: "none",
32+
disable: []string{"for", "range"},
33+
expectedChecks: NoChecks(),
34+
},
35+
{
36+
defaultName: "none",
37+
enable: []string{"for", "range"},
38+
disable: []string{"range"},
39+
expectedChecks: map[CheckType]struct{}{
40+
CheckFor: {},
41+
},
42+
},
43+
{
44+
defaultName: "none",
45+
enable: []string{"for", "range"},
46+
expectedChecks: map[CheckType]struct{}{
47+
CheckFor: {},
48+
CheckRange: {},
49+
},
50+
},
51+
{
52+
defaultName: "all",
53+
disable: []string{"invalid-disable"},
54+
expectedErrContains: "invalid check 'invalid-disable'",
55+
},
56+
{
57+
defaultName: "all",
58+
enable: []string{"invalid-enable"},
59+
expectedErrContains: "invalid check 'invalid-enable'",
60+
},
61+
{
62+
defaultName: "invalid",
63+
expectedErrContains: "invalid preset",
64+
},
65+
} {
66+
t.Run(tc.defaultName, func(t *testing.T) {
67+
checks, err := NewCheckSet(tc.defaultName, tc.enable, tc.disable)
68+
if tc.expectedErrContains != "" {
69+
assert.Contains(t, err.Error(), tc.expectedErrContains)
70+
return
71+
}
72+
73+
require.NoError(t, err)
74+
assert.Equal(t, tc.expectedChecks, checks)
75+
})
76+
}
77+
}
78+
79+
func TestToAndFromString(t *testing.T) {
80+
maxCheckNumber := 23
81+
82+
for n := range maxCheckNumber {
83+
check := CheckType(n)
84+
ct, err := CheckFromString(check.String())
85+
86+
if n == 0 {
87+
assert.Equal(t, "invalid", check.String())
88+
require.Error(t, err)
89+
90+
continue
91+
}
92+
93+
require.NoError(t, err)
94+
assert.Equal(t, check, ct)
95+
}
96+
}

wsl_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func TestDefaultConfig(t *testing.T) {
2424
}
2525
}
2626

27+
func TestDefaultConfigFromAnalyzer(t *testing.T) {
28+
testdata := analysistest.TestData()
29+
analyzer := NewAnalyzer(nil)
30+
31+
analysistest.RunWithSuggestedFixes(t, testdata, analyzer, filepath.Join("default_config", "if"))
32+
}
33+
2734
func TestWithConfig(t *testing.T) {
2835
testdata := analysistest.TestData()
2936

0 commit comments

Comments
 (0)