-
Notifications
You must be signed in to change notification settings - Fork 0
/
gouble.go
206 lines (175 loc) · 4.86 KB
/
gouble.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gouble
import (
"fmt"
"reflect"
)
type MockResponse struct {
method reflect.Method
ErrorResponse error // TODO: Remove
successResponse reflect.Value // TODO: Remove
successResponses []reflect.Value
}
// TODO: Remove
func (resp *MockResponse) SuccessValue() interface{} {
return resp.successResponse.Interface()
}
func (resp *MockResponse) ReturnValues() []interface{} {
resps := make([]interface{}, 0)
for _, resp := range resp.successResponses {
resps = append(resps, resp.Interface())
}
return resps
}
type iMock interface {
SetDouble(*Double)
}
type Double struct {
mock iMock
mockResponses map[string]*MockResponse
lastMethodName string
}
func Mock() *Double {
return &Double{mockResponses: make(map[string]*MockResponse)}
}
func (d *Double) Allow(mock iMock) *Double {
d.mock = mock
mock.SetDouble(d)
return d
}
func (d *Double) ToReceive(methodName string) *Double {
if d.mock == nil {
panic("No mock was allowed!")
}
mockType := typeOf(d.mock)
m, ok := mockType.MethodByName(methodName)
if !ok {
panic(fmt.Sprintf("%s does not implement %s", mockType.String(), methodName))
}
// TODO: Have default values for `successResponses`
defaultResponse := reflect.Indirect(reflect.New(m.Type.Out(0)))
d.mockResponses[methodName] = &MockResponse{successResponse: defaultResponse, method: m}
d.lastMethodName = methodName
return d
}
func (d *Double) lastMethod() reflect.Method {
lastResponse := d.mockResponses[d.lastMethodName]
if lastResponse == nil {
panic("No method was allowed!")
}
return lastResponse.method
}
func (d *Double) resetLastMethod() {
d.lastMethodName = ""
}
func (d *Double) AndReturn(responses ...interface{}) {
if len(responses) != d.lastMethod().Type.NumOut() {
msg := fmt.Sprintf(
"wrong number of return values for %s. Expected: %v | Got: %v",
d.lastMethod().Name,
d.lastMethod().Type.NumOut(),
len(responses),
)
panic(msg)
}
resps := make([]reflect.Value, 0)
var errResponse error
for i, r := range responses {
expectedType := d.lastMethod().Type.Out(i)
if expectedType.String() == "error" {
err, ok := r.(error)
if !ok {
panic(mismatchTypeErrorMessage(d, expectedType, r, i))
}
errResponse = err
} else if expectedType.String() != typeToString(r) {
panic(mismatchTypeErrorMessage(d, expectedType, r, i))
}
resps = append(resps, reflect.ValueOf(r))
}
d.mockResponses[d.lastMethod().Name] = &MockResponse{
successResponse: resps[0],
successResponses: resps,
ErrorResponse: errResponse,
}
d.resetLastMethod()
}
func mismatchTypeErrorMessage(d *Double, expected reflect.Type, got interface{}, index int) string {
return fmt.Sprintf(
"%s of %s [%s] does not return given types. Index: %v - Expected: %s | Got: %s",
d.lastMethod().Name,
typeToString(d.mock),
d.lastMethod().Type.String(),
index,
expected.String(),
typeToString(got),
)
}
// TODO: Remove (use one `AndReturn`)
func (d *Double) AndReturnWithError(successResponse interface{}, err error) {
expectedType := d.lastMethod().Type.Out(0)
if expectedType != typeOf(successResponse) {
msg := fmt.Sprintf(
"%s of %s does not return value of type: %s",
d.lastMethod().Type.String(),
typeToString(d.mock),
typeToString(successResponse),
)
panic(msg)
}
lastType := d.lastMethod().Type.Out(d.lastMethod().Type.NumOut() - 1)
if lastType.String() != "error" {
msg := fmt.Sprintf(
"%s of %s does not return error",
d.lastMethod().Type.String(),
typeToString(d.mock),
)
panic(msg)
}
d.mockResponses[d.lastMethod().Name] = &MockResponse{
successResponse: reflect.ValueOf(successResponse),
ErrorResponse: err,
}
d.resetLastMethod()
}
// TODO: Remove (use one `AndReturn`)
func (d *Double) AndReturnWithoutError(successResponse interface{}) {
expectedType := d.lastMethod().Type.Out(0)
if expectedType != typeOf(successResponse) {
msg := fmt.Sprintf(
"%s of %s does not return value of type: %s",
d.lastMethod().Type.String(),
typeToString(d.mock),
typeToString(successResponse),
)
panic(msg)
}
d.mockResponses[d.lastMethod().Name] = &MockResponse{successResponse: reflect.ValueOf(successResponse)}
d.resetLastMethod()
}
// TODO: Remove (use one `AndReturn`)
func (d *Double) AndThrowError(err error) {
lastType := d.lastMethod().Type.Out(d.lastMethod().Type.NumOut() - 1)
if lastType.String() != "error" {
msg := fmt.Sprintf(
"%s of %s does not return error",
d.lastMethod().Type.String(),
typeToString(d.mock),
)
panic(msg)
}
d.MockFor(d.lastMethod().Name).ErrorResponse = err
d.resetLastMethod()
}
func (d *Double) MockFor(method string) *MockResponse {
mock := d.mockResponses[method]
if mock == nil {
panic(fmt.Sprintf("mock not defined for method %s", method))
}
return mock
}
func typeOf(obj interface{}) reflect.Type {
return reflect.TypeOf(obj)
}
func typeToString(obj interface{}) string {
return typeOf(obj).String()
}