jocarreira / hamcrest

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

OrderingComparison#describeMismatchSafely doesn't work for comparables that return values other than (-1,0,1) #144

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

the code (trunk) says:
    @Override
    public void describeMismatchSafely(T actual, Description mismatchDescription) {
        mismatchDescription.appendValue(actual).appendText(" was ")
                .appendText(asText(actual.compareTo(expected)))
                .appendText(" ").appendValue(expected);
    }

but asText method can only accept range of [-1..1]. 
actual.compareTo(expected) should be wrapped with Integer.signum() here. (Or 
inside getText actually).

Simpliest test case would be:

assertThat(new MyInteger(5), lessThan(new MyInteger(10)));

private static class MyInteger implements Comparable<MyInteger> {
  private final int value;
  public MyInteger(int value) {
    this.value = value;
  }

  public int compareTo(MyInteger other) {
    return other.value - value;
  }
}

Original issue reported on code.google.com by ata...@gmail.com on 31 Mar 2011 at 11:03

GoogleCodeExporter commented 8 years ago
This seems to be fine in version 1.2 but you're right, the trunk code is messed 
up. I haven't tried it but it looks like it should throw an 
ArrayOutOfBoundsException. As far as I can tell, this was broken by r480. 

http://code.google.com/p/hamcrest/source/diff?spec=svn480&r=480&format=side&path
=/trunk/hamcrest-java/hamcrest-library/src/main/java/org/hamcrest/number/Orderin
gComparison.java

Original comment by MatrixFrog on 2 Jul 2011 at 7:17

GoogleCodeExporter commented 8 years ago
tagging

Original comment by t.denley on 12 May 2012 at 10:39

GoogleCodeExporter commented 8 years ago
Fixed with commit 
https://github.com/hamcrest/JavaHamcrest/commit/40b435e94ed6f17bf7668093bffc3c92
efbf0bc2 

Original comment by t.denley on 19 May 2012 at 9:40