all_of.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2011 Aaron Jacobs. All Rights Reserved.
  2. // Author: aaronjjacobs@gmail.com (Aaron Jacobs)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package oglematchers
  16. import (
  17. "strings"
  18. )
  19. // AllOf accepts a set of matchers S and returns a matcher that follows the
  20. // algorithm below when considering a candidate c:
  21. //
  22. // 1. Return true if for every Matcher m in S, m matches c.
  23. //
  24. // 2. Otherwise, if there is a matcher m in S such that m returns a fatal
  25. // error for c, return that matcher's error message.
  26. //
  27. // 3. Otherwise, return false with the error from some wrapped matcher.
  28. //
  29. // This is akin to a logical AND operation for matchers.
  30. func AllOf(matchers ...Matcher) Matcher {
  31. return &allOfMatcher{matchers}
  32. }
  33. type allOfMatcher struct {
  34. wrappedMatchers []Matcher
  35. }
  36. func (m *allOfMatcher) Description() string {
  37. // Special case: the empty set.
  38. if len(m.wrappedMatchers) == 0 {
  39. return "is anything"
  40. }
  41. // Join the descriptions for the wrapped matchers.
  42. wrappedDescs := make([]string, len(m.wrappedMatchers))
  43. for i, wrappedMatcher := range m.wrappedMatchers {
  44. wrappedDescs[i] = wrappedMatcher.Description()
  45. }
  46. return strings.Join(wrappedDescs, ", and ")
  47. }
  48. func (m *allOfMatcher) Matches(c interface{}) (err error) {
  49. for _, wrappedMatcher := range m.wrappedMatchers {
  50. if wrappedErr := wrappedMatcher.Matches(c); wrappedErr != nil {
  51. err = wrappedErr
  52. // If the error is fatal, return immediately with this error.
  53. _, ok := wrappedErr.(*FatalError)
  54. if ok {
  55. return
  56. }
  57. }
  58. }
  59. return
  60. }