elements_are.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2012 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. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. )
  22. // Given a list of arguments M, ElementsAre returns a matcher that matches
  23. // arrays and slices A where all of the following hold:
  24. //
  25. // * A is the same length as M.
  26. //
  27. // * For each i < len(A) where M[i] is a matcher, A[i] matches M[i].
  28. //
  29. // * For each i < len(A) where M[i] is not a matcher, A[i] matches
  30. // Equals(M[i]).
  31. //
  32. func ElementsAre(M ...interface{}) Matcher {
  33. // Copy over matchers, or convert to Equals(x) for non-matcher x.
  34. subMatchers := make([]Matcher, len(M))
  35. for i, x := range M {
  36. if matcher, ok := x.(Matcher); ok {
  37. subMatchers[i] = matcher
  38. continue
  39. }
  40. subMatchers[i] = Equals(x)
  41. }
  42. return &elementsAreMatcher{subMatchers}
  43. }
  44. type elementsAreMatcher struct {
  45. subMatchers []Matcher
  46. }
  47. func (m *elementsAreMatcher) Description() string {
  48. subDescs := make([]string, len(m.subMatchers))
  49. for i, sm := range m.subMatchers {
  50. subDescs[i] = sm.Description()
  51. }
  52. return fmt.Sprintf("elements are: [%s]", strings.Join(subDescs, ", "))
  53. }
  54. func (m *elementsAreMatcher) Matches(candidates interface{}) error {
  55. // The candidate must be a slice or an array.
  56. v := reflect.ValueOf(candidates)
  57. if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
  58. return NewFatalError("which is not a slice or array")
  59. }
  60. // The length must be correct.
  61. if v.Len() != len(m.subMatchers) {
  62. return errors.New(fmt.Sprintf("which is of length %d", v.Len()))
  63. }
  64. // Check each element.
  65. for i, subMatcher := range m.subMatchers {
  66. c := v.Index(i)
  67. if matchErr := subMatcher.Matches(c.Interface()); matchErr != nil {
  68. // Return an errors indicating which element doesn't match. If the
  69. // matcher error was fatal, make this one fatal too.
  70. err := errors.New(fmt.Sprintf("whose element %d doesn't match", i))
  71. if _, isFatal := matchErr.(*FatalError); isFatal {
  72. err = NewFatalError(err.Error())
  73. }
  74. return err
  75. }
  76. }
  77. return nil
  78. }