| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.truth0.subjects; |
| 17 | |
|
| 18 | |
import com.google.common.annotations.GwtCompatible; |
| 19 | |
|
| 20 | |
import org.truth0.FailureStrategy; |
| 21 | |
|
| 22 | |
import java.util.Arrays; |
| 23 | |
import java.util.Iterator; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
@GwtCompatible |
| 29 | |
public class IterableSubject<S extends IterableSubject<S, T, C>, T, C extends Iterable<T>> extends Subject<S, C> { |
| 30 | |
|
| 31 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 32 | |
public static <T, C extends Iterable<T>> IterableSubject<? extends IterableSubject<?, T, C>, T, C> create( |
| 33 | |
FailureStrategy failureStrategy, Iterable<T> list) { |
| 34 | 11 | return new IterableSubject(failureStrategy, list); |
| 35 | |
} |
| 36 | |
|
| 37 | |
|
| 38 | |
protected IterableSubject(FailureStrategy failureStrategy, C list) { |
| 39 | 83 | super(failureStrategy, list); |
| 40 | 83 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public void isEmpty() { |
| 46 | 2 | if (getSubject().iterator().hasNext()) { |
| 47 | 1 | fail("is empty"); |
| 48 | |
} |
| 49 | 1 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public void isNotEmpty() { |
| 55 | 2 | if (!getSubject().iterator().hasNext()) { |
| 56 | 1 | fail("is not empty"); |
| 57 | |
} |
| 58 | 1 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public void iteratesAs(Iterable<T> expectedItems) { |
| 68 | 7 | Iterator<T> actualItems = getSubject().iterator(); |
| 69 | 7 | for (Object expected : expectedItems) { |
| 70 | 16 | if (!actualItems.hasNext()) { |
| 71 | 1 | fail("iterates through", expectedItems); |
| 72 | |
} else { |
| 73 | 15 | Object actual = actualItems.next(); |
| 74 | 15 | if (actual == expected || actual != null && actual.equals(expected)) { |
| 75 | 0 | continue; |
| 76 | |
} else { |
| 77 | 1 | fail("iterates through", expectedItems); |
| 78 | |
} |
| 79 | 0 | } |
| 80 | |
} |
| 81 | 5 | if (actualItems.hasNext()) { |
| 82 | 1 | fail("iterates through", expectedItems); |
| 83 | |
} |
| 84 | 4 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
@Deprecated |
| 90 | |
public void iteratesOverSequence(T... expectedItems) { |
| 91 | 1 | iteratesAs(expectedItems); |
| 92 | 1 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public void iteratesAs(T... expectedItems) { |
| 101 | 6 | iteratesAs(Arrays.asList(expectedItems)); |
| 102 | 3 | } |
| 103 | |
} |