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