lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
893 stars 115 forks source link

Add assertion for floating point number format #789

Open kiwi-oss opened 4 months ago

kiwi-oss commented 4 months ago

Read the documentation, maybe we already have the feature There's a section about numerical comparisons, but I don't need to compare numbers, I need to determine the type.

There's a section about type placeholders, but it only mentions asserting the number type without distinguishing numbers with or without fractional part.

Is your feature request related to a problem? Please describe. I need to assert that a number has a fractional part, e.g. 653.401 or even 12.0, but not 12.

Describe the solution you'd like There's already JsonAssert#isIntegralNumber, but there's no negated counterpart. So one idea would be to add JsonAssert#isNotIntegralNumber.

This has the downside that the name leaves room for the interpretation that non-numbers like strings would also pass the assertion. To mitigate that, one could pick a name like isNumberWithFractionalPart. But then isIntegralNumber would have to be renamed to isNumberWithoutFractionalPart accordingly.

I'd find it even more intuitive to write JsonAssertions.assertThatJson("1.0").isNumber().hasFractionalPart() and .doesNotHaveFractionalPart() accordingly, but this would have to be added to AssertJ's BigDecimalAssert.

Describe alternatives you've considered I first used .isNumber().scale().isNotZero() as workaround, but the failure message is confusing because it refers to the scale, not the checked number. Adding withFailMessage isn't that useful as it doesn't allow printing the checked number.

Now I'm using .isNumber().has(new Condition<>(bd -> bd.scale() != 0, "a fractional part")) and that's fine for now, but I think users shouldn't be forced to write an own solution when the negated assertion is already available.

lukas-krecan commented 4 months ago

Hi, thanks for feedback. Just a clarification question, to what categories would you put 10e3 or 1.2e12? Can you try to file a similar ticket to AssertJ, adding it there would IMO help the most.

kiwi-oss commented 4 months ago

Thanks for the question! It made me a realise that my suggestions for the method names aren't precise enough. Both 10e3 and 1.2e12 should pass the assertion I'm looking for, i.e. isNumberWithFractionalPart or hasFractionalPart should return true.

I basically want to know whether the number is provided in a floating point syntax. I saw two possible points of confusion that I wanted to avoid in the name:

Therefore, I focussed on the fractional part in the names but forgot about the exponential "e" notation.

So for clarity, my updated propositions are JsonAssert#isNumberInFloatingPointFormat and JsonAssert#isNumberInIntegerFormat, but I will file a corresponding AssertJ ticket.

lukas-krecan commented 4 months ago

Cool, if the AssertJ team decides they don't want to add it, I can do it on JsonUnit side