Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue #69 by adding null safe check similar to the one in issue #25 #70

Merged
merged 1 commit into from Nov 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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