Skip to content

Commit d149d9f

Browse files
committed
Resolved Issue hamcrest#69 by adding null safe check similar to the one in issue hamcrest#25
1 parent 49fdbb6 commit d149d9f

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsIterableContainingInOrder.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.TypeSafeDiagnosingMatcher;
7+
import org.hamcrest.core.IsNull;
78

89
import java.util.ArrayList;
910
import java.util.List;
@@ -135,7 +136,8 @@ public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super
135136
*/
136137
@Factory
137138
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
138-
return contains(asList(itemMatchers));
139+
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = nullSafe(itemMatchers);
140+
return contains(nullSafeWithExplicitTypeMatchers);
139141
}
140142

141143
/**
@@ -155,4 +157,13 @@ public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>...
155157
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) {
156158
return new IsIterableContainingInOrder<E>(itemMatchers);
157159
}
160+
161+
@SuppressWarnings("unchecked")
162+
private static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
163+
final List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(itemMatchers.length);
164+
for (final Matcher<? super E> itemMatcher : itemMatchers) {
165+
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
166+
}
167+
return matchers;
168+
}
158169
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public void testDoesNotMatchEmptyIterable() throws Exception {
5353
public void testHasAReadableDescription() {
5454
assertDescription("iterable containing [<1>, <2>]", contains(1, 2));
5555
}
56+
57+
public void testCanHandleNullMatchers() {
58+
assertMatches(contains(null, null), asList(null, null));
59+
}
5660

5761
public static class WithValue {
5862
private final int value;

0 commit comments

Comments
 (0)