-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockingt_test.go
66 lines (55 loc) · 1.05 KB
/
mockingt_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
package be_test
import (
"fmt"
"runtime"
"sync"
"testing"
)
type mockingT struct {
testing.T
m sync.Mutex
hasFailed bool
cleanups []func()
}
func (m *mockingT) setFailed(b bool) {
m.m.Lock()
defer m.m.Unlock()
m.hasFailed = b
}
func (m *mockingT) failed() bool {
m.m.Lock()
defer m.m.Unlock()
return m.hasFailed
}
func (m *mockingT) Run(name string, f func(t *testing.T)) {
m.cleanups = nil
m.setFailed(false)
ch := make(chan struct{})
defer func() {
for _, f := range m.cleanups {
defer f()
}
}()
// Use a goroutine so Fatalf can call Goexit
go func() {
defer close(ch)
f(&m.T)
}()
<-ch
}
func (m *mockingT) Cleanup(f func()) {
m.cleanups = append(m.cleanups, f)
}
func (*mockingT) Log(args ...any) {
fmt.Println(args...)
}
func (*mockingT) Helper() {}
func (m *mockingT) Fatalf(format string, args ...any) {
m.Errorf(format, args...)
runtime.Goexit()
}
func (m *mockingT) Errorf(format string, args ...any) {
m.setFailed(true)
fmt.Printf(format+"\n", args...)
}
func (m *mockingT) Failed() bool { return m.failed() }