Path: blob/main/vendor/github.com/onsi/gomega/matchers/have_value.go
2880 views
package matchers12import (3"errors"4"reflect"56"github.com/onsi/gomega/format"7"github.com/onsi/gomega/types"8)910const maxIndirections = 311112type HaveValueMatcher struct {13Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value.14resolvedActual any // the ("resolved") value.15}1617func (m *HaveValueMatcher) Match(actual any) (bool, error) {18val := reflect.ValueOf(actual)19for allowedIndirs := maxIndirections; allowedIndirs > 0; allowedIndirs-- {20// return an error if value isn't valid. Please note that we cannot21// check for nil here, as we might not deal with a pointer or interface22// at this point.23if !val.IsValid() {24return false, errors.New(format.Message(25actual, "not to be <nil>"))26}27switch val.Kind() {28case reflect.Ptr, reflect.Interface:29// resolve pointers and interfaces to their values, then rinse and30// repeat.31if val.IsNil() {32return false, errors.New(format.Message(33actual, "not to be <nil>"))34}35val = val.Elem()36continue37default:38// forward the final value to the specified matcher.39m.resolvedActual = val.Interface()40return m.Matcher.Match(m.resolvedActual)41}42}43// too many indirections: extreme star gazing, indeed...?44return false, errors.New(format.Message(actual, "too many indirections"))45}4647func (m *HaveValueMatcher) FailureMessage(_ any) (message string) {48return m.Matcher.FailureMessage(m.resolvedActual)49}5051func (m *HaveValueMatcher) NegatedFailureMessage(_ any) (message string) {52return m.Matcher.NegatedFailureMessage(m.resolvedActual)53}545556