-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathor-matcher.go
58 lines (46 loc) · 967 Bytes
/
or-matcher.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
package extra
import (
"fmt"
"go.uber.org/mock/gomock"
)
type orMatcher struct {
matchers []gomock.Matcher
}
func (om *orMatcher) String() string {
if len(om.matchers) == 0 {
return "the \"or\" matcher will return false because list is empty"
}
// Initialize string
str := ""
// Loop over matchers
for i, m := range om.matchers {
// Ignore the first item
if i > 0 {
str += " or "
}
// Concat matcher string
str += fmt.Sprintf("(%s)", m.String())
}
return str
}
func (om *orMatcher) Matches(x interface{}) bool {
// Check empty case
if len(om.matchers) == 0 {
return false
}
// Loop over matchers
for _, m := range om.matchers {
// Check if matcher is ok
if m.Matches(x) {
// Matches so ... End
return true
}
}
// No match until now
// Or is false
return false
}
// OrMatcher will return a new Or matcher.
func OrMatcher(matchers ...gomock.Matcher) gomock.Matcher {
return &orMatcher{matchers: matchers}
}