-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
139 lines (105 loc) · 3.55 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
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
package emock
import (
"log"
"reflect"
)
// If the function being mocked returns a func, that function will be nil
type Mock struct {
originalFuncAddr reflect.Value
originalFuncValue reflect.Value
Calls [][]interface{}
returns []interface{}
}
// Restore the original behaviour of the function that was being mocked.
func (self *Mock) Restore() {
lg("OriginalFuncAddr", self.originalFuncAddr)
lg("OriginalFuncValue", self.originalFuncValue)
self.originalFuncAddr.Elem().Set(self.originalFuncValue)
}
// Returns the args for the n'th call to the mocked function
func (self *Mock) GetArgsForCall(n int) (interface{}) {
args := self.Calls[n]
return args
}
// Returns the number of calls made to the mocked function.
func (self *Mock) CallCount() int {
return len(self.Calls)
}
func lg(data string, item interface{}) {
return
log.Printf("*** %s %+V", data, item)
log.Printf("*** %s %+v", data, item)
}
// Mock the function at the specified address.
//
// By default the function will return zero values.
func MockFunc(funcAddress interface{}) *Mock {
mock := Mock{}
lg("funcToMock", funcAddress)
funcValue := reflect.ValueOf(funcAddress)
lg("funcValue", funcValue)
mock.originalFuncAddr = funcValue
funcType := funcValue.Type()
lg("funcType", funcType)
funcElem := funcValue.Elem()
lg("funcElem", funcElem)
mock.originalFuncValue = reflect.New(funcValue.Elem().Type()).Elem()
mock.originalFuncValue.Set(funcElem)
lg("mock.OriginalFuncAddr", mock.originalFuncAddr)
lg("mock.OriginalFuncValue", mock.originalFuncValue)
funcElemType := funcElem.Type()
lg("funcElemType", funcElemType)
fake := makeFuncStub(&mock, nil)
mock.originalFuncAddr.Elem().Set(reflect.MakeFunc(mock.originalFuncValue.Type(), fake))
return &mock
}
// Set what the mocked function should return. Panic if the number of return values
// doesn't match the original function, or if the types don't match
func (self *Mock) SetReturns(returns ...interface{}) {
fake := makeFuncStub(self, returns)
self.originalFuncAddr.Elem().Set(reflect.MakeFunc(self.originalFuncValue.Type(), fake))
}
// Replace the mocked function with the provided one. Panics if the given function
// signature does not match the original one.
func (self *Mock) SetReturnFunc(function interface{}) {
wrap := wrapFunc(self, function)
self.originalFuncAddr.Elem().Set(reflect.MakeFunc(self.originalFuncValue.Type(), wrap))
}
func makeFuncStub(mock *Mock, returns []interface{}) func(in []reflect.Value) []reflect.Value {
funcType := mock.originalFuncValue.Type()
if (returns != nil && len(returns) != funcType.NumOut()) {
panic("SetReturns: Wrong number of returns arguments.")
}
fake := func(in []reflect.Value) []reflect.Value {
inputArgs := []interface{}{}
for _, value := range in {
inputArgs = append(inputArgs, value.Interface())
}
mock.Calls = append(mock.Calls, inputArgs)
var outputs []reflect.Value
if (returns == nil) {
for i := 0; i < funcType.NumOut(); i++ {
lg("index", i)
newOutput := reflect.New(funcType.Out(i)).Elem()
outputs = append(outputs, newOutput)
}
} else {
for i := 0; i < funcType.NumOut(); i++ {
outputs = append(outputs, reflect.ValueOf(returns[i]))
}
}
return outputs
}
return fake
}
func wrapFunc(mock *Mock, function interface{}) func(in []reflect.Value) []reflect.Value {
fake := func(in []reflect.Value) []reflect.Value {
inputArgs := []interface{}{}
for _, value := range in {
inputArgs = append(inputArgs, value.Interface())
}
mock.Calls = append(mock.Calls, inputArgs)
return reflect.ValueOf(function).Call(in)
}
return fake
}