-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
95 lines (79 loc) · 2.21 KB
/
controller.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
package funcmock
import (
"reflect"
)
type MockController struct {
originalFunc reflect.Value
targetFunc reflect.Value
// we need map, not slice, to set call before it is called
callStack chan map[int]*call
// we need it to set call, before it is called
counter chan int
// the default call which shall be used for mint calls
defaultYield []reflect.Value
// Flag indicating the default return has been set
yieldSet bool
}
func (this *MockController) CallCount() int {
var count int
select {
case count = <-this.counter:
go func() { this.counter <- count }()
}
return count
}
func (this *MockController) NthCall(nth int) (theCall *call) {
callStack := <-this.callStack
theCall, ok := callStack[nth]
if ok == false {
theCall = &call{
param: make(chan []interface{}),
called: false,
}
}
go func() { this.callStack <- callStack }()
this.addCallAt(theCall, nth)
return theCall
}
func (this *MockController) incrementCounter() int {
var count int
select {
case count = <-this.counter:
count++
go func() { this.counter <- count }()
}
return count
}
func (this *MockController) SetDefaultReturn(args ...interface{}) {
if this.targetFunc == reflect.Zero(this.targetFunc.Type()) {
panic("Internal Error: Target Function should prior to calling SetDefaultReturn")
}
fnType := this.targetFunc.Type()
typeNumOut := fnType.NumOut()
if len(args) == typeNumOut && !this.yieldSet {
this.defaultYield = this.defaultYield[:0]
for i := 0; i < typeNumOut; i++ {
this.defaultYield = append(this.defaultYield, sanitizeReturn(fnType.Out(i), args[i]))
}
this.yieldSet = true
} else if this.yieldSet {
panic("Can only call SetDefaultReturn once")
} else {
panic("The number of returns should be the same as that of the function")
}
}
// func (this *MockController) getCallStack() map[int]*call {
// go func() { this.callStack <- callStack }()
// return callStack
// }
func (this *MockController) addCallAt(theCall *call, index int) {
callStack := <-this.callStack
callStack[index] = theCall
go func() { this.callStack <- callStack }()
}
func (this *MockController) Called() bool {
return this.CallCount() > 0
}
func (this *MockController) Restore() {
this.targetFunc.Set(this.originalFunc)
}