Skip to content

Commit 878940c

Browse files
committedJul 25, 2024·
fix incorrect handling of nil slices in HaveExactElements (fixes #771)
1 parent f5bec80 commit 878940c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎matchers/have_exact_elements.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool
3030

3131
lenMatchers := len(matchers)
3232
lenValues := len(values)
33+
success = true
3334

3435
for i := 0; i < lenMatchers || i < lenValues; i++ {
3536
if i >= lenMatchers {
3637
matcher.extraIndex = i
38+
success = false
3739
continue
3840
}
3941

4042
if i >= lenValues {
4143
matcher.missingIndex = i
44+
success = false
4245
return
4346
}
4447

@@ -49,15 +52,17 @@ func (matcher *HaveExactElementsMatcher) Match(actual interface{}) (success bool
4952
index: i,
5053
failure: err.Error(),
5154
})
55+
success = false
5256
} else if !match {
5357
matcher.mismatchFailures = append(matcher.mismatchFailures, mismatchFailure{
5458
index: i,
5559
failure: elemMatcher.FailureMessage(values[i]),
5660
})
61+
success = false
5762
}
5863
}
5964

60-
return matcher.missingIndex+matcher.extraIndex+len(matcher.mismatchFailures) == 0, nil
65+
return success, nil
6166
}
6267

6368
func (matcher *HaveExactElementsMatcher) FailureMessage(actual interface{}) (message string) {

‎matchers/have_exact_elements_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ var _ = Describe("HaveExactElements", func() {
7676
})
7777
})
7878

79+
When("passed nil", func() {
80+
It("should fail correctly", func() {
81+
failures := InterceptGomegaFailures(func() {
82+
var expected []any
83+
Expect([]string{"one"}).Should(HaveExactElements(expected...))
84+
})
85+
Expect(failures).Should(HaveLen(1))
86+
})
87+
})
88+
7989
Describe("Failure Message", func() {
8090
When("actual contains extra elements", func() {
8191
It("should print the starting index of the extra elements", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.