-
Notifications
You must be signed in to change notification settings - Fork 6
/
types_test.go
172 lines (157 loc) · 4.34 KB
/
types_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
162
163
164
165
166
167
168
169
170
171
172
package analysisutil_test
import (
"errors"
"fmt"
"go/token"
"go/types"
"path/filepath"
"testing"
"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestObjectOf(t *testing.T) {
t.Parallel()
cases := map[string]struct {
src string
pkg string // blank means same as the map key
name string
found bool
}{
"standard": {`import _ "fmt"`, "fmt", "Println", true},
"unimport": {"", "fmt", "Println", false},
"notexiststd": {`import _ "fmt"`, "fmt", "NOTEXIST", false},
"typename": {"type A int", "", "A", true},
"unexportvar": {"var n int", "", "n", true},
"exportvar": {"var N int", "", "N", true},
"notexist": {"", "", "NOTEXIST", false},
"vendored": {`import _ "fmt"`, "vendor/fmt", "Println", true},
"pointer": {"type A int", "", "*A", false},
}
for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()
a := &analysis.Analyzer{
Name: name + "Analyzer",
Run: func(pass *analysis.Pass) (interface{}, error) {
pkg := name
if tt.pkg != "" {
pkg = tt.pkg
}
obj := analysisutil.ObjectOf(pass, pkg, tt.name)
switch {
case tt.found && obj == nil:
return nil, errors.New("expect found but not found")
case !tt.found && obj != nil:
return nil, fmt.Errorf("unexpected return value: %v", obj)
}
return nil, nil
},
}
path := filepath.Join(name, name+".go")
dir := WriteFiles(t, map[string]string{
path: fmt.Sprintf("package %s\n%s", name, tt.src),
})
analysistest.Run(t, dir, a, name)
})
}
}
func TestUnder(t *testing.T) {
t.Parallel()
lookup := func(pass *analysis.Pass, n string) (types.Type, error) {
_, obj := pass.Pkg.Scope().LookupParent(n, token.NoPos)
if obj == nil {
return nil, fmt.Errorf("does not find: %s", n)
}
return obj.Type(), nil
}
cases := map[string]struct {
src string
typ string
want string
}{
"nonamed": {"", "int", "int"},
"named": {"type A int", "A", "int"},
"twonamed": {"type A int; type B A", "B", "int"},
}
for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()
a := &analysis.Analyzer{
Name: name + "Analyzer",
Run: func(pass *analysis.Pass) (interface{}, error) {
typ, err := lookup(pass, tt.typ)
if err != nil {
return nil, err
}
want, err := lookup(pass, tt.want)
if err != nil {
return nil, err
}
got := analysisutil.Under(typ)
if !types.Identical(want, got) {
return nil, fmt.Errorf("want %v but got %v", want, got)
}
return nil, nil
},
}
path := filepath.Join(name, name+".go")
dir := WriteFiles(t, map[string]string{
path: fmt.Sprintf("package %s\n%s", name, tt.src),
})
analysistest.Run(t, dir, a, name)
})
}
}
func TestField(t *testing.T) {
t.Parallel()
lookup := func(pass *analysis.Pass, n string) (types.Type, error) {
_, obj := pass.Pkg.Scope().LookupParent(n, token.NoPos)
if obj == nil {
return nil, fmt.Errorf("does not find: %s", n)
}
return obj.Type(), nil
}
cases := map[string]struct {
src string
typ string
field string
want int
}{
"nomarl": {"type a struct{n int}", "a", "n", 0},
"nofield": {"type a struct{n int}", "a", "m", -1},
"empty": {"type a struct{}", "a", "n", -1},
"two": {"type a struct{n, m int}", "a", "m", 1},
"nonamed": {"var a struct{n, m int}", "a", "m", 1},
"ptr": {"var a *struct{n, m int}", "a", "m", 1},
"namednamed": {"type a struct{n int}; type b a", "b", "n", 0},
"alias": {"type a struct{n int}; type b = a", "b", "n", 0},
}
for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()
a := &analysis.Analyzer{
Name: name + "Analyzer",
Run: func(pass *analysis.Pass) (interface{}, error) {
typ, err := lookup(pass, tt.typ)
if err != nil {
return nil, err
}
got, _ := analysisutil.Field(typ, tt.field)
if tt.want != got {
return nil, fmt.Errorf("want %v but got %v", tt.want, got)
}
return nil, nil
},
}
path := filepath.Join(name, name+".go")
dir := WriteFiles(t, map[string]string{
path: fmt.Sprintf("package %s\n%s", name, tt.src),
})
analysistest.Run(t, dir, a, name)
})
}
}