From 1fa946843c5fda1eb18fe7fdd483deceffc9f3bf Mon Sep 17 00:00:00 2001 From: George Blue Date: Tue, 2 Feb 2021 15:02:16 +0000 Subject: [PATCH] fix(BeElementOf): consistently flatten expected values --- matchers/be_element_of_matcher.go | 22 ++++------------------ matchers/be_element_of_matcher_test.go | 6 +++--- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/matchers/be_element_of_matcher.go b/matchers/be_element_of_matcher.go index 1f9d7a8e6..9ee75a5d5 100644 --- a/matchers/be_element_of_matcher.go +++ b/matchers/be_element_of_matcher.go @@ -18,23 +18,9 @@ func (matcher *BeElementOfMatcher) Match(actual interface{}) (success bool, err return false, fmt.Errorf("BeElement matcher expects actual to be typed") } - length := len(matcher.Elements) - valueAt := func(i int) interface{} { - return matcher.Elements[i] - } - // Special handling of a single element of type Array or Slice - if length == 1 && isArrayOrSlice(valueAt(0)) { - element := valueAt(0) - value := reflect.ValueOf(element) - length = value.Len() - valueAt = func(i int) interface{} { - return value.Index(i).Interface() - } - } - var lastError error - for i := 0; i < length; i++ { - matcher := &EqualMatcher{Expected: valueAt(i)} + for _, m := range flatten(matcher.Elements) { + matcher := &EqualMatcher{Expected: m} success, err := matcher.Match(actual) if err != nil { lastError = err @@ -49,9 +35,9 @@ func (matcher *BeElementOfMatcher) Match(actual interface{}) (success bool, err } func (matcher *BeElementOfMatcher) FailureMessage(actual interface{}) (message string) { - return format.Message(actual, "to be an element of", matcher.Elements) + 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", matcher.Elements) + return format.Message(actual, "not to be an element of", presentable(matcher.Elements)) } diff --git a/matchers/be_element_of_matcher_test.go b/matchers/be_element_of_matcher_test.go index a461aabcb..a42abf415 100644 --- a/matchers/be_element_of_matcher_test.go +++ b/matchers/be_element_of_matcher_test.go @@ -33,7 +33,7 @@ var _ = Describe("BeElementOf", func() { }) When("passed a correctly typed nil", func() { - It("should operate succesfully on the passed in value", func() { + It("should operate successfully on the passed in value", func() { var nilSlice []int Expect(1).ShouldNot(BeElementOf(nilSlice)) @@ -56,11 +56,11 @@ var _ = Describe("BeElementOf", func() { It("builds failure message", func() { actual := BeElementOf(1, 2).FailureMessage(123) - Expect(actual).To(Equal("Expected\n : 123\nto be an element of\n <[]interface {} | len:2, cap:2>: [1, 2]")) + Expect(actual).To(Equal("Expected\n : 123\nto be an element of\n <[]int | len:2, cap:2>: [1, 2]")) }) It("builds negated failure message", func() { actual := BeElementOf(1, 2).NegatedFailureMessage(123) - Expect(actual).To(Equal("Expected\n : 123\nnot to be an element of\n <[]interface {} | len:2, cap:2>: [1, 2]")) + Expect(actual).To(Equal("Expected\n : 123\nnot to be an element of\n <[]int | len:2, cap:2>: [1, 2]")) }) })