Skip to content

Commit 7e687ad

Browse files
authored
export HaveValue matcher in Gomega DSL (#489)
1 parent bdc087c commit 7e687ad

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

matchers.go

+20
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ func HaveField(field string, expected interface{}) types.GomegaMatcher {
370370
}
371371
}
372372

373+
// HaveValue applies the given matcher to the value of actual, optionally and
374+
// repeatedly dereferencing pointers or taking the concrete value of interfaces.
375+
// Thus, the matcher will always be applied to non-pointer and non-interface
376+
// values only. HaveValue will fail with an error if a pointer or interface is
377+
// nil. It will also fail for more than 31 pointer or interface dereferences to
378+
// guard against mistakenly applying it to arbitrarily deep linked pointers.
379+
//
380+
// HaveValue differs from gstruct.PointTo in that it does not expect actual to
381+
// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer
382+
// and even interface values.
383+
//
384+
// actual := 42
385+
// Expect(actual).To(HaveValue(42))
386+
// Expect(&actual).To(HaveValue(42))
387+
func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {
388+
return &matchers.HaveValueMatcher{
389+
Matcher: matcher,
390+
}
391+
}
392+
373393
//BeNumerically performs numerical assertions in a type-agnostic way.
374394
//Actual and expected should be numbers, though the specific type of
375395
//number is irrelevant (float32, float64, uint8, etc...).

matchers/have_value.go

-20
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@ import (
1010

1111
const maxIndirections = 31
1212

13-
// HaveValue applies the given matcher to the value of actual, optionally and
14-
// repeatedly dereferencing pointers or taking the concrete value of interfaces.
15-
// Thus, the matcher will always be applied to non-pointer and non-interface
16-
// values only. HaveValue will fail with an error if a pointer or interface is
17-
// nil. It will also fail for more than 31 pointer or interface dereferences to
18-
// guard against mistakenly applying it to arbitrarily deep linked pointers.
19-
//
20-
// HaveValue differs from gstruct.PointTo in that it does not expect actual to
21-
// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer
22-
// and even interface values.
23-
//
24-
// actual := 42
25-
// Expect(actual).To(HaveValue(42))
26-
// Expect(&actual).To(HaveValue(42))
27-
func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {
28-
return &HaveValueMatcher{
29-
Matcher: matcher,
30-
}
31-
}
32-
3313
type HaveValueMatcher struct {
3414
Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value.
3515
resolvedActual interface{} // the ("resolved") value.

matchers/have_value_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8-
. "github.com/onsi/gomega/matchers"
98
)
109

1110
type I interface {

0 commit comments

Comments
 (0)