1package org.hamcrest.collection; 2 3import org.hamcrest.AbstractMatcherTest; 4import org.hamcrest.FeatureMatcher; 5import org.hamcrest.Matcher; 6 7import java.util.ArrayList; 8import java.util.List; 9 10import static java.util.Arrays.asList; 11import static org.hamcrest.collection.IsIterableContainingInOrder.contains; 12import static org.hamcrest.core.IsEqual.equalTo; 13 14@SuppressWarnings("unchecked") 15public class IsIterableContainingInOrderTest extends AbstractMatcherTest { 16 // temporary hack until the Java type system works 17 private final Matcher<Iterable<? extends WithValue>> contains123 = contains(value(1), value(2), value(3)); 18 19 @Override 20 protected Matcher<?> createMatcher() { 21 return contains(1, 2); 22 } 23 24 public void testMatchingSingleItemIterable() throws Exception { 25 assertMatches("Single item iterable", contains(1), asList(1)); 26 } 27 28 public void testMatchingMultipleItemIterable() throws Exception { 29 assertMatches("Multiple item iterable", contains(1, 2, 3), asList(1, 2, 3)); 30 } 31 32 public void testDoesNotMatchWithMoreElementsThanExpected() throws Exception { 33 assertMismatchDescription("not matched: <4>", contains(1, 2, 3), asList(1, 2, 3, 4)); 34 } 35 36 public void testDoesNotMatchWithFewerElementsThanExpected() throws Exception { 37 List<WithValue> valueList = asList(make(1), make(2)); 38 assertMismatchDescription("no item was value with <3>", contains123, valueList); 39 } 40 41 public void testDoesNotMatchIfSingleItemMismatches() throws Exception { 42 assertMismatchDescription("item 0: value was <3>", contains(value(4)), asList(make(3))); 43 } 44 45 public void testDoesNotMatchIfOneOfMultipleItemsMismatch() throws Exception { 46 assertMismatchDescription("item 2: value was <4>", contains123, asList(make(1), make(2), make(4))); 47 } 48 49 public void testDoesNotMatchEmptyIterable() throws Exception { 50 assertMismatchDescription("no item was value with <4>", contains(value(4)), new ArrayList<WithValue>()); 51 } 52 53 public void testHasAReadableDescription() { 54 assertDescription("iterable containing [<1>, <2>]", contains(1, 2)); 55 } 56 57 public void testCanHandleNullMatchers() { 58 assertMatches(contains(null, null), asList(null, null)); 59 } 60 61 public static class WithValue { 62 private final int value; 63 public WithValue(int value) { this.value = value; } 64 public int getValue() { return value; } 65 @Override public String toString() { return "WithValue " + value; } 66 } 67 68 public static WithValue make(int value) { 69 return new WithValue(value); 70 } 71 72 public static Matcher<WithValue> value(int value) { 73 return new FeatureMatcher<WithValue, Integer>(equalTo(value), "value with", "value") { 74 @Override protected Integer featureValueOf(WithValue actual) { return actual.getValue(); } 75 }; 76 } 77} 78