Skip to content

Commit 69160fd

Browse files
author
joeretro
committed
Added isCollectionContainingAllOf().
1 parent f4053f4 commit 69160fd

4 files changed

Lines changed: 44 additions & 17 deletions

File tree

‎src/library/org/hamcrest/collection/IsCollectionContaining.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package org.hamcrest.collection;
22

3+
import static org.hamcrest.core.And.and;
34
import org.hamcrest.Description;
45
import org.hamcrest.Matcher;
56
import org.hamcrest.Factory;
67
import org.hamcrest.TypeSafeMatcher;
78
import static org.hamcrest.core.IsEqual.eq;
89

910
import java.util.Collection;
11+
import java.util.ArrayList;
1012

1113
public class IsCollectionContaining<T> extends TypeSafeMatcher<Collection<T>> {
1214
private final Matcher<T> elementMatcher;
13-
//Comparator<? super T> c
14-
//int binarySearch(List<? extends Comparable<? super T>> list, T key) {
1515

1616
public IsCollectionContaining(Matcher<T> elementMatcher) {
1717
this.elementMatcher = elementMatcher;
@@ -41,4 +41,13 @@ public static <T> Matcher<Collection<T>> collectionContaining(T element) {
4141
return collectionContaining(eq(element));
4242
}
4343

44+
@Factory
45+
public static <T> Matcher<Collection<T>> collectionContainingAllOf(Matcher<T>... elementMatchers) {
46+
Collection<Matcher<Collection<T>>> all = new ArrayList<Matcher<Collection<T>>>(elementMatchers.length);
47+
for (Matcher<T> elementMatcher : elementMatchers) {
48+
all.add(collectionContaining(elementMatcher));
49+
}
50+
return and(all);
51+
}
52+
4453
}

‎src/library/org/hamcrest/core/And.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
88

9+
import java.util.Arrays;
10+
911

1012
/**
1113
* Calculates the logical conjunction of two matchers. Evaluation is
1214
* shortcut, so that the second matcher is not called if the first
1315
* matcher returns <code>false</code>.
1416
*/
1517
public class And<T> implements Matcher<T> {
16-
private final Matcher<T>[] matchers;
17-
18+
private final Iterable<Matcher<T>> matchers;
1819

19-
public And(Matcher<T>[] matchers) {
20+
public And(Iterable<Matcher<T>> matchers) {
2021
this.matchers = matchers;
2122
}
2223

@@ -45,6 +46,11 @@ public void describeTo(Description description) {
4546

4647
@Factory
4748
public static <T> Matcher<T> and(Matcher<T>... matchers) {
49+
return and(Arrays.asList(matchers));
50+
}
51+
52+
@Factory
53+
public static <T> Matcher<T> and(Iterable<Matcher<T>> matchers) {
4854
return new And<T>(matchers);
4955
}
5056

‎src/library/org/hamcrest/core/Or.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
*/
33
package org.hamcrest.core;
44

5+
import static java.util.Arrays.asList;
6+
57
import org.hamcrest.Description;
68
import org.hamcrest.Matcher;
79
import org.hamcrest.Factory;
810

9-
1011
/**
1112
* Calculates the logical disjunction of two matchers. Evaluation is
1213
* shortcut, so that the second matcher is not called if the first
1314
* matcher returns <code>true</code>.
1415
*/
1516
public class Or<T> implements Matcher<T> {
1617

17-
private final Matcher<T>[] matchers;
18+
private final Iterable<Matcher<T>> matchers;
1819

19-
public Or(Matcher<T>... matchers) {
20+
public Or(Iterable<Matcher<T>> matchers) {
2021
this.matchers = matchers;
2122
}
2223

@@ -45,6 +46,11 @@ public void describeTo(Description description) {
4546

4647
@Factory
4748
public static <T> Matcher<T> or(Matcher<T>... matchers) {
49+
return new Or<T>(asList(matchers));
50+
}
51+
52+
@Factory
53+
public static <T> Matcher<T> or(Iterable<Matcher<T>> matchers) {
4854
return new Or<T>(matchers);
4955
}
5056
}

‎src/unit-test/org/hamcrest/collection/IsCollectionContainingTest.java‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
55
import static org.hamcrest.collection.IsCollectionContaining.collectionContaining;
6+
import static org.hamcrest.collection.IsCollectionContaining.collectionContainingAllOf;
67
import static org.hamcrest.core.IsEqual.eq;
78

89
import java.util.ArrayList;
@@ -34,13 +35,18 @@ public void testHasAReadableDescription() {
3435
assertDescription("a collection containing eq(\"a\")", collectionContaining(eq("a")));
3536
}
3637

37-
// Remaining code no longer compiles, thanks to generics. I think that's a good thing, but
38-
// I still need to investigate how this behaves with code that doesn't use generics.
39-
// I expect ClassCastExceptions will be thrown.
40-
// -Joe.
41-
42-
// public void testDoesNotMatchObjectThatIsNotCollections() {
43-
// assertDoesNotMatch("should not matches empty list", collectionContaining(eq("a")), "not a collection");
44-
// }
45-
38+
public void testMatchesAllItemsInCollection() {
39+
assertMatches("should match list containing all of items",
40+
collectionContainingAllOf(eq("a"), eq("b"), eq("c")),
41+
asList("a", "b", "c"));
42+
assertMatches("should match list containing all of items in any order",
43+
collectionContainingAllOf(eq("a"), eq("b"), eq("c")),
44+
asList("c", "b", "a"));
45+
assertMatches("should match list containing all of items plus others",
46+
collectionContainingAllOf(eq("a"), eq("b"), eq("c")),
47+
asList("e", "c", "b", "a", "d"));
48+
assertDoesNotMatch("should not match list unless it contains all items",
49+
collectionContainingAllOf(eq("a"), eq("b"), eq("c")),
50+
asList("e", "c", "b", "d")); // 'a' missing
51+
}
4652
}

0 commit comments

Comments
 (0)