File tree Expand file tree Collapse file tree
main/java/org/hamcrest/number
test/java/org/hamcrest/number Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments