Skip to content

Commit

Permalink
fix(ContainElements): consistently flatten expected values
Browse files Browse the repository at this point in the history
  • Loading branch information
blgm committed Jan 31, 2021
1 parent 7266efe commit 073b880
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 0 additions & 2 deletions matchers/consist_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ var _ = Describe("ConsistOf", func() {
expected := `Expected\n.*\["A", "B"\]\nnot to consist of\n.*: \["A", "B"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})

})
})

})
4 changes: 2 additions & 2 deletions matchers/contain_elements_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (matcher *ContainElementsMatcher) Match(actual interface{}) (success bool,
}

func (matcher *ContainElementsMatcher) FailureMessage(actual interface{}) (message string) {
message = format.Message(actual, "to contain elements", matcher.Elements)
message = format.Message(actual, "to contain elements", flatten(matcher.Elements))
return appendMissingElements(message, matcher.missingElements)
}

func (matcher *ContainElementsMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to contain elements", matcher.Elements)
return format.Message(actual, "not to contain elements", flatten(matcher.Elements))
}
20 changes: 20 additions & 0 deletions matchers/contain_elements_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,25 @@ var _ = Describe("ContainElements", func() {
expected := "Expected\n.*\\[2\\]\nto contain elements\n.*\\[1, 2, 3\\]\nthe missing elements were\n.*\\[1, 3\\]"
Expect(failures).To(ContainElements(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(ContainElements([]string{"A", "D"}))
})

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

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

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

0 comments on commit 073b880

Please sign in to comment.