Skip to content

Commit 3290087

Browse files
author
joeretro
committed
Renamed collectionContaining() -> hasItem(). arrayContaining() -> hasItemInArray()
1 parent 419f6c0 commit 3290087

File tree

5 files changed

+33
-37
lines changed

5 files changed

+33
-37
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import org.hamcrest.TypeSafeMatcher;
77
import static org.hamcrest.core.IsEqual.equalTo;
88

9-
import java.lang.reflect.Array;
10-
119
public class IsArrayContaining<T> extends TypeSafeMatcher<T[]> {
1210

1311
private final Matcher<T> elementMatcher;
@@ -17,9 +15,8 @@ public IsArrayContaining(Matcher<T> elementMatcher) {
1715
}
1816

1917
public boolean matchesSafely(T[] array) {
20-
int size = Array.getLength(array);
21-
for (int i = 0; i < size; i++) {
22-
if (elementMatcher.matches(Array.get(array, i))) {
18+
for (T item : array) {
19+
if (elementMatcher.matches(item)) {
2320
return true;
2421
}
2522
}
@@ -32,13 +29,13 @@ public void describeTo(Description description) {
3229
}
3330

3431
@Factory
35-
public static <T> Matcher<T[]> arrayContaining(Matcher<T> elementMatcher) {
32+
public static <T> Matcher<T[]> hasItemInArray(Matcher<T> elementMatcher) {
3633
return new IsArrayContaining<T>(elementMatcher);
3734
}
3835

3936
@Factory
40-
public static <T> Matcher<T[]> arrayContaining(T element) {
41-
return arrayContaining(equalTo(element));
37+
public static <T> Matcher<T[]> hasItemInArray(T element) {
38+
return hasItemInArray(equalTo(element));
4239
}
4340

4441
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import java.util.Collection;
1111
import java.util.ArrayList;
1212

13-
public class IsCollectionContaining<T> extends TypeSafeMatcher<Collection<T>> {
13+
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
1414
private final Matcher<T> elementMatcher;
1515

1616
public IsCollectionContaining(Matcher<T> elementMatcher) {
1717
this.elementMatcher = elementMatcher;
1818
}
1919

20-
public boolean matchesSafely(Collection<T> collection) {
20+
public boolean matchesSafely(Iterable<T> collection) {
2121
for (T item : collection) {
2222
if (elementMatcher.matches(item)){
2323
return true;
@@ -32,20 +32,20 @@ public void describeTo(Description description) {
3232
}
3333

3434
@Factory
35-
public static <T> Matcher<Collection<T>> collectionContaining(Matcher<T> elementMatcher) {
35+
public static <T> Matcher<Iterable<T>> hasItem(Matcher<T> elementMatcher) {
3636
return new IsCollectionContaining<T>(elementMatcher);
3737
}
3838

3939
@Factory
40-
public static <T> Matcher<Collection<T>> collectionContaining(T element) {
41-
return collectionContaining(equalTo(element));
40+
public static <T> Matcher<Iterable<T>> hasItem(T element) {
41+
return hasItem(equalTo(element));
4242
}
4343

4444
@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);
45+
public static <T> Matcher<Iterable<T>> hasItems(Matcher<T>... elementMatchers) {
46+
Collection<Matcher<Iterable<T>>> all = new ArrayList<Matcher<Iterable<T>>>(elementMatchers.length);
4747
for (Matcher<T> elementMatcher : elementMatchers) {
48-
all.add(collectionContaining(elementMatcher));
48+
all.add(hasItem(elementMatcher));
4949
}
5050
return allOf(all);
5151
}

src/unit-test/org/hamcrest/collection/IsArrayContainingTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
5-
import static org.hamcrest.collection.IsArrayContaining.arrayContaining;
5+
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
66

77
public class IsArrayContainingTest extends AbstractMatcherTest {
88

99
protected Matcher<?> createMatcher() {
10-
return arrayContaining("irrelevant");
10+
return hasItemInArray("irrelevant");
1111
}
1212

1313
public void testMatchesAnArrayThatContainsAnElementMatchingTheGivenMatcher() {
1414
assertMatches("should matches array that contains 'a'",
15-
arrayContaining("a"), new String[]{"a", "b", "c"});
15+
hasItemInArray("a"), new String[]{"a", "b", "c"});
1616
}
1717

1818
public void testDoesNotMatchAnArrayThatDoesntContainAnElementMatchingTheGivenMatcher() {
1919
assertDoesNotMatch("should not matches array that doesn't contain 'a'",
20-
arrayContaining("a"), new String[]{"b", "c"});
20+
hasItemInArray("a"), new String[]{"b", "c"});
2121
assertDoesNotMatch("should not matches empty array",
22-
arrayContaining("a"), new String[0]);
22+
hasItemInArray("a"), new String[0]);
2323
}
2424

2525
public void testDoesNotMatchNull() {
2626
assertDoesNotMatch("should not matches null",
27-
arrayContaining("a"), null);
27+
hasItemInArray("a"), null);
2828
}
2929

3030
public void testHasAReadableDescription() {
31-
assertDescription("an array containing \"a\"", arrayContaining("a"));
31+
assertDescription("an array containing \"a\"", hasItemInArray("a"));
3232
}
3333

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

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
5-
import static org.hamcrest.collection.IsCollectionContaining.collectionContaining;
6-
import static org.hamcrest.collection.IsCollectionContaining.collectionContainingAllOf;
5+
import static org.hamcrest.collection.IsCollectionContaining.hasItems;
76
import static org.hamcrest.core.IsEqual.equalTo;
87

98
import java.util.ArrayList;
@@ -12,41 +11,41 @@
1211
public class IsCollectionContainingTest extends AbstractMatcherTest {
1312

1413
protected Matcher<?> createMatcher() {
15-
return collectionContaining(equalTo("irrelevant"));
14+
return IsCollectionContaining.hasItem(equalTo("irrelevant"));
1615
}
1716

1817
public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
1918
assertMatches("should matches list that contains 'a'",
20-
collectionContaining(equalTo("a")), asList("a", "b", "c"));
19+
IsCollectionContaining.hasItem(equalTo("a")), asList("a", "b", "c"));
2120
}
2221

2322
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
2423
assertDoesNotMatch("should not matches list that doesn't contain 'a'",
25-
collectionContaining(equalTo("a")), asList("b", "c"));
24+
IsCollectionContaining.hasItem(equalTo("a")), asList("b", "c"));
2625
assertDoesNotMatch("should not matches empty list",
27-
collectionContaining(equalTo("a")), new ArrayList<String>());
26+
IsCollectionContaining.hasItem(equalTo("a")), new ArrayList<String>());
2827
}
2928

3029
public void testDoesNotMatchNull() {
31-
assertDoesNotMatch("should not matches null", collectionContaining(equalTo("a")), null);
30+
assertDoesNotMatch("should not matches null", IsCollectionContaining.hasItem(equalTo("a")), null);
3231
}
3332

3433
public void testHasAReadableDescription() {
35-
assertDescription("a collection containing \"a\"", collectionContaining(equalTo("a")));
34+
assertDescription("a collection containing \"a\"", IsCollectionContaining.hasItem(equalTo("a")));
3635
}
3736

3837
public void testMatchesAllItemsInCollection() {
3938
assertMatches("should match list containing all of items",
40-
collectionContainingAllOf(equalTo("a"), equalTo("b"), equalTo("c")),
39+
hasItems(equalTo("a"), equalTo("b"), equalTo("c")),
4140
asList("a", "b", "c"));
4241
assertMatches("should match list containing all of items in any order",
43-
collectionContainingAllOf(equalTo("a"), equalTo("b"), equalTo("c")),
42+
hasItems(equalTo("a"), equalTo("b"), equalTo("c")),
4443
asList("c", "b", "a"));
4544
assertMatches("should match list containing all of items plus others",
46-
collectionContainingAllOf(equalTo("a"), equalTo("b"), equalTo("c")),
45+
hasItems(equalTo("a"), equalTo("b"), equalTo("c")),
4746
asList("e", "c", "b", "a", "d"));
4847
assertDoesNotMatch("should not match list unless it contains all items",
49-
collectionContainingAllOf(equalTo("a"), equalTo("b"), equalTo("c")),
48+
hasItems(equalTo("a"), equalTo("b"), equalTo("c")),
5049
asList("e", "c", "b", "d")); // 'a' missing
5150
}
5251
}

src/unit-test/org/hamcrest/generator/config/XmlConfiguratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.hamcrest.Factory;
55
import org.hamcrest.Matcher;
66
import static org.hamcrest.MatcherAssert.assertThat;
7-
import static org.hamcrest.collection.IsCollectionContaining.collectionContainingAllOf;
7+
import static org.hamcrest.collection.IsCollectionContaining.hasItems;
88
import static org.hamcrest.core.IsEqual.equalTo;
99
import org.hamcrest.generator.FactoryMethod;
1010
import org.hamcrest.generator.FactoryWriter;
@@ -34,7 +34,7 @@ public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
3434
"</matchers>"));
3535

3636
assertThat(sugarConfiguration.factoryMethods(),
37-
collectionContainingAllOf(
37+
hasItems(
3838
equalTo(new FactoryMethod(SomeMatcher.class.getName(), "matcher1")),
3939
equalTo(new FactoryMethod(SomeMatcher.class.getName(), "matcher2")),
4040
equalTo(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3"))

0 commit comments

Comments
 (0)