Skip to content

Commit bf8b3e2

Browse files
committed
Bring back test for IsCollectionContaining.
1 parent faac95a commit bf8b3e2

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeDiagnosingMatcher;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
12+
import static java.util.Arrays.asList;
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.core.IsCollectionContaining.hasItem;
15+
import static org.hamcrest.core.IsCollectionContaining.hasItems;
16+
import static org.hamcrest.core.IsEqual.equalTo;
17+
18+
public class IsCollectionContainingTest extends AbstractMatcherTest {
19+
@Override
20+
protected Matcher<?> createMatcher() {
21+
return hasItem(equalTo("irrelevant"));
22+
}
23+
24+
public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
25+
Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a"));
26+
27+
assertMatches("should match list that contains 'a'",
28+
itemMatcher, asList("a", "b", "c"));
29+
}
30+
31+
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
32+
final Matcher<Iterable<? super String>> matcher1 = hasItem(mismatchable("a"));
33+
assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]", matcher1, asList("b", "c"));
34+
35+
36+
final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));
37+
assertMismatchDescription("was empty", matcher2, new ArrayList<String>());
38+
}
39+
40+
public void testDoesNotMatchNull() {
41+
assertDoesNotMatch("should not matches null", hasItem(equalTo("a")), null);
42+
}
43+
44+
public void testHasAReadableDescription() {
45+
assertDescription("a collection containing \"a\"", hasItem(equalTo("a")));
46+
}
47+
48+
public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 24
49+
{
50+
final Set<Number> s = new HashSet<Number>();
51+
s.add(Integer.valueOf(2));
52+
assertThat(s, new IsCollectionContaining<Number>(new IsEqual<Number>(Integer.valueOf(2))));
53+
assertThat(s, IsCollectionContaining.hasItem(Integer.valueOf(2)));
54+
}
55+
56+
@SuppressWarnings("unchecked")
57+
public void testMatchesAllItemsInCollection() {
58+
final Matcher<Iterable<String>> matcher1 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
59+
assertMatches("should match list containing all items",
60+
matcher1,
61+
asList("a", "b", "c"));
62+
63+
final Matcher<Iterable<String>> matcher2 = hasItems("a", "b", "c");
64+
assertMatches("should match list containing all items (without matchers)",
65+
matcher2,
66+
asList("a", "b", "c"));
67+
68+
final Matcher<Iterable<String>> matcher3 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
69+
assertMatches("should match list containing all items in any order",
70+
matcher3,
71+
asList("c", "b", "a"));
72+
73+
final Matcher<Iterable<String>> matcher4 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
74+
assertMatches("should match list containing all items plus others",
75+
matcher4,
76+
asList("e", "c", "b", "a", "d"));
77+
78+
final Matcher<Iterable<String>> matcher5 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
79+
assertDoesNotMatch("should not match list unless it contains all items",
80+
matcher5,
81+
asList("e", "c", "b", "d")); // 'a' missing
82+
}
83+
84+
85+
private static Matcher<? super String> mismatchable(final String string) {
86+
return new TypeSafeDiagnosingMatcher<String>() {
87+
@Override
88+
protected boolean matchesSafely(String item, Description mismatchDescription) {
89+
if (string.equals(item))
90+
return true;
91+
92+
mismatchDescription.appendText("mismatched: " + item);
93+
return false;
94+
}
95+
96+
@Override
97+
public void describeTo(Description description) {
98+
description.appendText("mismatchable: " + string);
99+
}
100+
};
101+
}
102+
}
103+

0 commit comments

Comments
 (0)