Skip to content

Commit 3e9f124

Browse files
committed
Introduce a ComparatorMatcher.
1 parent 40465aa commit 3e9f124

3 files changed

Lines changed: 409 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)