-
Notifications
You must be signed in to change notification settings - Fork 4
/
catalogwrapper_mock.go
82 lines (69 loc) · 2.08 KB
/
catalogwrapper_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
package object
import (
"github.com/gojuno/minimock/v3"
"github.com/insolar/assured-ledger/ledger-core/conveyor/smachine"
"github.com/insolar/assured-ledger/ledger-core/reference"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
type catalogMockWrapperEntry struct {
accessor SharedStateAccessor
wasAccessed bool
}
type CatalogMockWrapper struct {
t minimock.Tester
objects map[reference.Global]*catalogMockWrapperEntry
childMock *CatalogMock
}
type CatalogMockAccessType int
const (
CatalogMockAccessGetOrCreate CatalogMockAccessType = iota
CatalogMockAccessTryGet
)
func NewCatalogMockWrapper(t minimock.Tester) *CatalogMockWrapper {
return &CatalogMockWrapper{
t: t,
objects: make(map[reference.Global]*catalogMockWrapperEntry),
childMock: NewCatalogMock(t),
}
}
func (m *CatalogMockWrapper) AllowAccessMode(mode CatalogMockAccessType) {
switch mode {
case CatalogMockAccessGetOrCreate:
m.childMock.GetOrCreateMock.Set(m.mockGetOrCreate)
case CatalogMockAccessTryGet:
m.childMock.TryGetMock.Set(m.mockTryGet)
default:
m.t.Fatal(throw.NotImplemented())
}
}
func (m *CatalogMockWrapper) CheckDone() error {
for _, entry := range m.objects {
if !entry.wasAccessed {
return throw.New("not all object from catalog was used")
}
}
return nil
}
func (m *CatalogMockWrapper) Mock() *CatalogMock {
return m.childMock
}
func (m *CatalogMockWrapper) AddObject(objectRef reference.Global, objectAccessor SharedStateAccessor) *CatalogMockWrapper {
m.objects[objectRef] = &catalogMockWrapperEntry{accessor: objectAccessor, wasAccessed: false}
return m
}
func (m *CatalogMockWrapper) mockGetOrCreate(_ smachine.ExecutionContext, ref reference.Global) SharedStateAccessor {
entry, ok := m.objects[ref]
if !ok {
panic(throw.Unsupported())
}
entry.wasAccessed = true
return entry.accessor
}
func (m *CatalogMockWrapper) mockTryGet(_ smachine.ExecutionContext, ref reference.Global) (SharedStateAccessor, bool) {
entry, ok := m.objects[ref]
if !ok {
return SharedStateAccessor{}, false
}
entry.wasAccessed = true
return entry.accessor, true
}