Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HaveValue matcher #485

Merged
merged 4 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 16 additions & 20 deletions matchers/have_value.go
@@ -1,12 +1,15 @@
package matchers

import (
"errors"
"reflect"

"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)

const maxIndirections = 31

// HaveValue applies the given matcher to the value of actual, optionally and
// repeatedly dereferencing pointers or taking the concrete value of interfaces.
// Thus, the matcher will always be applied to non-pointer and non-interface
Expand All @@ -28,51 +31,44 @@ func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {
}

type HaveValueMatcher struct {
Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value.
failure string // failure message, if any.
Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value.
resolvedActual interface{} // the ("resolved") value.
}

func (m *HaveValueMatcher) Match(actual interface{}) (bool, error) {
val := reflect.ValueOf(actual)
for allowedIndirs := 32; allowedIndirs > 0; allowedIndirs-- {
for allowedIndirs := maxIndirections; allowedIndirs > 0; allowedIndirs-- {
// return an error if value isn't valid. Please note that we cannot
// check for nil here, as we might not deal with a pointer or interface
// at this point.
if !val.IsValid() {
m.failure = format.Message(
actual, "not to be <nil>")
return false, nil
return false, errors.New(format.Message(
actual, "not to be <nil>"))
}
switch val.Kind() {
case reflect.Ptr, reflect.Interface:
// resolve pointers and interfaces to their values, then rinse and
// repeat.
if val.IsNil() {
m.failure = format.Message(
actual, "not to be <nil>")
return false, nil
return false, errors.New(format.Message(
actual, "not to be <nil>"))
}
val = val.Elem()
continue
default:
// forward the final value to the specified matcher.
elem := val.Interface()
match, err := m.Matcher.Match(elem)
if !match {
m.failure = m.Matcher.FailureMessage(elem)
}
return match, err
m.resolvedActual = val.Interface()
return m.Matcher.Match(m.resolvedActual)
}
}
// too many indirections: extreme star gazing, indeed...?
m.failure = format.Message(actual, "indirecting too many times")
return false, nil
return false, errors.New(format.Message(actual, "too many indirections"))
}

func (m *HaveValueMatcher) FailureMessage(_ interface{}) (message string) {
return m.failure
return m.Matcher.NegatedFailureMessage(m.resolvedActual)
thediveo marked this conversation as resolved.
Show resolved Hide resolved
}

func (m *HaveValueMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return m.Matcher.NegatedFailureMessage(actual)
func (m *HaveValueMatcher) NegatedFailureMessage(_ interface{}) (message string) {
return m.Matcher.NegatedFailureMessage(m.resolvedActual)
}
22 changes: 18 additions & 4 deletions matchers/have_value_test.go
@@ -1,6 +1,8 @@
package matchers_test

import (
"reflect"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
Expand All @@ -21,15 +23,13 @@ var _ = Describe("HaveValue", func() {
It("should fail when passed nil", func() {
var p *struct{}
m := HaveValue(BeNil())
Expect(m.Match(p)).To(BeFalse())
Expect(m.FailureMessage(p)).To(MatchRegexp("not to be <nil>$"))
Expect(m.Match(p)).Error().To(MatchError(MatchRegexp("not to be <nil>$")))
})

It("should fail when passed nil indirectly", func() {
var p *struct{}
m := HaveValue(BeNil())
Expect(m.Match(&p)).To(BeFalse())
Expect(m.FailureMessage(&p)).To(MatchRegexp("not to be <nil>$"))
Expect(m.Match(&p)).Error().To(MatchError(MatchRegexp("not to be <nil>$")))
})

It("should unwrap the value pointed to, even repeatedly", func() {
Expand All @@ -45,6 +45,20 @@ var _ = Describe("HaveValue", func() {
Expect(&pi).NotTo(HaveValue(Equal(2)))
})

It("shouldn't endlessly star-gaze", func() {
dave := "It's full of stars!"
stargazer := reflect.ValueOf(dave)
for stars := 1; stars <= 31; stars++ {
p := reflect.New(stargazer.Type())
p.Elem().Set(stargazer)
stargazer = p
}
m := HaveValue(Equal(dave))
Expect(m.Match(stargazer.Interface())).Error().To(
MatchError(MatchRegexp(`too many indirections`)))
Expect(m.Match(stargazer.Elem().Interface())).To(BeTrue())
})

It("should unwrap the value of an interface", func() {
var i I = &S{V: 42}
Expect(i).To(HaveValue(Equal(S{V: 42})))
Expand Down