Skip to content

Commit f39c1e6

Browse files
committed
Introduce Double Not-a-number (NaN) matcher to help resolve GitHub issue hamcrest#7
1 parent bda1db3 commit f39c1e6

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎hamcrest-library/matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<!-- Number -->
3838
<factory class="org.hamcrest.number.IsCloseTo"/>
39+
<factory class="org.hamcrest.number.IsNaN"/>
3940
<factory class="org.hamcrest.number.BigDecimalCloseTo"/>
4041
<factory class="org.hamcrest.number.OrderingComparison"/>
4142

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.number;
4+
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
8+
import org.hamcrest.TypeSafeMatcher;
9+
10+
11+
/**
12+
* Is the value a number actually not a number (NaN)?
13+
*/
14+
public final class IsNaN extends TypeSafeMatcher<Double> {
15+
16+
private IsNaN() { }
17+
18+
@Override
19+
public boolean matchesSafely(Double item) {
20+
return Double.isNaN(item);
21+
}
22+
23+
@Override
24+
public void describeMismatchSafely(Double item, Description mismatchDescription) {
25+
mismatchDescription.appendText("was ").appendValue(item);
26+
}
27+
28+
@Override
29+
public void describeTo(Description description) {
30+
description.appendText("a double value of NaN");
31+
}
32+
33+
/**
34+
* Creates a matcher of {@link Double}s that matches when an examined double is not a number.
35+
* <p/>
36+
* For example:
37+
* <pre>assertThat(Double.NaN, is(notANumber()))</pre>
38+
*/
39+
@Factory
40+
public static Matcher<Double> notANumber() {
41+
return new IsNaN();
42+
}
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.hamcrest.number;
2+
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
5+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
6+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
7+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
8+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
9+
import static org.hamcrest.number.IsNaN.notANumber;
10+
11+
import org.hamcrest.Matcher;
12+
import org.junit.Test;
13+
14+
public final class IsNanTest {
15+
16+
@Test public void
17+
copesWithNullsAndUnknownTypes() {
18+
Matcher<Double> matcher = notANumber();
19+
20+
assertNullSafe(matcher);
21+
assertUnknownTypeSafe(matcher);
22+
}
23+
24+
@Test public void
25+
matchesNaN() {
26+
assertMatches("didn't match", notANumber(), Double.NaN);
27+
}
28+
29+
@Test public void
30+
doesNotMatchDoubleValue() {
31+
assertDoesNotMatch("matched unexpectedly", notANumber(), 1.25);
32+
}
33+
34+
@Test public void
35+
doesNotMatchInfinity() {
36+
assertDoesNotMatch("matched unexpectedly", notANumber(), Double.POSITIVE_INFINITY);
37+
}
38+
39+
@Test public void
40+
describesItself() {
41+
assertDescription("a double value of NaN", notANumber());
42+
}
43+
44+
@Test public void
45+
describesAMismatch() {
46+
assertMismatchDescription("was <1.25>", notANumber(), 1.25);
47+
}
48+
}

0 commit comments

Comments
 (0)