Skip to content

Commit 378a9d5

Browse files
author
neildunn
committed
Added IsArrayOnlyContaining and updated matchers.xml to match
1 parent 07fda5d commit 378a9d5

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed

CHANGES.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
generics bug in hasKey and hasValue static factories previously declared
55
in IsMapContaining (ngd)
66

7-
* Added IsCollectionOnlyContaining which matches collections with
8-
items only matching a specified item. E.g onlyContains(3,4,5) or
9-
onlyContains(lessThan(9))
7+
* Added IsCollectionOnlyContaining and IsArrayOnlyContaining which matches
8+
collections (and arrays) where all match a given matcher. E.g onlyContains(3,4,5)
9+
or onlyContains(lessThan(9))
1010

1111
== Version 1.1: Released Jun 30 2007 ==
1212

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.hamcrest.collection;
2+
3+
import static org.hamcrest.core.AnyOf.anyOf;
4+
import static org.hamcrest.core.IsEqual.equalTo;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
9+
import org.hamcrest.Description;
10+
import org.hamcrest.Factory;
11+
import org.hamcrest.Matcher;
12+
import org.hamcrest.TypeSafeMatcher;
13+
14+
/**
15+
* Matches arrays where all elements satisfy the specified matcher.
16+
* This is the array equivalent of {@link IsCollectionOnlyContaining}
17+
*
18+
* @author ngd
19+
*/
20+
public class IsArrayOnlyContaining<T> extends TypeSafeMatcher<T[]> {
21+
22+
private final Matcher<T> matcher;
23+
24+
public IsArrayOnlyContaining(Matcher<T> matcher) {
25+
this.matcher = matcher;
26+
}
27+
28+
@Override
29+
public boolean matchesSafely(T[] items) {
30+
if (items.length == 0) {
31+
return false;
32+
}
33+
for (T item : items) {
34+
if (!matcher.matches(item)) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
public void describeTo(Description description) {
42+
description
43+
.appendText("is array containing items matching ")
44+
.appendDescriptionOf(matcher);
45+
}
46+
47+
@Factory
48+
public static <T> Matcher<T[]> isArrayOnlyContaining(T...items) {
49+
Collection<Matcher<T>> matchers = new ArrayList<Matcher<T>>();
50+
for (T item : items) {
51+
matchers.add(equalTo(item));
52+
}
53+
return new IsArrayOnlyContaining<T>(anyOf(matchers));
54+
}
55+
56+
@Factory
57+
public static <T> Matcher<T[]> isArrayOnlyContaining(Matcher<T>...matchers) {
58+
return new IsArrayOnlyContaining<T>(anyOf(matchers));
59+
}
60+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
import org.hamcrest.TypeSafeMatcher;
1313

1414
/**
15-
* Matches collections that only contain elements that satisfy a given matcher.
15+
* Matches collections that only contain elements that satisfy a given matcher.
1616
* </p>
17-
* For example <pre>[1,2,3]</pre> satisfies onlyContains(lessThan(4))
17+
* For example <pre>[1,2,3]</pre> would satisfy <pre>onlyContains(lessThan(4))</pre>
18+
* and <pre>onlyContains(1,2,3)</pre>.
19+
* </p>
20+
* This matcher will never match an empty collection.
21+
* </p>
22+
* Note this matcher will also match repeated elements. E.g <pre>[1,2,2]</pre> would
23+
* satisfy <pre>onlyContains(1,2)</pre>
1824
*
1925
* @author ngd
2026
*/
@@ -28,6 +34,9 @@ public IsCollectionOnlyContaining(Matcher<T> matcher) {
2834

2935
@Override
3036
public boolean matchesSafely(Collection<T> items) {
37+
if (items.isEmpty()) {
38+
return false;
39+
}
3140
for (T item : items) {
3241
if (!matcher.matches(item)) {
3342
return false;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.hamcrest.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
import org.hamcrest.AbstractMatcherTest;
7+
import org.hamcrest.Matcher;
8+
import static org.hamcrest.collection.IsArrayOnlyContaining.isArrayOnlyContaining;
9+
10+
public class IsArrayOnlyContainingTest extends AbstractMatcherTest {
11+
12+
public void testDoesNotMatchEmptyArray() {
13+
String[] array = {};
14+
assertDoesNotMatch("empty collection", isArrayOnlyContaining("foo"), array);
15+
}
16+
17+
public void testMatchesSingletonArray() {
18+
Integer[] array = {1};
19+
assertMatches("singleton array", isArrayOnlyContaining(1), array);
20+
}
21+
22+
public void testMatchesArray() {
23+
Integer[] array = {1,2,3};
24+
assertMatches("array", isArrayOnlyContaining(1,2,3), array);
25+
}
26+
27+
public void testDoesNotMatchArrayWithMismatchingItem() {
28+
Integer[] array = {1,2,3};
29+
assertDoesNotMatch("array with mismatching item",
30+
isArrayOnlyContaining(1,2), array);
31+
}
32+
33+
@Override
34+
protected Matcher<?> createMatcher() {
35+
return isArrayOnlyContaining("foo");
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.hamcrest.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
import org.hamcrest.AbstractMatcherTest;
7+
import org.hamcrest.Matcher;
8+
import static org.hamcrest.collection.IsCollectionOnlyContaining.onlyContains;
9+
10+
public class IsCollectionOnlyContainingTest extends AbstractMatcherTest {
11+
12+
public void testDoesNotMatchEmptyCollection() {
13+
Collection<String> collection = new ArrayList<String>();
14+
assertDoesNotMatch("empty collection", onlyContains("foo"), collection);
15+
}
16+
17+
public void testMatchesSingletonCollection() {
18+
Collection<Integer> collection = new ArrayList<Integer>();
19+
collection.add(1);
20+
assertMatches("singleton collection", onlyContains(1), collection);
21+
}
22+
23+
public void testMatchesCollection() {
24+
Collection<Integer> collection = new ArrayList<Integer>();
25+
collection.add(1);
26+
collection.add(2);
27+
assertMatches("collection", onlyContains(1,2), collection);
28+
}
29+
30+
public void testDoesNotMatchCollectionWithMismatchingItem() {
31+
Collection<Integer> collection = new ArrayList<Integer>();
32+
collection.add(1);
33+
collection.add(2);
34+
collection.add(3);
35+
assertDoesNotMatch("collection", onlyContains(1,2), collection);
36+
}
37+
38+
@Override
39+
protected Matcher<?> createMatcher() {
40+
return onlyContains(1,2,3);
41+
}
42+
43+
}

matchers.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
<!-- Collection -->
1616
<factory class="org.hamcrest.collection.IsArray"/>
1717
<factory class="org.hamcrest.collection.IsArrayContaining"/>
18+
<factory class="org.hamcrest.collection.IsArrayOnlyContaining"/>
1819
<factory class="org.hamcrest.collection.IsCollectionContaining"/>
20+
<factory class="org.hamcrest.collection.IsCollectionOnlyContaining"/>
1921
<factory class="org.hamcrest.collection.IsMapContaining"/>
2022
<factory class="org.hamcrest.collection.IsMapContainingKey"/>
2123
<factory class="org.hamcrest.collection.IsMapContainingValue"/>

0 commit comments

Comments
 (0)