forked from razzie/mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
49 lines (42 loc) · 1.17 KB
/
mock.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
package mock
import (
"reflect"
"github.com/stretchr/testify/mock"
)
// Anything is used when the argument being tested
// shouldn't be taken into consideration.
const Anything = mock.Anything
// Mock takes a T interface type and returns a runtime generated mock implementation for it
// plus a testify mock.Mock object to control its behavior
func Mock[T any]() (T, *mock.Mock) {
typ := reflect.TypeOf((*T)(nil)).Elem()
if typ.Kind() != reflect.Interface {
panic("not an interface type: " + typ.Name())
}
mockTyp := reflect.StructOf([]reflect.StructField{
{
Name: "IcallBase__" + typ.Name(),
Type: reflect.TypeFor[icallBase](),
},
{
Name: "Mock__" + typ.Name(),
Type: reflect.TypeFor[*mock.Mock](),
},
{
Name: typ.Name(),
Type: typ,
Anonymous: true,
},
})
m := new(mock.Mock)
methodInfos := make(icallBase, typ.NumMethod())
abiMethods := getAbiMethods(mockTyp)
for i := range methodInfos {
methodInfos[i] = newMethodInfo(mockTyp, typ.Method(i))
abiMethods[i].setFn(icall_array[i])
}
t := reflect.New(mockTyp).Elem()
t.Field(0).Set(reflect.ValueOf(methodInfos))
t.Field(1).Set(reflect.ValueOf(m))
return t.Interface().(T), m
}