blob: d83a29164c6ce6d6fdaf0ec7e1a95d85690fb341 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package matchers
2
3import (
4 "fmt"
5
6 "github.com/onsi/gomega/format"
7 "github.com/onsi/gomega/internal/oraclematcher"
8 "github.com/onsi/gomega/types"
9)
10
11type AndMatcher struct {
12 Matchers []types.GomegaMatcher
13
14 // state
15 firstFailedMatcher types.GomegaMatcher
16}
17
18func (m *AndMatcher) Match(actual interface{}) (success bool, err error) {
19 m.firstFailedMatcher = nil
20 for _, matcher := range m.Matchers {
21 success, err := matcher.Match(actual)
22 if !success || err != nil {
23 m.firstFailedMatcher = matcher
24 return false, err
25 }
26 }
27 return true, nil
28}
29
30func (m *AndMatcher) FailureMessage(actual interface{}) (message string) {
31 return m.firstFailedMatcher.FailureMessage(actual)
32}
33
34func (m *AndMatcher) NegatedFailureMessage(actual interface{}) (message string) {
35 // not the most beautiful list of matchers, but not bad either...
36 return format.Message(actual, fmt.Sprintf("To not satisfy all of these matchers: %s", m.Matchers))
37}
38
39func (m *AndMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
40 /*
41 Example with 3 matchers: A, B, C
42
43 Match evaluates them: T, F, <?> => F
44 So match is currently F, what should MatchMayChangeInTheFuture() return?
45 Seems like it only depends on B, since currently B MUST change to allow the result to become T
46
47 Match eval: T, T, T => T
48 So match is currently T, what should MatchMayChangeInTheFuture() return?
49 Seems to depend on ANY of them being able to change to F.
50 */
51
52 if m.firstFailedMatcher == nil {
53 // so all matchers succeeded.. Any one of them changing would change the result.
54 for _, matcher := range m.Matchers {
55 if oraclematcher.MatchMayChangeInTheFuture(matcher, actual) {
56 return true
57 }
58 }
59 return false // none of were going to change
60 }
61 // one of the matchers failed.. it must be able to change in order to affect the result
62 return oraclematcher.MatchMayChangeInTheFuture(m.firstFailedMatcher, actual)
63}