Skip to content

Commit

Permalink
Resolved Issue hamcrest#69 by adding null safe check similar to the o…
Browse files Browse the repository at this point in the history
…ne in issue hamcrest#25
  • Loading branch information
npathai committed Nov 2, 2014
1 parent 49fdbb6 commit d149d9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -4,6 +4,7 @@
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.core.IsNull;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -135,7 +136,8 @@ public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super
*/
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
return contains(asList(itemMatchers));
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = nullSafe(itemMatchers);
return contains(nullSafeWithExplicitTypeMatchers);
}

/**
Expand All @@ -155,4 +157,13 @@ public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>...
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) {
return new IsIterableContainingInOrder<E>(itemMatchers);
}

@SuppressWarnings("unchecked")
private static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
final List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(itemMatchers.length);
for (final Matcher<? super E> itemMatcher : itemMatchers) {
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
}
return matchers;
}
}
Expand Up @@ -53,6 +53,10 @@ public void testDoesNotMatchEmptyIterable() throws Exception {
public void testHasAReadableDescription() {
assertDescription("iterable containing [<1>, <2>]", contains(1, 2));
}

public void testCanHandleNullMatchers() {
assertMatches(contains(null, null), asList(null, null));
}

public static class WithValue {
private final int value;
Expand Down

0 comments on commit d149d9f

Please sign in to comment.