Skip to content

Commit 6e77d9c

Browse files
author
smgfreeman
committed
Issue 20. added comparesEqualTo for Comparables
1 parent 2b358c0 commit 6e77d9c

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparison.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ else if (compare == 0) {
4141
}
4242
}
4343

44+
/**
45+
* Is value = expected?
46+
*/
47+
@Factory
48+
public static <T extends Comparable<T>> Matcher<T> comparesEqualTo(T value) {
49+
return new OrderingComparison<T>(value, 0, 0);
50+
}
51+
4452
/**
4553
* Is value > expected?
4654
*/

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import static org.hamcrest.MatcherAssert.assertThat;
66
import static org.hamcrest.core.IsNot.not;
7+
import static org.hamcrest.number.OrderingComparison.comparesEqualTo;
78
import static org.hamcrest.number.OrderingComparison.greaterThan;
89
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
910
import static org.hamcrest.number.OrderingComparison.lessThan;
@@ -31,20 +32,25 @@ public void testComparesObjectsForLessThan() {
3132
assertThat(0, lessThan(1));
3233
}
3334

35+
36+
public void testComparesObjectsForEquality() {
37+
assertThat(3, comparesEqualTo(3));
38+
assertThat("aa", comparesEqualTo("aa"));
39+
}
40+
3441
public void testAllowsForInclusiveComparisons() {
3542
assertThat(1, lessThanOrEqualTo(1));
3643
assertThat(1, greaterThanOrEqualTo(1));
3744
}
3845

3946
public void testSupportsDifferentTypesOfComparableObjects() {
4047
assertThat(1.1, greaterThan(1.0));
41-
4248
assertThat("cc", greaterThan("bb"));
4349
}
4450

4551
public void testComparesBigDecimalsWithDifferentScalesCorrectlyForIssue20() {
4652
assertThat(new BigDecimal("10.0"), greaterThanOrEqualTo(new BigDecimal("10")));
4753
assertThat(new BigDecimal(10), greaterThanOrEqualTo(new BigDecimal("10.0")));
48-
54+
assertThat(new BigDecimal("2"), comparesEqualTo(new BigDecimal("2.000")));
4955
}
5056
}

0 commit comments

Comments
 (0)