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