forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckResult.java
More file actions
299 lines (270 loc) · 10.6 KB
/
CheckResult.java
File metadata and controls
299 lines (270 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package fj.test;
import static fj.Bottom.decons;
import fj.F;
import fj.Show;
import fj.data.List;
import fj.data.Option;
import static fj.data.Option.some;
import static fj.Show.listShow;
import static fj.Show.showS;
import static fj.test.Arg.argShow;
import java.io.StringWriter;
import java.io.PrintWriter;
/**
* An enumeration of the possible results after checking a property. A <code>CheckResult</code> may
* be in one of six states:
* <ol>
* <li>Passed</li>
* <li>Proven</li>
* <li>Falsified</li>
* <li>Exhausted</li>
* <li>Exception executing the property</li>
* <li>Exception generating values to check the property</li>
* </ol>
*
* @version %build.number%
*/
public final class CheckResult {
private final R r;
private final Option<List<Arg<?>>> args;
private final Option<Throwable> ex;
private final int succeeded;
private final int discarded;
private enum R {
Passed, Proven, Falsified, Exhausted, PropException, GenException
}
private CheckResult(final R r, final Option<List<Arg<?>>> args, final Option<Throwable> ex, final int succeeded,
final int discarded) {
this.r = r;
this.args = args;
this.ex = ex;
this.succeeded = succeeded;
this.discarded = discarded;
}
/**
* Returns a result that the property has passed.
*
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that the property has passed.
*/
public static CheckResult passed(final int succeeded, final int discarded) {
return new CheckResult(R.Passed, Option.<List<Arg<?>>>none(), Option.<Throwable>none(), succeeded, discarded);
}
/**
* Returns a result that the property has been proven.
*
* @param args The arguments used to prove the property.
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that the property has been proven.
*/
public static CheckResult proven(final List<Arg<?>> args, final int succeeded, final int discarded) {
return new CheckResult(R.Proven, some(args), Option.<Throwable>none(), succeeded, discarded);
}
/**
* Returns a result that the property has been falsified.
*
* @param args The arguments used to falsify the property.
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that the property has been falsified.
*/
public static CheckResult falsified(final List<Arg<?>> args, final int succeeded, final int discarded) {
return new CheckResult(R.Falsified, some(args), Option.<Throwable>none(), succeeded, discarded);
}
/**
* Returns a result that the property been exhausted in checking.
*
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that the property has been exhausted in checking.
*/
public static CheckResult exhausted(final int succeeded, final int discarded) {
return new CheckResult(R.Exhausted, Option.<List<Arg<?>>>none(), Option.<Throwable>none(), succeeded, discarded);
}
/**
* Returns a result that checking the property threw an exception.
*
* @param args The arguments used when the exception was thrown.
* @param ex The exception that was thrown.
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that checking the property threw an exception.
*/
public static CheckResult propException(final List<Arg<?>> args, final Throwable ex, final int succeeded,
final int discarded) {
return new CheckResult(R.PropException, some(args), some(ex), succeeded, discarded);
}
/**
* Returns a result that generating values to check the property threw an exception.
*
* @param ex The exception that was thrown.
* @param succeeded The number of checks that succeeded.
* @param discarded The number of checks that were discarded.
* @return A result that generating values to check the property threw an exception.
*/
public static CheckResult genException(final Throwable ex, final int succeeded, final int discarded) {
return new CheckResult(R.GenException, Option.<List<Arg<?>>>none(), some(ex), succeeded, discarded);
}
/**
* Returns <code>true</code> if this result is passed, <code>false</code> otherwise.
*
* @return <code>true</code> if this result is passed, <code>false</code> otherwise.
*/
public boolean isPassed() {
return r == R.Passed;
}
/**
* Returns <code>true</code> if this result is proven, <code>false</code> otherwise.
*
* @return <code>true</code> if this result is proven, <code>false</code> otherwise.
*/
public boolean isProven() {
return r == R.Proven;
}
/**
* Returns <code>true</code> if this result is falsified, <code>false</code> otherwise.
*
* @return <code>true</code> if this result is falsified, <code>false</code> otherwise.
*/
public boolean isFalsified() {
return r == R.Falsified;
}
/**
* Returns <code>true</code> if this result is exhausted, <code>false</code> otherwise.
*
* @return <code>true</code> if this result is exhausted, <code>false</code> otherwise.
*/
public boolean isExhausted() {
return r == R.Exhausted;
}
/**
* Returns <code>true</code> if this result is an exception during property execution,
* <code>false</code> otherwise.
*
* @return <code>true</code> if this result is an exception during property execution,
* <code>false</code> otherwise.
*/
public boolean isPropException() {
return r == R.PropException;
}
/**
* Returns <code>true</code> if this result is an exception during generating of values for
* property checking, <code>false</code> otherwise.
*
* @return <code>true</code> if this result is an exception during generating of values for
* property checking, <code>false</code> otherwise.
*/
public boolean isGenException() {
return r == R.GenException;
}
/**
* Returns the arguments if the result is one of; proven, falsified or exception during property
* execution, otherwise, no arguments are returned.
*
* @return The arguments if the result is one of; proven, falsified or exception during property
* execution, otherwise, no arguments are returned.
*/
public Option<List<Arg<?>>> args() {
return args;
}
/**
* Returns the execption if the result is one of; exception during property execution or exception
* during argument value generation, otherwise, no exception are returned.
*
* @return The execption if the result is one of; exception during property execution or exception
* during argument value generation, otherwise, no exception are returned.
*/
public Option<Throwable> exception() {
return ex;
}
/**
* Returns the number of succeeded checks of the property in this result.
*
* @return The number of succeeded checks of the property in this result.
*/
public int succeeded() {
return succeeded;
}
/**
* Returns the number of discarded checks of the property in this result.
*
* @return The number of discarded checks of the property in this result.
*/
public int discarded() {
return discarded;
}
/**
* A rendering of a check result that summarises in one line.
*
* @param sa The rendering of arguments.
* @return A rendering of a check result that summarises in one line.
*/
public static Show<CheckResult> summary(final Show<Arg<?>> sa) {
return showS(new F<CheckResult, String>() {
private String test(final CheckResult r) {
return r.succeeded() == 1 ? "test" : "tests";
}
private String arguments(final CheckResult r) {
final List<Arg<?>> args = r.args().some();
return args.length() == 1 ? "argument: " + sa.showS(args.head()) : "arguments: " + listShow(sa).showS(args);
}
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
public String f(final CheckResult r) {
if (r.isProven())
return "OK, property proven with " + arguments(r);
else if (r.isPassed())
return "OK, passed " + r.succeeded() + ' ' + test(r) +
(r.discarded() > 0 ? " (" + r.discarded() + " discarded)" : "") + '.';
else if (r.isFalsified())
return "Falsified after " + r.succeeded() + " passed " + test(r) + " with " + arguments(r);
else if (r.isExhausted())
return "Gave up after " + r.succeeded() + " passed " + test(r) + " and " + r.discarded() +
" discarded tests.";
else if (r.isPropException()) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
r.exception().some().printStackTrace(pw);
return "Exception on property evaluation with " + arguments(r) + System.getProperty("line.separator") + sw;
} else if (r.isGenException()) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
r.exception().some().printStackTrace(pw);
return "Exception on argument generation " + System.getProperty("line.separator") + sw;
} else
throw decons(r.getClass());
}
});
}
/**
* A rendering of a check result that summarises in one line.
*/
public static final Show<CheckResult> summary = summary(argShow);
/**
* A rendering of a check result that summarises in one line but throws an exception in the result
* is a failure (falsified, property exception or generator exception).
*/
public static final Show<CheckResult> summaryEx = summaryEx(argShow);
/**
* A rendering of a check result that summarises in one line but throws an exception in the result
* is a failure (falsified, property exception or generator exception).
*
* @param sa The rendering of arguments.
* @return A rendering of a check result that summarises in one line but throws an exception in
* the result is a failure (falsified, property exception or generator exception).
*/
public static Show<CheckResult> summaryEx(final Show<Arg<?>> sa) {
return showS(new F<CheckResult, String>() {
public String f(final CheckResult r) {
final String s = summary(sa).show(r).toString();
if (r.isProven() || r.isPassed() || r.isExhausted())
return s;
else if (r.isFalsified() || r.isPropException() || r.isGenException())
throw new Error(s);
else
throw decons(r.getClass());
}
});
}
}