/** * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code> * method returns <code>true</code>. * <p/> * For example: * <pre>assertThat(new ArrayList<String>(), is(empty()))</pre> */ public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> empty() { return org.hamcrest.collection.IsEmptyCollection.<E>empty(); }
/** * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. * <p/> * For example: * <pre>assertThat(new ArrayList<String>(), is(emptyIterable()))</pre> */ public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIterable() { return org.hamcrest.collection.IsEmptyIterable.<E>emptyIterable(); }
@Override protected boolean matchesSafely(Iterable<? extends T> items, Description mismatchDescription) { Matching<T> matching = new Matching<T>(matchers, mismatchDescription); for (T item : items) { if (! matching.matches(item)) { return false; } } return matching.isFinished(items); }
/** * Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns * a value that satisfies the specified matcher. * <p/> * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre> * * @param sizeMatcher * a matcher for the size of an examined {@link java.util.Collection} */ public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { return org.hamcrest.collection.IsCollectionWithSize.<E>hasSize(sizeMatcher); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that is equal to the specified key. * <p/> * For example: * <pre>assertThat(myMap, hasKey("bar"))</pre> * * @param key * the key that satisfying maps must contain */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K, ?>> hasKey(K key) { return org.hamcrest.collection.IsMapContaining.<K>hasKey(key); }
@Override protected boolean matchesSafely(Iterable<? extends E> iterable, Description mismatchDescription) { MatchSeries<E> matchSeries = new MatchSeries<E>(matchers, mismatchDescription); for (E item : iterable) { if (!matchSeries.matches(item)) { return false; } } return matchSeries.isFinished(); }
/** * Creates a matcher that matches when the examined object is found within the * specified collection. * <p/> * For example: * <pre>assertThat("foo", isIn(Arrays.asList("bar", "foo")))</pre> * * @param collection * the collection in which matching items must be found */ public static <T> org.hamcrest.Matcher<T> isIn(java.util.Collection<T> collection) { return org.hamcrest.collection.IsIn.<T>isIn(collection); }
/** * Creates a matcher for arrays that matches when the <code>length</code> of the array * satisfies the specified matcher. * <p/> * For example: * <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))</pre> * * @param sizeMatcher * a matcher for the length of an examined array */ public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { return org.hamcrest.collection.IsArrayWithSize.<E>arrayWithSize(sizeMatcher); }
public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) { this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers); this.matchers = matchers; }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one value that is equal to the specified value. * <p/> * For example: * <pre>assertThat(myMap, hasValue("foo"))</pre> * * @param value * the value that satisfying maps must contain */ public static <V> org.hamcrest.Matcher<java.util.Map<?, ? extends V>> hasValue(V value) { return org.hamcrest.collection.IsMapContaining.<V>hasValue(value); }
/** * Creates a matcher for arrays that matches when the <code>length</code> of the array * is zero. * <p/> * For example: * <pre>assertThat(new String[0], emptyArray())</pre> */ public static <E> org.hamcrest.Matcher<E[]> emptyArray() { return org.hamcrest.collection.IsArrayWithSize.<E>emptyArray(); }
/** * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code> * method returns <code>true</code>. * <p/> * For example: * <pre>assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))</pre> * * @param type * the type of the collection's content */ public static <E> org.hamcrest.Matcher<java.util.Collection<E>> emptyCollectionOf(java.lang.Class<E> type) { return org.hamcrest.collection.IsEmptyCollection.<E>emptyCollectionOf(type); }
public IsArrayContainingInOrder(List<Matcher<? super E>> matchers) { this.iterableMatcher = new IsIterableContainingInOrder<E>(matchers); this.matchers = matchers; }
/** * Creates a matcher that matches when the examined object is equal to one of the * specified elements. * <p/> * For example: * <pre>assertThat("foo", isIn("bar", "foo"))</pre> * * @param elements * the elements amongst which matching items will be found */ public static <T> org.hamcrest.Matcher<T> isOneOf(T... elements) { return org.hamcrest.collection.IsIn.<T>isOneOf(elements); }
/** * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. * <p/> * For example: * <pre>assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))</pre> * * @param type * the type of the iterable's content */ public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> type) { return org.hamcrest.collection.IsEmptyIterable.<E>emptyIterableOf(type); }
public void testMismatchesItemsInAnyOrder() { Matcher<Integer[]> matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3); assertMismatchDescription("was null", matcher, null); assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1}); } }
/** * Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns * a value equal to the specified <code>size</code>. * <p/> * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre> * * @param size * the expected size of an examined {@link java.util.Collection} */ public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(int size) { return org.hamcrest.collection.IsCollectionWithSize.<E>hasSize(size); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that satisfies the specified matcher. * <p/> * For example: * <pre>assertThat(myMap, hasKey(equalTo("bar")))</pre> * * @param keyMatcher * the matcher that must be satisfied by at least one key */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K, ?>> hasKey(org.hamcrest.Matcher<? super K> keyMatcher) { return org.hamcrest.collection.IsMapContaining.<K>hasKey(keyMatcher); }
/** * Creates a matcher for arrays that matches when the <code>length</code> of the array * equals the specified <code>size</code>. * <p/> * For example: * <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))</pre> * * @param size * the length that an examined array must have for a positive match */ public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(int size) { return org.hamcrest.collection.IsArrayWithSize.<E>arrayWithSize(size); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one value that satisfies the specified valueMatcher. * <p/> * For example: * <pre>assertThat(myMap, hasValue(equalTo("foo")))</pre> * * @param valueMatcher * the matcher that must be satisfied by at least one value */ public static <V> org.hamcrest.Matcher<java.util.Map<?, ? extends V>> hasValue(org.hamcrest.Matcher<? super V> valueMatcher) { return org.hamcrest.collection.IsMapContaining.<V>hasValue(valueMatcher); }