-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyzer_test.go
106 lines (86 loc) · 2 KB
/
analyzer_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
package intrange_test
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"testing"
"github.com/gostaticanalysis/testutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"github.com/ckaznocha/intrange"
)
func TestAnalyzer(t *testing.T) {
t.Parallel()
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.RunWithSuggestedFixes(t, testdata, intrange.Analyzer, "main")
}
func FuzzAnalyzer(f *testing.F) {
f.Add(`package p
func f() {
// Basic for loop
for i := 0; i < 10; i++ {
println(i)
}
}`)
f.Add(`package p
func f() {
// Loop with len
arr := []int{1, 2, 3}
for i := 0; i < len(arr); i++ {
println(arr[i])
}
}`)
f.Add(`package p
func f() {
// Already using range
for i := range 10 {
println(i)
}
}`)
f.Add(`package p
func f() {
// Loop with different increment
for i := 0; i < 10; i += 1 {
println(i)
}
}`)
f.Add(`package p
func f() {
// Loop with assignment increment
for i := 0; i < 10; i = i + 1 {
println(i)
}
}`)
f.Fuzz(func(t *testing.T, code string) {
fSet := token.NewFileSet()
f, err := parser.ParseFile(fSet, "test.go", code, parser.ParseComments)
if err != nil {
return
}
files := []*ast.File{f}
typesInfo := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
pkg, err := (&types.Config{}).Check("p", fSet, files, typesInfo)
if err != nil {
return
}
if _, err := intrange.Analyzer.Run(&analysis.Pass{
Fset: fSet,
Files: files,
Pkg: pkg,
TypesInfo: typesInfo,
ResultOf: map[*analysis.Analyzer]any{
inspect.Analyzer: inspector.New(files),
},
Report: func(analysis.Diagnostic) {},
}); err != nil {
t.Errorf("Analyzer failed on code:\n%s\nError: %v", code, err)
}
})
}