...
1
16
17 package calculation
18
19 import (
20 "testing"
21
22 "golang.org/x/tools/cover"
23 )
24
25 func TestCovList(t *testing.T) {
26 profiles := []*cover.Profile{
27 {FileName: "a", Mode: "count", Blocks: []cover.ProfileBlock{
28 {NumStmt: 5, Count: 1},
29 {NumStmt: 100, Count: 2},
30 {NumStmt: 2018},
31 },
32 },
33 {FileName: "b", Mode: "count", Blocks: []cover.ProfileBlock{
34 {NumStmt: 59, Count: 1},
35 {NumStmt: 1500, Count: 2},
36 {NumStmt: 2000},
37 },
38 },
39 {FileName: "a/c", Mode: "count", Blocks: []cover.ProfileBlock{}},
40 }
41 covList := ProduceCovList(profiles)
42
43 if len(covList.Group) == 0 {
44 t.Fatalf("covlist is empty\n")
45 }
46
47 testCases := []struct {
48 covExpected Coverage
49 covActual Coverage
50 }{
51 {Coverage{Name: "a", NumCoveredStmts: 105, NumAllStmts: 2123},
52 covList.Group[0]},
53 {Coverage{Name: "b", NumCoveredStmts: 1559, NumAllStmts: 3559},
54 covList.Group[1]},
55 {Coverage{Name: "a/c"},
56 covList.Group[2]},
57 }
58
59 for _, tc := range testCases {
60 if tc.covExpected != tc.covActual {
61 t.Fatalf("File level summarized coverage data does does match expectation: "+
62 "expected = %v; actual = %v", tc.covExpected, tc.covActual)
63 }
64 }
65
66 expected := float32(1664) / float32(5682)
67 if expected != covList.Ratio() {
68 t.Fatalf("Overall summarized coverage data does does match expectation: "+
69 "expected = %v; actual = %v", expected, covList.Ratio())
70 }
71 }
72
View as plain text