Skip to content

Commit dff7af8

Browse files
author
smgfreeman
committed
Fixed Issue 77. OrderingComparison is describing mismatches backwards
1 parent 5992038 commit dff7af8

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* Copyright (c) 2000-2009 hamcrest.org
2+
*/
13
package org.hamcrest.number;
24

35
import org.hamcrest.Description;
@@ -6,84 +8,86 @@
68
import org.hamcrest.TypeSafeMatcher;
79

810
public class OrderingComparison<T extends Comparable<T>> extends TypeSafeMatcher<T> {
9-
private final T value;
11+
private static final int LESS_THAN = -1;
12+
private static final int GREATER_THAN = 1;
13+
private static final int EQUAL = 0;
14+
private final T expected;
1015
private final int minCompare, maxCompare;
1116

12-
private OrderingComparison(T value, int minCompare, int maxCompare) {
13-
this.value = value;
17+
private OrderingComparison(T expected, int minCompare, int maxCompare) {
18+
this.expected = expected;
1419
this.minCompare = minCompare;
1520
this.maxCompare = maxCompare;
1621
}
1722

1823
@Override
19-
public boolean matchesSafely(T other) {
20-
int compare = Integer.signum(value.compareTo(other));
24+
public boolean matchesSafely(T actual) {
25+
int compare = Integer.signum(actual.compareTo(expected));
2126
return minCompare <= compare && compare <= maxCompare;
2227
}
2328

2429
@Override
25-
public void describeMismatchSafely(T item, Description mismatchDescription) {
26-
mismatchDescription.appendValue(value) .appendText(" was ")
27-
.appendText(comparison(value.compareTo(item))).appendText(" ").appendValue(item);
30+
public void describeMismatchSafely(T actual, Description mismatchDescription) {
31+
mismatchDescription.appendValue(expected) .appendText(" was ")
32+
.appendText(comparison(actual.compareTo(expected)))
33+
.appendText(" ").appendValue(actual);
2834
};
2935

3036
public void describeTo(Description description) {
3137
description.appendText("a value ").appendText(comparison(minCompare));
3238
if (minCompare != maxCompare) {
3339
description.appendText(" or ").appendText(comparison(maxCompare));
3440
}
35-
description.appendText(" ").appendValue(value);
41+
description.appendText(" ").appendValue(expected);
3642
}
3743

3844
private String comparison(int compare) {
39-
if (compare > 0) {
40-
return "less than";
41-
}
42-
else if (compare == 0) {
43-
return "equal to ";
44-
}
45-
else {
46-
return "greater than";
47-
}
45+
if (compare == EQUAL) {
46+
return "equal to";
47+
} else if (compare > EQUAL) {
48+
return "greater than";
49+
} else {
50+
return "less than";
51+
}
4852
}
4953

5054
/**
5155
* Is value = expected?
5256
*/
5357
@Factory
5458
public static <T extends Comparable<T>> Matcher<? super T> comparesEqualTo(T value) {
55-
return new OrderingComparison<T>(value, 0, 0);
59+
return new OrderingComparison<T>(value, EQUAL, EQUAL);
5660
}
5761

5862
/**
5963
* Is value > expected?
6064
*/
6165
@Factory
6266
public static <T extends Comparable<T>> Matcher<? super T> greaterThan(T value) {
63-
return new OrderingComparison<T>(value, -1, -1);
67+
return new OrderingComparison<T>(value, GREATER_THAN, GREATER_THAN);
6468
}
6569

6670
/**
6771
* Is value >= expected?
6872
*/
6973
@Factory
7074
public static <T extends Comparable<T>> Matcher<? super T> greaterThanOrEqualTo(T value) {
71-
return new OrderingComparison<T>(value, -1, 0);
75+
return new OrderingComparison<T>(value, EQUAL, GREATER_THAN);
7276
}
7377

7478
/**
7579
* Is value < expected?
7680
*/
7781
@Factory
7882
public static <T extends Comparable<T>> Matcher<? super T> lessThan(T value) {
79-
return new OrderingComparison<T>(value, 1, 1);
83+
return new OrderingComparison<T>(value, LESS_THAN, LESS_THAN);
8084
}
8185

8286
/**
8387
* Is value <= expected?
8488
*/
8589
@Factory
8690
public static <T extends Comparable<T>> Matcher<? super T> lessThanOrEqualTo(T value) {
87-
return new OrderingComparison<T>(value, 0, 1);
91+
return new OrderingComparison<T>(value, LESS_THAN, EQUAL);
8892
}
8993
}

hamcrest-unit-test/src/main/java/org/hamcrest/number/OrderingComparisonTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000-2006 hamcrest.org
1+
/* Copyright (c) 2000-2009 hamcrest.org
22
*/
33
package org.hamcrest.number;
44

@@ -22,6 +22,20 @@ protected Matcher<?> createMatcher() {
2222
return greaterThan(1);
2323
}
2424

25+
public void testDescription() {
26+
assertDescription("a value greater than <1>", greaterThan(1));
27+
assertDescription("a value equal to or greater than <1>", greaterThanOrEqualTo(1));
28+
assertDescription("a value equal to <1>", comparesEqualTo(1));
29+
assertDescription("a value less than or equal to <1>", lessThanOrEqualTo(1));
30+
assertDescription("a value less than <1>", lessThan(1));
31+
}
32+
33+
public void testMismatchDescriptions() {
34+
assertMismatchDescription("<1> was less than <0>", greaterThan(1), 0);
35+
assertMismatchDescription("<1> was equal to <1>", greaterThan(1), 1);
36+
assertMismatchDescription("<0> was greater than <1>", lessThan(0), 1);
37+
}
38+
2539
public void testComparesObjectsForGreaterThan() {
2640
assertThat(2, greaterThan(1));
2741
assertThat(0, not(greaterThan(1)));
@@ -39,8 +53,8 @@ public void testComparesObjectsForEquality() {
3953
}
4054

4155
public void testAllowsForInclusiveComparisons() {
42-
assertThat(1, lessThanOrEqualTo(1));
43-
assertThat(1, greaterThanOrEqualTo(1));
56+
assertThat("less", 1, lessThanOrEqualTo(1));
57+
assertThat("greater", 1, greaterThanOrEqualTo(1));
4458
}
4559

4660
public void testSupportsDifferentTypesOfComparableObjects() {

0 commit comments

Comments
 (0)