-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
99 lines (77 loc) · 1.85 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
package funcmock
import "reflect"
type call struct {
// what parameter the function was passed
param chan []interface{}
// what the call to the function returned
yield []interface{}
// true if the call has been called, else false
called bool
}
// func (this *call) AllParam() interface{} {
// return this.getParams()
// }
func (this *call) NthParam(nth int) interface{} {
return this.getParams()[nth]
}
func (this *call) Called() bool {
return this.called
}
func (this *call) setCalled(called bool) *call {
this.called = called
return this
}
func (this *call) SetReturn(args ...interface{}) *call {
// Clears any previous return that was set
this.yield = this.yield[:0]
for _, nthIndex := range args {
this.yield = append(this.yield, nthIndex)
}
return this
}
func (this *call) Return(args ...interface{}) *call {
// needs to be thought over. Whether this function is required or not
return this
}
func (this *call) getParams() []interface{} {
if this.Called() {
select {
case param := <-this.param:
go func() { this.param <- param }()
return param
}
} else {
panic("The nth call to the mock function has not been made yet")
}
}
func (this *call) updateParam(parm []interface{}) {
this.setCalled(true)
select {
case <-this.param:
go func() {
this.param <- parm
}()
default:
go func() { this.param <- parm }()
}
}
func sanitizeReturn(returnType reflect.Type, yield interface{}) (sanitizedYield reflect.Value) {
if yield == nil {
// kind of return param, eg. ptr, slice, etc.
kind := returnType.Kind()
switch kind {
case reflect.Ptr:
case reflect.Chan:
case reflect.Func:
case reflect.Interface:
case reflect.Map:
case reflect.Slice:
default:
panic("Cannot set nil to not-pointer type")
}
v := reflect.Zero(returnType)
return v
} else {
return reflect.ValueOf(yield).Convert(returnType)
}
}