diff --git a/matchers.go b/matchers.go index 46ff4ddbf..b46e461a5 100644 --- a/matchers.go +++ b/matchers.go @@ -370,6 +370,26 @@ func HaveField(field string, expected interface{}) types.GomegaMatcher { } } +// 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 +// values only. HaveValue will fail with an error if a pointer or interface is +// nil. It will also fail for more than 31 pointer or interface dereferences to +// guard against mistakenly applying it to arbitrarily deep linked pointers. +// +// HaveValue differs from gstruct.PointTo in that it does not expect actual to +// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer +// and even interface values. +// +// actual := 42 +// Expect(actual).To(HaveValue(42)) +// Expect(&actual).To(HaveValue(42)) +func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher { + return &matchers.HaveValueMatcher{ + Matcher: matcher, + } +} + //BeNumerically performs numerical assertions in a type-agnostic way. //Actual and expected should be numbers, though the specific type of //number is irrelevant (float32, float64, uint8, etc...). diff --git a/matchers/have_value.go b/matchers/have_value.go index 58f4404db..f67252835 100644 --- a/matchers/have_value.go +++ b/matchers/have_value.go @@ -10,26 +10,6 @@ import ( 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 -// values only. HaveValue will fail with an error if a pointer or interface is -// nil. It will also fail for more than 31 pointer or interface dereferences to -// guard against mistakenly applying it to arbitrarily deep linked pointers. -// -// HaveValue differs from gstruct.PointTo in that it does not expect actual to -// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer -// and even interface values. -// -// actual := 42 -// Expect(actual).To(HaveValue(42)) -// Expect(&actual).To(HaveValue(42)) -func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher { - return &HaveValueMatcher{ - Matcher: matcher, - } -} - type HaveValueMatcher struct { Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value. resolvedActual interface{} // the ("resolved") value. diff --git a/matchers/have_value_test.go b/matchers/have_value_test.go index 32c8ade29..f2485a065 100644 --- a/matchers/have_value_test.go +++ b/matchers/have_value_test.go @@ -5,7 +5,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - . "github.com/onsi/gomega/matchers" ) type I interface {