-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
mock.go
114 lines (93 loc) · 2.34 KB
/
mock.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
package tgmock
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/gotd/td/bin"
)
// TestingT is simplified *testing.T interface.
type TestingT interface {
require.TestingT
assert.TestingT
Helper()
Cleanup(cb func())
}
// Mock is a mock for tg.Invoker with testify/require support.
type Mock struct {
calls []Handler
assert assertions
}
// Option configures Mock.
type Option interface {
apply(t TestingT, m *Mock)
}
type assertions interface {
Truef(value bool, msg string, args ...interface{})
Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{})
}
type assertAssertions struct {
assert *assert.Assertions
}
func (a assertAssertions) Equal(expected, actual interface{}, msgAndArgs ...interface{}) {
a.assert.Equal(expected, actual, msgAndArgs...)
}
func (a assertAssertions) Truef(value bool, msg string, args ...interface{}) {
a.assert.Truef(value, msg, args...)
}
type optionFunc func(t TestingT, m *Mock)
func (o optionFunc) apply(t TestingT, m *Mock) { o(t, m) }
// WithRequire configures mock to use "require" assertions.
func WithRequire() Option {
return optionFunc(func(t TestingT, m *Mock) {
m.assert = require.New(t)
})
}
// NewRequire creates new Mock with "require" assertions.
func NewRequire(t TestingT) *Mock {
return New(t, WithRequire())
}
// New creates new Mock.
func New(t TestingT, options ...Option) *Mock {
m := &Mock{
assert: &assertAssertions{
assert: assert.New(t),
},
}
for _, o := range options {
o.apply(t, m)
}
t.Cleanup(func() {
m.assert.Truef(
m.AllWereMet(),
"not all expected calls happen (expected yet %d)",
len(m.calls),
)
})
return m
}
// AllWereMet returns true if all expected calls happened.
func (i *Mock) AllWereMet() bool {
return len(i.calls) == 0
}
func (i *Mock) add(h Handler) *Mock {
i.calls = append(i.calls, h)
return i
}
func (i *Mock) pop() (Handler, bool) {
if len(i.calls) < 1 {
return nil, false
}
h := i.calls[0]
// Delete from SliceTricks.
copy(i.calls, i.calls[1:])
i.calls[len(i.calls)-1] = nil
i.calls = i.calls[:len(i.calls)-1]
return h, true
}
// Handler returns HandlerFunc of Mock.
func (i *Mock) Handler() HandlerFunc {
return func(id int64, body bin.Encoder) (bin.Encoder, error) {
h, ok := i.pop()
i.assert.Truef(ok, "unexpected call")
return h.Handle(id, body)
}
}