-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_test.go
182 lines (148 loc) · 3.9 KB
/
testing_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
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
package gotestingmock_test
import (
"reflect"
"strings"
"testing"
"github.com/newmo-oss/gotestingmock"
)
func TestTB_Fields(t *testing.T) {
t.Parallel()
typ := reflect.TypeFor[gotestingmock.TB]()
for i := range typ.NumField() {
ft := typ.Field(i)
if !strings.HasSuffix(ft.Name, "Func") &&
ft.Type.Kind() != reflect.Func {
continue
}
t.Run(ft.Name, func(t *testing.T) {
t.Parallel()
method := strings.TrimSuffix(ft.Name, "Func")
var call bool
rec := gotestingmock.Run(func(tb *gotestingmock.TB) {
v := reflect.ValueOf(tb)
fv := v.Elem().Field(i)
/*
tb.XxxFunc = func() {
call = true
}
tb.Xxx()
*/
fv.Set(reflect.MakeFunc(ft.Type, func([]reflect.Value) []reflect.Value {
call = true
ret := make([]reflect.Value, fv.Type().NumOut())
for i := range fv.Type().NumOut() {
ret[i] = reflect.New(fv.Type().Out(i)).Elem()
}
return ret
}))
callWithZeros(v.MethodByName(method))
})
if rec.PanicValue != nil {
t.Fatal("unexpected panic:", rec.PanicValue)
}
if !call {
t.Errorf("Field %s did not call with %s", ft.Name, method)
}
})
}
}
func TestTB_DefaultMethod(t *testing.T) {
t.Parallel()
typ := reflect.TypeFor[gotestingmock.TB]()
for i := range typ.NumField() {
ft := typ.Field(i)
if !strings.HasSuffix(ft.Name, "Func") &&
ft.Type.Kind() != reflect.Func {
continue
}
t.Run(ft.Name, func(t *testing.T) {
t.Parallel()
method := strings.TrimSuffix(ft.Name, "Func")
var call bool
rec := gotestingmock.Run(func(parent *gotestingmock.TB) {
tb := &gotestingmock.TB{TB: parent}
pv := reflect.ValueOf(parent)
pfv := pv.Elem().Field(i)
/*
parent.XxxFunc = func() {
call = true
}
parent.Xxx()
*/
pfv.Set(reflect.MakeFunc(ft.Type, func([]reflect.Value) []reflect.Value {
call = true
ret := make([]reflect.Value, pfv.Type().NumOut())
for i := range pfv.Type().NumOut() {
ret[i] = reflect.New(pfv.Type().Out(i)).Elem()
}
return ret
}))
v := reflect.ValueOf(tb)
callWithZeros(v.MethodByName(method))
})
if rec.PanicValue != nil {
t.Fatal("unexpected panic:", rec.PanicValue)
}
if !call {
t.Errorf("(*gotestingmock.TB).%[1]s did not call with (testing.TB).%[1]s", method)
}
})
}
}
func TestRecord(t *testing.T) {
t.Parallel()
cases := []struct {
method string
wantSkipped bool
wantFailed bool
wantGoexit bool
}{
{"Error", false, true, false},
{"Errorf", false, true, false},
{"Fail", false, true, false},
{"FailNow", false, true, true},
{"Fatal", false, true, true},
{"Fatalf", false, true, true},
{"Skip", true, false, false},
{"SkipNow", true, false, true},
{"Skipf", true, false, false},
}
for _, tt := range cases {
t.Run(tt.method, func(t *testing.T) {
t.Parallel()
rec := gotestingmock.Run(func(tb *gotestingmock.TB) {
v := reflect.ValueOf(tb)
callWithZeros(v.MethodByName(tt.method))
})
if rec.PanicValue != nil {
t.Fatal("unexpected panic:", rec.PanicValue)
}
if got := rec.Skipped; got != tt.wantSkipped {
t.Errorf("Skipped does not match: (got, want) = (%v, %v)", got, tt.wantSkipped)
}
if got := rec.Failed; got != tt.wantFailed {
t.Errorf("Failed does not match: (got, want) = (%v, %v)", got, tt.wantFailed)
}
if got := rec.Goexit; got != tt.wantGoexit {
t.Errorf("Goexit does not match: (got, want) = (%v, %v)", got, tt.wantGoexit)
}
})
}
}
func TestRecord_PanicValue(t *testing.T) {
t.Parallel()
want := "panic"
rec := gotestingmock.Run(func(tb *gotestingmock.TB) {
panic(want)
})
if got := rec.PanicValue; got != want {
t.Errorf("PanicValue does not match: (got, want) = (%v, %v)", got, want)
}
}
func callWithZeros(v reflect.Value) {
in := make([]reflect.Value, v.Type().NumIn())
for i := range v.Type().NumIn() {
in[i] = reflect.New(v.Type().In(i)).Elem()
}
v.Call(in)
}