-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathand.go
50 lines (42 loc) · 1.04 KB
/
and.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
package matchers
import (
"encoding/json"
)
type AndMatcher struct {
fakeOmegaMatcher
Matchers []GossMatcher
// state
firstFailedMatcher GossMatcher
}
func And(ms ...GossMatcher) GossMatcher {
return &AndMatcher{Matchers: ms}
}
func (m *AndMatcher) Match(actual interface{}) (success bool, err error) {
m.firstFailedMatcher = nil
for _, matcher := range m.Matchers {
success, err := matcher.Match(actual)
if !success || err != nil {
m.firstFailedMatcher = matcher
return false, err
}
}
return true, nil
}
func (m *AndMatcher) FailureResult(actual interface{}) MatcherResult {
return m.firstFailedMatcher.FailureResult(actual)
}
func (m *AndMatcher) NegatedFailureResult(actual interface{}) MatcherResult {
return MatcherResult{
Actual: actual,
Message: "not to satisfy all of these matchers",
Expected: m.Matchers,
}
}
func (m *AndMatcher) MarshalJSON() ([]byte, error) {
if len(m.Matchers) == 1 {
return json.Marshal(m.Matchers[0])
}
j := make(map[string]interface{})
j["and"] = m.Matchers
return json.Marshal(j)
}