-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
scoped.go
162 lines (128 loc) · 2.93 KB
/
scoped.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package mocha
import (
"fmt"
"strings"
)
// Scoped holds references to one or more added mocks allowing users perform operations on them, like enabling/disabling.
type Scoped struct {
storage storage
mocks []*Mock
}
func scope(repo storage, mocks []*Mock) *Scoped {
return &Scoped{storage: repo, mocks: mocks}
}
// Get returns a mock with the provided id.
func (s *Scoped) Get(id int) *Mock {
for _, mock := range s.mocks {
if mock.ID == id {
return mock
}
}
return nil
}
// ListAll returns all mocks scoped in this instance.
func (s *Scoped) ListAll() []*Mock {
return s.mocks
}
// Called returns true if all scoped mocks were called at least once.
func (s *Scoped) Called() bool {
for _, m := range s.mocks {
if !m.Called() {
return false
}
}
return true
}
// ListPending returns all mocks that were not called at least once.
func (s *Scoped) ListPending() []*Mock {
ret := make([]*Mock, 0)
for _, m := range s.mocks {
if !m.Called() {
ret = append(ret, m)
}
}
return ret
}
// ListCalled returns all mocks that were called.
func (s *Scoped) ListCalled() []*Mock {
ret := make([]*Mock, 0)
for _, m := range s.mocks {
if m.Called() {
ret = append(ret, m)
}
}
return ret
}
// IsPending returns true when there are one or more mocks that were not called at least once.
func (s *Scoped) IsPending() bool {
pending := false
for _, m := range s.mocks {
if !m.Called() {
pending = true
break
}
}
return pending
}
// Disable scoped mocks.
// Disabled mocks will be ignored.
func (s *Scoped) Disable() {
for _, m := range s.mocks {
m.Disable()
}
}
// Enable scoped mocks.
func (s *Scoped) Enable() {
for _, m := range s.mocks {
m.Enable()
}
}
// Clean all scoped mocks.
func (s *Scoped) Clean() {
ids := make([]int, len(s.mocks))
for i, m := range s.mocks {
ids[i] = m.ID
}
for _, id := range ids {
s.storage.Delete(id)
}
s.mocks = make([]*Mock, 0)
}
// AssertCalled reports an error if there are still pending mocks.
func (s *Scoped) AssertCalled(t T) bool {
t.Helper()
if s.IsPending() {
b := strings.Builder{}
pending := s.ListPending()
size := len(pending)
for _, p := range pending {
b.WriteString(fmt.Sprintf(" mock: %d %s\n", p.ID, p.Name))
}
t.Errorf("\nthere are still %d mocks that were not called.\npending:\n%s", size, b.String())
return false
}
return true
}
// AssertNotCalled reports an error if any mock was called.
func (s *Scoped) AssertNotCalled(t T) bool {
t.Helper()
if !s.IsPending() {
b := strings.Builder{}
called := s.ListCalled()
size := len(called)
for _, p := range called {
b.WriteString(fmt.Sprintf(" mock: %d %s\n", p.ID, p.Name))
}
t.Errorf("\nthere are %d mocks that were called at least once.\ncalled:\n%s", size, b.String())
return false
}
return true
}
// Hits returns the sum of the scoped mocks calls.
func (s *Scoped) Hits() int {
total := 0
for _, m := range s.mocks {
total += m.Hits()
}
return total
}