1 package org.hamcrest.collection; 2 3 import org.hamcrest.AbstractMatcherTest; 4 import org.hamcrest.FeatureMatcher; 5 import org.hamcrest.Matcher; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import static java.util.Arrays.asList; 11 import static org.hamcrest.collection.IsIterableContainingInOrder.contains; 12 import static org.hamcrest.core.IsEqual.equalTo; 13 14 @SuppressWarnings("unchecked") 15 public 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 createMatcher()20 protected Matcher<?> createMatcher() { 21 return contains(1, 2); 22 } 23 testMatchingSingleItemIterable()24 public void testMatchingSingleItemIterable() throws Exception { 25 assertMatches("Single item iterable", contains(1), asList(1)); 26 } 27 testMatchingMultipleItemIterable()28 public void testMatchingMultipleItemIterable() throws Exception { 29 assertMatches("Multiple item iterable", contains(1, 2, 3), asList(1, 2, 3)); 30 } 31 testDoesNotMatchWithMoreElementsThanExpected()32 public void testDoesNotMatchWithMoreElementsThanExpected() throws Exception { 33 assertMismatchDescription("not matched: <4>", contains(1, 2, 3), asList(1, 2, 3, 4)); 34 } 35 testDoesNotMatchWithFewerElementsThanExpected()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 testDoesNotMatchIfSingleItemMismatches()41 public void testDoesNotMatchIfSingleItemMismatches() throws Exception { 42 assertMismatchDescription("item 0: value was <3>", contains(value(4)), asList(make(3))); 43 } 44 testDoesNotMatchIfOneOfMultipleItemsMismatch()45 public void testDoesNotMatchIfOneOfMultipleItemsMismatch() throws Exception { 46 assertMismatchDescription("item 2: value was <4>", contains123, asList(make(1), make(2), make(4))); 47 } 48 testDoesNotMatchEmptyIterable()49 public void testDoesNotMatchEmptyIterable() throws Exception { 50 assertMismatchDescription("no item was value with <4>", contains(value(4)), new ArrayList<WithValue>()); 51 } 52 testHasAReadableDescription()53 public void testHasAReadableDescription() { 54 assertDescription("iterable containing [<1>, <2>]", contains(1, 2)); 55 } 56 testCanHandleNullMatchers()57 public void testCanHandleNullMatchers() { 58 assertMatches(contains(null, null), asList(null, null)); 59 } 60 61 public static class WithValue { 62 private final int value; WithValue(int value)63 public WithValue(int value) { this.value = value; } getValue()64 public int getValue() { return value; } toString()65 @Override public String toString() { return "WithValue " + value; } 66 } 67 make(int value)68 public static WithValue make(int value) { 69 return new WithValue(value); 70 } 71 value(int value)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