-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest.go
More file actions
425 lines (379 loc) · 11.4 KB
/
Copy pathtest.go
File metadata and controls
425 lines (379 loc) · 11.4 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package zlsgo
import (
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
)
const unableCallerInfo = "<Unable to get information>"
// testReporter defines the minimal reporting methods used by TestUtil.
type testReporter interface {
Helper()
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Fatal(args ...interface{})
}
// TestUtil wraps testing.TB and provides lightweight assertions, error checks,
// logging helpers, and subtest helpers.
//
// Use NewTest to create a helper in unit tests:
//
// tt := zlsgo.NewTest(t)
// tt.Equal(1, 1)
// tt.NoError(err)
// tt.Len(3, []int{1, 2, 3})
// tt.Run("sub", func(tt *zlsgo.TestUtil) {
// tt.EqualTrue(true)
// })
//
// Assertions cover equality, nil and error checks, string containment,
// length checks, and panic checks.
// Passing true in exit makes an assertion fail the current test immediately.
// Methods T, Run, and Parallel require the underlying value to be *testing.T.
type TestUtil struct {
tb testing.TB
reporter testReporter
}
// NewTest creates a TestUtil from testing.TB.
//
// The returned helper uses the testing logger for assertion output and is
// intended for test assertions plus lightweight subtest orchestration.
// Methods T, Run, and Parallel require the underlying value to be *testing.T.
func NewTest(t testing.TB) *TestUtil {
return newTestUtil(t, t)
}
// newTestUtil builds a TestUtil with a custom reporter.
func newTestUtil(tb testing.TB, reporter testReporter) *TestUtil {
return &TestUtil{tb: tb, reporter: reporter}
}
// GetCallerInfo returns the file name and line number of the test caller.
func (u *TestUtil) GetCallerInfo() string {
for depth := 1; depth < 20; depth++ {
_, file, line, ok := runtime.Caller(depth)
if !ok {
break
}
if !strings.HasSuffix(file, "_test.go") {
continue
}
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
return unableCallerInfo
}
// Equal compares expected and actual values with reflect.DeepEqual.
// Passing true in exit will stop the current test immediately on failure.
func (u *TestUtil) Equal(expected, actual interface{}, exit ...bool) bool {
if valuesEqual(expected, actual) {
return true
}
return u.failAssertion(exit,
"%s 期待:%v (type %s) - 结果:%v (type %s)",
u.PrintMyName(), expected, valueType(expected), actual, valueType(actual),
)
}
// NoEqual compares expected and actual values and asserts they are not equal.
func (u *TestUtil) NoEqual(expected, actual interface{}, exit ...bool) bool {
if !valuesEqual(expected, actual) {
return true
}
return u.failAssertion(exit,
"%s 期待不等于:%v (type %s) - 结果:%v (type %s)",
u.PrintMyName(), expected, valueType(expected), actual, valueType(actual),
)
}
// EqualTrue asserts that the actual value is true.
func (u *TestUtil) EqualTrue(actual interface{}, exit ...bool) {
u.Equal(true, actual, exit...)
}
// EqualFalse asserts that the actual value is false.
func (u *TestUtil) EqualFalse(actual interface{}, exit ...bool) {
u.Equal(false, actual, exit...)
}
// True asserts that the actual boolean value is true.
func (u *TestUtil) True(actual bool, exit ...bool) bool {
if actual {
return true
}
return u.failAssertion(exit, "%s 期待:true - 结果:false", u.PrintMyName())
}
// False asserts that the actual boolean value is false.
func (u *TestUtil) False(actual bool, exit ...bool) bool {
if !actual {
return true
}
return u.failAssertion(exit, "%s 期待:false - 结果:true", u.PrintMyName())
}
// EqualNil asserts that the actual value is nil.
func (u *TestUtil) EqualNil(actual interface{}, exit ...bool) {
u.IsNil(actual, exit...)
}
// NoError asserts that err is nil and reports failures through testing output.
func (u *TestUtil) NoError(err error, exit ...bool) bool {
if err == nil {
return true
}
return u.failAssertion(exit, "%s Error: %v", u.PrintMyName(), err)
}
// Error asserts that err is not nil.
func (u *TestUtil) Error(err error, exit ...bool) bool {
if err != nil {
return true
}
return u.failAssertion(exit, "%s 期待 error 不为 nil", u.PrintMyName())
}
// ErrorContains asserts that err is not nil and its message contains expected.
func (u *TestUtil) ErrorContains(expected string, err error, exit ...bool) bool {
if err == nil {
return u.failAssertion(exit, "%s 期待 error 包含:%q - 结果:nil", u.PrintMyName(), expected)
}
if strings.Contains(err.Error(), expected) {
return true
}
return u.failAssertion(exit,
"%s 期待 error 包含:%q - 结果:%q",
u.PrintMyName(), expected, err.Error(),
)
}
// EqualExit compares expected and actual values and immediately fails the test if not equal.
func (u *TestUtil) EqualExit(expected, actual interface{}) {
u.Equal(expected, actual, true)
}
// Contains asserts that actual contains expected.
func (u *TestUtil) Contains(expected, actual string, exit ...bool) bool {
if strings.Contains(actual, expected) {
return true
}
return u.failAssertion(exit,
"%s 期待包含:%q - 结果:%q",
u.PrintMyName(), expected, actual,
)
}
// NotContains asserts that actual does not contain expected.
func (u *TestUtil) NotContains(expected, actual string, exit ...bool) bool {
if !strings.Contains(actual, expected) {
return true
}
return u.failAssertion(exit,
"%s 期待不包含:%q - 结果:%q",
u.PrintMyName(), expected, actual,
)
}
// Len asserts that the actual value length matches the expected length.
func (u *TestUtil) Len(expected int, actual interface{}, exit ...bool) bool {
length, ok := lengthOf(actual)
if !ok {
return u.failAssertion(exit, "%s 无法获取长度 (type %s)", u.PrintMyName(), valueType(actual))
}
if length == expected {
return true
}
return u.failAssertion(exit,
"%s 期待长度:%d - 结果:%d (type %s)",
u.PrintMyName(), expected, length, valueType(actual),
)
}
// Panics asserts that fn panics.
func (u *TestUtil) Panics(fn func(), exit ...bool) bool {
if fn == nil {
return u.failAssertion(exit, "%s 期待 panic - 结果:函数为 nil", u.PrintMyName())
}
panicked, recovered := catchPanic(fn)
if panicked {
return true
}
return u.failAssertion(exit, "%s 期待 panic - 结果:未发生 panic (%v)", u.PrintMyName(), recovered)
}
// NotPanics asserts that fn does not panic.
func (u *TestUtil) NotPanics(fn func(), exit ...bool) bool {
if fn == nil {
return u.failAssertion(exit, "%s 期待不 panic - 结果:函数为 nil", u.PrintMyName())
}
panicked, recovered := catchPanic(fn)
if !panicked {
return true
}
return u.failAssertion(exit, "%s 期待不 panic - 结果:%v", u.PrintMyName(), recovered)
}
// Log logs the given values to the test output.
func (u *TestUtil) Log(v ...interface{}) {
u.reporter.Helper()
u.reporter.Log(v...)
}
// Logf logs the formatted string to the test output.
func (u *TestUtil) Logf(format string, args ...interface{}) {
u.reporter.Helper()
u.reporter.Logf(format, args...)
}
// Fatal logs the given values to the test output and immediately fails the test.
func (u *TestUtil) Fatal(v ...interface{}) {
u.reporter.Helper()
u.reporter.Fatal(v...)
}
// PrintMyName returns the caller information for the current test.
func (u *TestUtil) PrintMyName() string {
return u.GetCallerInfo()
}
// Run runs a subtest with the given name and function.
// It is only available when the underlying testing object is *testing.T.
func (u *TestUtil) Run(name string, f func(tt *TestUtil)) {
u.reporter.Helper()
t := u.requireTestingT("Run")
if t == nil {
return
}
t.Run(name, func(t *testing.T) {
f(NewTest(t))
})
}
// T returns the underlying *testing.T object.
// It fails immediately if TestUtil was not created from *testing.T.
func (u *TestUtil) T() *testing.T {
return u.requireTestingT("T")
}
// IsNil asserts that the actual value is nil.
func (u *TestUtil) IsNil(actual interface{}, exit ...bool) bool {
if isNilValue(actual) {
return true
}
return u.failAssertion(exit,
"%s 期待:nil - 结果:%v (type %s)",
u.PrintMyName(), actual, valueType(actual),
)
}
// NotNil asserts that the actual value is not nil.
func (u *TestUtil) NotNil(actual interface{}, exit ...bool) bool {
if !isNilValue(actual) {
return true
}
return u.failAssertion(exit, "%s 期待非 nil (type %s)", u.PrintMyName(), valueType(actual))
}
// Parallel marks the test as a parallel test.
// It is only available when the underlying testing object is *testing.T.
func (u *TestUtil) Parallel() {
u.reporter.Helper()
t := u.requireTestingT("Parallel")
if t == nil {
return
}
t.Parallel()
}
// TestCase represents a test case with a name and arbitrary data.
type TestCase struct {
Data interface{}
Name string
}
// RunTests runs a series of named test cases through subtests.
func (u *TestUtil) RunTests(tests []TestCase, testFunc func(tt *TestUtil, tc TestCase)) {
u.reporter.Helper()
for _, tc := range tests {
tc := tc
u.Run(tc.Name, func(tt *TestUtil) {
testFunc(tt, tc)
})
}
}
// ErrorTestCase represents a test case with error expectations.
type ErrorTestCase struct {
Input interface{}
Expected interface{}
Name string
WantErr bool
}
// RunErrorTests runs test cases for functions returning (result, error).
func (u *TestUtil) RunErrorTests(
tests []ErrorTestCase,
testFunc func(input interface{}) (interface{}, error),
) {
u.reporter.Helper()
for _, tc := range tests {
tc := tc
u.Run(tc.Name, func(tt *TestUtil) {
result, err := testFunc(tc.Input)
if tc.WantErr {
tt.Error(err, true)
return
}
if !tt.NoError(err, true) {
return
}
tt.Equal(tc.Expected, result)
})
}
}
// failAssertion reports an assertion failure and respects exit behavior.
func (u *TestUtil) failAssertion(exit []bool, format string, args ...interface{}) bool {
u.reporter.Helper()
if shouldExit(exit) {
u.reporter.Fatalf(format, args...)
return false
}
u.reporter.Errorf(format, args...)
return false
}
// requireTestingT returns the underlying *testing.T when available.
func (u *TestUtil) requireTestingT(method string) *testing.T {
t, ok := u.tb.(*testing.T)
if ok {
return t
}
u.reporter.Helper()
u.reporter.Fatalf("%s 仅支持 *testing.T,当前类型:%T", method, u.tb)
return nil
}
// shouldExit reports whether the assertion should stop the test.
func shouldExit(exit []bool) bool {
return len(exit) > 0 && exit[0]
}
// valueType returns the reflected type name of v.
func valueType(v interface{}) string {
t := reflect.TypeOf(v)
if t == nil {
return "<nil>"
}
return t.String()
}
// valuesEqual compares two values with reflect.DeepEqual.
func valuesEqual(expected, actual interface{}) bool {
return reflect.DeepEqual(expected, actual)
}
// isNilValue reports whether v is nil or a typed nil value.
func isNilValue(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
return rv.IsNil()
default:
return false
}
}
// lengthOf returns the length of supported collection values.
func lengthOf(v interface{}) (int, bool) {
rv := reflect.ValueOf(v)
if !rv.IsValid() {
return 0, false
}
switch rv.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return rv.Len(), true
default:
return 0, false
}
}
// catchPanic runs fn and captures any panic value.
func catchPanic(fn func()) (panicked bool, recovered interface{}) {
panicked = true
defer func() {
recovered = recover()
}()
fn()
panicked = false
return
}