/
RecursiveExamples.java
89 lines (73 loc) · 1.9 KB
/
RecursiveExamples.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
package net.jqwik.docs;
import net.jqwik.api.*;
import net.jqwik.api.arbitraries.*;
import static net.jqwik.api.Arbitraries.*;
@Group
@PropertyDefaults(afterFailure = AfterFailureMode.RANDOM_SEED)
class RecursiveExamples {
@Group
class LazyOf {
@Property
@Report(Reporting.GENERATED)
boolean sentencesEndWithAPoint(@ForAll("sentences") String aSentence) {
return !aSentence.contains("x");
// return aSentence.endsWith(".");
}
@Provide
Arbitrary<String> sentences() {
return Arbitraries.lazyOf(
() -> word().map(w -> w + "."),
this::sentence,
this::sentence,
this::sentence
);
}
private Arbitrary<String> sentence() {
return Combinators.combine(sentences(), word())
.as((s, w) -> w + " " + s);
}
}
@Group
class Lazy {
@Property
@Report(Reporting.GENERATED)
boolean sentencesEndWithAPoint(@ForAll("sentences") String aSentence) {
return !aSentence.contains("x");
// return aSentence.endsWith(".");
}
@Provide
Arbitrary<String> sentences() {
Arbitrary<String> sentence =
Combinators.combine(lazy(this::sentences), word())
.as((s, w) -> w + " " + s);
return Arbitraries.frequencyOf(
Tuple.of(1, word().map(w -> w + ".")),
Tuple.of(3, sentence)
);
}
}
@Group
class Recursive {
@Property
@Report(Reporting.GENERATED)
boolean sentencesEndWithAPoint(@ForAll("deterministic") String aSentence) {
// return aSentence.endsWith(".");
return !aSentence.contains("x");
}
@Provide
Arbitrary<String> deterministic() {
Arbitrary<String> lastWord = word().map(w -> w + ".");
return Arbitraries.recursive(
() -> lastWord,
this::prependWord,
0, 10
);
}
private Arbitrary<String> prependWord(Arbitrary<String> sentence) {
return Combinators.combine(word(), sentence).as((w, s) -> w + " " + s);
}
}
private StringArbitrary word() {
return Arbitraries.strings().alpha().ofLength(5);
}
}