-
Notifications
You must be signed in to change notification settings - Fork 1
/
expect.go
75 lines (58 loc) · 1.67 KB
/
expect.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
package gintestutil
import (
"sync"
"github.com/gin-gonic/gin"
)
// ExpectOption allows various options to be supplied to Expect* functions
type ExpectOption func(*calledConfig)
// TimesCalled is used to expect an invocation an X amount of times
func TimesCalled(times int) ExpectOption {
return func(config *calledConfig) {
config.Times = times
}
}
// Expectation is used to have a global wait group to wait for
// when asserting multiple calls made
func Expectation(expectation *sync.WaitGroup) ExpectOption {
return func(config *calledConfig) {
config.Expectation = expectation
}
}
type calledConfig struct {
Times int
Expectation *sync.WaitGroup
}
// ExpectCalled can be used on a gin endpoint to express an expectation that the endpoint will
// be called some time in the future. In combination with a test
// can wait for this expectation to be true or fail after some predetermined amount of time
func ExpectCalled(t TestingT, ctx *gin.Engine, path string, options ...ExpectOption) *sync.WaitGroup {
t.Helper()
if ctx == nil {
t.Errorf("context cannot be nil")
return nil
}
config := &calledConfig{
Times: 1,
Expectation: &sync.WaitGroup{},
}
for _, option := range options {
option(config)
}
// Set waitgroup for amount of times
config.Expectation.Add(config.Times)
// Add middleware for provided route
var timesCalled int
ctx.Use(func(c *gin.Context) {
c.Next()
if c.FullPath() != path {
return
}
timesCalled++
if timesCalled <= config.Times {
config.Expectation.Done()
return
}
t.Errorf("%s hook asserts called %d times but called at least %d times\n", path, config.Times, timesCalled)
})
return config.Expectation
}