forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contain_element_times.go
61 lines (52 loc) · 1.35 KB
/
contain_element_times.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package matchers
import (
"errors"
"fmt"
"github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"reflect"
)
type ContainElementTimesMatcher struct {
Element interface{}
ExpectedCount int
ActualCount int
}
func ContainElementTimes(element interface{}, count int) gomega.OmegaMatcher {
return &ContainElementTimesMatcher{
Element: element,
ExpectedCount: count,
}
}
func (matcher *ContainElementTimesMatcher) Match(actual interface{}) (success bool, err error) {
if !isArrayOrSlice(actual) {
return false, errors.New("expected an array")
}
actualValue := reflect.ValueOf(actual)
for i := 0; i < actualValue.Len(); i++ {
if reflect.DeepEqual(actualValue.Index(i).Interface(), matcher.Element) {
matcher.ActualCount++
}
}
return matcher.ActualCount == matcher.ExpectedCount, nil
}
func (matcher *ContainElementTimesMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(
actual,
fmt.Sprintf(
"to contain element %v %d times, but found it %d times",
matcher.Element,
matcher.ExpectedCount,
matcher.ActualCount,
),
)
}
func (matcher *ContainElementTimesMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(
actual,
fmt.Sprintf(
"not to contain element %v %d times, but it did",
matcher.Element,
matcher.ExpectedCount,
),
)
}