Skip to content

fix(assert): InDeltaSlice should fail on length mismatch (#1263) - #2000

Open
piyush295 wants to merge 1 commit into
stretchr:masterfrom
piyush295:fix/indeltaslice-length-check-1263
Open

piyush295 wants to merge 1 commit into
stretchr:masterfrom
piyush295:fix/indeltaslice-length-check-1263

Conversation

@piyush295

Copy link
Copy Markdown

Fixes #1263.

Problem

InDeltaSlice only iterates over the actual slice, so two slices of different lengths can pass as long as every element of the shorter one matches:

assert.InDeltaSlice(t, []float64{1, 2}, []float64{1}, 0.001) // incorrectly passed

As @dolmen noted in the issue, this is inconsistent with InEpsilonSlice, which already enforces equal length via Len(t, actual, expectedLen), and it should be fixed without waiting for v2.0.0.

Fix

Add a length check to InDeltaSlice before the element-wise comparison, mirroring InEpsilonSlice:

if expectedSlice.Len() != actualSlice.Len() {
    return Fail(t, "Parameters must have the same number of elements", msgAndArgs...)
}

Testing

  • Extended TestInDeltaSlice with both length-mismatch directions (shorter actual and longer actual); both now correctly fail.
  • go test ./assert/ and go test ./require/ pass (no regression).
  • go vet ./assert/ clean.

InDeltaSlice only iterated over the actual slice, so comparing slices of
different lengths could pass as long as every element of the shorter
slice matched, e.g.:

    assert.InDeltaSlice(t, []float64{1, 2}, []float64{1}, 0.001) // passed

This is inconsistent with InEpsilonSlice, which already enforces equal
length. Add the same length check so mismatched-length slices fail.

Fixes stretchr#1263
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert.InDeltaSlice doesn't check all expected values present

1 participant