|
| 1 | +package org.hamcrest.collection; |
| 2 | + |
| 3 | +import org.hamcrest.Matcher; |
| 4 | +import org.hamcrest.internal.NullSafety; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import static java.util.Arrays.asList; |
| 11 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Steve Freeman 2016 http://www.hamcrest.com |
| 15 | + * Collected helper code for converting matchers between lists and iterables. |
| 16 | + */ |
| 17 | +public class ArrayMatching { |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a matcher for arrays that matches when the examined array contains at least one item |
| 22 | + * that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal |
| 23 | + * of the examined array will stop as soon as a matching element is found. |
| 24 | + * For example: |
| 25 | + * <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre> |
| 26 | + * |
| 27 | + * @param elementMatcher |
| 28 | + * the matcher to apply to elements in examined arrays |
| 29 | + */ |
| 30 | + public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) { |
| 31 | + return new IsArrayContaining<>(elementMatcher); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>. |
| 36 | + * For example: |
| 37 | + * <pre>assertThat(hasItemInArray(x))</pre> |
| 38 | + * instead of: |
| 39 | + * <pre>assertThat(hasItemInArray(equalTo(x)))</pre> |
| 40 | + * |
| 41 | + * @param element |
| 42 | + * the element that should be present in examined arrays |
| 43 | + */ |
| 44 | + public static <T> Matcher<T[]> hasItemInArray(T element) { |
| 45 | + Matcher<? super T> matcher = equalTo(element); |
| 46 | + return hasItemInArray(matcher); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * <p> |
| 51 | + * Creates an order agnostic matcher for arrays that matches when each item in the |
| 52 | + * examined array satisfies one matcher anywhere in the specified matchers. |
| 53 | + * For a positive match, the examined array must be of the same length as the number of |
| 54 | + * specified matchers. |
| 55 | + * </p> |
| 56 | + * <p> |
| 57 | + * N.B. each of the specified matchers will only be used once during a given examination, so be |
| 58 | + * careful when specifying matchers that may be satisfied by more than one entry in an examined |
| 59 | + * array. |
| 60 | + * </p> |
| 61 | + * <p> |
| 62 | + * For example: |
| 63 | + * </p> |
| 64 | + * <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre> |
| 65 | + * |
| 66 | + * @param itemMatchers |
| 67 | + * a list of matchers, each of which must be satisfied by an entry in an examined array |
| 68 | + */ |
| 69 | + @SafeVarargs |
| 70 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers) { |
| 71 | + return arrayContainingInAnyOrder(asList(itemMatchers)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * <p> |
| 76 | + * Creates an order agnostic matcher for arrays that matches when each item in the |
| 77 | + * examined array satisfies one matcher anywhere in the specified collection of matchers. |
| 78 | + * For a positive match, the examined array must be of the same length as the specified collection |
| 79 | + * of matchers. |
| 80 | + * </p> |
| 81 | + * <p> |
| 82 | + * N.B. each matcher in the specified collection will only be used once during a given |
| 83 | + * examination, so be careful when specifying matchers that may be satisfied by more than |
| 84 | + * one entry in an examined array. |
| 85 | + * </p> |
| 86 | + * <p> |
| 87 | + * For example: |
| 88 | + * </p> |
| 89 | + * <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre> |
| 90 | + * |
| 91 | + * @param itemMatchers |
| 92 | + * a list of matchers, each of which must be satisfied by an item provided by an examined array |
| 93 | + */ |
| 94 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> itemMatchers) { |
| 95 | + return new IsArrayContainingInAnyOrder<>(itemMatchers); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * <p>Creates an order agnostic matcher for arrays that matches when each item in the |
| 100 | + * examined array is logically equal to one item anywhere in the specified items. |
| 101 | + * For a positive match, the examined array must be of the same length as the number of |
| 102 | + * specified items. |
| 103 | + * </p> |
| 104 | + * <p>N.B. each of the specified items will only be used once during a given examination, so be |
| 105 | + * careful when specifying items that may be equal to more than one entry in an examined |
| 106 | + * array. |
| 107 | + * </p> |
| 108 | + * <p> |
| 109 | + * For example: |
| 110 | + * </p> |
| 111 | + * <pre>assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))</pre> |
| 112 | + * |
| 113 | + * @param items |
| 114 | + * the items that must equal the entries of an examined array, in any order |
| 115 | + */ |
| 116 | + @SafeVarargs |
| 117 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) { |
| 118 | + List<Matcher<? super E>> matchers = new ArrayList<>(); |
| 119 | + for (E item : items) { |
| 120 | + matchers.add(equalTo(item)); |
| 121 | + } |
| 122 | + return new IsArrayContainingInAnyOrder<>(matchers); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Creates a matcher for arrays that matches when each item in the examined array is |
| 127 | + * logically equal to the corresponding item in the specified items. For a positive match, |
| 128 | + * the examined array must be of the same length as the number of specified items. |
| 129 | + * For example: |
| 130 | + * <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre> |
| 131 | + * |
| 132 | + * @param items |
| 133 | + * the items that must equal the items within an examined array |
| 134 | + */ |
| 135 | + @SafeVarargs |
| 136 | + public static <E> Matcher<E[]> arrayContaining(E... items) { |
| 137 | + List<Matcher<? super E>> matchers = new ArrayList<>(); |
| 138 | + for (E item : items) { |
| 139 | + matchers.add(equalTo(item)); |
| 140 | + } |
| 141 | + return arrayContaining(matchers); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a matcher for arrays that matches when each item in the examined array satisfies the |
| 146 | + * corresponding matcher in the specified matchers. For a positive match, the examined array |
| 147 | + * must be of the same length as the number of specified matchers. |
| 148 | + * For example: |
| 149 | + * <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))</pre> |
| 150 | + * |
| 151 | + * @param itemMatchers |
| 152 | + * the matchers that must be satisfied by the items in the examined array |
| 153 | + */ |
| 154 | + @SafeVarargs |
| 155 | + public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) { |
| 156 | + //required for JDK 1.6 |
| 157 | + //noinspection RedundantTypeArguments |
| 158 | + final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = NullSafety.<E>nullSafe(itemMatchers); |
| 159 | + |
| 160 | + return arrayContaining(nullSafeWithExplicitTypeMatchers); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Creates a matcher for arrays that matches when each item in the examined array satisfies the |
| 165 | + * corresponding matcher in the specified list of matchers. For a positive match, the examined array |
| 166 | + * must be of the same length as the specified list of matchers. |
| 167 | + * For example: |
| 168 | + * <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre> |
| 169 | + * |
| 170 | + * @param itemMatchers |
| 171 | + * a list of matchers, each of which must be satisfied by the corresponding item in an examined array |
| 172 | + */ |
| 173 | + public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) { |
| 174 | + return new IsArrayContainingInOrder<>(itemMatchers); |
| 175 | + } |
| 176 | +} |
0 commit comments