Permalink
Browse files

#8

Fixed mismatch reporting on isCloseTo()
  • Loading branch information...
1 parent 749ef4b commit 46b5ec01781f74e7c5aa0857150ed2ef30c662c4 @sf105 sf105 committed Aug 28, 2012
@@ -1,13 +1,13 @@
package org.hamcrest.number;
-import java.math.BigDecimal;
-import java.math.MathContext;
-
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
+import java.math.BigDecimal;
+import java.math.MathContext;
+
public class BigDecimalCloseTo extends TypeSafeMatcher<BigDecimal> {
private final BigDecimal delta;
@@ -27,7 +27,9 @@ public boolean matchesSafely(BigDecimal item) {
public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" differed by ")
- .appendValue(actualDelta(item));
+ .appendValue(actualDelta(item))
+ .appendText(" more than delta ")
+ .appendValue(delta);
}
@Override
@@ -3,10 +3,12 @@
package org.hamcrest.number;
import org.hamcrest.Description;
-import org.hamcrest.Matcher;
import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
+import static java.lang.Math.abs;
+
/**
* Is the value a number equal to a value within some range of
@@ -30,7 +32,9 @@ public boolean matchesSafely(Double item) {
public void describeMismatchSafely(Double item, Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" differed by ")
- .appendValue(actualDelta(item));
+ .appendValue(actualDelta(item))
+ .appendText(" more than delta ")
+ .appendValue(delta);
}
@Override
@@ -42,7 +46,7 @@ public void describeTo(Description description) {
}
private double actualDelta(Double item) {
- return (Math.abs((item - value)) - delta);
+ return abs(item - value) - delta;
}
/**
@@ -7,43 +7,49 @@
public abstract class AbstractMatcherTest extends TestCase {
- /**
- * Create an instance of the Matcher so some generic safety-net tests can be run on it.
- */
- protected abstract Matcher<?> createMatcher();
-
- public static <T> void assertMatches(String message, Matcher<? super T> c, T arg) {
- Assert.assertTrue(message, c.matches(arg));
- }
-
- public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) {
- Assert.assertFalse(message, c.matches(arg));
- }
-
- public static void assertDescription(String expected, Matcher<?> matcher) {
- Description description = new StringDescription();
- description.appendDescriptionOf(matcher);
- Assert.assertEquals("Expected description", expected, description.toString().trim());
- }
-
- public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) {
- Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg));
- Description description = new StringDescription();
- matcher.describeMismatch(arg, description);
- Assert.assertEquals("Expected mismatch description", expected, description.toString().trim());
- }
-
- public void testIsNullSafe() {
- // should not throw a NullPointerException
- createMatcher().matches(null);
- }
-
- public void testCopesWithUnknownTypes() {
- // should not throw ClassCastException
- createMatcher().matches(new UnknownType());
- }
-
- public static class UnknownType {
+ /**
+ * Create an instance of the Matcher so some generic safety-net tests can be run on it.
+ */
+ protected abstract Matcher<?> createMatcher();
+
+ public static <T> void assertMatches(String message, Matcher<? super T> matcher, T arg) {
+ if (!matcher.matches(arg)) {
+ Assert.fail(message + " because: '" + mismatchDescription(matcher, arg) + "'");
}
+ }
+
+ public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) {
+ Assert.assertFalse(message, c.matches(arg));
+ }
+
+ public static void assertDescription(String expected, Matcher<?> matcher) {
+ Description description = new StringDescription();
+ description.appendDescriptionOf(matcher);
+ Assert.assertEquals("Expected description", expected, description.toString().trim());
+ }
+
+ public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) {
+ Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg));
+ Assert.assertEquals("Expected mismatch description", expected, mismatchDescription(matcher, arg));
+ }
+
+ private static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
+ Description description = new StringDescription();
+ matcher.describeMismatch(arg, description);
+ return description.toString().trim();
+ }
+
+ public void testIsNullSafe() {
+ // should not throw a NullPointerException
+ createMatcher().matches(null);
+ }
+
+ public void testCopesWithUnknownTypes() {
+ // should not throw ClassCastException
+ createMatcher().matches(new UnknownType());
+ }
+
+ public static class UnknownType {
+ }
}
@@ -1,11 +1,11 @@
package org.hamcrest.number;
-import static org.hamcrest.number.BigDecimalCloseTo.closeTo;
+import org.hamcrest.AbstractMatcherTest;
+import org.hamcrest.Matcher;
import java.math.BigDecimal;
-import org.hamcrest.AbstractMatcherTest;
-import org.hamcrest.Matcher;
+import static org.hamcrest.number.BigDecimalCloseTo.closeTo;
public class BigDecimalCloseToTest extends AbstractMatcherTest {
@@ -23,9 +23,9 @@ public void testEvaluatesToTrueIfArgumentIsEqualToABigDecimalWithinSomeError() {
assertTrue(p.matches(new BigDecimal("1.5")));
assertDoesNotMatch("too large", p, new BigDecimal("2.0"));
- assertMismatchDescription("<2.0> differed by <0.5>", p, new BigDecimal("2.0"));
+ assertMismatchDescription("<2.0> differed by <0.5> more than delta <0.5>", p, new BigDecimal("2.0"));
assertDoesNotMatch("number too small", p, new BigDecimal("0.0"));
- assertMismatchDescription("<0.0> differed by <0.5>", p, new BigDecimal("0.0"));
+ assertMismatchDescription("<0.0> differed by <0.5> more than delta <0.5>", p, new BigDecimal("0.0"));
}
public void testEvaluatesToTrueIfArgumentHasDifferentScale() {
@@ -36,9 +36,9 @@ public void testEvaluatesToTrueIfArgumentHasDifferentScale() {
assertTrue(p.matches(new BigDecimal("1.500000")));
assertDoesNotMatch("too large", p, new BigDecimal("2.000000"));
- assertMismatchDescription("<2.000000> differed by <0.5>", p, new BigDecimal("2.000000"));
+ assertMismatchDescription("<2.000000> differed by <0.5> more than delta <0.5>", p, new BigDecimal("2.000000"));
assertDoesNotMatch("number too small", p, new BigDecimal("0.000000"));
- assertMismatchDescription("<0.000000> differed by <0.5>", p, new BigDecimal("0.000000"));
+ assertMismatchDescription("<0.000000> differed by <0.5> more than delta <0.5>", p, new BigDecimal("0.000000"));
}
}
@@ -4,27 +4,27 @@
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
+
import static org.hamcrest.number.IsCloseTo.closeTo;
public class IsCloseToTest extends AbstractMatcherTest {
@Override
protected Matcher<?> createMatcher() {
- double irrelevant = 0.1;
+ final double irrelevant = 0.1;
return closeTo(irrelevant, irrelevant);
}
- public void testEvaluatesToTrueIfArgumentIsEqualToADoubleValueWithinSomeError() {
- Matcher<Double> p = closeTo(1.0, 0.5);
+ public void test_matchesIfArgumentIsEqualToADoubleValueWithinSomeError() {
+ final Matcher<Double> p = closeTo(1.0d, 0.5d);
- assertTrue(p.matches(1.0));
- assertTrue(p.matches(0.5d));
- assertTrue(p.matches(1.5d));
+ assertMatches("1.0", p, 1.0);
+ assertMatches("0.5d", p, 0.5d);
+ assertMatches("1.5d", p, 1.5d);
assertDoesNotMatch("too large", p, 2.0);
- assertMismatchDescription("<2.0> differed by <0.5>", p, 2.0);
+ assertMismatchDescription("<3.0> differed by <1.5> more than delta <0.5>", p, 3.0d);
assertDoesNotMatch("number too small", p, 0.0);
- assertMismatchDescription("<0.0> differed by <0.5>", p, 0.0);
+ assertMismatchDescription("<0.1> differed by <0.4> more than delta <0.5>", p, 0.1);
}
-
}

0 comments on commit 46b5ec0

Please sign in to comment.