-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.go
135 lines (114 loc) · 3.22 KB
/
call.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
package gosmock
import (
"fmt"
"reflect"
)
type Call struct {
mTool *MockTool
fn interface{}
fnType reflect.Type
fnName string
Count int
Params []interface{}
hasParams bool
paramsHash string
Response []interface{}
ParamUpdate map[int]interface{}
}
func (c *Call) WithParams(p ...interface{}) *Call {
c.Params = p
c.hasParams = true
c.paramsHash = hash(p...)
return c
}
func (c *Call) WithParamUpdate(paramPosition int, value interface{}) *Call {
c.ParamUpdate[paramPosition] = value
return c
}
func (c *Call) WithResponse(r ...interface{}) *Call {
c.Response = r
return c
}
func (c *Call) Times(n int) *Call {
// Validate input params
if c.Params != nil {
c.compareTypes(c.fnType.NumIn(), c.fnType.In, c.Params, false)
}
// Validate output params
c.compareTypes(c.fnType.NumOut(), c.fnType.Out, c.Response, true)
// Validate param update
for paramPos, _ := range c.ParamUpdate {
if paramPos < 0 || c.fnType.NumIn() == 0 || paramPos >= c.fnType.NumIn() {
panic(fmt.Sprintf("wrong number of param position %d for update in fn %s", paramPos, c.fnName))
}
}
// Add responses
for i := 0; i < n; i++ {
c.mTool.responses[c.fnName] = append(c.mTool.responses[c.fnName], *c)
}
return c
}
func (c *Call) compareTypes(number int, getExpected func(i int) reflect.Type, values []interface{}, skipNil bool) {
// Validate number of values
if number != len(values) {
panic(fmt.Sprintf("wrong number of values for fn %s. Expected %d, Recived %d",
c.fnName, number, len(c.Response)))
}
// Validate type of params values
for i := 0; i < number; i++ {
expected := getExpected(i)
received := reflect.TypeOf(values[i])
c.compareType(expected, received, skipNil)
}
}
func (c *Call) compareType(expected reflect.Type, received reflect.Type, skipNil bool) {
// Skip validation of nil values
if received == nil && skipNil {
return
}
// nil is zero value for pointers, interfaces, maps, slices, channels and function types
ek := expected.Kind()
if received == nil && isNullable(expected) {
return
}
// Is received value assignable to the expected?
if received.AssignableTo(expected) {
return
}
// When an interfaced is expected check implementation
if (ek == reflect.Interface || ek == reflect.Ptr) && received.Implements(expected) {
return
}
// Error
panic(fmt.Sprintf("func %s expect value to be %s, given %s",
c.fnName, expected.Name(), received.Name()))
}
func (c *Call) Fill(params ...interface{}) {
if len(c.Response) != len(params) {
panic("The numbers of values doesn't match the number of params")
}
// Update params
for i, v := range c.ParamUpdate {
elem := reflect.ValueOf(c.Params[i]).Elem()
if elem.Kind() == reflect.Ptr {
elem.Elem().Set(reflect.ValueOf(v))
} else {
elem.Set(reflect.ValueOf(v))
}
}
// Set return values
for i, v := range c.Response {
// Param must be a pointer
pType := reflect.TypeOf(params[i])
if pType == nil || pType.Kind() != reflect.Ptr {
panic(fmt.Sprintf("func %s expect fill param %d to be a pointer",
c.fnName, i))
}
// If mocked value is nil, we use the zero value of the param
if v == nil {
continue
}
param := reflect.ValueOf(params[i]).Elem()
param.Set(reflect.ValueOf(v))
}
}