/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) { return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) { return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Matcher)}. * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Object)}. * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> Matcher<Iterable<? super T>> hasItem(T item) { // Doesn't forward to hasItem() method so compiler can sort out generics. return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the * examined {@link Iterable} yield at least one item that is equal to the corresponding * item from the specified <code>items</code>. Whilst matching, each traversal of the * examined {@link Iterable} will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre> * * @param items * the items to compare against the items provided by the examined {@link Iterable} */ @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(T... items) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(items.length); for (T item : items) { all.add(hasItem(item)); } return allOf(all); }
@Test public void matchesACollectionThatContainsAnElementForTheGivenMatcher() { final Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a")); assertMatches("list containing 'a'", itemMatcher, asList("a", "b", "c")); }
@Test public void doesNotMatchNull() { assertDoesNotMatch("doesn't match null", hasItem(equalTo("a")), null); }
@Test public void copesWithNullsAndUnknownTypes() { Matcher<?> matcher = hasItem(equalTo("irrelevant")); assertNullSafe(matcher); assertUnknownTypeSafe(matcher); }
@Test public void doesNotMatchCollectionWithoutAnElementForGivenMatcher() { final Matcher<Iterable<? super String>> matcher = hasItem(mismatchable("a")); assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]", matcher, asList("b", "c")); assertMismatchDescription("was empty", matcher, new ArrayList<String>()); }
@Test public void hasAReadableDescription() { assertDescription("a collection containing mismatchable: a", hasItem(mismatchable("a"))); }
@Test public void canMatchItemWhenCollectionHoldsSuperclass() { // Issue 24 final Set<Number> s = new HashSet<>(); s.add(2); assertMatches(new IsIterableContaining<>(new IsEqual<Number>(2)), s); assertMatches(IsIterableContaining.hasItem(2), s); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) { return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) { return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is matched by the specified * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> * * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Matcher)}. * * @param itemMatcher * the matcher to apply to items provided by the examined {@link Iterable} */ public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) { return IsIterableContaining.hasItem(itemMatcher); }
/** * Creates a matcher for {@link Iterable}s that only matches when a single pass over the * examined {@link Iterable} yields at least one item that is equal to the specified * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} * will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> * * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Object)}. * * @param item * the item to compare against the items provided by the examined {@link Iterable} */ public static <T> Matcher<Iterable<? super T>> hasItem(T item) { // Doesn't forward to hasItem() method so compiler can sort out generics. return IsIterableContaining.hasItem(item); }
/** * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the * examined {@link Iterable} yield at least one item that is equal to the corresponding * item from the specified <code>items</code>. Whilst matching, each traversal of the * examined {@link Iterable} will stop as soon as a matching item is found. * For example: * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre> * * @param items * the items to compare against the items provided by the examined {@link Iterable} */ @SafeVarargs public static <T> Matcher<Iterable<T>> hasItems(T... items) { List<Matcher<? super Iterable<T>>> all = new ArrayList<>(items.length); for (T item : items) { all.add(hasItem(item)); } return allOf(all); }