-
Notifications
You must be signed in to change notification settings - Fork 1
/
testhelper_test.go
98 lines (85 loc) · 1.66 KB
/
testhelper_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
package jaws
import (
"reflect"
"testing"
"time"
)
type testHelper struct {
*time.Timer
*testing.T
}
func newTestHelper(t *testing.T) (th *testHelper) {
th = &testHelper{
T: t,
Timer: time.NewTimer(time.Second * 3),
}
t.Cleanup(th.Cleanup)
return
}
func (th *testHelper) Cleanup() {
th.Timer.Stop()
}
func (th *testHelper) Equal(a, b any) {
if !testEqual(a, b) {
th.Helper()
th.Errorf("%T(%v) != %T(%v)", a, a, b, b)
}
}
func (th *testHelper) True(a bool) {
if !a {
th.Helper()
th.Error("not true")
}
}
func (th *testHelper) NoErr(err error) {
if err != nil {
th.Helper()
th.Error(err)
}
}
func (th *testHelper) Timeout() {
th.Helper()
th.Fatal("timeout")
}
func Test_testHelper(t *testing.T) {
mustEqual := func(a, b any) {
if !testEqual(a, b) {
t.Helper()
t.Errorf("%#v != %#v", a, b)
}
}
mustNotEqual := func(a, b any) {
if testEqual(a, b) {
t.Helper()
t.Errorf("%#v == %#v", a, b)
}
}
mustEqual(1, 1)
mustEqual(nil, nil)
mustEqual(nil, (*testHelper)(nil))
mustNotEqual(1, nil)
mustNotEqual(nil, 1)
mustNotEqual((*testing.T)(nil), 1)
mustNotEqual(1, 2)
mustNotEqual((*testing.T)(nil), (*testHelper)(nil))
mustNotEqual(int(1), int32(1))
}
func testNil(object any) (bool, reflect.Type) {
if object == nil {
return true, nil
}
value := reflect.ValueOf(object)
kind := value.Kind()
return kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil(), value.Type()
}
func testEqual(a, b any) bool {
if reflect.DeepEqual(a, b) {
return true
}
aIsNil, aType := testNil(a)
bIsNil, bType := testNil(b)
if !(aIsNil && bIsNil) {
return false
}
return aType == nil || bType == nil || (aType == bType)
}