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.

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