aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/onsi/gomega/gstruct/pointer.go
blob: 0a2f35de312fcdf61bac20fb27fc54e787469123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gstruct

import (
	"fmt"
	"reflect"

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

//PointTo applies the given matcher to the value pointed to by actual. It fails if the pointer is
//nil.
//  actual := 5
//  Expect(&actual).To(PointTo(Equal(5)))
func PointTo(matcher types.GomegaMatcher) types.GomegaMatcher {
	return &PointerMatcher{
		Matcher: matcher,
	}
}

type PointerMatcher struct {
	Matcher types.GomegaMatcher

	// Failure message.
	failure string
}

func (m *PointerMatcher) Match(actual interface{}) (bool, error) {
	val := reflect.ValueOf(actual)

	// return error if actual type is not a pointer
	if val.Kind() != reflect.Ptr {
		return false, fmt.Errorf("PointerMatcher expects a pointer but we have '%s'", val.Kind())
	}

	if !val.IsValid() || val.IsNil() {
		m.failure = format.Message(actual, "not to be <nil>")
		return false, nil
	}

	// Forward the value.
	elem := val.Elem().Interface()
	match, err := m.Matcher.Match(elem)
	if !match {
		m.failure = m.Matcher.FailureMessage(elem)
	}
	return match, err
}

func (m *PointerMatcher) FailureMessage(_ interface{}) (message string) {
	return m.failure
}

func (m *PointerMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return m.Matcher.NegatedFailureMessage(actual)
}