-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
574 lines (531 loc) · 13.4 KB
/
Copy pathmain_test.go
File metadata and controls
574 lines (531 loc) · 13.4 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package main
import (
"bufio"
"bytes"
"errors"
"os"
"reflect"
"sort"
"strings"
"testing"
"golang.org/x/tools/cover"
)
func TestPrintProfile_Format(t *testing.T) {
// test cases for the printProfile function
buf := bytes.Buffer{}
profiles := []*cover.Profile{
{
FileName: "file1.go",
Mode: "set",
Blocks: []cover.ProfileBlock{
{
StartLine: 1,
StartCol: 2,
EndLine: 3,
EndCol: 4,
NumStmt: 5,
Count: 6,
},
},
},
}
printProfile(profiles[0], &buf)
actual := strings.Trim(buf.String(), "\n")
expected := "file1.go:1.2,3.4 5 6"
if actual != expected {
t.Errorf("expected %s, got %s", expected, actual)
}
}
func TestPrintProfiles_Format(t *testing.T) {
buf := bytes.Buffer{}
profiles := []*cover.Profile{
{
FileName: "file1.go",
Mode: "count",
Blocks: []cover.ProfileBlock{
{
StartLine: 1,
StartCol: 2,
EndLine: 3,
EndCol: 4,
NumStmt: 5,
Count: 6,
},
{
StartLine: 11,
StartCol: 12,
EndLine: 13,
EndCol: 14,
NumStmt: 15,
Count: 16,
},
},
},
{
FileName: "file2.go",
Mode: "set",
Blocks: []cover.ProfileBlock{
{
StartLine: 1,
StartCol: 2,
EndLine: 3,
EndCol: 4,
NumStmt: 5,
Count: 6,
},
{
StartLine: 21,
StartCol: 22,
EndLine: 23,
EndCol: 24,
NumStmt: 25,
Count: 26,
},
},
},
}
expected := []string{
"mode: count",
"file1.go:1.2,3.4 5 6",
"file1.go:11.12,13.14 15 16",
"file2.go:1.2,3.4 5 6",
"file2.go:21.22,23.24 25 26",
}
var actual []string
printProfiles(profiles, &buf)
for reader := bufio.NewReader(&buf); ; {
line, err := reader.ReadString('\n')
if err != nil {
break
}
actual = append(actual, strings.Trim(line, "\n"))
}
if len(expected) != len(actual) {
t.Errorf("expected %d lines, got %d\n%v", len(actual), len(expected), actual)
}
for i, expect := range expected {
if actual[i] != expect {
t.Errorf("%d expected %s, got %s", i+1, expect, actual[i])
}
}
}
func TestCheckError(t *testing.T) {
count := 0
testCases := []struct {
name string
err error
output string
fn func(i int)
}{
{"nil", nil, "", func(i int) {
t.Error("should not be called")
}},
{"help", errHelp, `usage: gocovdedup [<file1> <file2> ... <fileN>|-]
files must be in go cover format or if '-' is supplied then read from stdin`, func(i int) {
if i != 99 {
t.Errorf("expected 99, got %d", i)
}
count++
}},
{"general", errors.New("general"), "general", func(i int) {
if i != 1 {
t.Errorf("expected 1, got %d", i)
}
count++
}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
count = 0
var buf bytes.Buffer
checkError(tc.err, &buf, tc.fn)
actual := strings.Trim(buf.String(), "\n")
if actual != tc.output {
t.Errorf("expected %s, got %s", tc.output, actual)
}
if tc.err != nil && count != 1 {
t.Errorf("expected 1, got %d", count)
}
})
}
}
func TestMax(t *testing.T) {
testCases := []struct {
name string
a, b int
expected int
}{
{"a", 1, 0, 1},
{"b", 0, 1, 1},
{"equal", 1, 1, 1},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := max(tc.a, tc.b)
if actual != tc.expected {
t.Errorf("expected %d, got %d", tc.expected, actual)
}
})
}
}
func TestByFileName(t *testing.T) {
// test byFileName sorting
testCases := []struct {
name string
order []*cover.Profile
expected []*cover.Profile
}{
{"empty", []*cover.Profile{}, []*cover.Profile{}},
{"one", []*cover.Profile{{FileName: "a"}}, []*cover.Profile{{FileName: "a"}}},
{"two", []*cover.Profile{{FileName: "b"}, {FileName: "a"}}, []*cover.Profile{{FileName: "a"}, {FileName: "b"}}},
{"three", []*cover.Profile{{FileName: "b"}, {FileName: "c"}, {FileName: "a"}}, []*cover.Profile{{FileName: "a"}, {FileName: "b"}, {FileName: "c"}}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sort.Sort(byFileName(tc.order))
if !reflect.DeepEqual(tc.order, tc.expected) {
t.Errorf("expected %v, got %v", tc.expected, tc.order)
}
})
}
}
func TestCombine(t *testing.T) {
// test function combine
testCases := []struct {
name string
order []*cover.Profile
expected map[string]*cover.Profile
}{
{"empty", []*cover.Profile{}, map[string]*cover.Profile{}},
{"one", []*cover.Profile{{FileName: "a"}}, map[string]*cover.Profile{"a": {FileName: "a"}}},
{"two", []*cover.Profile{{FileName: "b"}, {FileName: "a"}}, map[string]*cover.Profile{"a": {FileName: "a"}, "b": {FileName: "b"}}},
{"three", []*cover.Profile{{FileName: "b"}, {FileName: "c"}, {FileName: "a"}}, map[string]*cover.Profile{"a": {FileName: "a"}, "b": {FileName: "b"}, "c": {FileName: "c"}}},
{
"blocks",
[]*cover.Profile{
{FileName: "b", Blocks: []cover.ProfileBlock{{StartLine: 1}}},
{FileName: "b", Blocks: []cover.ProfileBlock{{StartLine: 2}}},
{FileName: "a"},
},
map[string]*cover.Profile{
"a": {FileName: "a"},
"b": {FileName: "b", Blocks: []cover.ProfileBlock{{StartLine: 1}, {StartLine: 2}}},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := combine(tc.order)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("expected %v, got %v", tc.expected, actual)
}
})
}
}
func TestMaxEndLine(t *testing.T) {
// test function maxEndLine
testCases := []struct {
name string
a, b cover.ProfileBlock
line, col int
}{
{
name: "equal",
a: cover.ProfileBlock{EndLine: 1, EndCol: 1},
b: cover.ProfileBlock{EndLine: 1, EndCol: 1},
line: 1,
col: 1,
},
{
name: "disjoint",
a: cover.ProfileBlock{EndLine: 2, EndCol: 25},
b: cover.ProfileBlock{EndLine: 1, EndCol: 1},
line: 2,
col: 25,
},
{
name: "overlapping",
a: cover.ProfileBlock{EndLine: 1, EndCol: 25},
b: cover.ProfileBlock{EndLine: 7, EndCol: 1},
line: 7,
col: 1,
},
{
name: "same line",
a: cover.ProfileBlock{EndLine: 7, EndCol: 25},
b: cover.ProfileBlock{EndLine: 7, EndCol: 1},
line: 7,
col: 25,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
line, col := maxEndLine(&tc.a, &tc.b)
if line != tc.line || col != tc.col {
t.Errorf("expected %d:%d, got %d:%d", tc.line, tc.col, line, col)
}
})
}
}
func TestOrderedBlocks(t *testing.T) {
// test sorting of orderedBlocks
testCases := []struct {
name string
blocks []cover.ProfileBlock
expected []cover.ProfileBlock
}{
{
name: "basic",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
},
},
{
name: "overlapping",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 16, StartCol: 76, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 16, StartCol: 76, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
},
},
{
name: "same start",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 76, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
},
},
{
name: "same start col",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 19, EndCol: 22, NumStmt: 2, Count: 0},
},
},
{
name: "same end line",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 22, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
},
},
{
name: "same end line and col",
blocks: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
},
expected: []cover.ProfileBlock{
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
{StartLine: 17, StartCol: 32, EndLine: 17, EndCol: 23, NumStmt: 2, Count: 0},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := orderedBlocks(tc.blocks)
sort.Sort(actual)
if len(actual) != len(tc.expected) {
t.Fatal("len wrong", tc.expected, actual)
}
for i, v := range tc.expected {
if v != actual[i] {
t.Error("order fail", i, v, actual[i])
}
}
})
}
}
func sp(s string) *string {
return &s
}
func ps(sp *string) string {
if sp == nil {
return ""
}
return *sp
}
func TestLoadProfilesForFiles(t *testing.T) {
// test loadProfilesForFiles
testCases := []struct {
name string
files []string
expected []*cover.Profile
err *string
}{
{
name: "missing",
files: []string{"testdata/notfound.out"},
err: sp("open testdata/notfound.out: no such file or directory"),
},
{
name: "one",
files: []string{"testdata/cover_1.out"},
expected: newProfileOne(),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := loadProfilesForFiles(tc.files)
if err != nil {
if tc.err == nil || err.Error() != *tc.err {
t.Fatalf("unexpected:\n%s\n%s\n", ps(tc.err), err)
}
} else if tc.err != nil {
t.Fatal("expected", err)
}
if len(actual) != len(tc.expected) {
t.Errorf("len wrong %v %+v\n", tc.expected, actual)
}
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("wrong %v %+v\n", tc.expected, actual[0])
}
})
}
}
func TestProcessArgsFiles(t *testing.T) {
args := []string{
"app",
"testdata/cover_1.out",
"testdata/cover_2.out",
}
p, err := processArgs(args, nil)
if err != nil {
t.Error("unexpected err", err)
}
if len(p) != 2 {
t.Error("profiles len != 2", len(p))
}
}
func TestProcessArgsFilesNone(t *testing.T) {
args := []string{
"app",
}
p, err := processArgs(args, nil)
if !errors.Is(err, errHelp) {
t.Error("unexpected not errHelp", err)
}
if len(p) != 0 {
t.Error("profiles len != 0", len(p))
}
}
func TestProcessArgsStdIn(t *testing.T) {
f, err := os.Open("testdata/cover_2.out")
if err != nil {
t.Fatal("open file", err)
}
defer f.Close()
args := []string{
"app",
"-",
}
p, err := processArgs(args, f)
if err != nil {
t.Error("unexpected err", err)
}
if len(p) != 1 {
t.Error("profiles len != 1", len(p))
}
}
func TestOverlaps(t *testing.T) {
// test overlaps
testCases := []struct {
name string
b1 cover.ProfileBlock
b2 cover.ProfileBlock
expected bool
}{
{
name: "none",
expected: true,
},
{
name: "same",
b1: cover.ProfileBlock{StartLine: 1, StartCol: 1, EndLine: 1, EndCol: 1},
b2: cover.ProfileBlock{StartLine: 1, StartCol: 1, EndLine: 1, EndCol: 1},
expected: true,
},
{
name: "adjacent",
b1: cover.ProfileBlock{StartLine: 1, StartCol: 1, EndLine: 1, EndCol: 1},
b2: cover.ProfileBlock{StartLine: 1, StartCol: 2, EndLine: 1, EndCol: 2},
expected: false,
},
{
name: "overlap",
b1: cover.ProfileBlock{StartLine: 1, StartCol: 1, EndLine: 1, EndCol: 2},
b2: cover.ProfileBlock{StartLine: 1, StartCol: 2, EndLine: 1, EndCol: 3},
expected: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := overlaps(&tc.b1, &tc.b2)
if actual != tc.expected {
t.Error("overlaps fail", tc.expected, actual)
}
})
}
}
func TestDeDuplicate(t *testing.T) {
disjoint := append(newProfileOne(), newDisjoint()...)
disjointExpected := append(newCombined("github.com/repo/gocovdedup/alt.go"), newCombined("")...)
overlapped := append(newProfileOne(), newProfileOne()...)
// Test the deduplication logic
testCases := []struct {
name string
profiles []*cover.Profile
expected []*cover.Profile
}{
{
name: "empty",
expected: []*cover.Profile{},
},
{
name: "combined",
profiles: newProfileOne(),
expected: newCombined(""),
},
{
name: "disjoint",
profiles: disjoint,
expected: disjointExpected,
},
{
name: "overlapped",
profiles: overlapped,
expected: newCombined(""),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := deDuplicate(tc.profiles)
if len(actual) != len(tc.expected) {
t.Fatal("len wrong", tc.expected, actual)
}
for i, v := range tc.expected {
if !reflect.DeepEqual(v, actual[i]) {
t.Errorf("order fail %d\n%v\n%v", i, v, actual[i])
}
}
})
}
}