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 | 0 | return new CollectionSubject(failureStrategy, list); |
38 | |
} |
39 | |
|
40 | |
|
41 | |
protected CollectionSubject(FailureStrategy failureStrategy, C list) { |
42 | 0 | super(failureStrategy, list); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
@Override public void isEmpty() { |
49 | 0 | if (!getSubject().isEmpty()) { |
50 | 0 | fail("is empty"); |
51 | |
} |
52 | 0 | } |
53 | |
|
54 | |
public Has<T, C> has() { |
55 | 0 | return new Has<T, C>() { |
56 | |
@Override public void item(T item) { |
57 | 0 | if (!getSubject().contains(item)) { |
58 | 0 | fail("has item", item); |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
@Override public void anyOf(T first) { |
63 | 0 | anyFrom(accumulate(first)); |
64 | 0 | } |
65 | |
@SafeVarargs @Override public final void anyOf(T first, T second, T ... rest) { |
66 | 0 | anyFrom(accumulate(first, second, rest)); |
67 | 0 | } |
68 | |
@Override public void anyFrom(Collection<T> col) { |
69 | 0 | for (Object item : col) { |
70 | 0 | if (getSubject().contains(item)) { |
71 | 0 | return; |
72 | |
} |
73 | 0 | } |
74 | 0 | fail("contains", col); |
75 | 0 | } |
76 | |
|
77 | |
@Override public Ordered allOf(T first) { |
78 | 0 | return allFrom(accumulate(first)); |
79 | |
} |
80 | |
@SafeVarargs @Override public final Ordered allOf(T first, T second, T ... rest) { |
81 | 0 | return allFrom(accumulate(first, second, rest)); |
82 | |
} |
83 | |
@Override public Ordered allFrom(final Collection<T> required) { |
84 | 0 | Collection<T> toRemove = new ArrayList<T>(required); |
85 | |
|
86 | 0 | for (Object item : getSubject()) { |
87 | 0 | toRemove.remove(item); |
88 | 0 | } |
89 | 0 | if (!toRemove.isEmpty()) { |
90 | 0 | failWithBadResults("has all of", required, "is missing", countDuplicates(toRemove)); |
91 | |
} |
92 | 0 | 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 | |
@SafeVarargs @Override public final Ordered exactly(T first, T second, T ... rest) { |
99 | 0 | return exactlyAs(accumulate(first, second, rest)); |
100 | |
} |
101 | |
@Override public Ordered exactlyAs(Collection<T> required) { |
102 | 0 | Collection<T> toRemove = new ArrayList<T>(required); |
103 | 0 | Collection<Object> extra = new ArrayList<Object>(); |
104 | |
|
105 | 0 | for (Object item : getSubject()) { |
106 | 0 | if (!toRemove.remove(item)) { |
107 | 0 | extra.add(item); |
108 | |
} |
109 | 0 | } |
110 | 0 | if (!toRemove.isEmpty()) { |
111 | 0 | failWithBadResults("has exactly", required, "is missing", countDuplicates(toRemove)); |
112 | |
} |
113 | 0 | if (!extra.isEmpty()) { |
114 | 0 | failWithBadResults("has exactly", required, "has unexpected items", countDuplicates(extra)); |
115 | |
} |
116 | 0 | 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 | 0 | InOrder(String check, Collection<T> required) { |
126 | 0 | this.check = check; |
127 | 0 | this.required = required; |
128 | 0 | } |
129 | |
|
130 | |
@Override public void inOrder() { |
131 | 0 | Iterator<T> actualItems = getSubject().iterator(); |
132 | 0 | for (Object expected : required) { |
133 | 0 | if (!actualItems.hasNext()) { |
134 | 0 | fail(check, required); |
135 | |
} else { |
136 | 0 | Object actual = actualItems.next(); |
137 | 0 | if (actual == expected || actual != null && actual.equals(expected)) { |
138 | 0 | continue; |
139 | |
} else { |
140 | 0 | fail(check, required); |
141 | |
} |
142 | |
} |
143 | 0 | } |
144 | 0 | if (actualItems.hasNext()) { |
145 | 0 | fail(check, required); |
146 | |
} |
147 | 0 | } |
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 | |
} |