Skip to content

Commit 9632e7e

Browse files
author
neildunn
committed
Yaroslav's is empty or null string matcher
1 parent 0a1e8a6 commit 9632e7e

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
package org.hamcrest.text;
3+
4+
import org.hamcrest.BaseMatcher;
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
8+
9+
/**
10+
* Matches empty Strings (and null).
11+
*/
12+
public final class IsEmptyOrNullString extends BaseMatcher<String> {
13+
private static final IsEmptyOrNullString INSTANCE = new IsEmptyOrNullString();
14+
15+
public boolean matches(Object item) {
16+
return item == null || ((item instanceof String) && ((String) item).equals(""));
17+
}
18+
19+
public void describeTo(Description description) {
20+
description.appendText("an empty string or null");
21+
}
22+
23+
/**
24+
* Matches if value is null or zero-length string.
25+
*/
26+
@Factory
27+
public static Matcher<String> isEmptyString() {
28+
return INSTANCE;
29+
}
30+
31+
/**
32+
* Matches if value is null or zero-length string.
33+
*/
34+
@Factory
35+
public static Matcher<String> isEmptyOrNullString() {
36+
return INSTANCE;
37+
}
38+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public void testCopesWithUnknownTypes() {
3838
createMatcher().matches(new UnknownType());
3939
}
4040

41+
public void testDoesNotHaveAnEmptyDescription() throws Exception {
42+
Description description = new StringDescription();
43+
description.appendDescriptionOf(createMatcher());
44+
assertFalse(description.toString().length() == 0);
45+
}
46+
4147
public static class UnknownType {
4248
}
4349

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.hamcrest.text;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.text.IsEmptyOrNullString.isEmptyString;
7+
8+
public class IsEmptyOrNullStringTest extends AbstractMatcherTest {
9+
10+
@Override
11+
protected Matcher<?> createMatcher() {
12+
return IsEmptyOrNullString.isEmptyOrNullString();
13+
}
14+
15+
public void testMatchesNull() {
16+
assertMatches("null", isEmptyString(), null);
17+
}
18+
19+
public void testMatchesEmptyString() {
20+
assertMatches("empty string", isEmptyString(), "");
21+
}
22+
23+
public void testDoesNotMatchNonEmptyString() {
24+
assertDoesNotMatch("non empty string", isEmptyString(), "a");
25+
}
26+
27+
public void testHasAReadableDescription() {
28+
assertDescription("an empty string or null", isEmptyString());
29+
}
30+
}

‎matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<!-- Text -->
4040
<factory class="org.hamcrest.text.IsEqualIgnoringCase"/>
4141
<factory class="org.hamcrest.text.IsEqualIgnoringWhiteSpace"/>
42+
<factory class="org.hamcrest.text.IsEmptyOrNullString"/>
4243
<factory class="org.hamcrest.text.StringContains"/>
4344
<factory class="org.hamcrest.text.StringEndsWith"/>
4445
<factory class="org.hamcrest.text.StringStartsWith"/>

0 commit comments

Comments
 (0)