-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathparse_test.go
394 lines (322 loc) · 8.69 KB
/
parse_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package typewriter
import (
"fmt"
"go/ast"
"go/token"
"os"
"strings"
"testing"
)
type findDirectiveTest struct {
text string
found bool
}
// dummy type evaluator
type eval struct{}
func (eval) Eval(name string) (Type, error) {
return Type{
Name: name,
}, nil
}
func TestFindDirective(t *testing.T) {
tests := []findDirectiveTest{
{`// +test`, true},
{`// +test foo:"bar,Baz"`, true},
{`// there's nothing here`, false},
{`//+test foo:"bar,Baz"`, true},
{`//+test foo:"bar,Baz"`, true},
{`// +test * foo:"bar,Baz"`, true},
{`// +test foo:"bar,Baz" qux:"thing"`, true},
{`// +tested`, false},
}
for i, test := range tests {
g := &ast.CommentGroup{
List: []*ast.Comment{{Text: test.text}},
}
c := findAnnotation(g, "+test")
found := c != nil
if found != test.found {
t.Errorf("[test %v] found should have been %v for:\n%s", i, test.found, test.text)
}
}
}
type parseTest struct {
comment string
pointer Pointer
tags TagSlice
valid bool
}
func TestParse(t *testing.T) {
tests := []parseTest{
{`// +test foo`, false, TagSlice{
{"foo", []TagValue{}, false},
}, true},
{`// +test foo bar`, false, TagSlice{
{"foo", []TagValue{}, false},
{"bar", []TagValue{}, false},
}, true},
{`// +test foo:"bar,Baz"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, nil},
}, false},
}, true},
{`// +test * foo:"bar,Baz"`, true, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, nil},
}, false},
}, true},
{`// +test foo:"bar,Baz" qux:"stuff"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, nil},
}, false},
{"qux", []TagValue{
{"stuff", nil, nil},
}, false},
}, true},
{`// +test foo:"-bar,Baz"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, nil},
}, true},
}, true},
{`// +test foo:"bar ,Baz " `, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, nil},
}, false},
}, true},
{`// +test foo:"bar,Baz[qaz], qux"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, []item{{val: "qaz"}}},
{"qux", nil, nil},
}, false},
}, true},
{`// +test foo:"bar,Baz[[]qaz]"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, []item{{val: "[]qaz"}}},
}, false},
}, true},
{`// +test foo:"bar,Baz[qaz,hey]" qux:"stuff"`, false, TagSlice{
{"foo", []TagValue{
{"bar", nil, nil},
{"Baz", nil, []item{{val: "qaz"}, {val: "hey"}}},
}, false},
{"qux", []TagValue{
{"stuff", nil, nil},
}, false},
}, true},
{`// +test foo:"Baz[qaz],yo[dude]" qux:"stuff[things]"`, false, TagSlice{
{"foo", []TagValue{
{"Baz", nil, []item{{val: "qaz"}}},
{"yo", nil, []item{{val: "dude"}}},
}, false},
{"qux", []TagValue{
{"stuff", nil, []item{{val: "things"}}},
}, false},
}, true},
{`// +test foo:"bar,Baz`, false, nil, false},
{`// +test foo:"bar,-Baz"`, false, nil, false},
{`// +test foo:"bar,Baz-"`, false, nil, false},
{`// +test foo:bar,Baz" qux:"stuff"`, false, nil, false},
{`// +test foo"bar,Baz" qux:"stuff"`, false, nil, false},
{`// +test foo:"bar,Baz" 8qux:"stuff"`, false, nil, false},
{`// +test fo^o:"bar,Baz" qux:"stuff"`, false, nil, false},
{`// +test foo:"bar,Ba|z" qux:"stuff"`, false, nil, false},
{`// +test foo:"bar,Baz" qux:"stuff`, false, nil, false},
{`// +test *foo:"bar,Baz" qux:"stuff"`, false, nil, false},
{`// +test foo:"bar,Baz" * qux:"stuff"`, false, nil, false},
{`// +test * foo:"bar,Baz" * qux:"stuff"`, false, nil, false},
{`// +test foo:"bar,Baz[foo"`, false, nil, false},
{`// +test foo:"bar,Baz[foo]]"`, false, nil, false},
{`// +test foo:"bar,Baz[[]foo"`, false, nil, false},
{`// +test foof:"bar,Baz" foof:"qux"`, false, nil, false},
}
fset := token.NewFileSet()
for i, test := range tests {
c := &ast.Comment{
Text: test.comment,
}
pointer, tags, err := parse(fset, c, "+test")
if test.valid != (err == nil) {
t.Errorf("[test %v] valid should have been %v for: %s\n%s", i, test.valid, test.comment, err)
}
if pointer != test.pointer {
t.Errorf("[test %v] pointer should have been %v for: \n%s", i, bool(test.pointer), test.comment)
}
if !tagsEqual(tags, test.tags) {
t.Fatalf("[test %v] tags should have been \n%v, got \n%v", i, test.tags, tags)
}
}
}
func tagsEqual(tags, other TagSlice) bool {
if len(tags) != len(other) {
return false
}
for i := range tags {
t := tags[i]
o := other[i]
if t.Name != o.Name {
return false
}
if t.Negated != o.Negated {
return false
}
if len(t.Values) != len(o.Values) {
return false
}
for j := range t.Values {
tv := t.Values[j]
ov := o.Values[j]
if tv.Name != ov.Name {
return false
}
if len(tv.TypeParameters) != len(ov.TypeParameters) {
return false
}
for k := range tv.TypeParameters {
if tv.TypeParameters[k].String() != ov.TypeParameters[k].String() {
return false
}
}
}
}
return true
}
func TestGetTypes(t *testing.T) {
// app and dummy types are marked up with +test
pkgs, err := getPackages("+test", DefaultConfig)
if err != nil {
t.Error(err)
return
}
typs := pkgs[0].Types
if len(typs) != 4 {
t.Errorf("should have found the 4 marked-up types, found %v", len(typs))
}
// put 'em into a map for convenience
m := typeSliceToMap(typs)
if _, found := m["App"]; !found {
t.Errorf("should have found the app type")
}
if _, found := m["dummy"]; !found {
t.Errorf("should have found the dummy type")
}
if _, found := m["dummy2"]; !found {
t.Errorf("should have found the dummy2 type")
}
if _, found := m["dummy3"]; !found {
t.Errorf("should have found the dummy3 type")
}
if t.Failed() {
return // get out instead of nil panics below
}
dummy := m["dummy"]
if !dummy.comparable {
t.Errorf("dummy type should be comparable")
}
if !dummy.ordered {
t.Errorf("dummy type should be ordered")
}
if !dummy.numeric {
t.Errorf("dummy type should be numeric")
}
if len(dummy.Tags) != 1 {
t.Fatalf("typ should have 1 tag, found %v", len(m["dummy"].Tags))
}
if len(dummy.Tags[0].Values) != 1 {
fmt.Println(dummy.Tags[0].Values)
t.Errorf("Tag should have 1 Item, found %v", len(m["dummy"].Tags[0].Values))
}
dummy2 := m["dummy2"]
if dummy2.comparable {
t.Errorf("dummy2 type should not be comparable")
}
if dummy2.ordered {
t.Errorf("dummy2 type should not be ordered")
}
if dummy2.numeric {
t.Errorf("dummy2 type should not be numeric")
}
dummy3 := m["dummy3"]
if !dummy3.comparable {
t.Errorf("dummy3 type should be comparable")
}
if !dummy3.ordered {
t.Errorf("dummy3 type should be ordered")
}
if dummy3.numeric {
t.Errorf("dummy3 type should not be numeric")
}
// check tag existence at a high level here, see also tag parsing tests
app := m["App"]
if len(app.Tags) != 2 {
t.Errorf("typ should have 2 TagSlice, found %v", len(app.Tags))
}
if len(app.Tags[0].Values) != 1 {
t.Errorf("Tag should have 1 Item, found %v", len(app.Tags[0].Values))
}
if len(app.Tags[1].Values) != 2 {
t.Errorf("Tag should have 2 Values, found %v", len(app.Tags[1].Values))
}
if len(app.Tags[1].Values[0].TypeParameters) != 1 {
t.Fatalf("TagValue should have 1 TypeParameter, found %v", len(app.Tags[1].Values[0].TypeParameters))
}
// filtered types should not show up
conf := &Config{}
conf.Filter = func(f os.FileInfo) bool {
return !strings.HasPrefix(f.Name(), "dummy")
}
pkgs2, err2 := getPackages("+test", conf)
if err2 != nil {
t.Error(err2)
}
typs2 := pkgs2[0].Types
if len(typs2) != 1 {
t.Errorf("should have found the 1 marked-up type when filtered, found %v", len(typs2))
}
m2 := typeSliceToMap(typs2)
if _, found := m2["dummy"]; found {
t.Errorf("should not have found the dummy type")
}
if _, found := m2["App"]; !found {
t.Errorf("should have found the app type")
}
// no false positives
pkgs3, err3 := getPackages("+notreal", DefaultConfig)
typs3 := pkgs3[0].Types
if len(typs3) != 0 {
t.Errorf("should have no marked-up types for +notreal")
}
if err3 != nil {
t.Error(err3)
}
// should fail if types can't be evaluated
// package.go by itself can't compile since it depends on other types
conf4 := &Config{}
conf4.Filter = func(f os.FileInfo) bool {
return f.Name() == "package.go"
}
_, err4 := getPackages("+test", conf4)
if err4 == nil {
t.Error("should have been unable to evaluate types of incomplete package")
}
}
func typeSliceToMap(typs []Type) map[string]Type {
result := make(map[string]Type)
for _, v := range typs {
result[v.Name] = v
}
return result
}
func tagSliceToMap(typs []Tag) map[string]Tag {
result := make(map[string]Tag)
for _, v := range typs {
result[v.Name] = v
}
return result
}