-
Notifications
You must be signed in to change notification settings - Fork 39
/
mock_controller.go
86 lines (72 loc) · 1.95 KB
/
mock_controller.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
package minimock
import (
"sync"
"time"
)
// Mocker describes common interface for all mocks generated by minimock
type Mocker interface {
MinimockFinish()
MinimockWait(time.Duration)
}
// Tester contains subset of the testing.T methods used by the generated code
type Tester interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Error(...interface{})
Errorf(format string, args ...interface{})
FailNow()
Cleanup(f func())
Helper()
}
// MockController can be passed to mocks generated by minimock
type MockController interface {
Tester
RegisterMocker(Mocker)
}
// Controller implements MockController interface and has to be used in your tests:
// mockController := minimock.NewController(t)
// defer mockController.Finish()
// stringerMock := NewStringerMock(mockController)
type Controller struct {
Tester
sync.Mutex
mockers []Mocker
}
// Check if Controller supports MockController interface
var _ MockController = &Controller{}
// NewController returns an instance of Controller
func NewController(t Tester) *Controller {
c := Controller{Tester: newSafeTester(t)}
t.Cleanup(c.Finish)
return &c
}
// RegisterMocker puts mocker to the list of controller mockers
func (c *Controller) RegisterMocker(m Mocker) {
c.Lock()
c.mockers = append(c.mockers, m)
c.Unlock()
}
// Finish calls to MinimockFinish method for all registered mockers
//
// Deprecated: Finish exists for historical compatibility
// and should not be used. Current implementation of controller registers
// Finish as Cleanup function for the testing.T instance passed to NewController.
func (c *Controller) Finish() {
c.Lock()
for _, m := range c.mockers {
m.MinimockFinish()
}
c.Unlock()
}
// Wait calls to MinimockWait method for all registered mockers
func (c *Controller) Wait(d time.Duration) {
wg := sync.WaitGroup{}
wg.Add(len(c.mockers))
for _, m := range c.mockers {
go func(m Mocker) {
defer wg.Done()
m.MinimockWait(d)
}(m)
}
wg.Wait()
}