-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_mock.go
60 lines (54 loc) · 1.34 KB
/
testing_mock.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
package expect
import (
"sync/atomic"
"testing"
)
// TMock is a mock implementation of the T
// interface.
type TMock struct {
T *testing.T
HelperStub func()
HelperCalled int32
ErrorfStub func(format string, args ...any)
ErrorfCalled int32
FatalfStub func(format string, args ...any)
FatalfCalled int32
}
// Verify that *TMock implements T.
var _ T = &TMock{}
// Helper is a stub for the T.Helper
// method that records the number of times it has been called.
func (m *TMock) Helper() {
atomic.AddInt32(&m.HelperCalled, 1)
if m.HelperStub == nil {
if m.T != nil {
m.T.Error("HelperStub is nil")
}
panic("Helper unimplemented")
}
m.HelperStub()
}
// Errorf is a stub for the T.Errorf
// method that records the number of times it has been called.
func (m *TMock) Errorf(format string, args ...any) {
atomic.AddInt32(&m.ErrorfCalled, 1)
if m.ErrorfStub == nil {
if m.T != nil {
m.T.Error("ErrorfStub is nil")
}
panic("Errorf unimplemented")
}
m.ErrorfStub(format, args...)
}
// Fatalf is a stub for the T.Fatalf
// method that records the number of times it has been called.
func (m *TMock) Fatalf(format string, args ...any) {
atomic.AddInt32(&m.FatalfCalled, 1)
if m.FatalfStub == nil {
if m.T != nil {
m.T.Error("FatalfStub is nil")
}
panic("Fatalf unimplemented")
}
m.FatalfStub(format, args...)
}