| 1 | |
package net.sf.cotta.test.assertion; |
| 2 | |
|
| 3 | |
import org.hamcrest.BaseMatcher; |
| 4 | |
import org.hamcrest.Description; |
| 5 | |
import org.hamcrest.Matcher; |
| 6 | |
import org.hamcrest.Matchers; |
| 7 | |
import org.hamcrest.core.IsNot; |
| 8 | |
|
| 9 | |
import java.util.Arrays; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
public class ListAssert<T> extends BaseAssert<List<T>, ListAssert<T>> { |
| 13 | |
public ListAssert(T[] value) { |
| 14 | 4 | super(value == null ? null : Arrays.asList(value)); |
| 15 | 4 | } |
| 16 | |
|
| 17 | |
public ListAssert(List<T> value) { |
| 18 | 2 | super(value); |
| 19 | 2 | } |
| 20 | |
|
| 21 | |
public ListAssert<T> eq(T... expected) { |
| 22 | 0 | eq(Arrays.asList(expected)); |
| 23 | 0 | return this; |
| 24 | |
} |
| 25 | |
|
| 26 | |
public ListAssert<T> contains(T... expected) { |
| 27 | 0 | iterableMatches(Matchers.hasItems(expected)); |
| 28 | 0 | return this; |
| 29 | |
} |
| 30 | |
|
| 31 | |
private void iterableMatches(Matcher<Iterable<T>> matcher) { |
| 32 | 0 | assertThat(value(), matcher); |
| 33 | 0 | } |
| 34 | |
|
| 35 | |
public ListAssert<T> isEmpty() { |
| 36 | 0 | matches(matcherIsEmpty()); |
| 37 | 0 | return this; |
| 38 | |
} |
| 39 | |
|
| 40 | |
private BaseMatcher<List<T>> matcherIsEmpty() { |
| 41 | 0 | return new BaseMatcher<List<T>>() { |
| 42 | |
|
| 43 | |
@SuppressWarnings({"unchecked"}) |
| 44 | |
public boolean matches(Object item) { |
| 45 | 0 | return ((List<T>) item).isEmpty(); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public void describeTo(Description description) { |
| 49 | 0 | description.appendText("list should be empty"); |
| 50 | 0 | } |
| 51 | |
}; |
| 52 | |
} |
| 53 | |
|
| 54 | |
public T hasOneItem() { |
| 55 | 4 | hasSize(1); |
| 56 | 4 | return value().get(0); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public ListAssert<T> hasSize(int expected) { |
| 60 | 4 | isOfSize(expected); |
| 61 | 4 | return this; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public ListAssert<T> isOfSize(final int expected) { |
| 65 | 4 | matches(new BaseMatcher<List<T>>() { |
| 66 | |
@SuppressWarnings({"unchecked"}) |
| 67 | |
public boolean matches(Object o) { |
| 68 | 4 | return ((List<T>) o).size() == expected; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void describeTo(Description description) { |
| 72 | 0 | description.appendText("list should have size of <").appendValue(expected).appendText(">"); |
| 73 | 0 | } |
| 74 | |
}); |
| 75 | 4 | return this; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public ListAssert<T> notEmpty() { |
| 79 | 0 | matches(IsNot.not(matcherIsEmpty())); |
| 80 | 0 | return this; |
| 81 | |
} |
| 82 | |
} |