-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverage_test.go
79 lines (65 loc) · 1.83 KB
/
coverage_test.go
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
package coverage
import (
"strings"
"testing"
)
func mockExit(t *testing.T, code int) func(int) {
t.Helper()
return func(c int) {
if code != c {
t.Errorf("[expected]: %d \n[received]:%d\n", code, c)
}
}
}
type mockTestingM struct {
code int
}
func (m mockTestingM) Run() int {
return m.code
}
func TestCoverage(t *testing.T) {
t.Run("should run tests successfully", func(t *testing.T) {
exitFunc = mockExit(t, 0)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}
Run(m, 100)
})
t.Run("should fail if no cover enabled", func(t *testing.T) {
exitFunc = mockExit(t, 1)
coverModeFunc = func() string { return "" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}
s := strings.Builder{}
writer = &s
Run(m, 100)
if s.String() != "\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n" {
t.Errorf("expected coverage not enabled msg but got: %s\f", s.String())
}
})
t.Run("should fail if coverage is not met", func(t *testing.T) {
exitFunc = mockExit(t, 1)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 0.5 }
m := mockTestingM{code: 0}
s := strings.Builder{}
writer = &s
Run(m, 51)
if s.String() != "\nFAIL Coverage threshold not met 51.0 >= 50.0 for coverage.TestCoverage\n\n" {
t.Errorf("expected coverage threshold error but got: %s\n", s.String())
}
})
t.Run("should call callback funcs", func(t *testing.T) {
exitFunc = mockExit(t, 0)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}
called := 0
Run(m, 100, func() {
called++
})
if called != 1 {
t.Errorf("callback expected to be called once but called: %d\n", called)
}
})
}