jocarreira / hamcrest

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

OrderingComparisons does not work correctly #20

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
it uses equalTo() in greaterThanOrEqualTo() which is not correct as
equals() and compareTo() behave differently.

Example:
BigDecimal values 10.0 and 10 and not considered equal using equals() as
they differ in scale though the values are the same, but they are
considered equal when using compareTo() as the values are equal (scale is
not compared in this case).

This code also should not work as expected:
Assert.assertThat(BigDecimal.valueOf("10.0"),
OrderingComparisons.greaterThanOrEqualTo(BigDecimal.valueOf("10"));

10.0 is not greater than 10 (using compareTo()) and they are also not equal
using equals(). The funny thing is even the third comparison, 10.0 being
lower than 10 is not true. So this always results to false just because
equalTo() is used instead of compareTo() == 0 in greaterThanOrEqualTo().

I think it would be good to introduce method like compareEqualTo() to
OrderingComparisons which should be public and also used in
greaterThanOrEqualTo() instead of equalTo().

Original issue reported on code.google.com by fordf...@gmail.com on 10 Aug 2007 at 4:42

GoogleCodeExporter commented 8 years ago
The current version seems to work for gt, lt, etc.
Should we change IsEqual to detect Comparables?

Original comment by smgfree...@gmail.com on 13 Oct 2008 at 6:57

GoogleCodeExporter commented 8 years ago
No.   If some classes define equals and numeric comparison differently, then 
IsEqual
should reflect that.

We could use the numeric comparison matcher to implement a numericallyEqualTo(n)
factory function.

Original comment by nat.pr...@gmail.com on 13 Oct 2008 at 7:09

GoogleCodeExporter commented 8 years ago
added comparesEqualTo

Original comment by smgfree...@gmail.com on 13 Oct 2008 at 9:26