-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssainspect_test.go
78 lines (62 loc) · 1.6 KB
/
ssainspect_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
package ssainspect_test
import (
"bytes"
"errors"
"flag"
"fmt"
"reflect"
"testing"
"github.com/gostaticanalysis/testutil"
"github.com/tenntenn/golden"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/gostaticanalysis/ssainspect"
)
var flagUpdate bool
func init() {
flag.BoolVar(&flagUpdate, "update", false, "update golden files")
}
func TestInspectorWithAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
pkgs := []string{"a", "b"}
rs := analysistest.Run(t, testdata, testAnalyzer, pkgs...)
for i, pkg := range pkgs {
buf, ok := rs[i].Result.(*bytes.Buffer)
if !ok {
t.Fatal("unexpected error")
}
if flagUpdate {
golden.Update(t, analysistest.TestData(), pkg, buf)
continue
}
if diff := golden.Diff(t, analysistest.TestData(), pkg, buf); diff != "" {
t.Error(diff)
}
}
}
var testAnalyzer = &analysis.Analyzer{
Name: "testssainspect",
Doc: "test analyzer for ssainspect.All",
Run: run,
Requires: []*analysis.Analyzer{
ssainspect.Analyzer,
},
ResultType: reflect.TypeFor[*bytes.Buffer](),
}
func run(pass *analysis.Pass) (any, error) {
inspector, ok := pass.ResultOf[ssainspect.Analyzer].(*ssainspect.Inspector)
if !ok {
return nil, errors.New("failed to get result of ssainspect.Analyzer")
}
var buf bytes.Buffer
for cur := range inspector.All() {
if cur.FirstInstr() {
if cur.FirstBlock() {
fmt.Fprintln(&buf, "Func", cur.Func)
}
fmt.Fprintln(&buf, "Block", cur.Block, "InCycle=", cur.InCycle())
}
fmt.Fprintln(&buf, "\t", cur.Instr)
}
return &buf, nil
}