Skip to content

Commit fd2b762

Browse files
committed
Merge branch 'master' of github.com:hamcrest/JavaHamcrest
2 parents 82bda3c + fe36747 commit fd2b762

10 files changed

Lines changed: 342 additions & 56 deletions

File tree

‎CHANGES.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
== Version 1.4: Released xx ==
2+
3+
* Removed deprecated methods from previous release
4+
5+
* Improve mismatch description of hasItem/hasItems
6+
7+
* General improvements to mismatch descriptions
8+
9+
* Several JavaDoc improvements and corrections
10+
11+
* Fix gh issue #8 - isCloseTo() shows wrong delta in mismatch description
12+
13+
* Fix issue 131 - Implement IsEmptyMap, IsMapWithSize
14+
15+
* Fix issue 155 - Deprecated several matcher factory methods of the for "isXyz"
16+
17+
* Fix issue 187 - IsArray.describeMismatchSafely() should use Matcher.describeMismatch
18+
19+
* Fix issue 155 - Add Matcher implementation for files
20+
121
== Version 1.3: Released July 09 2012 ==
222

323
* Introduce Condition class to ease the implementation of multi-step matches

‎hamcrest-library/matchers.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
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

4243
<!-- Text -->
4344
<factory class="org.hamcrest.text.IsEqualIgnoringCase"/>
4445
<factory class="org.hamcrest.text.IsEqualIgnoringWhiteSpace"/>
4546
<factory class="org.hamcrest.text.IsEmptyString"/>
47+
<factory class="org.hamcrest.text.IsBlankString"/>
4648
<factory class="org.hamcrest.text.StringContainsInOrder"/>
4749

4850
<!-- Object -->
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
package org.hamcrest.text;
3+
4+
import static org.hamcrest.core.AnyOf.anyOf;
5+
import static org.hamcrest.core.IsNull.nullValue;
6+
7+
import java.util.regex.Pattern;
8+
9+
import org.hamcrest.Description;
10+
import org.hamcrest.Factory;
11+
import org.hamcrest.Matcher;
12+
import org.hamcrest.TypeSafeMatcher;
13+
14+
/**
15+
* Matches blank Strings (and null).
16+
*/
17+
public final class IsBlankString extends TypeSafeMatcher<String> {
18+
private static final IsBlankString BLANK_INSTANCE = new IsBlankString();
19+
@SuppressWarnings("unchecked")
20+
private static final Matcher<String> NULL_OR_BLANK_INSTANCE = anyOf(nullValue(), BLANK_INSTANCE);
21+
22+
private static final Pattern REGEX_WHITESPACE = Pattern.compile("\\s*");
23+
24+
private IsBlankString() { }
25+
26+
@Override
27+
public boolean matchesSafely(String item) {
28+
return REGEX_WHITESPACE.matcher(item).matches();
29+
}
30+
31+
@Override
32+
public void describeTo(Description description) {
33+
description.appendText("a blank string");
34+
}
35+
36+
/**
37+
* Creates a matcher of {@link String} that matches when the examined string contains
38+
* zero or more whitespace characters and nothing else.
39+
* <p/>
40+
* For example:
41+
* <pre>assertThat(" ", is(blankString()))</pre>
42+
*/
43+
@Factory
44+
public static Matcher<String> blankString() {
45+
return BLANK_INSTANCE;
46+
}
47+
48+
/**
49+
* Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or
50+
* contains zero or more whitespace characters and nothing else.
51+
* <p/>
52+
* For example:
53+
* <pre>assertThat(((String)null), is(blankOrNullString()))</pre>
54+
*
55+
*/
56+
@Factory
57+
public static Matcher<String> blankOrNullString() {
58+
return NULL_OR_BLANK_INSTANCE;
59+
}
60+
}

‎hamcrest-library/src/main/java/org/hamcrest/text/IsEmptyString.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44
import static org.hamcrest.core.AnyOf.anyOf;
55
import static org.hamcrest.core.IsNull.nullValue;
66

7-
import org.hamcrest.BaseMatcher;
87
import org.hamcrest.Description;
98
import org.hamcrest.Factory;
109
import org.hamcrest.Matcher;
10+
import org.hamcrest.TypeSafeMatcher;
1111

1212
/**
1313
* Matches empty Strings (and null).
1414
*/
15-
public final class IsEmptyString extends BaseMatcher<String> {
15+
public final class IsEmptyString extends TypeSafeMatcher<String> {
1616
private static final IsEmptyString INSTANCE = new IsEmptyString();
1717
@SuppressWarnings("unchecked")
1818
private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), INSTANCE);
1919

