|
| 1 | +package org.hamcrest.collection; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.hamcrest.AbstractMatcherTest; |
| 8 | +import org.hamcrest.FeatureMatcher; |
| 9 | +import org.hamcrest.Matcher; |
| 10 | + |
| 11 | +import static java.util.Arrays.asList; |
| 12 | +import static org.hamcrest.collection.HasSubsequence.hasSubsequence; |
| 13 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 14 | + |
| 15 | +@SuppressWarnings("unchecked") |
| 16 | +public class HasSubsequenceTest extends AbstractMatcherTest { |
| 17 | + private final Matcher<Collection<? extends WithValue>> contains123 = hasSubsequence(value(1), value(2), value(3)); |
| 18 | + |
| 19 | + @Override |
| 20 | + protected Matcher<?> createMatcher() { |
| 21 | + return hasSubsequence(1, 2); |
| 22 | + } |
| 23 | + |
| 24 | + public void testMatchingSingleItemCollection() throws Exception { |
| 25 | + assertMatches("Single item iterable", hasSubsequence(1), asList(1)); |
| 26 | + } |
| 27 | + |
| 28 | + public void testMatchingMultipleItemCollection() throws Exception { |
| 29 | + assertMatches("Multiple item iterable", hasSubsequence(1, 2, 3), asList(1, 2, 3)); |
| 30 | + } |
| 31 | + |
| 32 | + public void testMatchesWithMoreElementsThanExpectedAtBeginning() throws Exception { |
| 33 | + assertMatches("More elements at beginning", hasSubsequence(2, 3, 4), asList(1, 2, 3, 4)); |
| 34 | + } |
| 35 | + |
| 36 | + public void testMatchesWithMoreElementsThanExpectedAtEnd() throws Exception { |
| 37 | + assertMatches("More elements at end", hasSubsequence(1, 2, 3), asList(1, 2, 3, 4)); |
| 38 | + } |
| 39 | + |
| 40 | + public void testDoesNotMatchWithMoreElementsThanExpectedInBetween() throws Exception { |
| 41 | + assertMismatchDescription("could not find subsequence inside collection [<1>, <2>, <3>]", hasSubsequence(1, 3), asList(1, 2, 3)); |
| 42 | + } |
| 43 | + |
| 44 | + public void testMatchesSubSection() throws Exception { |
| 45 | + assertMatches("Sub section of iterable", hasSubsequence(2, 3), asList(1, 2, 3, 4)); |
| 46 | + } |
| 47 | + |
| 48 | + public void testDoesNotMatchWithFewerElementsThanExpected() throws Exception { |
| 49 | + List<WithValue> valueList = asList(make(1), make(2)); |
| 50 | + assertMismatchDescription("could not find subsequence inside collection [<WithValue 1>, <WithValue 2>]", contains123, valueList); |
| 51 | + } |
| 52 | + |
| 53 | + public void testDoesNotMatchIfSingleItemNotFound() throws Exception { |
| 54 | + assertMismatchDescription("could not find subsequence inside collection [<WithValue 3>]", hasSubsequence(value(4)), asList(make(3))); |
| 55 | + } |
| 56 | + |
| 57 | + public void testDoesNotMatchIfOneOfMultipleItemsNotFound() throws Exception { |
| 58 | + assertMismatchDescription("could not find subsequence inside collection [<WithValue 1>, <WithValue 2>, <WithValue 4>]", contains123, asList(make(1), make(2), make(4))); |
| 59 | + } |
| 60 | + |
| 61 | + public void testDoesNotMatchEmptyCollection() throws Exception { |
| 62 | + assertMismatchDescription("could not find subsequence inside collection []", hasSubsequence(value(4)), new ArrayList<WithValue>()); |
| 63 | + } |
| 64 | + |
| 65 | + public void testHasAReadableDescription() { |
| 66 | + assertDescription("collection contains subsequence matching [<1>, <2>]", hasSubsequence(1, 2)); |
| 67 | + } |
| 68 | + |
| 69 | + public static class WithValue { |
| 70 | + private final int value; |
| 71 | + public WithValue(int value) { this.value = value; } |
| 72 | + public int getValue() { return value; } |
| 73 | + @Override public String toString() { return "WithValue " + value; } |
| 74 | + } |
| 75 | + |
| 76 | + public static WithValue make(int value) { |
| 77 | + return new WithValue(value); |
| 78 | + } |
| 79 | + |
| 80 | + public static Matcher<WithValue> value(int value) { |
| 81 | + return new FeatureMatcher<WithValue, Integer>(equalTo(value), "value with", "value") { |
| 82 | + @Override protected Integer featureValueOf(WithValue actual) { return actual.getValue(); } |
| 83 | + }; |
| 84 | + } |
| 85 | +} |
0 commit comments