Skip to content

Commit e8314a6

Browse files
author
joeretro
committed
Java5ified number matchers. Combined IsGreaterThan and IsLessThan into IsComparableTo matcher. Added factory methods for <= and >=.
1 parent 5f9f9e3 commit e8314a6

12 files changed

Lines changed: 149 additions & 119 deletions

File tree

‎src/library/org/hamcrest/Matchers.java‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.hamcrest.core.IsNull;
1313
import org.hamcrest.core.IsSame;
1414
import org.hamcrest.core.Or;
15-
import org.hamcrest.number.IsCloseTo;
1615
import org.hamcrest.object.HasProperty;
1716
import org.hamcrest.object.HasPropertyWithValue;
1817
import org.hamcrest.object.HasToString;
@@ -79,10 +78,6 @@ public static IsEqual eq(double operand) {
7978
return eq(new Double(operand));
8079
}
8180

82-
public static IsCloseTo eq(double operand, double error) {
83-
return new IsCloseTo(operand, error);
84-
}
85-
8681
public static IsSame same(Object operand) {
8782
return new IsSame(operand);
8883
}
@@ -107,16 +102,16 @@ public static StringEndsWith endsWith(String substring) {
107102
return new StringEndsWith(substring);
108103
}
109104

110-
public static IsNot not(Matcher c) {
111-
return new IsNot(c);
105+
public static <T> Matcher<T> not(Matcher<T> c) {
106+
return new IsNot<T>(c);
112107
}
113108

114-
public static And and(Matcher left, Matcher right) {
115-
return new And(left, right);
109+
public static <T> Matcher<T> and(Matcher<T> left, Matcher<T> right) {
110+
return new And<T>(left, right);
116111
}
117112

118-
public static Or or(Matcher left, Matcher right) {
119-
return new Or(left, right);
113+
public static <T> Matcher<T> or(Matcher<T> left, Matcher<T> right) {
114+
return new Or<T>(left, right);
120115
}
121116

122117
public static HasPropertyWithValue hasProperty(String propertyName, Matcher expectation) {

‎src/library/org/hamcrest/core/And.java‎

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

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
@@ -31,4 +32,10 @@ public void describeTo(Description description) {
3132
right.describeTo(description);
3233
description.appendText(")");
3334
}
35+
36+
@Factory
37+
public static <T> Matcher<T> and(Matcher<T> left, Matcher<T> right) {
38+
return new And<T>(left, right);
39+
}
40+
3441
}

‎src/library/org/hamcrest/core/IsNot.java‎

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

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
@@ -24,4 +25,10 @@ public void describeTo(Description description) {
2425
description.appendText("not ");
2526
matcher.describeTo(description);
2627
}
28+
29+
@Factory
30+
public static <T> Matcher<T> not(Matcher<T> c) {
31+
return new IsNot<T>(c);
32+
}
33+
2734
}

‎src/library/org/hamcrest/core/Or.java‎

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

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
@@ -32,4 +33,10 @@ public void describeTo(Description description) {
3233
right.describeTo(description);
3334
description.appendText(")");
3435
}
36+
37+
@Factory
38+
public static <T> Matcher<T> or(Matcher<T> left, Matcher<T> right) {
39+
return new Or<T>(left, right);
40+
}
41+
3542
}

‎src/library/org/hamcrest/number/IsCloseTo.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
1011
* Is the value a number equal to a value within some range of
1112
* acceptable error?
1213
*/
13-
public class IsCloseTo implements Matcher {
14+
public class IsCloseTo implements Matcher<Double> {
1415
private final double error;
1516
private final double value;
1617

@@ -19,9 +20,8 @@ public IsCloseTo(double value, double error) {
1920
this.value = value;
2021
}
2122

22-
public boolean match(Object arg) {
23-
double argValue = ((Number) arg).doubleValue();
24-
return Math.abs((argValue - value)) <= error;
23+
public boolean match(Double item) {
24+
return Math.abs((item - value)) <= error;
2525
}
2626

2727
public void describeTo(Description description) {
@@ -30,4 +30,11 @@ public void describeTo(Description description) {
3030
.appendText(" of ")
3131
.appendValue(value);
3232
}
33+
34+
@Factory
35+
public static Matcher<Double> eq(double operand, double error) {
36+
return new IsCloseTo(operand, error);
37+
}
38+
39+
3340
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.number;
4+
5+
import static org.hamcrest.core.IsEqual.eq;
6+
import static org.hamcrest.core.Or.or;
7+
import org.hamcrest.Matcher;
8+
import org.hamcrest.Description;
9+
import org.hamcrest.Factory;
10+
11+
/**
12+
* Is the value less than or greater than another {@link java.lang.Comparable} value?
13+
*/
14+
public class IsComparableTo<T extends Comparable> implements Matcher<T> {
15+
16+
public static enum Operator {
17+
LESS_THAN,
18+
GREATER_THAN
19+
}
20+
21+
private final Comparable<T> compareTo;
22+
private final Operator operator;
23+
24+
public IsComparableTo(Comparable<T> compareTo, Operator operator) {
25+
this.compareTo = compareTo;
26+
this.operator = operator;
27+
}
28+
29+
public boolean match(T item) {
30+
int comparison = compareTo.compareTo(item);
31+
return operator == Operator.GREATER_THAN ? comparison < 0 : comparison > 0;
32+
}
33+
34+
public void describeTo(Description description) {
35+
description.appendText("a value ");
36+
description.appendText(operator == Operator.GREATER_THAN ? "greater than " : "less than ");
37+
description.appendValue(compareTo);
38+
}
39+
40+
@Factory
41+
public static <T extends Comparable> Matcher<Comparable<T>> greaterThan(Comparable<T> value) {
42+
return comparableTo(value, Operator.GREATER_THAN);
43+
}
44+
45+
@Factory
46+
public static <T extends Comparable> Matcher<Comparable<T>> greaterThanOrEqualTo(Comparable<T> value) {
47+
return or(greaterThan(value), eq(value));
48+
}
49+
50+
@Factory
51+
public static <T extends Comparable> Matcher<Comparable<T>> lessThan(Comparable<T> value) {
52+
return comparableTo(value, Operator.LESS_THAN);
53+
}
54+
55+
@Factory
56+
public static <T extends Comparable> Matcher<Comparable<T>> lessThanOrEqualTo(Comparable<T> value) {
57+
return or(lessThan(value), eq(value));
58+
}
59+
60+
private static <T extends Comparable> Matcher<Comparable<T>> comparableTo(Comparable<T> value, Operator operator) {
61+
return new IsComparableTo(value, operator);
62+
}
63+
64+
}

‎src/library/org/hamcrest/number/IsGreaterThan.java‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/library/org/hamcrest/number/IsLessThan.java‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/unit-test/org/hamcrest/number/IsCloseToTest.java‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,20 @@
33
package org.hamcrest.number;
44

55
import org.hamcrest.AbstractMatcherTest;
6-
6+
import org.hamcrest.Matcher;
7+
import static org.hamcrest.number.IsCloseTo.eq;
78

89
public class IsCloseToTest extends AbstractMatcherTest {
910

1011
public void testEvaluatesToTrueIfArgumentIsEqualToADoubleValueWithinSomeError() {
11-
IsCloseTo p = new IsCloseTo(1.0, 0.5);
12-
13-
assertTrue(p.match(new Double(1.0)));
14-
assertTrue(p.match(new Double(0.5)));
15-
assertTrue(p.match(new Double(1.5)));
16-
17-
assertTrue(p.match(new Float(1.0)));
18-
assertTrue(p.match(new Integer(1)));
12+
Matcher<Double> p = eq(1.0, 0.5);
1913

20-
assertTrue("number too large", !p.match(new Double(2.0)));
21-
assertTrue("number too small", !p.match(new Double(0.0)));
14+
assertTrue(p.match(1.0));
15+
assertTrue(p.match(0.5d));
16+
assertTrue(p.match(1.5d));
2217

23-
try {
24-
p.match("wrong type");
25-
fail("ClassCastException expected for wrong type of argument");
26-
}
27-
catch (ClassCastException ex) {
28-
// expected
29-
}
18+
assertTrue("number too large", !p.match(2.0));
19+
assertTrue("number too small", !p.match(0.0));
3020
}
3121

3222
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.number;
4+
5+
import org.hamcrest.AbstractMatcherTest;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.core.IsNot.not;
8+
import static org.hamcrest.number.IsComparableTo.*;
9+
10+
public class IsComparableToTest extends AbstractMatcherTest {
11+
12+
public void testComparesObjectsForGreaterThan() {
13+
assertThat(2, greaterThan(1));
14+
assertThat(0, not(greaterThan(1)));
15+
}
16+
17+
public void testComparesObjectsForLessThan() {
18+
assertThat(2, not(lessThan(1)));
19+
assertThat(0, lessThan(1));
20+
}
21+
22+
public void testAllowsForInclusiveComparisons() {
23+
assertThat(1, not(lessThan(1)));
24+
assertThat(1, not(greaterThan(1)));
25+
26+
assertThat(1, lessThanOrEqualTo(1));
27+
assertThat(1, greaterThanOrEqualTo(1));
28+
}
29+
30+
public void testSupportsDifferentTypesOfComparableObjects() {
31+
assertThat(1.1, greaterThan(1.0));
32+
assertThat(1.0, not(greaterThan(1.0)));
33+
assertThat(0.9, not(greaterThan(1.0)));
34+
35+
assertThat("cc", greaterThan("bb"));
36+
assertThat("bb", not(greaterThan("bb")));
37+
assertThat("aa", not(greaterThan("bb")));
38+
}
39+
}

0 commit comments

Comments
 (0)