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