-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocker.go
More file actions
126 lines (106 loc) · 2.58 KB
/
mocker.go
File metadata and controls
126 lines (106 loc) · 2.58 KB
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
package interfacemocker
import (
"fmt"
"reflect"
"strings"
)
type Foo struct{}
type Bar struct{}
type Baz struct{}
type Waldo struct{}
type Mocker interface {
GetFoo() (Foo, error)
GetBar() Bar
GetBaz() *Baz
GetWaldo() (*Waldo, error)
GetError() error
}
type mocker struct {
iter int
Mocks []interface{}
}
func (m *mocker) GetFoo() (Foo, error) {
value := m.getMultiMock("interfacemocker.Foo", "errors.errorString")
if value[1] == nil {
return value[0].(Foo), nil
}
return value[0].(Foo), value[1].(error)
}
func (m *mocker) GetBar() Bar {
value := m.getSingleMock("interfacemocker.Bar")
return value.(Bar)
}
func (m *mocker) GetBaz() *Baz {
value := m.getSingleMock("interfacemocker.Baz")
return value.(*Baz)
}
func (m *mocker) GetWaldo() (*Waldo, error) {
value := m.getMultiMock("interfacemocker.Waldo", "errors.errorString")
if value[1] == nil {
return value[0].(*Waldo), nil
}
return value[0].(*Waldo), value[1].(error)
}
func (m *mocker) GetError() error {
value := m.getSingleMock("errors.errorString")
if value == nil {
return nil
}
return value.(error)
}
func (m mocker) checkIteration() {
if m.iter == len(m.Mocks) {
panic("no more mocks available")
}
}
func (m *mocker) getMultiMock(types ...string) []interface{} {
m.checkIteration()
mock := m.Mocks[m.iter]
switch reflect.TypeOf(mock).Kind() {
case reflect.Slice:
mockValues := mock.([]interface{})
if len(types) != len(mockValues) {
msg := fmt.Sprintf(
"mocker expected %d values, but got %d on call %d",
len(types), len(mockValues), m.iter,
)
panic(msg)
}
for idx, mockValue := range mockValues {
expectedType := types[idx]
// nill values should be skipped
if mockValue == nil {
continue
}
mockType := reflect.TypeOf(mockValue).String()
if !strings.Contains(mockType, expectedType) {
msg := fmt.Sprintf(
"mocker expected '%s', but got '%s' on call %d and position %d",
expectedType, mockType, m.iter, idx,
)
panic(msg)
}
}
m.iter++
return mock.([]interface{})
default:
panic(fmt.Sprintf("mocker expected slice, but got '%s' on call %d", reflect.TypeOf(mock).String(), m.iter))
}
}
func (m *mocker) getSingleMock(t string) interface{} {
m.checkIteration()
mock := m.Mocks[m.iter]
// don't cast nil
if mock == nil {
m.iter++
return mock
}
if strings.Contains(reflect.TypeOf(mock).String(), t) {
m.iter++
return mock
}
panic(fmt.Sprintf("Mocker expected '%s' return value, but got '%s' on call %d", reflect.TypeOf(mock).String(), t, m.iter))
}
func NewMocker(mocks []interface{}) Mocker {
return &mocker{Mocks: mocks}
}