-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
setup_test.go
111 lines (89 loc) · 1.78 KB
/
setup_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package got_test
import (
"fmt"
"sync"
"testing"
"github.com/ysmood/gop"
"github.com/ysmood/got"
)
var setup = got.Setup(func(g got.G) {
g.Parallel()
})
var _ got.Testable = &mock{}
type mock struct {
sync.Mutex
t *testing.T
failed bool
skipped bool
msg string
cleanupList []func()
recover bool
name string
}
func (m *mock) Name() string {
if m.name == "" {
return "mock"
}
return m.name
}
func (m *mock) Skipped() bool { return m.skipped }
func (m *mock) Failed() bool { return m.failed }
func (m *mock) Helper() {}
func (m *mock) Cleanup(f func()) { m.cleanupList = append([]func(){f}, m.cleanupList...) }
func (m *mock) SkipNow() {}
func (m *mock) Fail() { m.failed = true }
func (m *mock) FailNow() {
m.Lock()
defer m.Unlock()
m.failed = true
if !m.recover {
panic("fail now")
}
m.recover = false
}
func (m *mock) Logf(format string, args ...interface{}) {
m.Lock()
defer m.Unlock()
if m.msg != "" {
m.msg += "\n"
}
m.msg += fmt.Sprintf(format, args...)
}
func (m *mock) Run(_ string, fn func(*mock)) {
fn(m)
}
func (m *mock) cleanup() {
for _, f := range m.cleanupList {
f()
}
m.cleanupList = nil
}
func (m *mock) check(expected string) {
m.t.Helper()
m.checkWithStyle(false, expected)
}
func (m *mock) reset() {
m.Lock()
defer m.Unlock()
m.failed = false
m.msg = ""
}
func (m *mock) checkWithStyle(visualizeStyle bool, expected string) {
m.Lock()
defer m.Unlock()
m.t.Helper()
if !m.failed {
m.t.Error("should fail")
}
msg := ""
if visualizeStyle {
msg = gop.VisualizeANSI(m.msg)
} else {
msg = gop.StripANSI(m.msg)
}
if msg != expected {
m.t.Errorf("\n\n[[[msg]]]\n\n%s\n\n[[[doesn't equal]]]\n\n%s\n\n", msg, expected)
}
m.failed = false
m.msg = ""
}