/
PropertiesWithExplicitGeneratorsTest.java
365 lines (297 loc) · 12.2 KB
/
PropertiesWithExplicitGeneratorsTest.java
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
The MIT License
Copyright (c) 2010-2017 Paul R. Holser, Jr.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.pholser.junit.quickcheck;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import com.pholser.junit.quickcheck.test.generator.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import static org.junit.rules.ExpectedException.none;
public class PropertiesWithExplicitGeneratorsTest {
@Rule public final ExpectedException thrown = none();
@Test public void explicitPrimitiveBoolean() {
assertThat(testResult(ExplicitPrimitiveBoolean.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveBoolean {
@Property public void shouldHold(@From(ABool.class) boolean b) {
}
}
@Test public void explicitWrapperBoolean() {
assertThat(testResult(ExplicitWrapperBoolean.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperBoolean {
@Property public void shouldHold(@From(ABool.class) boolean b) {
}
}
@Test public void explicitPrimitiveByte() {
assertThat(testResult(ExplicitPrimitiveByte.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveByte {
@Property public void shouldHold(@From(AByte.class) byte b) {
}
}
@Test public void explicitWrapperByte() {
assertThat(testResult(ExplicitWrapperByte.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperByte {
@Property public void shouldHold(@From(AByte.class) byte b) {
}
}
@Test public void explicitPrimitiveChar() {
assertThat(testResult(ExplicitPrimitiveChar.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveChar {
@Property public void shouldHold(@From(AChar.class) char ch) {
}
}
@Test public void explicitWrapperChar() {
assertThat(testResult(ExplicitWrapperChar.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperChar {
@Property public void shouldHold(@From(AChar.class) char ch) {
}
}
@Test public void explicitPrimitiveDouble() {
assertThat(testResult(ExplicitPrimitiveDouble.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveDouble {
@Property public void shouldHold(@From(ADouble.class) double d) {
}
}
@Test public void explicitWrapperDouble() {
assertThat(testResult(ExplicitWrapperDouble.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperDouble {
@Property public void shouldHold(@From(ADouble.class) Double d) {
}
}
@Test public void explicitPrimitiveFloat() {
assertThat(testResult(ExplicitPrimitiveFloat.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveFloat {
@Property public void shouldHold(@From(AFloat.class) float f) {
}
}
@Test public void explicitWrapperFloat() {
assertThat(testResult(ExplicitWrapperFloat.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperFloat {
@Property public void shouldHold(@From(AFloat.class) Float f) {
}
}
@Test public void explicitPrimitiveInt() {
assertThat(testResult(ExplicitPrimitiveInt.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveInt {
@Property public void shouldHold(@From(AnInt.class) int i) {
}
}
@Test public void explicitWrapperInt() {
assertThat(testResult(ExplicitWrapperInt.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperInt {
@Property public void shouldHold(@From(AnInt.class) Integer i) {
}
}
@Test public void explicitPrimitiveLong() {
assertThat(testResult(ExplicitPrimitiveLong.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveLong {
@Property public void shouldHold(@From(ALong.class) long ell) {
}
}
@Test public void explicitWrapperLong() {
assertThat(testResult(ExplicitWrapperLong.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperLong {
@Property public void shouldHold(@From(ALong.class) long ell) {
}
}
@Test public void explicitPrimitiveShort() {
assertThat(testResult(ExplicitPrimitiveShort.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveShort {
@Property public void shouldHold(@From(AShort.class) short sh) {
}
}
@Test public void explicitWrapperShort() {
assertThat(testResult(ExplicitWrapperShort.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWrapperShort {
@Property public void shouldHold(@From(AShort.class) short sh) {
}
}
@Test public void explicitWithParameterizedType() {
assertThat(testResult(ExplicitWithParameterizedType.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitWithParameterizedType {
@Property public void shouldHold(@From(AList.class) List<Foo> list) {
for (Foo each : list) {
// ensure the cast works
}
}
}
@Test public void explicitPrimitiveIntFixedSeed() {
assertThat(testResult(ExplicitPrimitiveIntFixedSeed.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class ExplicitPrimitiveIntFixedSeed {
@Property(trials = 1) public void shouldHold(@When(seed = -1) @From(AnInt.class) int i) {
assertEquals(1155099827, i);
}
}
@Test public void indirectPrimitiveInt() {
assertThat(testResult(IndirectPrimitiveInt.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class IndirectPrimitiveInt {
@Target({PARAMETER, FIELD, ANNOTATION_TYPE, TYPE_USE})
@Retention(RUNTIME)
@From(AnInt.class)
@Between(min = 1, max = 5)
public @interface Small {
}
@Property public void shouldHold(@Small int i) {
assertThat(i, allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(5)));
}
}
@Test public void explicitGeneratorTakesPrecedence() throws Exception {
assertThat(testResult(WithExplicitGenerator.class), isSuccessful());
assertEquals(asList(0, 1, 2, 3, 4), WithExplicitGenerator.values);
}
@RunWith(JUnitQuickcheck.class)
public static class WithExplicitGenerator {
public static final List<Integer> values = new ArrayList<>();
@Property(trials = 5) public void shouldHold(@From(Sequence.class) int i) {
values.add(i);
}
}
public static class Sequence extends Generator<Integer> {
private int next = 0;
public Sequence() {
super(int.class);
}
@Override public Integer generate(SourceOfRandomness random, GenerationStatus status) {
return next++;
}
}
@Test public void typeMismatch() throws Exception {
assertThat(
testResult(WithGeneratorTypeThatDoesNotMatchParameterType.class),
hasSingleFailureContaining(IllegalArgumentException.class.getName()));
}
@RunWith(JUnitQuickcheck.class)
public static class WithGeneratorTypeThatDoesNotMatchParameterType {
@Property public void shouldHold(@From(StringEmitter.class) Number n) {
}
}
public static class StringEmitter extends Generator<String> {
public StringEmitter() {
super(String.class);
}
@Override public String generate(SourceOfRandomness random, GenerationStatus status) {
return "foo";
}
}
@Test public void alternateGeneratorOnBasicTypeParameter() throws Exception {
assertThat(testResult(AlternateGeneratorOnBasicTypeParameter.class), isSuccessful());
}
@RunWith(JUnitQuickcheck.class)
public static class AlternateGeneratorOnBasicTypeParameter {
@Property public void holds(Box<@From(AnInt.class) @Between(min = 3, max = 4) Integer> box) {
assertThat(box.contents(), allOf(greaterThanOrEqualTo(3), lessThanOrEqualTo(4)));
}
}
@Test public void alternateGeneratorOnHuhTypeParameter() throws Exception {
assertThat(
testResult(AlternateGeneratorOnHuhTypeParameter.class),
hasSingleFailureContaining("Wildcards cannot be marked with @From"));
}
@RunWith(JUnitQuickcheck.class)
public static class AlternateGeneratorOnHuhTypeParameter {
@Property public void holds(Box<@From(AnInt.class) ?> box) {
}
}
@Test public void alternateGeneratorOnExtendsTypeParameter() throws Exception {
assertThat(
testResult(AlternateGeneratorOnExtendsTypeParameter.class),
hasSingleFailureContaining("Wildcards cannot be marked with @From"));
}
@RunWith(JUnitQuickcheck.class)
public static class AlternateGeneratorOnExtendsTypeParameter {
@Property public void holds(Box<@From(AnInt.class) ? extends Integer> box) {
}
}
@Test public void alternateGeneratorOnSuperTypeParameter() throws Exception {
assertThat(
testResult(AlternateGeneratorOnSuperTypeParameter.class),
hasSingleFailureContaining("Wildcards cannot be marked with @From"));
}
@RunWith(JUnitQuickcheck.class)
public static class AlternateGeneratorOnSuperTypeParameter {
@Property public void holds(Box<@From(AnInt.class) ? super Integer> box) {
}
}
@Test public void typeMismatchOnBasicTypeParameter() {
assertThat(
testResult(TypeMismatchOnBasicTypeParameter.class),
hasSingleFailureContaining(IllegalArgumentException.class.getName()));
}
@RunWith(JUnitQuickcheck.class)
public static class TypeMismatchOnBasicTypeParameter {
@Property public void holds(Box<@From(AnInt.class) String> box) {
}
}
}