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