|
| 1 | +package matchers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/onsi/gomega/format" |
| 8 | + "github.com/onsi/gomega/types" |
| 9 | +) |
| 10 | + |
| 11 | +const maxIndirections = 31 |
| 12 | + |
| 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 | + |
| 33 | +type HaveValueMatcher struct { |
| 34 | + Matcher types.GomegaMatcher // the matcher to apply to the "resolved" actual value. |
| 35 | + resolvedActual interface{} // the ("resolved") value. |
| 36 | +} |
| 37 | + |
| 38 | +func (m *HaveValueMatcher) Match(actual interface{}) (bool, error) { |
| 39 | + val := reflect.ValueOf(actual) |
| 40 | + for allowedIndirs := maxIndirections; allowedIndirs > 0; allowedIndirs-- { |
| 41 | + // return an error if value isn't valid. Please note that we cannot |
| 42 | + // check for nil here, as we might not deal with a pointer or interface |
| 43 | + // at this point. |
| 44 | + if !val.IsValid() { |
| 45 | + return false, errors.New(format.Message( |
| 46 | + actual, "not to be <nil>")) |
| 47 | + } |
| 48 | + switch val.Kind() { |
| 49 | + case reflect.Ptr, reflect.Interface: |
| 50 | + // resolve pointers and interfaces to their values, then rinse and |
| 51 | + // repeat. |
| 52 | + if val.IsNil() { |
| 53 | + return false, errors.New(format.Message( |
| 54 | + actual, "not to be <nil>")) |
| 55 | + } |
| 56 | + val = val.Elem() |
| 57 | + continue |
| 58 | + default: |
| 59 | + // forward the final value to the specified matcher. |
| 60 | + m.resolvedActual = val.Interface() |
| 61 | + return m.Matcher.Match(m.resolvedActual) |
| 62 | + } |
| 63 | + } |
| 64 | + // too many indirections: extreme star gazing, indeed...? |
| 65 | + return false, errors.New(format.Message(actual, "too many indirections")) |
| 66 | +} |
| 67 | + |
| 68 | +func (m *HaveValueMatcher) FailureMessage(_ interface{}) (message string) { |
| 69 | + return m.Matcher.FailureMessage(m.resolvedActual) |
| 70 | +} |
| 71 | + |
| 72 | +func (m *HaveValueMatcher) NegatedFailureMessage(_ interface{}) (message string) { |
| 73 | + return m.Matcher.NegatedFailureMessage(m.resolvedActual) |
| 74 | +} |
0 commit comments