/
testexecutor.go
59 lines (45 loc) · 1.18 KB
/
testexecutor.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
package aws
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type TestExecution struct {
Output interface{}
Err error
}
type TestableExecutor struct {
clientMock *mock.Mock
executions []TestExecution
current int
}
func NewTestableExecutor(clientMock *mock.Mock, executions ...TestExecution) *TestableExecutor {
return &TestableExecutor{
clientMock: clientMock,
executions: executions,
}
}
func (e *TestableExecutor) Execute(_ context.Context, f RequestFunction) (interface{}, error) {
f()
c := e.current
e.current++
if c >= len(e.executions) {
panic("there is no available test execution")
}
return e.executions[c].Output, e.executions[c].Err
}
func (e *TestableExecutor) ExpectExecution(clientMethod string, input interface{}, output interface{}, err error) {
e.clientMock.On(clientMethod, input).Return(nil, output)
e.executions = append(e.executions, TestExecution{
Output: output,
Err: err,
})
}
func (e *TestableExecutor) AssertExpectations(t *testing.T) {
e.clientMock.AssertExpectations(t)
if e.current == len(e.executions) {
return
}
assert.Fail(t, "not all executions have been executed")
}