Skip to content

Commit 404f2fe

Browse files
committed
Introduce a ComparatorMatcher.
1 parent 40465aa commit 404f2fe

3 files changed

Lines changed: 381 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package org.hamcrest.comparator;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
8+
import java.util.Comparator;
9+
10+
import static java.lang.Integer.signum;
11+
12+
public final class ComparatorMatcherBuilder<T> {
13+
14+
private final Comparator<T> comparator;
15+
16+
/**
17+
* Creates a matcher factory for matchers of {@code Comparable}s.
18+
* <p/>
19+
* For example:
20+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().lessThanOrEqualTo(1))</pre>
21+
*/
22+
public static <T extends Comparable<T>> ComparatorMatcherBuilder<T> usingDefaultComparison() {
23+
return new ComparatorMatcherBuilder<T>(new Comparator<T>() {
24+
@Override
25+
public int compare(T o1, T o2) {
26+
return o1.compareTo(o2);
27+
}
28+
});
29+
}
30+
31+
/**
32+
* Creates a matcher factory for matchers of {@code Comparators}s of {@code T}.
33+
* <p/>
34+
* For example:
35+
* <pre>assertThat(5, comparedBy(new Comparator<Integer>() {
36+
* public int compare(Integer o1, Integer o2) {
37+
* return -o1.compareTo(o2);
38+
* }
39+
* }).lessThan(4))</pre>
40+
*/
41+
public static <T> ComparatorMatcherBuilder<T> comparedBy(Comparator<T> comparator) {
42+
return new ComparatorMatcherBuilder<T>(comparator);
43+
}
44+
45+
private ComparatorMatcherBuilder(Comparator<T> comparator) {
46+
this.comparator = comparator;
47+
}
48+
49+
private static final class ComparatorMatcher<T> extends TypeSafeMatcher<T> {
50+
private static final int LESS_THAN = -1;
51+
private static final int GREATER_THAN = 1;
52+
private static final int EQUAL = 0;
53+
54+
private final Comparator<T> comparator;
55+
private final T expected;
56+
private final int minCompare, maxCompare;
57+
58+
private static final String[] comparisonDescriptions = {
59+
"less than",
60+
"equal to",
61+
"greater than"
62+
};
63+
64+
private ComparatorMatcher(Comparator<T> comparator, T expected, int minCompare, int maxCompare) {
65+
this.comparator = comparator;
66+
this.expected = expected;
67+
this.minCompare = minCompare;
68+
this.maxCompare = maxCompare;
69+
}
70+
71+
@Override
72+
public boolean matchesSafely(T actual) {
73+
try {
74+
int compare = signum(comparator.compare(actual, expected));
75+
return minCompare <= compare && compare <= maxCompare;
76+
} catch (ClassCastException e) {
77+
return false; // type erasure means someone can shonk in a non-T :(
78+
}
79+
}
80+
81+
@Override
82+
public void describeMismatchSafely(T actual, Description mismatchDescription) {
83+
mismatchDescription.appendValue(actual).appendText(" was ")
84+
.appendText(asText(comparator.compare(actual, expected)))
85+
.appendText(" ").appendValue(expected);
86+
}
87+
88+
@Override
89+
public void describeTo(Description description) {
90+
description.appendText("a value ").appendText(asText(minCompare));
91+
if (minCompare != maxCompare) {
92+
description.appendText(" or ").appendText(asText(maxCompare));
93+
}
94+
description.appendText(" ").appendValue(expected);
95+
}
96+
97+
private static String asText(int comparison) {
98+
return comparisonDescriptions[signum(comparison) + 1];
99+
}
100+
}
101+
102+
/**
103+
* Creates a matcher of {@code T} object that matches when the examined object is
104+
* equal to the specified value, as reported by the {@code Comparator} used to
105+
* create this builder.
106+
* <p/>
107+
* For example:
108+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().comparesEqualTo(1))</pre>
109+
*
110+
* @param value the value which, when passed to the Comparator supplied to this builder, should return zero
111+
*/
112+
@Factory
113+
public Matcher<T> comparesEqualTo(T value) {
114+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.EQUAL);
115+
}
116+
117+
/**
118+
* Creates a matcher of {@code T} object that matches when the examined object is
119+
* greater than the specified value, as reported by the {@code Comparator} used to
120+
* create this builder.
121+
* <p/>
122+
* For example:
123+
* <pre>assertThat(2, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().greaterThan(1))</pre>
124+
*
125+
* @param value the value which, when passed to the Comparator supplied to this builder, should return greater
126+
* than zero
127+
*/
128+
@Factory
129+
public Matcher<T> greaterThan(T value) {
130+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.GREATER_THAN, ComparatorMatcher.GREATER_THAN);
131+
}
132+
133+
/**
134+
* Creates a matcher of {@code T} object that matches when the examined object is
135+
* greater than or equal to the specified value, as reported by the {@code Comparator} used to
136+
* create this builder.
137+
* <p/>
138+
* For example:
139+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().greaterThanOrEqualTo(1))</pre>
140+
*
141+
* @param value the value which, when passed to the Comparator supplied to this builder, should return greater
142+
* than or equal to zero
143+
*/
144+
@Factory
145+
public Matcher<T> greaterThanOrEqualTo(T value) {
146+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.GREATER_THAN);
147+
}
148+
149+
/**
150+
* Creates a matcher of {@code T} object that matches when the examined object is
151+
* less than the specified value, as reported by the {@code Comparator} used to
152+
* create this builder.
153+
* <p/>
154+
* For example:
155+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().lessThan(2))</pre>
156+
*
157+
* @param value the value which, when passed to the Comparator supplied to this builder, should return less
158+
* than zero
159+
*/
160+
@Factory
161+
public Matcher<T> lessThan(T value) {
162+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.LESS_THAN);
163+
}
164+
165+
/**
166+
* Creates a matcher of {@code T} object that matches when the examined object is
167+
* less than or equal to the specified value, as reported by the {@code Comparator} used to
168+
* create this builder.
169+
* <p/>
170+
* For example:
171+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().lessThanOrEqualTo(1))</pre>
172+
*
173+
* @param value the value which, when passed to the Comparator supplied to this builder, should return less
174+
* than or equal to zero
175+
*/
176+
@Factory
177+
public Matcher<T> lessThanOrEqualTo(T value) {
178+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.EQUAL);
179+
}
180+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Copyright (c) 2000-2009 hamcrest.org
2+
*/
3+
package org.hamcrest.comparator;
4+
5+
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
7+
8+
import java.math.BigDecimal;
9+
import java.util.Comparator;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.comparator.ComparatorMatcherBuilder.comparedBy;
13+
import static org.hamcrest.core.IsNot.not;
14+
15+
public class ComparatorMatcherBuilderTest extends AbstractMatcherTest {
16+
17+
private final ComparatorMatcherBuilder<Integer> integerComparatorMatcherBuilder = ComparatorMatcherBuilder.usingDefaultComparison();
18+
private final ComparatorMatcherBuilder<Double> doubleComparatorMatcherBuilder = ComparatorMatcherBuilder.usingDefaultComparison();
19+
private final ComparatorMatcherBuilder<String> stringComparatorMatcherBuilder = ComparatorMatcherBuilder.usingDefaultComparison();
20+
private final ComparatorMatcherBuilder<BigDecimal> bigDecimalComparatorMatcherBuilder = ComparatorMatcherBuilder.usingDefaultComparison();
21+
22+
@Override
23+
protected Matcher<Integer> createMatcher() {
24+
return integerComparatorMatcherBuilder.greaterThan(1);
25+
}
26+
27+
public void testDescription() {
28+
assertDescription("a value greater than <1>", integerComparatorMatcherBuilder.greaterThan(1));
29+
assertDescription("a value equal to or greater than <1>", integerComparatorMatcherBuilder.greaterThanOrEqualTo(1));
30+
assertDescription("a value equal to <1>", integerComparatorMatcherBuilder.comparesEqualTo(1));
31+
assertDescription("a value less than or equal to <1>", integerComparatorMatcherBuilder.lessThanOrEqualTo(1));
32+
assertDescription("a value less than <1>", integerComparatorMatcherBuilder.lessThan(1));
33+
}
34+
35+
public void testMismatchDescriptions() {
36+
assertMismatchDescription("<0> was less than <1>", integerComparatorMatcherBuilder.greaterThan(1), 0);
37+
assertMismatchDescription("<1> was equal to <1>", integerComparatorMatcherBuilder.greaterThan(1), 1);
38+
assertMismatchDescription("<1> was greater than <0>", integerComparatorMatcherBuilder.lessThan(0), 1);
39+
assertMismatchDescription("<2> was equal to <2>", integerComparatorMatcherBuilder.lessThan(2), 2);
40+
}
41+
42+
public void testComparesObjectsForGreaterThan() {
43+
assertThat(2, integerComparatorMatcherBuilder.greaterThan(1));
44+
assertThat(0, not(integerComparatorMatcherBuilder.greaterThan(1)));
45+
}
46+
47+
public void testComparesObjectsForLessThan() {
48+
assertThat(2, integerComparatorMatcherBuilder.lessThan(3));
49+
assertThat(0, integerComparatorMatcherBuilder.lessThan(1));
50+
}
51+
52+
53+
public void testComparesObjectsForEquality() {
54+
assertThat(3, integerComparatorMatcherBuilder.comparesEqualTo(3));
55+
assertThat("aa", stringComparatorMatcherBuilder.comparesEqualTo("aa"));
56+
}
57+
58+
public void testAllowsForInclusiveComparisons() {
59+
assertThat("less", 1, integerComparatorMatcherBuilder.lessThanOrEqualTo(1));
60+
assertThat("greater", 1, integerComparatorMatcherBuilder.greaterThanOrEqualTo(1));
61+
}
62+
63+
public void testSupportsDifferentTypesOfComparableObjects() {
64+
assertThat(1.1, doubleComparatorMatcherBuilder.greaterThan(1.0));
65+
assertThat("cc", stringComparatorMatcherBuilder.greaterThan("bb"));
66+
}
67+
68+
public void testComparesBigDecimalsWithDifferentScalesCorrectlyForIssue20() {
69+
assertThat(new BigDecimal("10.0"), bigDecimalComparatorMatcherBuilder.greaterThanOrEqualTo(new BigDecimal("10")));
70+
assertThat(new BigDecimal(10), bigDecimalComparatorMatcherBuilder.greaterThanOrEqualTo(new BigDecimal("10.0")));
71+
assertThat(new BigDecimal("2"), bigDecimalComparatorMatcherBuilder.comparesEqualTo(new BigDecimal("2.000")));
72+
}
73+
74+
public void testComparesCustomTypesWhoseCompareToReturnsValuesGreaterThatOne() {
75+
assertThat(new CustomInt(5), ComparatorMatcherBuilder.<CustomInt>usingDefaultComparison().lessThan(new CustomInt(10)));
76+
}
77+
78+
public void testComparesByCustomComparator() {
79+
assertThat(5, comparedBy(new Comparator<Integer>() {
80+
@Override
81+
public int compare(Integer o1, Integer o2) {
82+
return -o1.compareTo(o2);
83+
}
84+
}).lessThan(4));
85+
}
86+
87+
public void testJavadocExamples() {
88+
assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().comparesEqualTo(1));
89+
assertThat(2, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().greaterThan(1));
90+
assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().greaterThanOrEqualTo(1));
91+
assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().lessThan(2));
92+
assertThat(1, ComparatorMatcherBuilder.<Integer>usingDefaultComparison().lessThanOrEqualTo(1));
93+
assertThat(5, comparedBy(new Comparator<Integer>() {
94+
@Override
95+
public int compare(Integer o1, Integer o2) {
96+
return -o1.compareTo(o2);
97+
}
98+
}).lessThan(4));
99+
}
100+
101+
private static final class CustomInt implements Comparable<CustomInt> {
102+
private final int value;
103+
104+
public CustomInt(int value) {
105+
this.value = value;
106+
}
107+
108+
public int compareTo(CustomInt other) {
109+
return value - other.value;
110+
}
111+
}
112+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Copyright (c) 2000-2009 hamcrest.org
2+
*/
3+
package org.hamcrest.comparator;
4+
5+
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
7+
8+
import java.math.BigDecimal;
9+
import java.util.Comparator;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.core.IsNot.not;
13+
import static org.hamcrest.number.OrderingComparison.*;
14+
15+
public class ComparatorMatcherTest extends AbstractMatcherTest {
16+
17+
@Override
18+
protected Matcher<Integer> createMatcher() {
19+
return ComparatorMatcherBuilder.comparedBy(new Comparator<Integer>() {
20+
@Override
21+
public int compare(Integer o1, Integer o2) {
22+
return o1.compareTo(o2);
23+
}
24+
}).greaterThan(1);
25+
}
26+
27+
public void testDescription() {
28+
assertDescription("a value greater than <1>", greaterThan(1));
29+
assertDescription("a value equal to or greater than <1>", greaterThanOrEqualTo(1));
30+
assertDescription("a value equal to <1>", comparesEqualTo(1));
31+
assertDescription("a value less than or equal to <1>", lessThanOrEqualTo(1));
32+
assertDescription("a value less than <1>", lessThan(1));
33+
}
34+
35+
public void testMismatchDescriptions() {
36+
assertMismatchDescription("<0> was less than <1>", greaterThan(1), 0);
37+
assertMismatchDescription("<1> was equal to <1>", greaterThan(1), 1);
38+
assertMismatchDescription("<1> was greater than <0>", lessThan(0), 1);
39+
assertMismatchDescription("<2> was equal to <2>", lessThan(2), 2);
40+
}
41+
42+
public void testComparesObjectsForGreaterThan() {
43+
assertThat(2, greaterThan(1));
44+
assertThat(0, not(greaterThan(1)));
45+
}
46+
47+
public void testComparesObjectsForLessThan() {
48+
assertThat(2, lessThan(3));
49+
assertThat(0, lessThan(1));
50+
}
51+
52+
53+
public void testComparesObjectsForEquality() {
54+
assertThat(3, comparesEqualTo(3));
55+
assertThat("aa", comparesEqualTo("aa"));
56+
}
57+
58+
public void testAllowsForInclusiveComparisons() {
59+
assertThat("less", 1, lessThanOrEqualTo(1));
60+
assertThat("greater", 1, greaterThanOrEqualTo(1));
61+
}
62+
63+
public void testSupportsDifferentTypesOfComparableObjects() {
64+
assertThat(1.1, greaterThan(1.0));
65+
assertThat("cc", greaterThan("bb"));
66+
}
67+
68+
public void testComparesBigDecimalsWithDifferentScalesCorrectlyForIssue20() {
69+
assertThat(new BigDecimal("10.0"), greaterThanOrEqualTo(new BigDecimal("10")));
70+
assertThat(new BigDecimal(10), greaterThanOrEqualTo(new BigDecimal("10.0")));
71+
assertThat(new BigDecimal("2"), comparesEqualTo(new BigDecimal("2.000")));
72+
}
73+
74+
public void testComparesCustomTypesWhoseCompareToReturnsValuesGreaterThatOne() {
75+
assertThat(new CustomInt(5), lessThan(new CustomInt(10)));
76+
}
77+
78+
private static final class CustomInt implements Comparable<CustomInt> {
79+
private final int value;
80+
81+
public CustomInt(int value) {
82+
this.value = value;
83+
}
84+
85+
public int compareTo(CustomInt other) {
86+
return value - other.value;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)