From 5a42d2af7c799af3db07e80bfbd307d0ea98b8f2 Mon Sep 17 00:00:00 2001 From: x0Lazarus <113273587+x0Lazarus@users.noreply.github.com> Date: Sat, 26 Sep 2026 01:50:24 -0700 Subject: [PATCH] assert: reject NaN tolerance in InDelta --- assert/assertions.go | 4 ++++ assert/assertions_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 166f63726..9358cc1aa 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1456,6 +1456,10 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs h.Helper() } + if math.IsNaN(delta) { + return Fail(t, "delta must not be NaN", msgAndArgs...) + } + af, aok := toFloat(expected) bf, bok := toFloat(actual) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 11642e096..5708b7c51 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2339,6 +2339,26 @@ func TestInDelta(t *testing.T) { } } +func TestInDeltaNaNDelta(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + expected, actual float64 + }{ + {"different values", 1, 100}, + {"equal values", 1, 1}, + {"NaN values", math.NaN(), math.NaN()}, + } { + t.Run(tc.name, func(t *testing.T) { + mockT := new(mockTestingT) + False(t, InDelta(mockT, tc.expected, tc.actual, math.NaN(), "comparison %d", 42)) + Contains(t, mockT.errorString(), "delta must not be NaN") + Contains(t, mockT.errorString(), "comparison 42") + }) + } +} + func TestInDeltaSlice(t *testing.T) { t.Parallel()