benjamin84 / fest

Automatically exported from code.google.com/p/fest
0 stars 0 forks source link

BigDecimal in assertions will fail if scale is different #107

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If you do something like this:

BigDecimal actual = new BigDecimal("50.00");
BigDecimal expected = new BigDecimal("50");
Assertions.assertThat(actual).isEqualTo(expected);

Instead of a passing test, you'll get:

java.lang.AssertionError: expected:<50> but was:<50.00>
    at org.fest.assertions.Fail.fail(Fail.java:125)
    at org.fest.assertions.Fail.failIfNotEqual(Fail.java:73)
    at org.fest.assertions.GenericAssert.assertEqualTo(GenericAssert.java:146)
    at org.fest.assertions.ObjectAssert.isEqualTo(ObjectAssert.java:134)

This is due to the strange way that equals() works for BigDecimals (see the
JavaDoc for BigDecimal#equals()) -- you need to use compareTo() instead.

So, this means that FEST needs to explicitly handle BigDecimals differently
than all other objects.

Original issue reported on code.google.com by tedyo...@gmail.com on 31 Jan 2008 at 12:40

GoogleCodeExporter commented 9 years ago

Original comment by Alex.Rui...@gmail.com on 31 Jan 2008 at 12:58

GoogleCodeExporter commented 9 years ago
I've implemented this and there are two things to note:
1. isEqualTo() uses .equals() and .isEqualByComparingTo() uses compareTo() == 0.
2. The isZero() assertion uses the compareTo() instead of .equals() so that 0 
== 0.0
== 0.000, which I think makes more sense than 0 != 0.0. Otherwise we'd have to 
have
an isZeroByComparingTo(), which is awkward.

Original comment by tedyo...@gmail.com on 12 Feb 2008 at 3:47

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks Ted! and sorry for the late reply :)

Regards,
-Yvonne.

Original comment by wanghy1...@gmail.com on 17 Feb 2008 at 3:16

GoogleCodeExporter commented 9 years ago
Applied provided patch.

Thanks a lot Ted for your contribution! :)

-Yvonne

Original comment by wanghy1...@gmail.com on 17 Feb 2008 at 3:48

GoogleCodeExporter commented 9 years ago
Set the module as a label, instead of being part of the title.

Original comment by Alex.Rui...@gmail.com on 1 Dec 2008 at 1:50