smartystreets / goconvey

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
http://smartystreets.github.io/goconvey/
Other
8.23k stars 554 forks source link

ShouldResemble float slices #651

Closed kkbase closed 2 years ago

kkbase commented 2 years ago

Expected: '[]float64{1.1, 2}' Actual: '[]float64{1.100000023841858, 2}' (Should resemble)! Diff: '[]float64{1.100000023841858, 2}'

ShouldAlmostEqual can not handle slices...

riannucci commented 2 years ago

Floats, as you are probably very aware, are difficult to work with correctly because they don't provide exact representation for the numbers they store (they are imprecise).

ShouldResemble is an analog to reflect.DeepEqual, and is meant to indicate that two things have the exact same value (but unlike ShouldEqual, don't have to be precisely the same object in memory, just another object which holds the same value). I don't think it would be appropriate to have it fuzzy-match floats, because the underlying values are different.

ShouldAlmostEqual already accepts a delta parameter for its fudge-factor (or uses a "small delta" as a default if you don't provide one). It's asserting that the two floats are "close, but not necessarily equal" for some value of "close".

I would recommend looping over your slices to compare item-by-item (or maybe write a custom assertion for this, if this is common in your code).

I suppose it could be possible for ShouldAlmostEqual to gain the ability to do this slice iteration, but you'd have to ask in smartystreets/assertions.