You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB

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