• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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
createMatcher()20     protected Matcher<?> createMatcher() {
21         return containsInRelativeOrder(1, 2);
22     }
23 
testMatchingSingleItemIterable()24     public void testMatchingSingleItemIterable() throws Exception {
25         assertMatches("Single item iterable", containsInRelativeOrder(1), asList(1));
26     }
27 
testMatchingMultipleItemIterable()28     public void testMatchingMultipleItemIterable() throws Exception {
29         assertMatches("Multiple item iterable", containsInRelativeOrder(1, 2, 3), asList(1, 2, 3));
30     }
31 
testMatchesWithMoreElementsThanExpectedAtBeginning()32     public void testMatchesWithMoreElementsThanExpectedAtBeginning() throws Exception {
33         assertMatches("More elements at beginning", containsInRelativeOrder(2, 3, 4), asList(1, 2, 3, 4));
34     }
35 
testMatchesWithMoreElementsThanExpectedAtEnd()36     public void testMatchesWithMoreElementsThanExpectedAtEnd() throws Exception {
37         assertMatches("More elements at end", containsInRelativeOrder(1, 2, 3), asList(1, 2, 3, 4));
38     }
39 
testMatchesWithMoreElementsThanExpectedInBetween()40     public void testMatchesWithMoreElementsThanExpectedInBetween() throws Exception {
41         assertMatches("More elements in between", containsInRelativeOrder(1, 3), asList(1, 2, 3));
42     }
43 
testMatchesSubSection()44     public void testMatchesSubSection() throws Exception {
45         assertMatches("Sub section of iterable", containsInRelativeOrder(2, 3), asList(1, 2, 3, 4));
46     }
47 
testMatchesWithSingleGapAndNotFirstOrLast()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 
testMatchingSubSectionWithManyGaps()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 
testDoesNotMatchWithFewerElementsThanExpected()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 
testDoesNotMatchIfSingleItemNotFound()61     public void testDoesNotMatchIfSingleItemNotFound() throws Exception {
62         assertMismatchDescription("value with <4> was not found", containsInRelativeOrder(value(4)), asList(make(3)));
63     }
64 
testDoesNotMatchIfOneOfMultipleItemsNotFound()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 
testDoesNotMatchEmptyIterable()69     public void testDoesNotMatchEmptyIterable() throws Exception {
70         assertMismatchDescription("value with <4> was not found", containsInRelativeOrder(value(4)), new ArrayList<WithValue>());
71     }
72 
testHasAReadableDescription()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;
WithValue(int value)79       public WithValue(int value) { this.value = value; }
getValue()80       public int getValue() { return value; }
toString()81       @Override public String toString() { return "WithValue " + value; }
82     }
83 
make(int value)84     public static WithValue make(int value) {
85       return new WithValue(value);
86     }
87 
value(int value)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 }
94