1package org.hamcrest.collection;
2
3import org.hamcrest.AbstractMatcherTest;
4import org.hamcrest.BaseMatcher;
5import org.hamcrest.Description;
6import org.hamcrest.Matcher;
7
8import static org.hamcrest.collection.IsArray.array;
9import static org.hamcrest.core.IsEqual.equalTo;
10
11@SuppressWarnings("unchecked")
12public class IsArrayTest extends AbstractMatcherTest {
13
14    @Override
15    protected Matcher<?> createMatcher() {
16        return array(equalTo("irrelevant"));
17    }
18
19    public void testMatchesAnArrayThatMatchesAllTheElementMatchers() {
20        assertMatches("should match array with matching elements",
21                array(equalTo("a"), equalTo("b"), equalTo("c")), new String[]{"a", "b", "c"});
22    }
23
24    public void testDoesNotMatchAnArrayWhenElementsDoNotMatch() {
25        assertDoesNotMatch("should not match array with different elements",
26                array(equalTo("a"), equalTo("b")), new String[]{"b", "c"});
27    }
28
29    public void testDoesNotMatchAnArrayOfDifferentSize() {
30        assertDoesNotMatch("should not match larger array",
31                           array(equalTo("a"), equalTo("b")), new String[]{"a", "b", "c"});
32        assertDoesNotMatch("should not match smaller array",
33                           array(equalTo("a"), equalTo("b")), new String[]{"a"});
34    }
35
36    public void testDoesNotMatchNull() {
37        assertDoesNotMatch("should not match null",
38                array(equalTo("a")), null);
39    }
40
41    public void testHasAReadableDescription() {
42        assertDescription("[\"a\", \"b\"]", array(equalTo("a"), equalTo("b")));
43    }
44
45    public void testHasAReadableMismatchDescriptionUsing() {
46        assertMismatchDescription("element <0> was \"c\"", array(equalTo("a"), equalTo("b")), new String[]{"c", "b"});
47    }
48
49    public void testHasAReadableMismatchDescriptionUsingCustomMatchers() {
50        final BaseMatcher<String> m = new BaseMatcher<String>() {
51            @Override public boolean matches(Object item) { return false; }
52            @Override public void describeTo(Description description) { description.appendText("c"); }
53            @Override public void describeMismatch(Object item, Description description) {
54                description.appendText("didn't match");
55            }
56        };
57        assertMismatchDescription("element <0> didn't match", array(m, equalTo("b")), new String[]{"c", "b"});
58    }
59}
60