github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/matchers/contain_elements_in_order.go (about) 1 package matchers 2 3 import ( 4 "errors" 5 "github.com/onsi/gomega" 6 "github.com/onsi/gomega/format" 7 "reflect" 8 ) 9 10 type ContainElementsInOrderMatcher struct { 11 Elements interface{} 12 } 13 14 func ContainElementsInOrder(elements ...interface{}) gomega.OmegaMatcher { 15 return &ContainElementsInOrderMatcher{ 16 Elements: elements, 17 } 18 } 19 20 func (matcher *ContainElementsInOrderMatcher) Match(actual interface{}) (success bool, err error) { 21 if !isArrayOrSlice(actual) || !isArrayOrSlice(matcher.Elements) { 22 return false, errors.New("expected an array") 23 } 24 25 actualValue := reflect.ValueOf(actual) 26 expectedValue := reflect.ValueOf(matcher.Elements) 27 28 index := 0 29 30 OUTER: 31 for i := 0; i < expectedValue.Len(); i++ { 32 for ; index < actualValue.Len(); index++ { 33 if reflect.DeepEqual(expectedValue.Index(i).Interface(), actualValue.Index(index).Interface()) { 34 index = index + 1 35 continue OUTER 36 } 37 } 38 39 return false, nil 40 } 41 42 return true, nil 43 } 44 45 func (matcher *ContainElementsInOrderMatcher) FailureMessage(actual interface{}) (message string) { 46 return format.Message(actual, "to contain elements in order", matcher.Elements) 47 } 48 49 func (matcher *ContainElementsInOrderMatcher) NegatedFailureMessage(actual interface{}) (message string) { 50 return format.Message(actual, "not to contain elements in order", matcher.Elements) 51 } 52 53 func isArrayOrSlice(a interface{}) bool { 54 if a == nil { 55 return false 56 } 57 switch reflect.TypeOf(a).Kind() { 58 case reflect.Array, reflect.Slice: 59 return true 60 default: 61 return false 62 } 63 }