-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
269 lines (243 loc) · 4.82 KB
/
testing.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gotestingmock
import (
"runtime"
"sync"
"testing"
)
// TB is mock for testing.TB.
// XxxFunc is a mock function for the method Xxx of testing.TB.
// If you confirm more usage please see [Run].
type TB struct {
record Record
// mock funcs
CleanupFunc func(func())
ErrorFunc func(args ...any)
ErrorfFunc func(format string, args ...any)
FailFunc func()
FailNowFunc func()
FailedFunc func() bool
FatalFunc func(args ...any)
FatalfFunc func(format string, args ...any)
HelperFunc func()
LogFunc func(args ...any)
LogfFunc func(format string, args ...any)
NameFunc func() string
SetenvFunc func(key, value string)
SkipFunc func(args ...any)
SkipNowFunc func()
SkipfFunc func(format string, args ...any)
SkippedFunc func() bool
TempDirFunc func() string
testing.TB // for default behavior andd private method
}
// Record records the result of [Run].
type Record struct {
Failed bool
Skipped bool
Goexit bool
PanicValue any
}
// Run runs the given mocking test function with [testing.TB].
// The f can be described in the same way as the test function of the go test.
// Run call the f on new goroutine.
// The return value records whether the test function failed (e.g. t.Error),
// was skipped (e.g. t.Skip), failed and exited its goroutine (e.g. t.Fatal)
// or panic occured.
func Run(f func(*TB)) *Record {
var (
tb TB
wg sync.WaitGroup
ret *Record
)
wg.Add(1)
go func() {
defer func() {
if p := recover(); p != nil {
tb.record.PanicValue = p
}
record := tb.record // copy
ret = &record
wg.Done()
}()
f(&tb)
}()
wg.Wait()
return ret
}
func (tb *TB) Cleanup(f func()) {
switch {
case tb.CleanupFunc != nil:
tb.CleanupFunc(f)
case tb.TB != nil:
tb.TB.Cleanup(f)
}
}
func (tb *TB) Error(args ...any) {
tb.record.Failed = true
switch {
case tb.ErrorFunc != nil:
tb.ErrorFunc(args...)
case tb.TB != nil:
tb.TB.Error(args...)
}
}
func (tb *TB) Errorf(format string, args ...any) {
tb.record.Failed = true
switch {
case tb.ErrorfFunc != nil:
tb.ErrorfFunc(format, args...)
case tb.TB != nil:
tb.TB.Errorf(format, args...)
}
}
func (tb *TB) Fail() {
tb.record.Failed = true
switch {
case tb.FailFunc != nil:
tb.FailFunc()
case tb.TB != nil:
tb.TB.Fail()
}
}
func (tb *TB) FailNow() {
tb.record.Failed = true
tb.record.Goexit = true
switch {
case tb.FailNowFunc != nil:
tb.FailNowFunc()
runtime.Goexit()
case tb.TB != nil:
tb.TB.FailNow()
default:
runtime.Goexit()
}
}
func (tb *TB) Failed() bool {
switch {
case tb.FailedFunc != nil:
return tb.FailedFunc()
case tb.TB != nil:
return tb.TB.Failed()
default:
return tb.record.Failed
}
}
func (tb *TB) Fatal(args ...any) {
tb.record.Failed = true
tb.record.Goexit = true
switch {
case tb.FatalFunc != nil:
tb.FatalFunc(args...)
runtime.Goexit()
case tb.TB != nil:
tb.TB.Fatal(args...)
default:
runtime.Goexit()
}
}
func (tb *TB) Fatalf(format string, args ...any) {
tb.record.Failed = true
tb.record.Goexit = true
switch {
case tb.FatalfFunc != nil:
tb.FatalfFunc(format, args...)
runtime.Goexit()
case tb.TB != nil:
tb.TB.Fatalf(format, args...)
default:
runtime.Goexit()
}
}
func (tb *TB) Helper() {
switch {
case tb.HelperFunc != nil:
tb.HelperFunc()
case tb.TB != nil:
tb.TB.Helper()
}
}
func (tb *TB) Log(args ...any) {
switch {
case tb.LogFunc != nil:
tb.LogFunc(args...)
case tb.TB != nil:
tb.TB.Log(args...)
}
}
func (tb *TB) Logf(format string, args ...any) {
switch {
case tb.LogfFunc != nil:
tb.LogfFunc(format, args...)
case tb.TB != nil:
tb.TB.Logf(format, args...)
}
}
func (tb *TB) Name() string {
switch {
case tb.NameFunc != nil:
return tb.NameFunc()
case tb.TB != nil:
return tb.TB.Name()
default:
return ""
}
}
func (tb *TB) Setenv(key, value string) {
switch {
case tb.SetenvFunc != nil:
tb.SetenvFunc(key, value)
case tb.TB != nil:
tb.TB.Setenv(key, value)
}
}
func (tb *TB) Skip(args ...any) {
tb.record.Skipped = true
switch {
case tb.SkipFunc != nil:
tb.SkipFunc(args...)
case tb.TB != nil:
tb.TB.Skip(args...)
}
}
func (tb *TB) SkipNow() {
tb.record.Skipped = true
tb.record.Goexit = true
switch {
case tb.SkipNowFunc != nil:
tb.SkipNowFunc()
runtime.Goexit()
case tb.TB != nil:
tb.TB.SkipNow()
default:
runtime.Goexit()
}
}
func (tb *TB) Skipf(format string, args ...any) {
tb.record.Skipped = true
switch {
case tb.SkipfFunc != nil:
tb.SkipfFunc(format, args...)
case tb.TB != nil:
tb.TB.Skipf(format, args...)
}
}
func (tb *TB) Skipped() bool {
switch {
case tb.SkippedFunc != nil:
return tb.SkippedFunc()
case tb.TB != nil:
return tb.TB.Skipped()
default:
return tb.record.Skipped
}
}
func (tb *TB) TempDir() string {
switch {
case tb.TempDirFunc != nil:
return tb.TempDirFunc()
case tb.TB != nil:
return tb.TB.TempDir()
default:
return ""
}
}