summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/be_element_of_matcher.go
blob: 9ee75a5d51159b35ee25c3dd1fb9083e81533ee1 (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
// untested sections: 1

package matchers

import (
	"fmt"
	"reflect"

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

type BeElementOfMatcher struct {
	Elements []interface{}
}

func (matcher *BeElementOfMatcher) Match(actual interface{}) (success bool, err error) {
	if reflect.TypeOf(actual) == nil {
		return false, fmt.Errorf("BeElement matcher expects actual to be typed")
	}

	var lastError error
	for _, m := range flatten(matcher.Elements) {
		matcher := &EqualMatcher{Expected: m}
		success, err := matcher.Match(actual)
		if err != nil {
			lastError = err
			continue
		}
		if success {
			return true, nil
		}
	}

	return false, lastError
}

func (matcher *BeElementOfMatcher) FailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "to be an element of", presentable(matcher.Elements))
}

func (matcher *BeElementOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "not to be an element of", presentable(matcher.Elements))
}