summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/have_existing_field_matcher.go
blob: b57018745fae085cb89705b5fef772797aff99d2 (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
package matchers

import (
	"errors"
	"fmt"

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

type HaveExistingFieldMatcher struct {
	Field string
}

func (matcher *HaveExistingFieldMatcher) Match(actual interface{}) (success bool, err error) {
	// we don't care about the field's actual value, just about any error in
	// trying to find the field (or method).
	_, err = extractField(actual, matcher.Field, "HaveExistingField")
	if err == nil {
		return true, nil
	}
	var mferr missingFieldError
	if errors.As(err, &mferr) {
		// missing field errors aren't errors in this context, but instead
		// unsuccessful matches.
		return false, nil
	}
	return false, err
}

func (matcher *HaveExistingFieldMatcher) FailureMessage(actual interface{}) (message string) {
	return fmt.Sprintf("Expected\n%s\nto have field '%s'", format.Object(actual, 1), matcher.Field)
}

func (matcher *HaveExistingFieldMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return fmt.Sprintf("Expected\n%s\nnot to have field '%s'", format.Object(actual, 1), matcher.Field)
}