Path: blob/main/vendor/github.com/onsi/gomega/matchers/consist_of.go
2880 views
// untested sections: 312package matchers34import (5"fmt"6"reflect"78"github.com/onsi/gomega/format"9"github.com/onsi/gomega/matchers/internal/miter"10"github.com/onsi/gomega/matchers/support/goraph/bipartitegraph"11)1213type ConsistOfMatcher struct {14Elements []any15missingElements []any16extraElements []any17}1819func (matcher *ConsistOfMatcher) Match(actual any) (success bool, err error) {20if !isArrayOrSlice(actual) && !isMap(actual) && !miter.IsIter(actual) {21return false, fmt.Errorf("ConsistOf matcher expects an array/slice/map/iter.Seq/iter.Seq2. Got:\n%s", format.Object(actual, 1))22}2324matchers := matchers(matcher.Elements)25values := valuesOf(actual)2627bipartiteGraph, err := bipartitegraph.NewBipartiteGraph(values, matchers, neighbours)28if err != nil {29return false, err30}3132edges := bipartiteGraph.LargestMatching()33if len(edges) == len(values) && len(edges) == len(matchers) {34return true, nil35}3637var missingMatchers []any38matcher.extraElements, missingMatchers = bipartiteGraph.FreeLeftRight(edges)39matcher.missingElements = equalMatchersToElements(missingMatchers)4041return false, nil42}4344func neighbours(value, matcher any) (bool, error) {45match, err := matcher.(omegaMatcher).Match(value)46return match && err == nil, nil47}4849func equalMatchersToElements(matchers []any) (elements []any) {50for _, matcher := range matchers {51if equalMatcher, ok := matcher.(*EqualMatcher); ok {52elements = append(elements, equalMatcher.Expected)53} else if _, ok := matcher.(*BeNilMatcher); ok {54elements = append(elements, nil)55} else {56elements = append(elements, matcher)57}58}59return60}6162func flatten(elems []any) []any {63if len(elems) != 1 ||64!(isArrayOrSlice(elems[0]) ||65(miter.IsIter(elems[0]) && !miter.IsSeq2(elems[0]))) {66return elems67}6869if miter.IsIter(elems[0]) {70flattened := []any{}71miter.IterateV(elems[0], func(v reflect.Value) bool {72flattened = append(flattened, v.Interface())73return true74})75return flattened76}7778value := reflect.ValueOf(elems[0])79flattened := make([]any, value.Len())80for i := 0; i < value.Len(); i++ {81flattened[i] = value.Index(i).Interface()82}83return flattened84}8586func matchers(expectedElems []any) (matchers []any) {87for _, e := range flatten(expectedElems) {88if e == nil {89matchers = append(matchers, &BeNilMatcher{})90} else if matcher, isMatcher := e.(omegaMatcher); isMatcher {91matchers = append(matchers, matcher)92} else {93matchers = append(matchers, &EqualMatcher{Expected: e})94}95}96return97}9899func presentable(elems []any) any {100elems = flatten(elems)101102if len(elems) == 0 {103return []any{}104}105106sv := reflect.ValueOf(elems)107firstEl := sv.Index(0)108if firstEl.IsNil() {109return elems110}111tt := firstEl.Elem().Type()112for i := 1; i < sv.Len(); i++ {113el := sv.Index(i)114if el.IsNil() || (sv.Index(i).Elem().Type() != tt) {115return elems116}117}118119ss := reflect.MakeSlice(reflect.SliceOf(tt), sv.Len(), sv.Len())120for i := 0; i < sv.Len(); i++ {121ss.Index(i).Set(sv.Index(i).Elem())122}123124return ss.Interface()125}126127func valuesOf(actual any) []any {128value := reflect.ValueOf(actual)129values := []any{}130if miter.IsIter(actual) {131if miter.IsSeq2(actual) {132miter.IterateKV(actual, func(k, v reflect.Value) bool {133values = append(values, v.Interface())134return true135})136} else {137miter.IterateV(actual, func(v reflect.Value) bool {138values = append(values, v.Interface())139return true140})141}142} else if isMap(actual) {143keys := value.MapKeys()144for i := 0; i < value.Len(); i++ {145values = append(values, value.MapIndex(keys[i]).Interface())146}147} else {148for i := 0; i < value.Len(); i++ {149values = append(values, value.Index(i).Interface())150}151}152153return values154}155156func (matcher *ConsistOfMatcher) FailureMessage(actual any) (message string) {157message = format.Message(actual, "to consist of", presentable(matcher.Elements))158message = appendMissingElements(message, matcher.missingElements)159if len(matcher.extraElements) > 0 {160message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,161format.Object(presentable(matcher.extraElements), 1))162}163return164}165166func appendMissingElements(message string, missingElements []any) string {167if len(missingElements) == 0 {168return message169}170return fmt.Sprintf("%s\nthe missing elements were\n%s", message,171format.Object(presentable(missingElements), 1))172}173174func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual any) (message string) {175return format.Message(actual, "not to consist of", presentable(matcher.Elements))176}177178179