|
| 1 | +package org.hamcrest.collection; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hamcrest.AbstractMatcherTest; |
| 7 | +import org.hamcrest.FeatureMatcher; |
| 8 | +import org.hamcrest.Matcher; |
| 9 | + |
| 10 | +import static java.util.Arrays.asList; |
| 11 | +import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder; |
| 12 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 13 | + |
| 14 | +@SuppressWarnings("unchecked") |
| 15 | +public class IsIterableContainingInRelativeOrderTest extends AbstractMatcherTest { |
| 16 | + // temporary hack until the Java type system works |
| 17 | + private final Matcher<Iterable<? extends WithValue>> contains123 = containsInRelativeOrder(value(1), value(2), value(3)); |
| 18 | + |
| 19 | + @Override |
| 20 | + protected Matcher<?> createMatcher() { |
| 21 | + return containsInRelativeOrder(1, 2); |
| 22 | + } |
| 23 | + |
| 24 | + public void testMatchingSingleItemIterable() throws Exception { |
| 25 | + assertMatches("Single item iterable", containsInRelativeOrder(1), asList(1)); |
| 26 | + } |
| 27 | + |
| 28 | + public void testMatchingMultipleItemIterable() throws Exception { |
| 29 | + assertMatches("Multiple item iterable", containsInRelativeOrder(1, 2, 3), asList(1, 2, 3)); |
| 30 | + } |
| 31 | + |
| 32 | + public void testMatchesWithMoreElementsThanExpectedAtBeginning() throws Exception { |
| 33 | + assertMatches("More elements at beginning", containsInRelativeOrder(2, 3, 4), asList(1, 2, 3, 4)); |
| 34 | + } |
| 35 | + |
| 36 | + public void testMatchesWithMoreElementsThanExpectedAtEnd() throws Exception { |
| 37 | + assertMatches("More elements at end", containsInRelativeOrder(1, 2, 3), asList(1, 2, 3, 4)); |
| 38 | + } |
| 39 | + |
| 40 | + public void testMatchesWithMoreElementsThanExpectedInBetween() throws Exception { |
| 41 | + assertMatches("More elements in between", containsInRelativeOrder(1, 3), asList(1, 2, 3)); |
| 42 | + } |
| 43 | + |
| 44 | + public void testMatchesSubSection() throws Exception { |
| 45 | + assertMatches("Sub section of iterable", containsInRelativeOrder(2, 3), asList(1, 2, 3, 4)); |
| 46 | + } |
| 47 | + |
| 48 | + public void testMatchesWithSingleGapAndNotFirstOrLast() throws Exception { |
| 49 | + assertMatches("Sub section with single gaps without a first or last match", containsInRelativeOrder(2, 4), asList(1, 2, 3, 4, 5)); |
| 50 | + } |
| 51 | + |
| 52 | + public void testMatchingSubSectionWithManyGaps() throws Exception { |
| 53 | + assertMatches("Sub section with many gaps iterable", containsInRelativeOrder(2, 4, 6), asList(1, 2, 3, 4, 5, 6, 7)); |
| 54 | + } |
| 55 | + |
| 56 | + public void testDoesNotMatchWithFewerElementsThanExpected() throws Exception { |
| 57 | + List<WithValue> valueList = asList(make(1), make(2)); |
| 58 | + assertMismatchDescription("value with <3> was not found after <WithValue 2>", contains123, valueList); |
| 59 | + } |
| 60 | + |
| 61 | + public void testDoesNotMatchIfSingleItemNotFound() throws Exception { |
| 62 | + assertMismatchDescription("value with <4> was not found", containsInRelativeOrder(value(4)), asList(make(3))); |
| 63 | + } |
| 64 | + |
| 65 | + public void testDoesNotMatchIfOneOfMultipleItemsNotFound() throws Exception { |
| 66 | + assertMismatchDescription("value with <3> was not found after <WithValue 2>", contains123, asList(make(1), make(2), make(4))); |
| 67 | + } |
| 68 | + |
| 69 | + public void testDoesNotMatchEmptyIterable() throws Exception { |
| 70 | + assertMismatchDescription("value with <4> was not found", containsInRelativeOrder(value(4)), new ArrayList<WithValue>()); |
| 71 | + } |
| 72 | + |
| 73 | + public void testHasAReadableDescription() { |
| 74 | + assertDescription("iterable containing [<1>, <2>] in relative order", containsInRelativeOrder(1, 2)); |
| 75 | + } |
| 76 | + |
| 77 | + public static class WithValue { |
| 78 | + private final int value; |
| 79 | + public WithValue(int value) { this.value = value; } |
| 80 | + public int getValue() { return value; } |
| 81 | + @Override public String toString() { return "WithValue " + value; } |
| 82 | + } |
| 83 | + |
| 84 | + public static WithValue make(int value) { |
| 85 | + return new WithValue(value); |
| 86 | + } |
| 87 | + |
| 88 | + public static Matcher<WithValue> value(int value) { |
| 89 | + return new FeatureMatcher<WithValue, Integer>(equalTo(value), "value with", "value") { |
| 90 | + @Override protected Integer featureValueOf(WithValue actual) { return actual.getValue(); } |
| 91 | + }; |
| 92 | + } |
| 93 | +} |
0 commit comments