Skip to content

Commit 46b5ec0

Browse files
committed
https://github.com/hamcrest/JavaHamcrest/issues/8
Fixed mismatch reporting on isCloseTo()
1 parent 749ef4b commit 46b5ec0

5 files changed

Lines changed: 72 additions & 60 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.hamcrest.number;
22

3-
import java.math.BigDecimal;
4-
import java.math.MathContext;
5-
63
import org.hamcrest.Description;
74
import org.hamcrest.Factory;
85
import org.hamcrest.Matcher;
96
import org.hamcrest.TypeSafeMatcher;
107

8+
import java.math.BigDecimal;
9+
import java.math.MathContext;
10+
1111
public class BigDecimalCloseTo extends TypeSafeMatcher<BigDecimal> {
1212

1313
private final BigDecimal delta;
@@ -27,7 +27,9 @@ public boolean matchesSafely(BigDecimal item) {
2727
public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) {
2828
mismatchDescription.appendValue(item)
2929
.appendText(" differed by ")
30-
.appendValue(actualDelta(item));
30+
.appendValue(actualDelta(item))
31+
.appendText(" more than delta ")
32+
.appendValue(delta);
3133
}
3234

3335
@Override

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
package org.hamcrest.number;
44

55
import org.hamcrest.Description;
6-
import org.hamcrest.Matcher;
76
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
88
import org.hamcrest.TypeSafeMatcher;
99

10+
import static java.lang.Math.abs;
11+
1012

1113
/**
1214
* Is the value a number equal to a value within some range of
@@ -30,7 +32,9 @@ public boolean matchesSafely(Double item) {
3032
public void describeMismatchSafely(Double item, Description mismatchDescription) {
3133
mismatchDescription.appendValue(item)
3234
.appendText(" differed by ")
33-
.appendValue(actualDelta(item));
35+
.appendValue(actualDelta(item))
36+
.appendText(" more than delta ")
37+
.appendValue(delta);
3438
}
3539

3640
@Override
@@ -42,7 +46,7 @@ public void describeTo(Description description) {
4246
}
4347

4448
private double actualDelta(Double item) {
45-
return (Math.abs((item - value)) - delta);
49+
return abs(item - value) - delta;
4650
}
4751

4852
/**

‎hamcrest-unit-test/src/main/java/org/hamcrest/AbstractMatcherTest.java‎

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,49 @@
77

88
public abstract class AbstractMatcherTest extends TestCase {
99

10-
/**
11-
* Create an instance of the Matcher so some generic safety-net tests can be run on it.
12-
*/
13-
protected abstract Matcher<?> createMatcher();
14-
15-
public static <T> void assertMatches(String message, Matcher<? super T> c, T arg) {
16-
Assert.assertTrue(message, c.matches(arg));
17-
}
18-
19-
public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) {
20-
Assert.assertFalse(message, c.matches(arg));
21-
}
22-
23-
public static void assertDescription(String expected, Matcher<?> matcher) {
24-
Description description = new StringDescription();
25-
description.appendDescriptionOf(matcher);
26-
Assert.assertEquals("Expected description", expected, description.toString().trim());
27-
}
28-
29-
public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) {
30-
Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg));
31-
Description description = new StringDescription();
32-
matcher.describeMismatch(arg, description);
33-
Assert.assertEquals("Expected mismatch description", expected, description.toString().trim());
34-
}
35-
36-
public void testIsNullSafe() {
37-
// should not throw a NullPointerException
38-
createMatcher().matches(null);
39-
}
40-
41-
public void testCopesWithUnknownTypes() {
42-
// should not throw ClassCastException
43-
createMatcher().matches(new UnknownType());
44-
}
45-
46-
public static class UnknownType {
10+
/**
11+
* Create an instance of the Matcher so some generic safety-net tests can be run on it.
12+
*/
13+
protected abstract Matcher<?> createMatcher();
14+
15+
public static <T> void assertMatches(String message, Matcher<? super T> matcher, T arg) {
16+
if (!matcher.matches(arg)) {
17+
Assert.fail(message + " because: '" + mismatchDescription(matcher, arg) + "'");
4718
}
19+
}
20+
21+
public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) {
22+
Assert.assertFalse(message, c.matches(arg));
23+
}
24+
25+
public static void assertDescription(String expected, Matcher<?> matcher) {
26+
Description description = new StringDescription();
27+
description.appendDescriptionOf(matcher);
28+
Assert.assertEquals("Expected description", expected, description.toString().trim());
29+
}
30+
31+
public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) {
32+
Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg));
33+
Assert.assertEquals("Expected mismatch description", expected, mismatchDescription(matcher, arg));
34+
}
35+
36+
private static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
37+
Description description = new StringDescription();
38+
matcher.describeMismatch(arg, description);
39+
return description.toString().trim();
40+
}
41+
42+
public void testIsNullSafe() {
43+
// should not throw a NullPointerException
44+
createMatcher().matches(null);
45+
}
46+
47+
public void testCopesWithUnknownTypes() {
48+
// should not throw ClassCastException
49+
createMatcher().matches(new UnknownType());
50+
}
51+
52+
public static class UnknownType {
53+
}
4854

