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.

144 lines
3.7 KiB

  1. // untested sections: 3
  2. package matchers
  3. import (
  4. "fmt"
  5. "reflect"
  6. "github.com/onsi/gomega/format"
  7. "github.com/onsi/gomega/matchers/support/goraph/bipartitegraph"
  8. )
  9. type ConsistOfMatcher struct {
  10. Elements []interface{}
  11. missingElements []interface{}
  12. extraElements []interface{}
  13. }
  14. func (matcher *ConsistOfMatcher) Match(actual interface{}) (success bool, err error) {
  15. if !isArrayOrSlice(actual) && !isMap(actual) {
  16. return false, fmt.Errorf("ConsistOf matcher expects an array/slice/map. Got:\n%s", format.Object(actual, 1))
  17. }
  18. matchers := matchers(matcher.Elements)
  19. values := valuesOf(actual)
  20. bipartiteGraph, err := bipartitegraph.NewBipartiteGraph(values, matchers, neighbours)
  21. if err != nil {
  22. return false, err
  23. }
  24. edges := bipartiteGraph.LargestMatching()
  25. if len(edges) == len(values) && len(edges) == len(matchers) {
  26. return true, nil
  27. }
  28. var missingMatchers []interface{}
  29. matcher.extraElements, missingMatchers = bipartiteGraph.FreeLeftRight(edges)
  30. matcher.missingElements = equalMatchersToElements(missingMatchers)
  31. return false, nil
  32. }
  33. func neighbours(value, matcher interface{}) (bool, error) {
  34. match, err := matcher.(omegaMatcher).Match(value)
  35. return match && err == nil, nil
  36. }
  37. func equalMatchersToElements(matchers []interface{}) (elements []interface{}) {
  38. for _, matcher := range matchers {
  39. equalMatcher, ok := matcher.(*EqualMatcher)
  40. if ok {
  41. matcher = equalMatcher.Expected
  42. }
  43. elements = append(elements, matcher)
  44. }
  45. return
  46. }
  47. func flatten(elems []interface{}) []interface{} {
  48. if len(elems) != 1 || !isArrayOrSlice(elems[0]) {
  49. return elems
  50. }
  51. value := reflect.ValueOf(elems[0])
  52. flattened := make([]interface{}, value.Len())
  53. for i := 0; i < value.Len(); i++ {
  54. flattened[i] = value.Index(i).Interface()
  55. }
  56. return flattened
  57. }
  58. func matchers(expectedElems []interface{}) (matchers []interface{}) {
  59. for _, e := range flatten(expectedElems) {
  60. matcher, isMatcher := e.(omegaMatcher)
  61. if !isMatcher {
  62. matcher = &EqualMatcher{Expected: e}
  63. }
  64. matchers = append(matchers, matcher)
  65. }
  66. return
  67. }
  68. func presentable(elems []interface{}) interface{} {
  69. elems = flatten(elems)
  70. if len(elems) == 0 {
  71. return []interface{}{}
  72. }
  73. sv := reflect.ValueOf(elems)
  74. tt := sv.Index(0).Elem().Type()
  75. for i := 1; i < sv.Len(); i++ {
  76. if sv.Index(i).Elem().Type() != tt {
  77. return elems
  78. }
  79. }
  80. ss := reflect.MakeSlice(reflect.SliceOf(tt), sv.Len(), sv.Len())
  81. for i := 0; i < sv.Len(); i++ {
  82. ss.Index(i).Set(sv.Index(i).Elem())
  83. }
  84. return ss.Interface()
  85. }
  86. func valuesOf(actual interface{}) []interface{} {
  87. value := reflect.ValueOf(actual)
  88. values := []interface{}{}
  89. if isMap(actual) {
  90. keys := value.MapKeys()
  91. for i := 0; i < value.Len(); i++ {
  92. values = append(values, value.MapIndex(keys[i]).Interface())
  93. }
  94. } else {
  95. for i := 0; i < value.Len(); i++ {
  96. values = append(values, value.Index(i).Interface())
  97. }
  98. }
  99. return values
  100. }
  101. func (matcher *ConsistOfMatcher) FailureMessage(actual interface{}) (message string) {
  102. message = format.Message(actual, "to consist of", presentable(matcher.Elements))
  103. message = appendMissingElements(message, matcher.missingElements)
  104. if len(matcher.extraElements) > 0 {
  105. message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,
  106. format.Object(presentable(matcher.extraElements), 1))
  107. }
  108. return
  109. }
  110. func appendMissingElements(message string, missingElements []interface{}) string {
  111. if len(missingElements) == 0 {
  112. return message
  113. }
  114. return fmt.Sprintf("%s\nthe missing elements were\n%s", message,
  115. format.Object(presentable(missingElements), 1))
  116. }
  117. func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  118. return format.Message(actual, "not to consist of", presentable(matcher.Elements))
  119. }