20+
private IsEmptyString() { }
21+
2022
@Override
21-
public boolean matches(Object item) {
22-
return item != null && item instanceof String && ((String) item).equals("");
23+
public boolean matchesSafely(String item) {
24+
return item.equals("");
2325
}
2426

2527
@Override
@@ -40,7 +42,7 @@ public void describeTo(Description description) {
4042
public static Matcher<String> isEmptyString() {
4143
return emptyString();
4244
}
43-
45+
4446
/**
4547
* Creates a matcher of {@link String} that matches when the examined string has zero length.
4648
* <p/>
@@ -68,7 +70,7 @@ public static Matcher<String> emptyString() {
6870
public static Matcher<String> isEmptyOrNullString() {
6971
return emptyOrNullString();
7072
}
71-
73+
7274
/**
7375
* Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or
7476
* has zero length.

‎hamcrest-library/src/main/java/org/hamcrest/text/StringContainsInOrder.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.hamcrest.Matcher;
66
import org.hamcrest.TypeSafeMatcher;
77

8+
import java.util.Arrays;
9+
810
public class StringContainsInOrder extends TypeSafeMatcher<String> {
911
private final Iterable<String> substrings;
1012

1113
public StringContainsInOrder(Iterable<String> substrings) {
1214
this.substrings = substrings;
1315
}
14-
16+
1517
@Override
1618
public boolean matchesSafely(String s) {
1719
int fromIndex = 0;
@@ -52,4 +54,19 @@ public void describeTo(Description description) {
5254
public static Matcher<String> stringContainsInOrder(Iterable<String> substrings) {
5355
return new StringContainsInOrder(substrings);
5456
}
57+
58+
/**
59+
* Creates a matcher of {@link String} that matches when the examined string contains all of
60+
* the specified substrings, regardless of the order of their appearance.
61+
* <p/>
62+
* For example:
63+
* <pre>assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"))</pre>
64+
*
65+
* @param substrings
66+
* the substrings that must be contained within matching strings
67+
*/
68+
@Factory
69+
public static Matcher<String> stringContainsInOrder(String... substrings) {
70+
return new StringContainsInOrder(Arrays.asList(substrings));
71+
}
5572
}

‎hamcrest-library/src/test/java/org/hamcrest/beans/HasPropertyTest.java‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,54 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
6+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
7+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
8+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
9+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
10+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
611
import static org.hamcrest.beans.HasProperty.hasProperty;
7-
import static org.hamcrest.core.IsNot.not;
812

9-
import org.hamcrest.AbstractMatcherTest;
1013
import org.hamcrest.Matcher;
14+
import org.junit.Test;
1115

1216
/**
1317
* @author Iain McGinniss
1418
* @author Nat Pryce
1519
* @author Steve Freeman
20+
* @author Tom Denley
1621
* @since 1.1.0
1722
*/
18-
public class HasPropertyTest extends AbstractMatcherTest {
23+
public final class HasPropertyTest {
1924

20-
private final HasPropertyWithValueTest.BeanWithoutInfo bean
21-
= new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
25+
private final HasPropertyWithValueTest.BeanWithoutInfo bean = new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
2226

23-
@Override
24-
protected Matcher<?> createMatcher() {
25-
return hasProperty("irrelevant");
27+
@Test public void
28+
copesWithNullsAndUnknownTypes() {
29+
Matcher<Object> matcher = hasProperty("irrelevant");
30+
31+
assertNullSafe(matcher);
32+
assertUnknownTypeSafe(matcher);
2633
}
2734

28-
public void testReturnsTrueIfPropertyExists() {
29-
assertThat(bean, hasProperty("writeOnlyProperty"));
35+
@Test public void
36+
matchesWhenThePropertyExists() {
37+
assertMatches("didn't match", hasProperty("writeOnlyProperty"), bean);
3038
}
3139

32-
public void testReturnsFalseIfPropertyDoesNotExist() {
33-
final Matcher<Object> hasProperty = hasProperty("aNonExistentProp");
34-
assertThat(bean, not(hasProperty));
35-
assertMismatchDescription("no \"aNonExistentProp\" in <[Person: a bean]>", hasProperty, bean);
40+
@Test public void
41+
doesNotMatchIfPropertyDoesNotExist() {
42+
assertDoesNotMatch("matched unexpectedly", hasProperty("aNonExistentProp"), bean);
3643
}
3744

38-
public void testDescribeTo() {
45+
@Test public void
46+
describesItself() {
3947
assertDescription("hasProperty(\"property\")", hasProperty("property"));
4048
}
49+
50+
@Test public void
51+
describesAMismatch() {
52+
assertMismatchDescription("no \"aNonExistentProp\" in <[Person: a bean]>",
53+
hasProperty("aNonExistentProp"), bean);
54+
}
4155
}
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)