4955
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hamcrest.number;
22

3-
import static org.hamcrest.number.BigDecimalCloseTo.closeTo;
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
45

56
import java.math.BigDecimal;
67

7-
import org.hamcrest.AbstractMatcherTest;
8-
import org.hamcrest.Matcher;
8+
import static org.hamcrest.number.BigDecimalCloseTo.closeTo;
99

1010
public class BigDecimalCloseToTest extends AbstractMatcherTest {
1111

@@ -23,9 +23,9 @@ public void testEvaluatesToTrueIfArgumentIsEqualToABigDecimalWithinSomeError() {
2323
assertTrue(p.matches(new BigDecimal("1.5")));
2424

2525
assertDoesNotMatch("too large", p, new BigDecimal("2.0"));
26-
assertMismatchDescription("<2.0> differed by <0.5>", p, new BigDecimal("2.0"));
26+
assertMismatchDescription("<2.0> differed by <0.5> more than delta <0.5>", p, new BigDecimal("2.0"));
2727
assertDoesNotMatch("number too small", p, new BigDecimal("0.0"));
28-
assertMismatchDescription("<0.0> differed by <0.5>", p, new BigDecimal("0.0"));
28+
assertMismatchDescription("<0.0> differed by <0.5> more than delta <0.5>", p, new BigDecimal("0.0"));
2929
}
3030

3131
public void testEvaluatesToTrueIfArgumentHasDifferentScale() {
@@ -36,9 +36,9 @@ public void testEvaluatesToTrueIfArgumentHasDifferentScale() {
3636
assertTrue(p.matches(new BigDecimal("1.500000")));
3737

3838
assertDoesNotMatch("too large", p, new BigDecimal("2.000000"));
39-
assertMismatchDescription("<2.000000> differed by <0.5>", p, new BigDecimal("2.000000"));
39+
assertMismatchDescription("<2.000000> differed by <0.5> more than delta <0.5>", p, new BigDecimal("2.000000"));
4040
assertDoesNotMatch("number too small", p, new BigDecimal("0.000000"));
41-
assertMismatchDescription("<0.000000> differed by <0.5>", p, new BigDecimal("0.000000"));
41+
assertMismatchDescription("<0.000000> differed by <0.5> more than delta <0.5>", p, new BigDecimal("0.000000"));
4242
}
4343

4444
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55
import org.hamcrest.AbstractMatcherTest;
66
import org.hamcrest.Matcher;
7+
78
import static org.hamcrest.number.IsCloseTo.closeTo;
89

910
public class IsCloseToTest extends AbstractMatcherTest {
1011

1112
@Override
1213
protected Matcher<?> createMatcher() {
13-
double irrelevant = 0.1;
14+
final double irrelevant = 0.1;
1415
return closeTo(irrelevant, irrelevant);
1516
}
1617

17-
public void testEvaluatesToTrueIfArgumentIsEqualToADoubleValueWithinSomeError() {
18-
Matcher<Double> p = closeTo(1.0, 0.5);
18+
public void test_matchesIfArgumentIsEqualToADoubleValueWithinSomeError() {
19+
final Matcher<Double> p = closeTo(1.0d, 0.5d);
1920

20-
assertTrue(p.matches(1.0));
21-
assertTrue(p.matches(0.5d));
22-
assertTrue(p.matches(1.5d));
21+
assertMatches("1.0", p, 1.0);
22+
assertMatches("0.5d", p, 0.5d);
23+
assertMatches("1.5d", p, 1.5d);
2324

2425
assertDoesNotMatch("too large", p, 2.0);
25-
assertMismatchDescription("<2.0> differed by <0.5>", p, 2.0);
26+
assertMismatchDescription("<3.0> differed by <1.5> more than delta <0.5>", p, 3.0d);
2627
assertDoesNotMatch("number too small", p, 0.0);
27-
assertMismatchDescription("<0.0> differed by <0.5>", p, 0.0);
28+
assertMismatchDescription("<0.1> differed by <0.4> more than delta <0.5>", p, 0.1);
2829
}
29-
3030
}

0 commit comments

Comments
 (0)