-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathannotation_test.go
More file actions
495 lines (453 loc) · 12 KB
/
Copy pathannotation_test.go
File metadata and controls
495 lines (453 loc) · 12 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package annotation
import (
"go/ast"
"go/parser"
"go/token"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldSkipMutator(t *testing.T) {
tests := []struct {
name string
mutatorInfo mutatorInfo
mutatorName string
expected bool
}{
{
name: "Mutator matches exactly",
mutatorInfo: mutatorInfo{Names: []string{"MutatorA", "MutatorB"}},
mutatorName: "MutatorA",
expected: true,
},
{
name: "Mutator matches wildcard",
mutatorInfo: mutatorInfo{Names: []string{"*"}},
mutatorName: "AnyMutator",
expected: true,
},
{
name: "Mutator does not match",
mutatorInfo: mutatorInfo{Names: []string{"MutatorA", "MutatorB"}},
mutatorName: "MutatorC",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := shouldSkipMutator(tt.mutatorInfo, tt.mutatorName)
if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}
})
}
}
func TestParseMutators(t *testing.T) {
tests := []struct {
name string
mutatorList string
expected []string
}{
{
name: "Valid list of mutators",
mutatorList: "MutatorA, MutatorB, MutatorC",
expected: []string{"MutatorA", "MutatorB", "MutatorC"},
},
{
name: "List with leading and trailing spaces",
mutatorList: " MutatorA, MutatorB , MutatorC ",
expected: []string{"MutatorA", "MutatorB", "MutatorC"},
},
{
name: "Empty string",
mutatorList: "",
expected: []string{},
},
{
name: "String with only commas",
mutatorList: ",,,",
expected: []string{},
},
{
name: "Single mutator",
mutatorList: "MutatorA",
expected: []string{"MutatorA"},
},
{
name: "Multiple empty elements",
mutatorList: "MutatorA,,,MutatorB,,MutatorC,,",
expected: []string{"MutatorA", "MutatorB", "MutatorC"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseMutators(tt.mutatorList)
if !assert.Equal(t, result, tt.expected) {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}
})
}
}
func TestParseRegexAnnotation(t *testing.T) {
tests := []struct {
name string
commentText string
expectedRegex *regexp.Regexp
expectedInfo mutatorInfo
}{
{
name: "Valid regex and mutators",
commentText: "RegexName ^[a-z]+$ MutatorA, MutatorB",
expectedRegex: func() *regexp.Regexp {
re, _ := regexp.Compile("^[a-z]+$")
return re
}(),
expectedInfo: mutatorInfo{
Names: []string{"MutatorA", "MutatorB"},
},
},
{
name: "Valid regex without mutators",
commentText: "RegexName ^[0-9]{4}$",
expectedRegex: func() *regexp.Regexp {
re, _ := regexp.Compile("^[0-9]{4}$")
return re
}(),
expectedInfo: mutatorInfo{
Names: []string{},
},
},
{
name: "Invalid regex",
commentText: "RegexName [a-z",
expectedRegex: nil,
expectedInfo: mutatorInfo{},
},
{
name: "Empty comment",
commentText: "RegexName ",
expectedRegex: nil,
expectedInfo: mutatorInfo{},
},
}
r := &RegexAnnotation{Name: "RegexName"}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resultRegex, resultInfo := r.parseRegexAnnotation(tt.commentText)
if resultRegex == nil && tt.expectedRegex != nil || resultRegex != nil && resultRegex.String() != tt.expectedRegex.String() {
t.Errorf("Expected regex %v, but got %v", tt.expectedRegex, resultRegex)
}
if len(resultInfo.Names) != len(tt.expectedInfo.Names) {
t.Errorf("Expected mutators %v, but got %v", tt.expectedInfo.Names, resultInfo.Names)
} else {
for i, mutator := range resultInfo.Names {
if mutator != tt.expectedInfo.Names[i] {
t.Errorf("Expected mutator %v, but got %v", tt.expectedInfo.Names[i], mutator)
}
}
}
})
}
}
func TestParseLineAnnotation(t *testing.T) {
tests := []struct {
name string
commentText string
expectedInfo mutatorInfo
}{
{
name: "Valid mutators",
commentText: "LineName MutatorA, MutatorB, MutatorC",
expectedInfo: mutatorInfo{
Names: []string{"MutatorA", "MutatorB", "MutatorC"},
},
},
{
name: "Single mutator",
commentText: "LineName MutatorA",
expectedInfo: mutatorInfo{
Names: []string{"MutatorA"},
},
},
{
name: "Multiple mutators with spaces",
commentText: "LineName MutatorA, MutatorB , MutatorC ",
expectedInfo: mutatorInfo{
Names: []string{"MutatorA", "MutatorB", "MutatorC"},
},
},
{
name: "Empty comment",
commentText: "LineName ",
expectedInfo: mutatorInfo{
Names: []string{},
},
},
{
name: "Only spaces in mutators",
commentText: "LineName ,,,",
expectedInfo: mutatorInfo{
Names: []string{},
},
},
{
name: "Empty mutators",
commentText: "LineName",
expectedInfo: mutatorInfo{
Names: []string{},
},
},
}
l := &LineAnnotation{Name: "LineName"}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := l.parseLineAnnotation(tt.commentText)
if len(result.Names) != len(tt.expectedInfo.Names) {
t.Errorf("Expected mutators %v, but got %v", tt.expectedInfo.Names, result.Names)
} else {
for i, mutator := range result.Names {
if mutator != tt.expectedInfo.Names[i] {
t.Errorf("Expected mutator %v, but got %v", tt.expectedInfo.Names[i], mutator)
}
}
}
})
}
}
func TestExistsFuncAnnotation(t *testing.T) {
tests := []struct {
name string
funcDecl *ast.FuncDecl
expectedExist bool
}{
{
name: "No annotations",
funcDecl: &ast.FuncDecl{
Doc: &ast.CommentGroup{
List: []*ast.Comment{
{Text: "// Some other comment"},
},
},
},
expectedExist: false,
},
{
name: "Valid annotation exists",
funcDecl: &ast.FuncDecl{
Doc: &ast.CommentGroup{
List: []*ast.Comment{
{Text: "// mutator-disable-func something here"},
},
},
},
expectedExist: true,
},
{
name: "Multiple comments, valid annotation exists",
funcDecl: &ast.FuncDecl{
Doc: &ast.CommentGroup{
List: []*ast.Comment{
{Text: "// Another comment"},
{Text: "// mutator-disable-func something here"},
},
},
},
expectedExist: true,
},
{
name: "Doc is nil",
funcDecl: &ast.FuncDecl{
Doc: nil,
},
expectedExist: false,
},
}
p := NewProcessor()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := p.existsFuncAnnotation(tt.funcDecl)
if result != tt.expectedExist {
t.Errorf("Expected %v, but got %v", tt.expectedExist, result)
}
})
}
}
func TestCollectFunctionsAndFilterFunctions(t *testing.T) {
tests := []struct {
name string
code string
expected bool
filterPos token.Pos
}{
{
name: "Function with one statement",
code: `package main; func test() { var a = 10 }`,
expected: true,
filterPos: token.Pos(1),
},
{
name: "Function with nested statements",
code: `package main; func test() { if true { var a = 10 } }`,
expected: true,
filterPos: token.Pos(2),
},
}
f := &FunctionAnnotation{Exclusions: make(map[token.Pos]struct{})}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fs := token.NewFileSet()
node, err := parser.ParseFile(fs, "func_annotation_test.go", tt.code, parser.Mode(0))
if err != nil {
t.Fatalf("Failed to parse code: %v", err)
}
// Находим первую функцию в коде
var funcDecl *ast.FuncDecl
ast.Inspect(node, func(n ast.Node) bool {
if fDecl, ok := n.(*ast.FuncDecl); ok {
funcDecl = fDecl
return false
}
return true
})
f.collectFunctions(funcDecl)
filtered := f.filterFunctions(funcDecl)
assert.Equal(t, tt.expected, filtered)
})
}
}
func TestFindLinesMatchedRegex(t *testing.T) {
tests := []struct {
name string
filePath string
re *regexp.Regexp
expectedLines []int
}{
{
name: "No regex match",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile("notmatching"),
expectedLines: []int{},
},
{
name: "Match variable declaration",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`test`),
expectedLines: []int{37, 38},
},
{
name: "Match Println",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`Println`),
expectedLines: []int{16, 17, 23, 33, 38},
},
{
name: "Multiple matches on multiple lines",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`xx+`),
expectedLines: []int{12, 13, 16, 17},
},
{
name: "Match MyStruct",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`MyStruct`),
expectedLines: []int{19, 27, 31},
},
{
name: "Match interface declaration",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`interface`),
expectedLines: []int{42},
},
{
name: "Match slog.Info",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`slog\.Info`),
expectedLines: []int{49},
},
{
name: "Match s.Method()",
filePath: "../../testdata/annotation/regex.go",
re: regexp.MustCompile(`s\.Method\(\)`),
expectedLines: []int{20},
},
{
name: "Regex is nil",
filePath: "../../testdata/annotation/regex.go",
re: nil,
expectedLines: []int{},
},
{
name: "Empty file",
filePath: "../../testdata/annotation/empty.go",
re: regexp.MustCompile(`test`),
expectedLines: []int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &RegexAnnotation{Name: "TestRegexAnnotation"}
actual, _ := r.findLinesMatchingRegex(tt.filePath, tt.re)
assert.ElementsMatch(t, tt.expectedLines, actual)
})
}
}
func TestCollect(t *testing.T) {
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "../../testdata/annotation/collect.go", nil, parser.AllErrors|parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse file: %v", err)
}
processor := NewProcessor()
processor.Collect(file, fs, "../../testdata/annotation/collect.go")
assert.NotEmpty(t, processor.FunctionAnnotation.Exclusions)
assert.Equal(t, processor.FunctionAnnotation.Exclusions, map[token.Pos]struct{}{
75: {}, 99: {}, 104: {}, 114: {}, 115: {}, 117: {}, 122: {}, 126: {}, 129: {}, 136: {}, 140: {},
})
assert.NotEmpty(t, processor.RegexAnnotation.Exclusions)
assert.Equal(t, processor.RegexAnnotation.Exclusions, map[int]map[token.Pos]mutatorInfo{
14: {
169: {Names: []string{"*"}},
173: {Names: []string{"*"}},
181: {Names: []string{"*"}},
},
22: {
304: {Names: []string{"*"}},
308: {Names: []string{"*"}},
316: {Names: []string{"*"}},
},
21: {
288: {Names: []string{"*"}},
292: {Names: []string{"*"}},
300: {Names: []string{"*"}},
},
})
assert.NotEmpty(t, processor.LineAnnotation.Exclusions)
assert.Equal(t, processor.LineAnnotation.Exclusions, map[int]map[token.Pos]mutatorInfo{
19: {
275: {Names: []string{"numbers/incrementer"}},
279: {Names: []string{"numbers/incrementer"}},
283: {Names: []string{"numbers/incrementer"}},
},
})
}
func TestCollectGlobal(t *testing.T) {
filePath := "../../testdata/annotation/global/collect.go"
fs := token.NewFileSet()
file, err := parser.ParseFile(
fs,
filePath,
nil,
parser.AllErrors|parser.ParseComments,
)
assert.NoError(t, err)
processor := NewProcessor(WithGlobalRegexpFilter("\\.Log *"))
processor.Collect(file, fs, filePath)
assert.NotEmpty(t, processor.RegexAnnotation.GlobalRegexCollector.Exclusions)
assert.Equal(t, processor.RegexAnnotation.GlobalRegexCollector.Exclusions, map[int]map[token.Pos]mutatorInfo{
17: {
256: {Names: []string{"*"}},
263: {Names: []string{"*"}},
267: {Names: []string{"*"}},
},
})
}