Skip to content

Commit

Permalink
fix(ConsistOf): consistently flatten expected values
Browse files Browse the repository at this point in the history
When `ConsistOf()` is used with a slice:
```go
Expect([]string{"A", "B", "C"}).To(ConsistOf([]string{"A", "B"}))
```

It flattens the slice to mean the same as:
```go
Expect([]string{"A", "B", "C"}).To(ConsistOf("A", "B"))
```

It now applies the same flattening in the failure messages,
so instead of getting:
```
Expected
      <[]string | len:3, cap:3>: ["A", "B", "C"]
to consist of
      <[]interface {} | len:1, cap:1>: [["A", "B"]]
```

You now get the more more helpful
```
Expected
      <[]string | len:3, cap:3>: ["A", "B", "C"]
to consist of
      <[]interface {} | len:2, cap:2>: ["A", "B"]
```

Resolves #169
  • Loading branch information
blgm committed Jan 30, 2021
1 parent 5a2b45f commit 7266efe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
26 changes: 15 additions & 11 deletions matchers/consist_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ func equalMatchersToElements(matchers []interface{}) (elements []interface{}) {
return
}

func matchers(expectedElems []interface{}) (matchers []interface{}) {
elems := expectedElems
if len(expectedElems) == 1 && isArrayOrSlice(expectedElems[0]) {
elems = []interface{}{}
value := reflect.ValueOf(expectedElems[0])
for i := 0; i < value.Len(); i++ {
elems = append(elems, value.Index(i).Interface())
}
func flatten(elems []interface{}) []interface{} {
if len(elems) != 1 || !isArrayOrSlice(elems[0]) {
return elems
}

for _, e := range elems {
value := reflect.ValueOf(elems[0])
flattened := make([]interface{}, value.Len())
for i := 0; i < value.Len(); i++ {
flattened[i] = value.Index(i).Interface()
}
return flattened
}

func matchers(expectedElems []interface{}) (matchers []interface{}) {
for _, e := range flatten(expectedElems) {
matcher, isMatcher := e.(omegaMatcher)
if !isMatcher {
matcher = &EqualMatcher{Expected: e}
Expand Down Expand Up @@ -95,7 +99,7 @@ func valuesOf(actual interface{}) []interface{} {
}

func (matcher *ConsistOfMatcher) FailureMessage(actual interface{}) (message string) {
message = format.Message(actual, "to consist of", matcher.Elements)
message = format.Message(actual, "to consist of", flatten(matcher.Elements))
message = appendMissingElements(message, matcher.missingElements)
if len(matcher.extraElements) > 0 {
message = fmt.Sprintf("%s\nthe extra elements were\n%s", message,
Expand All @@ -113,5 +117,5 @@ func appendMissingElements(message string, missingElements []interface{}) string
}

func (matcher *ConsistOfMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to consist of", matcher.Elements)
return format.Message(actual, "not to consist of", flatten(matcher.Elements))
}
22 changes: 22 additions & 0 deletions matchers/consist_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,27 @@ var _ = Describe("ConsistOf", func() {
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
})

When("expected was specified as an array", func() {
It("flattens the array in the expectation message", func() {
failures := InterceptGomegaFailures(func() {
Expect([]string{"A", "B", "C"}).To(ConsistOf([]string{"A", "B"}))
})

expected := `Expected\n.*\["A", "B", "C"\]\nto consist of\n.*: \["A", "B"\]\nthe extra elements were\n.*\["C"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})

It("flattens the array in the negated expectation message", func() {
failures := InterceptGomegaFailures(func() {
Expect([]string{"A", "B"}).NotTo(ConsistOf([]string{"A", "B"}))
})

expected := `Expected\n.*\["A", "B"\]\nnot to consist of\n.*: \["A", "B"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})

})
})

})

0 comments on commit 7266efe

Please sign in to comment.