-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks_spy_test.go
90 lines (80 loc) · 2.02 KB
/
callbacks_spy_test.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
package spool
import (
"sync"
"github.com/dc0d/actor"
)
var _ actor.Callbacks[int] = &CallbacksSpy[int]{}
type CallbacksSpy[T any] struct {
// ReceivedFunc mocks the Received method.
ReceivedFunc func(ifaceVal T)
// StoppedFunc mocks the Stopped method.
StoppedFunc func()
// calls tracks calls to the methods.
calls struct {
// Received holds details about calls to the Received method.
Received []struct {
// IfaceVal is the ifaceVal argument value.
IfaceVal T
}
// Stopped holds details about calls to the Stopped method.
Stopped []struct {
}
}
lockReceived sync.RWMutex
lockStopped sync.RWMutex
}
// Received calls ReceivedFunc.
func (mock *CallbacksSpy[T]) Received(ifaceVal T) {
if mock.ReceivedFunc == nil {
panic("CallbacksSpy.ReceivedFunc: method is nil but Callbacks.Received was just called")
}
callInfo := struct {
IfaceVal T
}{
IfaceVal: ifaceVal,
}
mock.lockReceived.Lock()
mock.calls.Received = append(mock.calls.Received, callInfo)
mock.lockReceived.Unlock()
mock.ReceivedFunc(ifaceVal)
}
// ReceivedCalls gets all the calls that were made to Received.
// Check the length with:
//
// len(mockedCallbacks.ReceivedCalls())
func (mock *CallbacksSpy[T]) ReceivedCalls() []struct {
IfaceVal T
} {
var calls []struct {
IfaceVal T
}
mock.lockReceived.RLock()
calls = mock.calls.Received
mock.lockReceived.RUnlock()
return calls
}
// Stopped calls StoppedFunc.
func (mock *CallbacksSpy[T]) Stopped() {
if mock.StoppedFunc == nil {
panic("CallbacksSpy.StoppedFunc: method is nil but Callbacks.Stopped was just called")
}
callInfo := struct {
}{}
mock.lockStopped.Lock()
mock.calls.Stopped = append(mock.calls.Stopped, callInfo)
mock.lockStopped.Unlock()
mock.StoppedFunc()
}
// StoppedCalls gets all the calls that were made to Stopped.
// Check the length with:
//
// len(mockedCallbacks.StoppedCalls())
func (mock *CallbacksSpy[T]) StoppedCalls() []struct {
} {
var calls []struct {
}
mock.lockStopped.RLock()
calls = mock.calls.Stopped
mock.lockStopped.RUnlock()
return calls
}