-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyze_test.go
86 lines (73 loc) · 2.12 KB
/
analyze_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
package cautiouspancake
import (
"go/build"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/go/loader"
)
func loadFixture(t *testing.T) *loader.Program {
t.Helper()
pkgs := []string{"github.com/tam7t/cautious-pancake/fixtures"}
conf := loader.Config{Build: &build.Default}
// Use the initial packages from the command line.
_, err := conf.FromArgs(pkgs, false)
if err != nil {
t.Fatal(err)
}
// Load, parse and type-check the whole program.
iprog, err := conf.Load()
if err != nil {
t.Fatal(err)
}
return iprog
}
func TestAnalyze(t *testing.T) {
want := map[string]bool{
"YesManipulate": true,
"YesPanicArray": true,
"YesPanicNil": true,
"YesAnonymousDynamicCall": true,
"YesFuncParam": true,
"YesAnonymousDynamicCall$1": true,
"YesParser": true,
"YesParse": true,
"YesMultArgs": true,
"YesMaybePanic": true,
"Yes": true,
"init$1": true,
"YesAppend": true,
"yes": true,
"YesPanic": true,
"YesRead": true,
"YesLog": true,
"YesErr": true,
"YesFmtErr": true,
"YesVariadic": true,
"YesArgs": true,
"NoDynamicCall": false,
"NoGlobalRead": false,
"NoGlobalWrite": false,
"NoInterface": false,
"NoNet": false,
"NoPrint": false,
"NoWrite": false,
"NoWriteErr": false,
"NoWriter": false,
"init": false,
}
iprog := loadFixture(t)
cg := NewCallGraph(iprog)
if err := cg.Analyze(); err != nil {
t.Errorf("Analyze() returned error: %v", err)
}
got := make(map[string]bool)
for _, v := range cg.Pure() {
got[v.Name()] = true
}
for _, v := range cg.Impure() {
got[v.Name()] = false
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Analyze() mismatch (-want +got):\n%s", diff)
}
}