1
2
3
4
5 package cformat_test
6
7 import (
8 "internal/coverage"
9 "internal/coverage/cformat"
10 "slices"
11 "strings"
12 "testing"
13 )
14
15 func TestBasics(t *testing.T) {
16 fm := cformat.NewFormatter(coverage.CtrModeAtomic)
17
18 mku := func(stl, enl, nx uint32) coverage.CoverableUnit {
19 return coverage.CoverableUnit{
20 StLine: stl,
21 EnLine: enl,
22 NxStmts: nx,
23 }
24 }
25 fn1units := []coverage.CoverableUnit{
26 mku(10, 11, 2),
27 mku(15, 11, 1),
28 }
29 fn2units := []coverage.CoverableUnit{
30 mku(20, 25, 3),
31 mku(30, 31, 2),
32 mku(33, 40, 7),
33 }
34 fn3units := []coverage.CoverableUnit{
35 mku(99, 100, 1),
36 }
37 fm.SetPackage("my/pack1")
38 for k, u := range fn1units {
39 fm.AddUnit("p.go", "f1", false, u, uint32(k))
40 }
41 for k, u := range fn2units {
42 fm.AddUnit("q.go", "f2", false, u, 0)
43 fm.AddUnit("q.go", "f2", false, u, uint32(k))
44 }
45 fm.SetPackage("my/pack2")
46 for _, u := range fn3units {
47 fm.AddUnit("lit.go", "f3", true, u, 0)
48 }
49
50 var b1, b2, b3, b4, b5 strings.Builder
51 if err := fm.EmitTextual(nil, &b1); err != nil {
52 t.Fatalf("EmitTextual returned %v", err)
53 }
54 wantText := strings.TrimSpace(`
55 mode: atomic
56 p.go:10.0,11.0 2 0
57 p.go:15.0,11.0 1 1
58 q.go:20.0,25.0 3 0
59 q.go:30.0,31.0 2 1
60 q.go:33.0,40.0 7 2
61 lit.go:99.0,100.0 1 0`)
62 gotText := strings.TrimSpace(b1.String())
63 if wantText != gotText {
64 t.Errorf("emit text: got:\n%s\nwant:\n%s\n", gotText, wantText)
65 }
66
67 selected := []string{"my/pack2"}
68 if err := fm.EmitTextual(selected, &b5); err != nil {
69 t.Fatalf("EmitTextual returned %v", err)
70 }
71 wantText = strings.TrimSpace(`
72 mode: atomic
73 lit.go:99.0,100.0 1 0`)
74 gotText = strings.TrimSpace(b5.String())
75 if wantText != gotText {
76 t.Errorf("emit text: got:\n%s\nwant:\n%s\n", gotText, wantText)
77 }
78
79
80 noCoverPkg := ""
81 if err := fm.EmitPercent(&b2, nil, noCoverPkg, false, false); err != nil {
82 t.Fatalf("EmitPercent returned %v", err)
83 }
84 wantPercent := strings.Fields(`
85 my/pack1 coverage: 66.7% of statements
86 my/pack2 coverage: 0.0% of statements
87 `)
88 gotPercent := strings.Fields(b2.String())
89 if !slices.Equal(wantPercent, gotPercent) {
90 t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
91 gotPercent, wantPercent)
92 }
93
94
95 withCoverPkg := " in ./..."
96 if err := fm.EmitPercent(&b3, nil, withCoverPkg, false, true); err != nil {
97 t.Fatalf("EmitPercent returned %v", err)
98 }
99 wantPercent = strings.Fields(`
100 coverage: 62.5% of statements in ./...
101 `)
102 gotPercent = strings.Fields(b3.String())
103 if !slices.Equal(wantPercent, gotPercent) {
104 t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
105 gotPercent, wantPercent)
106 }
107
108 if err := fm.EmitFuncs(&b4); err != nil {
109 t.Fatalf("EmitFuncs returned %v", err)
110 }
111 wantFuncs := strings.TrimSpace(`
112 p.go:10: f1 33.3%
113 q.go:20: f2 75.0%
114 total (statements) 62.5%`)
115 gotFuncs := strings.TrimSpace(b4.String())
116 if wantFuncs != gotFuncs {
117 t.Errorf("emit funcs: got:\n%s\nwant:\n%s\n", gotFuncs, wantFuncs)
118 }
119 if false {
120 t.Logf("text is %s\n", b1.String())
121 t.Logf("perc is %s\n", b2.String())
122 t.Logf("perc2 is %s\n", b3.String())
123 t.Logf("funcs is %s\n", b4.String())
124 }
125
126
127 {
128 var b strings.Builder
129 selpkgs := []string{"foo/bar", "my/pack1"}
130 if err := fm.EmitPercent(&b, selpkgs, noCoverPkg, false, false); err != nil {
131 t.Fatalf("EmitPercent returned %v", err)
132 }
133 wantPercent := strings.Fields(`
134 my/pack1 coverage: 66.7% of statements
135 `)
136 gotPercent := strings.Fields(b.String())
137 if !slices.Equal(wantPercent, gotPercent) {
138 t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
139 gotPercent, wantPercent)
140 }
141 }
142
143 }
144
145 func TestEmptyPackages(t *testing.T) {
146
147 fm := cformat.NewFormatter(coverage.CtrModeAtomic)
148 fm.SetPackage("my/pack1")
149 fm.SetPackage("my/pack2")
150
151
152 {
153 var b strings.Builder
154 noCoverPkg := ""
155 if err := fm.EmitPercent(&b, nil, noCoverPkg, true, false); err != nil {
156 t.Fatalf("EmitPercent returned %v", err)
157 }
158 wantPercent := strings.Fields(`
159 my/pack1 coverage: [no statements]
160 my/pack2 coverage: [no statements]
161 `)
162 gotPercent := strings.Fields(b.String())
163 if !slices.Equal(wantPercent, gotPercent) {
164 t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
165 gotPercent, wantPercent)
166 }
167 }
168
169
170 {
171 var b strings.Builder
172 noCoverPkg := ""
173 if err := fm.EmitPercent(&b, nil, noCoverPkg, true, true); err != nil {
174 t.Fatalf("EmitPercent returned %v", err)
175 }
176 wantPercent := strings.Fields(`
177 coverage: [no statements]
178 `)
179 gotPercent := strings.Fields(b.String())
180 if !slices.Equal(wantPercent, gotPercent) {
181 t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
182 gotPercent, wantPercent)
183 }
184 }
185 }
186
View as plain text