Skip to content

Commit 40465aa

Browse files
committed
fixed arrayContaining(null, null). hamcrest#25
1 parent 2a94ed2 commit 40465aa

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎.idea/misc.xml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎hamcrest-core/src/test/java/org/hamcrest/AbstractMatcherTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void assertUnknownTypeSafe(Matcher<?> matcher) {
5959
}
6060
}
6161

62-
private static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
62+
public static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
6363
Description description = new StringDescription();
6464
matcher.describeMismatch(arg, description);
6565
return description.toString().trim();

‎hamcrest-examples/hamcrest-examples.iml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
<orderEntry type="sourceFolder" forTests="false" />
1010
<orderEntry type="library" name="integration" level="project" />
1111
<orderEntry type="library" name="ant_output" level="project" />
12-
<orderEntry type="module" module-name="hamcrest-core" />
13-
<orderEntry type="module" module-name="hamcrest-api" />
1412
<orderEntry type="module" module-name="hamcrest-integration" />
1513
</component>
1614
</module>

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hamcrest.Factory;
55
import org.hamcrest.Matcher;
66
import org.hamcrest.TypeSafeMatcher;
7+
import org.hamcrest.core.IsNull;
78

89
import java.util.ArrayList;
910
import java.util.Collection;
@@ -69,7 +70,8 @@ public static <E> Matcher<E[]> arrayContaining(E... items) {
6970
*/
7071
@Factory
7172
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
72-
return arrayContaining(asList(itemMatchers));
73+
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = nullSafe(itemMatchers);
74+
return arrayContaining(nullSafeWithExplicitTypeMatchers);
7375
}
7476

7577
/**
@@ -87,4 +89,13 @@ public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatcher
8789
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
8890
return new IsArrayContainingInOrder<E>(itemMatchers);
8991
}
92+
93+
@SuppressWarnings("unchecked")
94+
private static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
95+
final List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(itemMatchers.length);
96+
for (final Matcher<? super E> itemMatcher : itemMatchers) {
97+
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
98+
}
99+
return matchers;
100+
}
90101
}

‎hamcrest-library/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public void testMismatchesItemsInOrder() {
3838
assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1});
3939
assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4});
4040
}
41+
42+
public void testCanHandleNullValuesInAnArray() {
43+
assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null});
44+
}
4145
}

0 commit comments

Comments
 (0)