-
Notifications
You must be signed in to change notification settings - Fork 2
/
asasalint_test.go
161 lines (146 loc) · 3.27 KB
/
asasalint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package asasalint
import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"testing"
"golang.org/x/tools/go/analysis/analysistest"
)
const filename = "<src>"
func makePkg(src string) (*types.Package, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, src, parser.DeclarationErrors)
if err != nil {
return nil, err
}
// use the package name as package path
conf := types.Config{Importer: importer.Default()}
return conf.Check(file.Name.Name, fset, []*ast.File{file}, nil)
}
func TestIsSliceAnyType(t *testing.T) {
p, _ := makePkg("package p; var A []any\nvar B any = []any{1, 2, 3}\nvar C []int")
aT := p.Scope().Lookup("A").Type()
if !isSliceAnyType(aT) {
t.Errorf("isSliceAnyType(%v) = false, want true", aT)
}
bT := p.Scope().Lookup("B").Type()
if isSliceAnyType(bT) {
t.Errorf("isSliceAnyType(%v) = true, want false", bT)
}
cT := p.Scope().Lookup("C").Type()
if isSliceAnyType(cT) {
t.Errorf("isSliceAnyType(%v) = true, want false", cT)
}
}
func TestIsSliceAnyVariadicFuncType(t *testing.T) {
p, _ := makePkg("package p; func hello(a int, b ...any) {}\nfunc hello2(a int, b int) {}")
ft := p.Scope().Lookup("hello").Type()
if !isSliceAnyVariadicFuncType(ft) {
t.Errorf("isSliceAnyVariadicFuncType(%v) = false, want true", ft)
}
ft2 := p.Scope().Lookup("hello2").Type()
if isSliceAnyVariadicFuncType(ft2) {
t.Errorf("isSliceAnyVariadicFuncType(%v) = true, want false", ft2)
}
}
func TestGetFuncName(t *testing.T) {
testCases := []struct {
desc string
src string
expected string
}{
{
desc: "function",
src: `package p
func hello(a int, b ...any) {}
func hello2(a int, b int) { hello(a, b)}
`,
expected: "hello",
},
{
desc: "method",
src: `package p
type A struct {}
func (a *A) hello(a int, b ...any) {}
func (a *A) hello2(a int, b int) {
a.hello(a, b)
}
`,
expected: "a.hello",
},
{
desc: "function inside a method",
src: `package p
type A struct {}
func (a *A) hello(a int, b ...any) {
hello(a, b...)
}
`,
expected: "hello",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "src.go", test.src, 0)
if err != nil {
panic(err)
}
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
s, err := getFuncName(fset, x)
if err != nil {
t.Fatal(err)
}
if s != test.expected {
t.Errorf("%s: got %s, want %s", fset.Position(x.Fun.Pos()), s, test.expected)
}
}
return true
})
})
}
}
func TestAnalyzer(t *testing.T) {
testCases := []struct {
desc string
settings LinterSetting
}{
{
desc: "basic",
settings: LinterSetting{},
},
{
desc: "nobuiltin",
settings: LinterSetting{
NoBuiltinExclusions: true,
},
},
{
desc: "ignoretest",
settings: LinterSetting{
IgnoreTest: true,
},
},
{
desc: "custom",
settings: LinterSetting{
Exclude: []string{"get.+"},
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
a, err := NewAnalyzer(test.settings)
if err != nil {
t.Fatal(err)
}
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.desc)
})
}
}