1 package org.hamcrest.collection; 2 3 import org.hamcrest.AbstractMatcherTest; 4 import org.hamcrest.BaseMatcher; 5 import org.hamcrest.Description; 6 import org.hamcrest.Matcher; 7 8 import static org.hamcrest.collection.IsArray.array; 9 import static org.hamcrest.core.IsEqual.equalTo; 10 11 @SuppressWarnings("unchecked") 12 public class IsArrayTest extends AbstractMatcherTest { 13 14 @Override createMatcher()15 protected Matcher<?> createMatcher() { 16 return array(equalTo("irrelevant")); 17 } 18 testMatchesAnArrayThatMatchesAllTheElementMatchers()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 testDoesNotMatchAnArrayWhenElementsDoNotMatch()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 testDoesNotMatchAnArrayOfDifferentSize()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 testDoesNotMatchNull()36 public void testDoesNotMatchNull() { 37 assertDoesNotMatch("should not match null", 38 array(equalTo("a")), null); 39 } 40 testHasAReadableDescription()41 public void testHasAReadableDescription() { 42 assertDescription("[\"a\", \"b\"]", array(equalTo("a"), equalTo("b"))); 43 } 44 testHasAReadableMismatchDescriptionUsing()45 public void testHasAReadableMismatchDescriptionUsing() { 46 assertMismatchDescription("element <0> was \"c\"", array(equalTo("a"), equalTo("b")), new String[]{"c", "b"}); 47 } 48 testHasAReadableMismatchDescriptionUsingCustomMatchers()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