testify
testify copied to clipboard
assert two NaN values
when i try to assert two NaN values the assert result was
$ go test
--- FAIL: TestNaN (0.00s)
main_test.go:10:
Error Trace: main_test.go:10
Error: Not equal:
expected: NaN
actual : NaN
Test: TestNaN
FAIL
FAIL command-line-arguments 0.230s
the code that genrate this error is
package test
import (
"github.com/stretchr/testify/assert"
"math"
"testing"
)
func TestNaN(t *testing.T) {
assert.Equal(t, math.NaN(), math.NaN())
}
because the assert.Equal
is based on reflerct.DeepEqual
in it documentations written that two floats a,b will be equal if a == b is true.
the golang the act according IEEE 754 is defined NaN == NaN as false.
so this may need discussion about adding assert checking for NaN values.
https://github.com/stretchr/testify/issues/632#issuecomment-404729847
@yahelmanor2 NaN its not a number, so you can't compare those 2 values. math.NaN() == math.NaN()
its false
, so the assert Equal output you've got is as per expectations.
thanks @devdinu,but there is any way I can test that value equal to NaN
within the assert package. I know that assert.True(t,math.IsNaN(i))
will work but I think that it will be better if a solution will be exist within the assert package(i can open PR if needed).
@yahelmanor2 I think that you should do that :)
This NaN comparison logic, though not necessarily this function, would be useful to have when comparing deeply nested structs and slices. For example, if I expected to get a slice [0.0, NaN, 8.0]
there is nothing I can do (that I know of) except loop through elements.