Skip to content

Commit 5418862

Browse files
committed
HasItemInArray implemented with Collection matcher
1 parent f4bec1e commit 5418862

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

hamcrest-library/src/main/java/org/hamcrest/collection/ArrayMatching.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ArrayMatching {
2828
* the matcher to apply to elements in examined arrays
2929
*/
3030
public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) {
31-
return new IsArrayContaining<>(elementMatcher);
31+
return new HasItemInArray<>(elementMatcher);
3232
}
3333

3434
/**
@@ -164,7 +164,6 @@ public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatc
164164
return new ArrayAsIterableMatcher<>(new IsIterableContainingInOrder<>(itemMatchers), itemMatchers, "");
165165
}
166166

167-
168167
private static <E> List<Matcher<? super E>> asEqualMatchers(E[] items) {
169168
final List<Matcher<? super E>> matchers = new ArrayList<>();
170169
for (E item : items) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeDiagnosingMatcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
import org.hamcrest.core.IsCollectionContaining;
8+
9+
import static java.util.Arrays.asList;
10+
11+
/**
12+
* Matches if an array contains an item satisfying a nested matcher.
13+
*/
14+
public class HasItemInArray<T> extends TypeSafeMatcher<T[]> {
15+
private final Matcher<? super T> elementMatcher;
16+
private final TypeSafeDiagnosingMatcher<Iterable<? super T>> collectionMatcher;
17+
18+
public HasItemInArray(Matcher<? super T> elementMatcher) {
19+
this.elementMatcher = elementMatcher;
20+
this.collectionMatcher = new IsCollectionContaining<>(elementMatcher);
21+
}
22+
23+
@Override
24+
public boolean matchesSafely(T[] actual) {
25+
return collectionMatcher.matches(asList(actual));
26+
}
27+
28+
@Override
29+
public void describeMismatchSafely(T[] actual, Description mismatchDescription) {
30+
collectionMatcher.describeMismatch(asList(actual), mismatchDescription);
31+
}
32+
33+
@Override
34+
public void describeTo(Description description) {
35+
description
36+
.appendText("an array containing ")
37+
.appendDescriptionOf(elementMatcher);
38+
}
39+
40+
}

hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

hamcrest-library/src/test/java/org/hamcrest/collection/IsArrayContainingTest.java renamed to hamcrest-library/src/test/java/org/hamcrest/collection/HasItemInArrayTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,38 @@
33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
55

6+
import static org.hamcrest.Matchers.lessThan;
67
import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
78

8-
public class IsArrayContainingTest extends AbstractMatcherTest {
9+
public class HasItemInArrayTest extends AbstractMatcherTest {
910

1011
@Override
1112
protected Matcher<?> createMatcher() {
12-
return ArrayMatching.hasItemInArray("irrelevant");
13+
return hasItemInArray("irrelevant");
1314
}
1415

1516
public void testMatchesAnArrayThatContainsAnElementMatchingTheGivenMatcher() {
1617
assertMatches("should matches array that contains 'a'",
17-
ArrayMatching.hasItemInArray("a"), new String[]{"a", "b", "c"});
18+
hasItemInArray("a"), new String[]{"a", "b", "c"});
1819
}
1920

2021
public void testDoesNotMatchAnArrayThatDoesntContainAnElementMatchingTheGivenMatcher() {
2122
assertDoesNotMatch("should not matches array that doesn't contain 'a'",
22-
ArrayMatching.hasItemInArray("a"), new String[]{"b", "c"});
23+
hasItemInArray("a"), new String[]{"b", "c"});
2324
assertDoesNotMatch("should not matches empty array",
24-
ArrayMatching.hasItemInArray("a"), new String[0]);
25+
hasItemInArray("a"), new String[0]);
26+
assertMismatchDescription(
27+
"mismatches were: [<3> was greater than <2>, <4> was greater than <2>, <5> was greater than <2>]",
28+
hasItemInArray(lessThan(2)), new Integer[] {3, 4, 5});
2529
}
2630

2731
public void testDoesNotMatchNull() {
2832
assertDoesNotMatch("should not matches null",
29-
ArrayMatching.hasItemInArray("a"), null);
33+
hasItemInArray("a"), null);
3034
}
3135

3236
public void testHasAReadableDescription() {
33-
assertDescription("an array containing \"a\"", ArrayMatching.hasItemInArray("a"));
37+
assertDescription("an array containing a value less than <2>", hasItemInArray(lessThan(2)));
3438
}
3539

3640
// Remaining code no longer compiles, thanks to generics. I think that's a good thing, but

0 commit comments

Comments
 (0)