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

fix: contains/containsInAnyOrder overloading issue #380

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,17 @@ public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(Matcher<? su
@SafeVarargs
public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(T... items) {
List<Matcher<? super T>> matchers = new ArrayList<>();
for (T item : items) {
if (items.length == 1 && items[0].getClass().isArray()) {
@SuppressWarnings("unchecked")
T[] realItems = (T[]) items[0];
for (T item : realItems) {
matchers.add(equalTo(item));
}
} else {
for (T item : items) {
matchers.add(equalTo(item));
}
}

return new IsIterableContainingInAnyOrder<>(matchers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ private void describeMismatch(Matcher<? super F> matcher, F item) {
*/
@SafeVarargs
public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
if (items.length == 1 && items[0].getClass().isArray()) {
@SuppressWarnings("unchecked")
E[] realItems = (E[]) items[0];
return contains(asEqualMatchers(realItems));
}
return contains(asEqualMatchers(items));
}

Expand Down
28 changes: 28 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/MatchersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.hamcrest;

import java.util.Arrays;
import org.hamcrest.core.AnyOf;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;

public class MatchersTest {

@Test
public void matchersContainsInAnyOrderCastTest() {
String[] truth = new String[] { "1", "2" };
assertThat("doesn't contain elements", Arrays.asList("1", "2"), containsInAnyOrder(truth));
Object otherTruth = truth;
assertThat("doesn't contain elements", Arrays.asList("1", "2"), containsInAnyOrder(otherTruth));
}

@Test
public void matchersContainsCastTest() {
String[] truth = new String[] { "1", "2" };
assertThat("doesn't contain elements", Arrays.asList("1", "2"), contains(truth));
Object otherTruth = truth;
assertThat("doesn't contain elements", Arrays.asList("1", "2"), contains(otherTruth));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static java.util.Arrays.asList;
import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.core.IsIterableContaining.hasItem;
import static org.hamcrest.core.IsIterableContaining.hasItems;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.core.IsEqual.equalTo;

public final class IsIterableContainingTest {
Expand Down Expand Up @@ -86,6 +89,25 @@ public final class IsIterableContainingTest {
matcher, asList(1, 2, 3));
}

@Test public void
isIterableContainingAnyOrderCast() {
String[] array = new String[]{"1", "2"};
Object object = array;
Iterable<String> real = Arrays.asList("1", "2");
final Matcher<Iterable<? extends Object>> matcher = containsInAnyOrder(object);
assertTrue("does not match the same object", matcher.matches(real));
}

@Test
public void
isIterableContainingCast() {
String[] array = new String[]{"1", "2"};
Object object = array;
Iterable<String> real = Arrays.asList("1", "2");
final Matcher<Iterable<? extends Object>> matcher = contains(object);
assertTrue("does not match the same object", matcher.matches(real));
}

private static Matcher<? super String> mismatchable(final String string) {
return new TypeSafeDiagnosingMatcher<String>() {
@Override
Expand Down