forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contain_elements_in_order.go
63 lines (51 loc) · 1.47 KB
/
contain_elements_in_order.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
62
63
package matchers
import (
"errors"
"github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"reflect"
)
type ContainElementsInOrderMatcher struct {
Elements interface{}
}
func ContainElementsInOrder(elements ...interface{}) gomega.OmegaMatcher {
return &ContainElementsInOrderMatcher{
Elements: elements,
}
}
func (matcher *ContainElementsInOrderMatcher) Match(actual interface{}) (success bool, err error) {
if !isArrayOrSlice(actual) || !isArrayOrSlice(matcher.Elements) {
return false, errors.New("expected an array")
}
actualValue := reflect.ValueOf(actual)
expectedValue := reflect.ValueOf(matcher.Elements)
index := 0
OUTER:
for i := 0; i < expectedValue.Len(); i++ {
for ; index < actualValue.Len(); index++ {
if reflect.DeepEqual(expectedValue.Index(i).Interface(), actualValue.Index(index).Interface()) {
index = index + 1
continue OUTER
}
}
return false, nil
}
return true, nil
}
func (matcher *ContainElementsInOrderMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to contain elements in order", matcher.Elements)
}
func (matcher *ContainElementsInOrderMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to contain elements in order", matcher.Elements)
}
func isArrayOrSlice(a interface{}) bool {
if a == nil {
return false
}
switch reflect.TypeOf(a).Kind() {
case reflect.Array, reflect.Slice:
return true
default:
return false
}
}