forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_of_matcher.go
45 lines (37 loc) · 1.15 KB
/
one_of_matcher.go
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
package matchers
import (
"fmt"
"strings"
"github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
type OneOfMatcher struct {
Elements []interface{}
}
func (m *OneOfMatcher) Match(actual interface{}) (success bool, err error) {
for _, value := range m.Elements {
submatcher, elementIsMatcher := value.(types.GomegaMatcher)
if !elementIsMatcher {
submatcher = gomega.Equal(value)
}
success, err = submatcher.Match(actual)
if success || err != nil {
return
}
}
return
}
func (m *OneOfMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\n%s\n%s", format.Object(actual, 1), "to match one of", m.expectedValues())
}
func (m *OneOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\n%s\n%s", format.Object(actual, 1), "not to match one of", m.expectedValues())
}
func (m *OneOfMatcher) expectedValues() string {
expectedValues := make([]string, len(m.Elements), len(m.Elements))
for i, matcher := range m.Elements {
expectedValues[i] = format.Object(matcher, 1)
}
return strings.Join(expectedValues, "\nor\n")
}