1 package org.hamcrest.collection; 2 3 import org.hamcrest.AbstractMatcherTest; 4 import org.hamcrest.Matcher; 5 6 import static org.hamcrest.collection.IsArrayContaining.hasItemInArray; 7 8 public class IsArrayContainingTest extends AbstractMatcherTest { 9 10 @Override createMatcher()11 protected Matcher<?> createMatcher() { 12 return hasItemInArray("irrelevant"); 13 } 14 testMatchesAnArrayThatContainsAnElementMatchingTheGivenMatcher()15 public void testMatchesAnArrayThatContainsAnElementMatchingTheGivenMatcher() { 16 assertMatches("should matches array that contains 'a'", 17 hasItemInArray("a"), new String[]{"a", "b", "c"}); 18 } 19 testDoesNotMatchAnArrayThatDoesntContainAnElementMatchingTheGivenMatcher()20 public void testDoesNotMatchAnArrayThatDoesntContainAnElementMatchingTheGivenMatcher() { 21 assertDoesNotMatch("should not matches array that doesn't contain 'a'", 22 hasItemInArray("a"), new String[]{"b", "c"}); 23 assertDoesNotMatch("should not matches empty array", 24 hasItemInArray("a"), new String[0]); 25 } 26 testDoesNotMatchNull()27 public void testDoesNotMatchNull() { 28 assertDoesNotMatch("should not matches null", 29 hasItemInArray("a"), null); 30 } 31 testHasAReadableDescription()32 public void testHasAReadableDescription() { 33 assertDescription("an array containing \"a\"", hasItemInArray("a")); 34 } 35 36 // Remaining code no longer compiles, thanks to generics. I think that's a good thing, but 37 // I still need to investigate how this behaves with code that doesn't use generics. 38 // I expect ClassCastExceptions will be thrown. 39 // -Joe. 40 41 // public void testDoesNotMatchObjectThatIsNotAnArray() { 42 // assertDoesNotMatch("should not matches empty list", 43 // arrayContaining("a"), "not a collection"); 44 // } 45 46 // public void testMatchesPrimitiveArrayElements() { 47 // assertMatches("boolean", arrayContaining(true), new boolean[]{true, false}); 48 // assertDoesNotMatch("boolean", arrayContaining(false), new boolean[]{false}); 49 // 50 // assertMatches("byte", arrayContaining((byte) 1), new byte[]{1, 2, 3}); 51 // assertDoesNotMatch("byte", arrayContaining((byte) 0), new byte[]{1, 2, 3}); 52 // 53 // assertMatches("char", arrayContaining('a'), new char[]{'a', 'b', 'c'}); 54 // assertDoesNotMatch("char", arrayContaining('z'), new char[]{'a', 'b', 'c'}); 55 // 56 // assertMatches("short", arrayContaining((short) 1), new short[]{1, 2, 3}); 57 // assertDoesNotMatch("short", arrayContaining((short) 0), new short[]{1, 2, 3}); 58 // 59 // assertMatches("int", arrayContaining(1), new int[]{1, 2, 3}); 60 // assertDoesNotMatch("int", arrayContaining(0), new int[]{1, 2, 3}); 61 // 62 // assertMatches("long", arrayContaining(1L), new long[]{1, 2, 3}); 63 // assertDoesNotMatch("long", arrayContaining(0L), new long[]{1, 2, 3}); 64 // 65 // assertMatches("float", arrayContaining(1f), new float[]{1f, 2f, 3f}); 66 // assertDoesNotMatch("float", arrayContaining(0f), new float[]{1f, 2f, 3f}); 67 // 68 // assertMatches("double", arrayContaining(1.0), new double[]{1.0, 2.0, 3.0}); 69 // assertDoesNotMatch("double", arrayContaining(0.0), new double[]{1.0, 2.0, 3.0}); 70 // } 71 72 } 73