-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitfile_analysis_test.go
201 lines (182 loc) · 3.64 KB
/
splitfile_analysis_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package splitfile_test
import (
"testing"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/mccurdyc/splitfile"
)
// TestFromFileSystem demonstrates how to test an analysis using input
// files stored in the file system.
//
// These tests have the advantages that test data can be edited
// directly, and that files named in error messages can be opened.
// However, they tend to spread a small number of lines of text across a
// rather deep directory hierarchy, and obscure similarities among
// related tests, especially when tests involve multiple packages, or
// multiple variants of a single scenario.
// func TestFromFileSystem(t *testing.T) {
// testdata := analysistest.TestData()
// analysistest.Run(t, testdata, splitfile.Analyzer, "abc")
// }
// TestFromStringLiterals demonstrates how to test an analysis using
// a table of string literals for each test case.
//
// Such tests are typically quite compact.
func TestFromStringLiterals(t *testing.T) {
for _, test := range [...]struct {
name string
pkgpath string
files map[string]string
}{
{
name: "single_type",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
`,
},
},
{
name: "small_file_un-related_types",
pkgpath: "ab",
files: map[string]string{"ab/ab.go": `package ab
type a int
type b int
`,
},
},
{
name: "small file un-related types different syntax",
pkgpath: "ab",
files: map[string]string{"ab/ab.go": `package ab
type (
a int
b int
)
`,
},
},
{
name: "related types through struct fields",
pkgpath: "ab",
files: map[string]string{"ab/ab.go": `package ab
type a int
type b struct {
a a
}
`,
},
},
{
name: "related type and function param",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func fa(a a) {
}
`,
},
},
{
name: "related type and function usage",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func fa() {
_ = a(123)
}
`,
},
},
{
name: "related type and multiple functions",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func fa(a a) {
}
func faa() {
_ = a(123)
}
`,
},
},
{
name: "related type and method receiver (non-pointer)",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func (a a) ma() {
}
`,
},
},
{
name: "related type and method receiver (pointer)",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func (a *a) ma() {
}
`,
},
},
{
name: "related type and method param",
pkgpath: "ab",
files: map[string]string{"ab/ab.go": `package ab
type a int
type b int
func (b b) mb(a a) {
}
`,
},
},
{
name: "related type method param and return values",
pkgpath: "abc",
files: map[string]string{"abc/abc.go": `package abc
type a int
type b int
type c int
func (b b) mb(a a) c {
return c(1)
}
`,
},
},
{
name: "related type and method usage",
pkgpath: "ab",
files: map[string]string{"ab/ab.go": `package ab
type a int
type b int
func (b b) mb() {
_ = a(123)
}
`,
},
},
{
name: "related type and multiple methods",
pkgpath: "a",
files: map[string]string{"a/a.go": `package a
type a int
func fa(a a) {
}
func faa() {
_ = a(123)
}
`,
},
},
} {
t.Run(test.name, func(t *testing.T) {
dir, cleanup, err := analysistest.WriteFiles(test.files)
if err != nil {
t.Fatal(err)
}
defer cleanup()
analysistest.Run(t, dir, splitfile.Analyzer, test.pkgpath)
})
}
}