Skip to content

Commit 60e7cf3

Browse files
committed
Allow collections matchers to work correctly when expected has nil elements
1 parent 67b869d commit 60e7cf3

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

matchers/consist_of.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ func neighbours(value, matcher interface{}) (bool, error) {
4848

4949
func equalMatchersToElements(matchers []interface{}) (elements []interface{}) {
5050
for _, matcher := range matchers {
51-
equalMatcher, ok := matcher.(*EqualMatcher)
52-
if ok {
53-
matcher = equalMatcher.Expected
51+
if equalMatcher, ok := matcher.(*EqualMatcher); ok {
52+
elements = append(elements, equalMatcher.Expected)
53+
} else if _, ok := matcher.(*BeNilMatcher); ok {
54+
elements = append(elements, nil)
55+
} else {
56+
elements = append(elements, matcher)
5457
}
55-
elements = append(elements, matcher)
5658
}
5759
return
5860
}
@@ -72,11 +74,13 @@ func flatten(elems []interface{}) []interface{} {
7274

7375
func matchers(expectedElems []interface{}) (matchers []interface{}) {
7476
for _, e := range flatten(expectedElems) {
75-
matcher, isMatcher := e.(omegaMatcher)
76-
if !isMatcher {
77-
matcher = &EqualMatcher{Expected: e}
77+
if e == nil {
78+
matchers = append(matchers, &BeNilMatcher{})
79+
} else if matcher, isMatcher := e.(omegaMatcher); isMatcher {
80+
matchers = append(matchers, matcher)
81+
} else {
82+
matchers = append(matchers, &EqualMatcher{Expected: e})
7883
}
79-
matchers = append(matchers, matcher)
8084
}
8185
return
8286
}
@@ -89,9 +93,14 @@ func presentable(elems []interface{}) interface{} {
8993
}
9094

9195
sv := reflect.ValueOf(elems)
92-
tt := sv.Index(0).Elem().Type()
96+
firstEl := sv.Index(0)
97+
if firstEl.IsNil() {
98+
return elems
99+
}
100+
tt := firstEl.Elem().Type()
93101
for i := 1; i < sv.Len(); i++ {
94-
if sv.Index(i).Elem().Type() != tt {
102+
el := sv.Index(i)
103+
if el.IsNil() || (sv.Index(i).Elem().Type() != tt) {
95104
return elems
96105
}
97106
}

matchers/consist_of_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ var _ = Describe("ConsistOf", func() {
7474
})
7575
})
7676

77+
When("provided an expectation that has a nil value", func() {
78+
It("should match without failure", func() {
79+
Expect([]any{1, "two", nil}).Should(ConsistOf([]any{nil, 1, "two"}))
80+
Expect([]any{1, "two", "nil", nil}).ShouldNot(ConsistOf([]any{nil, nil, 1, "two"}))
81+
})
82+
})
83+
7784
Describe("FailureMessage", func() {
7885
When("actual contains an extra element", func() {
7986
It("prints the extra element", func() {
@@ -97,6 +104,17 @@ var _ = Describe("ConsistOf", func() {
97104
})
98105
})
99106

107+
When("actual misses a nil element", func() {
108+
It("prints the missing element", func() {
109+
failures := InterceptGomegaFailures(func() {
110+
Expect([]int{2}).Should(ConsistOf(nil, 2))
111+
})
112+
113+
expected := "Expected\n.*\\[2\\]\nto consist of\n.*\\[nil, <int>2\\]\nthe missing elements were\n.*\\[nil\\]"
114+
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
115+
})
116+
})
117+
100118
When("actual contains an extra element and misses an element", func() {
101119
It("prints both the extra and missing element", func() {
102120
failures := InterceptGomegaFailures(func() {

matchers/have_exact_elements_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ var _ = Describe("HaveExactElements", func() {
1313
Expect([]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo", "bar", "baz"))
1414
Expect([]string{"foo", "bar"}).ShouldNot(HaveExactElements("bar", "foo"))
1515
})
16+
17+
It("should work with arbitrary types, including nil", func() {
18+
Expect([]any{"foo", nil, "bar", 17, true, []string{"hi", "there"}}).Should(HaveExactElements("foo", nil, "bar", 17, true, []string{"hi", "there"}))
19+
})
1620
})
1721
Context("with an array", func() {
1822
It("should do the right thing", func() {

0 commit comments

Comments
 (0)