From d28fbca74ce59a4576e255704bc991c4a34a3353 Mon Sep 17 00:00:00 2001 From: piyush295 <126451870+piyush295@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:53:07 +0530 Subject: [PATCH] fix(assert): InDeltaSlice should fail on length mismatch 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 #1263 --- assert/assertions.go | 4 ++++ assert/assertions_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 166f63726..79224f345 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1497,6 +1497,10 @@ func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAn actualSlice := reflect.ValueOf(actual) expectedSlice := reflect.ValueOf(expected) + if expectedSlice.Len() != actualSlice.Len() { + return Fail(t, "Parameters must have the same number of elements", msgAndArgs...) + } + for i := 0; i < actualSlice.Len(); i++ { result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, msgAndArgs...) if !result { diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 11642e096..a408123f7 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2360,6 +2360,16 @@ func TestInDeltaSlice(t *testing.T) { 0.1), "{1, NaN, 2} is not element-wise close to {0, NaN, 3} in delta=0.1") False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail") + + False(t, InDeltaSlice(mockT, + []float64{1, 2}, + []float64{1}, + 0.001), "Slices of different lengths should fail even if the common prefix matches") + + False(t, InDeltaSlice(mockT, + []float64{1}, + []float64{1, 2}, + 0.001), "Slices of different lengths should fail regardless of which is longer") } func TestInDeltaMapValues(t *testing.T) {