@SuppressWarnings("unchecked") @Test public void matchesMultipleItemsInCollection() { final Matcher<Iterable<String>> matcher1 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); assertMatches("list containing all items", matcher1, asList("a", "b", "c")); final Matcher<Iterable<String>> matcher2 = hasItems("a", "b", "c"); assertMatches("list containing all items (without matchers)", matcher2, asList("a", "b", "c")); final Matcher<Iterable<String>> matcher3 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); assertMatches("list containing all items in any order", matcher3, asList("c", "b", "a")); final Matcher<Iterable<String>> matcher4 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); assertMatches("list containing all items plus others", matcher4, asList("e", "c", "b", "a", "d")); final Matcher<Iterable<String>> matcher5 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); assertDoesNotMatch("not match list unless it contains all items", matcher5, asList("e", "c", "b", "d")); }
@SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length); for (Matcher<? super T> elementMatcher : itemMatchers) { all.add(new IsIterableContaining<>(elementMatcher)); } return allOf(all); }
IsIterableContaining extends TypeSafeDiagnosingMatcher<Iterable<? super T>> { @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length); for (Matcher<? super T> elementMatcher : itemMatchers) { all.add(new IsIterableContaining<>(elementMatcher)); } return allOf(all); } }
IsIterableContaining extends TypeSafeDiagnosingMatcher<Iterable<? super T>> { @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length); for (Matcher<? super T> elementMatcher : itemMatchers) { all.add(new IsIterableContaining<>(elementMatcher)); } return allOf(all); } IsIterableContaining(Matcher<? super T> elementMatcher); }
IsIterableContaining extends TypeSafeDiagnosingMatcher<Iterable<? super T>> { @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length); for (Matcher<? super T> elementMatcher : itemMatchers) { all.add(new IsIterableContaining<>(elementMatcher)); } return allOf(all); } IsIterableContaining(Matcher<? super T> elementMatcher); @Override void describeTo(Description description); static Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher); static Matcher<Iterable<? super T>> hasItem(T item); @SafeVarargs static Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers); @SafeVarargs static Matcher<Iterable<T>> hasItems(T... items); }
IsIterableContaining extends TypeSafeDiagnosingMatcher<Iterable<? super T>> { @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length); for (Matcher<? super T> elementMatcher : itemMatchers) { all.add(new IsIterableContaining<>(elementMatcher)); } return allOf(all); } IsIterableContaining(Matcher<? super T> elementMatcher); @Override void describeTo(Description description); static Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher); static Matcher<Iterable<? super T>> hasItem(T item); @SafeVarargs static Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers); @SafeVarargs static Matcher<Iterable<T>> hasItems(T... items);  }