This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
122 lines (101 loc) · 2.56 KB
/
api.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
package api
import (
"reflect"
)
type function interface{}
var Map = make(map[function][]*functionCall)
type functionCall struct {
args []Matcher
values []interface{}
}
func getFunType(fun function) interface{} {
return reflect.ValueOf(fun)
}
var mocking = false
var lastFunctionCall *functionCall
func Mock(fun func()) {
mocking = true
fun()
mocking = false
}
func When(args ...interface{}) *functionCall {
return lastFunctionCall
}
func (m *functionCall) Return(values ...interface{}) *functionCall {
m.values = values
return m
}
type Matcher interface {
Matches(interface{}) bool
}
func (m *functionCall) WithMatchers(matchers ...Matcher) *functionCall {
if len(m.args) > len(matchers) {
m.args = append(matchers, m.args[len(matchers):]...)
} else {
m.args = matchers
}
return m
}
func addFunctionCall(funType interface{}, call *functionCall) {
Map[funType] = append(Map[funType], call)
}
func ZeroValues(fun function) []interface{} {
funType := reflect.TypeOf(fun)
values := make([]interface{}, 0)
for i := 0; i < funType.NumOut(); i++ {
values = append(values, reflect.Zero(funType.Out(i)).Interface())
}
return values
}
type EqualsMatcher struct {
value interface{}
}
func (m *EqualsMatcher) Matches(other interface{}) bool {
return reflect.DeepEqual(m.value, other)
}
type DeepEqualMatcher struct {
value interface{}
}
func (m *DeepEqualMatcher) Matches(other interface{}) bool {
return m.value == other
}
// Returns the (return values, true, nil) if the method/function is mocked
// and the args match the expected values. Otherwise, it returns (nil, true, nil)
// if there was an error this function returns (nil, false, error)
func FunctionCalled(fun function, args ...interface{}) ([]interface{}, bool, error) {
funType := getFunType(fun)
if mocking {
argsMatchers := make([]Matcher, 0)
for _, arg := range args {
argType := reflect.TypeOf(arg)
if argType.Kind() == reflect.Ptr {
argsMatchers = append(argsMatchers, &EqualsMatcher{value: arg})
} else {
argsMatchers = append(argsMatchers, &DeepEqualMatcher{value: arg})
}
}
lastFunctionCall = &functionCall{
args: argsMatchers,
}
addFunctionCall(funType, lastFunctionCall)
return ZeroValues(fun), true, nil
}
calls := Map[funType]
outer:
for _, call := range calls {
if len(call.args) > len(args) {
continue
}
for idx, arg := range call.args {
if !arg.Matches(args[idx]) {
continue outer
}
}
return call.values, true, nil
}
// what should we do here
return nil, false, nil
}
func ResetMocks() {
Map = make(map[function][]*functionCall)
}