-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathfunc_mock.go
More file actions
66 lines (55 loc) · 1.47 KB
/
func_mock.go
File metadata and controls
66 lines (55 loc) · 1.47 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
package mock
import (
"errors"
"reflect"
"testing"
)
var ErrFuncMockNotFunc = errors.New("not a function")
func FuncMockFor(fn interface{}) (*FuncMock, error) {
typ := reflect.TypeOf(fn)
if typ == nil || typ.Kind() != reflect.Func {
return nil, ErrFuncMockNotFunc
}
return &FuncMock{
mock: Mock{},
typ: typ,
}, nil
}
type FuncMock struct {
mock Mock
typ reflect.Type
}
func (m *FuncMock) Build() interface{} {
return reflect.MakeFunc(m.typ, func(args []reflect.Value) []reflect.Value {
argsAsInterface := make([]interface{}, len(args))
for i, arg := range args {
argsAsInterface[i] = arg.Interface()
}
outs := m.mock.MethodCalled("func", argsAsInterface...)
res := make([]reflect.Value, m.typ.NumOut())
for i := 0; i < m.typ.NumOut(); i++ {
val := outs.Get(i)
if val == nil {
res[i] = reflect.Zero(m.typ.Out(i))
continue
}
res[i] = reflect.ValueOf(val)
}
return res
}).Interface()
}
func (m *FuncMock) On(args ...interface{}) *Call {
return m.mock.On("func", args...)
}
func (m *FuncMock) AssertExpectations(t *testing.T) {
m.mock.AssertExpectations(t)
}
func (m *FuncMock) AssertNotCalled(t *testing.T, arguments ...interface{}) {
m.mock.AssertNotCalled(t, "func", arguments...)
}
func (m *FuncMock) AssertCalled(t *testing.T, arguments ...interface{}) {
m.mock.AssertCalled(t, "func", arguments...)
}
func (m *FuncMock) AssertNumberOfCalls(t *testing.T, expectedCalls int) {
m.mock.AssertNumberOfCalls(t, "func", expectedCalls)
}