fluentassertions / fluentassertions.json

NewtonSoft.Json extensions for FluentAssertions
Apache License 2.0
72 stars 26 forks source link

Feature request: Compare floating numbers using BeEquivalentTo based on a given approximation #58

Closed souhailelk closed 2 years ago

souhailelk commented 2 years ago

We would like to add a new feature while comparing two json objects containing floating numbers as values. We would like to compare the floating numbers with some margin of error.

Let’s take an example: jsonA and jsonB two json objects that contain as values floating numbers with 9 decimal values.

image

Thus, this unit test fails because json1 and jsonB are differents at the ninth decimal value. We would like to be able to consider jsonA and JsonB equal if their floating numbers values are equal at the eighth decimal value.

So, we want to have a feature that allows us to control the approximate equality of floating numbers. Like for instance something like this for absolute tolerance:

jsonA.Should().BeEquivalentTo(jsonB, options => options.Using<double>(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-8, false)).WhenTypeIs<double>());

And for relative tolerance:

jsonA.Should().BeEquivalentTo(jsonB, options => options.Using<double>(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-8, true)).WhenTypeIs<double>());

note : We will be happy to contribute adding this feature by a pull request 😀.

dennisdoomen commented 2 years ago

I like the idea and would most definitely welcome a PR.

souhailelk commented 2 years ago

Thank you, I'll prepare a P.R.

souhailelk commented 2 years ago

Hello,

When will you generate a new package version with this feature?

dennisdoomen commented 2 years ago

I just did. See 6.1.0.

souhailelk commented 2 years ago

I just did. See 6.1.0.

Thank you.