1
16
17 package cov_test
18
19 import (
20 "golang.org/x/tools/cover"
21
22 "edge-infra.dev/third_party/gopherage/pkg/cov"
23
24 "reflect"
25 "testing"
26 )
27
28 func TestDiffProfilesBasicDiff(t *testing.T) {
29 a := []*cover.Profile{
30 {
31 FileName: "a.go",
32 Mode: "count",
33 Blocks: []cover.ProfileBlock{
34 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
35 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
36 },
37 },
38 }
39 b := []*cover.Profile{
40 {
41 FileName: "a.go",
42 Mode: "count",
43 Blocks: []cover.ProfileBlock{
44 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
45 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
46 },
47 },
48 }
49
50 result, err := cov.DiffProfiles(a, b)
51 if err != nil {
52 t.Fatalf("DiffProfiles failed: %v", err)
53 }
54
55 expected := []*cover.Profile{
56 {
57 FileName: "a.go",
58 Mode: "count",
59 Blocks: []cover.ProfileBlock{
60 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 4},
61 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 0},
62 },
63 },
64 }
65
66 if !reflect.DeepEqual(result, expected) {
67 t.Fatal("diffed profile incorrect")
68 }
69 }
70
71 func TestDiffProfilesWrongFileName(t *testing.T) {
72 a := []*cover.Profile{
73 {
74 FileName: "a.go",
75 Mode: "count",
76 Blocks: []cover.ProfileBlock{
77 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
78 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
79 },
80 },
81 }
82 b := []*cover.Profile{
83 {
84 FileName: "b.go",
85 Mode: "count",
86 Blocks: []cover.ProfileBlock{
87 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
88 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
89 },
90 },
91 }
92 if _, err := cov.DiffProfiles(a, b); err == nil {
93 t.Fatal("expected DiffProfiles to fail when diffing mismatched files")
94 }
95 }
96
97 func TestDiffProfilesWrongFileCount(t *testing.T) {
98 a := []*cover.Profile{
99 {
100 FileName: "a.go",
101 Mode: "count",
102 Blocks: []cover.ProfileBlock{
103 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
104 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
105 },
106 },
107 {
108 FileName: "b.go",
109 Mode: "count",
110 Blocks: []cover.ProfileBlock{
111 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3},
112 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
113 },
114 },
115 }
116 b := []*cover.Profile{
117 {
118 FileName: "a.go",
119 Mode: "count",
120 Blocks: []cover.ProfileBlock{
121 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7},
122 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2},
123 },
124 },
125 }
126 if _, err := cov.DiffProfiles(a, b); err == nil {
127 t.Fatal("expected DiffProfiles to fail when diffing mismatched profiles")
128 }
129 }
130
View as plain text