| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.truth0.subjects; |
| 19 | |
|
| 20 | |
import static org.truth0.subjects.SubjectUtils.accumulate; |
| 21 | |
import static org.truth0.subjects.SubjectUtils.countDuplicates; |
| 22 | |
|
| 23 | |
import com.google.common.annotations.GwtCompatible; |
| 24 | |
|
| 25 | |
import org.truth0.FailureStrategy; |
| 26 | |
|
| 27 | |
import java.util.ArrayList; |
| 28 | |
import java.util.Collection; |
| 29 | |
import java.util.Iterator; |
| 30 | |
|
| 31 | |
@GwtCompatible |
| 32 | |
public class CollectionSubject<S extends CollectionSubject<S, T, C>, T, C extends Collection<T>> extends IterableSubject<S, T, C> { |
| 33 | |
|
| 34 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 35 | |
public static <T, C extends Collection<T>> CollectionSubject<? extends CollectionSubject<?, T, C>, T, C> create( |
| 36 | |
FailureStrategy failureStrategy, Collection<T> list) { |
| 37 | 42 | return new CollectionSubject(failureStrategy, list); |
| 38 | |
} |
| 39 | |
|
| 40 | |
|
| 41 | |
protected CollectionSubject(FailureStrategy failureStrategy, C list) { |
| 42 | 72 | super(failureStrategy, list); |
| 43 | 72 | } |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
@Override public void isEmpty() { |
| 49 | 2 | if (!getSubject().isEmpty()) { |
| 50 | 1 | fail("is empty"); |
| 51 | |
} |
| 52 | 1 | } |
| 53 | |
|
| 54 | |
public Has<T, C> has() { |
| 55 | 45 | return new Has<T, C>() { |
| 56 | |
@Override public void item(T item) { |
| 57 | 4 | if (!getSubject().contains(item)) { |
| 58 | 1 | fail("has item", item); |
| 59 | |
} |
| 60 | 3 | } |
| 61 | |
|
| 62 | |
@Override public void anyOf(T first) { |
| 63 | 0 | anyFrom(accumulate(first)); |
| 64 | 0 | } |
| 65 | |
@Override public final void anyOf(T first, T second, T ... rest) { |
| 66 | 6 | anyFrom(accumulate(first, second, rest)); |
| 67 | 5 | } |
| 68 | |
@Override public void anyFrom(Collection<T> col) { |
| 69 | 6 | for (Object item : col) { |
| 70 | 11 | if (getSubject().contains(item)) { |
| 71 | 4 | return; |
| 72 | |
} |
| 73 | |
} |
| 74 | 2 | fail("contains", col); |
| 75 | 1 | } |
| 76 | |
|
| 77 | |
@Override public Ordered allOf(T first) { |
| 78 | 0 | return allFrom(accumulate(first)); |
| 79 | |
} |
| 80 | |
@Override public final Ordered allOf(T first, T second, T ... rest) { |
| 81 | 16 | return allFrom(accumulate(first, second, rest)); |
| 82 | |
} |
| 83 | |
@Override public Ordered allFrom(final Collection<T> required) { |
| 84 | 16 | Collection<T> toRemove = new ArrayList<T>(required); |
| 85 | |
|
| 86 | 16 | for (Object item : getSubject()) { |
| 87 | 49 | toRemove.remove(item); |
| 88 | |
} |
| 89 | 16 | if (!toRemove.isEmpty()) { |
| 90 | 5 | failWithBadResults("has all of", required, "is missing", countDuplicates(toRemove)); |
| 91 | |
} |
| 92 | 12 | return new InOrder("has all in order", required); |
| 93 | |
} |
| 94 | |
|
| 95 | |
@Override public Ordered exactly(T first) { |
| 96 | 0 | return exactlyAs(accumulate(first)); |
| 97 | |
} |
| 98 | |
@Override public final Ordered exactly(T first, T second, T ... rest) { |
| 99 | 19 | return exactlyAs(accumulate(first, second, rest)); |
| 100 | |
} |
| 101 | |
@Override public Ordered exactlyAs(Collection<T> required) { |
| 102 | 19 | Collection<T> toRemove = new ArrayList<T>(required); |
| 103 | 19 | Collection<Object> extra = new ArrayList<Object>(); |
| 104 | |
|
| 105 | 19 | for (Object item : getSubject()) { |
| 106 | 61 | if (!toRemove.remove(item)) { |
| 107 | 3 | extra.add(item); |
| 108 | |
} |
| 109 | |
} |
| 110 | 19 | if (!toRemove.isEmpty()) { |
| 111 | 5 | failWithBadResults("has exactly", required, "is missing", countDuplicates(toRemove)); |
| 112 | |
} |
| 113 | 14 | if (!extra.isEmpty()) { |
| 114 | 2 | failWithBadResults("has exactly", required, "has unexpected items", countDuplicates(extra)); |
| 115 | |
} |
| 116 | 12 | return new InOrder("has exactly in order", required); |
| 117 | |
} |
| 118 | |
}; |
| 119 | |
} |
| 120 | |
|
| 121 | |
private class InOrder implements Ordered { |
| 122 | |
private final String check; |
| 123 | |
private final Collection<T> required; |
| 124 | |
|
| 125 | 24 | InOrder(String check, Collection<T> required) { |
| 126 | 24 | this.check = check; |
| 127 | 24 | this.required = required; |
| 128 | 24 | } |
| 129 | |
|
| 130 | |
@Override public void inOrder() { |
| 131 | 12 | Iterator<T> actualItems = getSubject().iterator(); |
| 132 | 12 | for (Object expected : required) { |
| 133 | 26 | if (!actualItems.hasNext()) { |
| 134 | 2 | fail(check, required); |
| 135 | |
} else { |
| 136 | 24 | Object actual = actualItems.next(); |
| 137 | 24 | if (actual == expected || actual != null && actual.equals(expected)) { |
| 138 | 0 | continue; |
| 139 | |
} else { |
| 140 | 4 | fail(check, required); |
| 141 | |
} |
| 142 | 0 | } |
| 143 | |
} |
| 144 | 6 | if (actualItems.hasNext()) { |
| 145 | 2 | fail(check, required); |
| 146 | |
} |
| 147 | 4 | } |
| 148 | |
} |
| 149 | |
|
| 150 | |
public interface Has<E, C extends Collection<E>> { |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
void item(E item); |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
void anyOf(E first); |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
void anyOf(E first, E second, E... rest); |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
void anyFrom(Collection<E> expected); |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
Ordered allOf(E first); |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
Ordered allOf(E first, E second, E... rest); |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
Ordered allFrom(Collection<E> expected); |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
Ordered exactly(E first); |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
Ordered exactly(E first, E second, E... rest); |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
Ordered exactlyAs(Collection<E> expected); |
| 218 | |
} |
| 219 | |
} |