-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilders.dart
191 lines (145 loc) · 4.56 KB
/
builders.dart
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
Int([int start = 0, int end = 42]) {
if (start <= end)
return new ArbitraryInt(start, end + 1);
else
return new ArbitraryInt(end, start + 1);
}
Arbitrary<List<Object>> ListOf(Iterator<Object> g, [minLength, maxLength]) =>
new ArbitraryList<Object>(g, minLength, maxLength);
Arbitrary<Object> Choice(List<Object> elements) =>
new ArbitraryChoice<Object>(elements);
Arbitrary<Object> Single(Object o) => Choice([o]);
Arbitrary<Object> CharRange(String a, String b) =>
Int(a.charCodeAt(0), b.charCodeAt(0)).passThrough((x) => new String.fromCharCodes([x]));
interface ArbitraryBuilderMarker<T> {
}
class ArbitraryIntBuilder implements ArbitraryBuilderMarker<int> {
int start = 0, end = 42;
var parent;
ArbitraryIntBuilder();
ArbitraryIntBuilder.withParent(this.parent);
ArbitraryIntBuilder between(int a, int b) =>
greaterOrEqual(a).lessThan(b);
ArbitraryIntBuilder lessThan(int x) => lessOrEqual(x-1);
ArbitraryIntBuilder greaterThan(int x) => greaterOrEqual(x+1);
ArbitraryIntBuilder lessOrEqual(int x) {
end = x;
return this;
}
ArbitraryIntBuilder greaterOrEqual(int x) {
start = x;
return this;
}
Arbitrary<Object> toArbitrary() {
if(parent !== null) {
return parent.toArbitrary(Int(start, end));
} else {
return Int(start, end);
}
}
}
class ArbitraryListBuilder implements ArbitraryBuilderMarker {
int minLength = 0, maxLength = 42;
var parent;
ArbitraryListBuilder();
ArbitraryListBuilder.withParent(this.parent);
ArbitraryListBuilder ofLength(int min, [int max]) {
if (max === null) {
max = min;
}
this.minLength = min;
this.maxLength = max;
return this;
}
ForAllProxy get with() => new ForAllProxy(this);
Arbitrary toArbitrary(iterator) {
if (parent !== null) {
return parent.toArbitrary(ListOf(iterator, minLength, maxLength));
} else {
return ListOf(iterator, minLength, maxLength);
}
}
}
class ArbitraryCharBuilder implements ArbitraryBuilderMarker<String> {
String rangeStart = 'a', rangeEnd = 'z';
var parent;
ArbitraryCharBuilder();
ArbitraryCharBuilder.withParent(this.parent);
ArbitraryCharBuilder ofRange(String a, String b) {
this.rangeStart = a;
this.rangeEnd = b;
return this;
}
Arbitrary toArbitrary() {
if (parent !== null) {
return parent.toArbitrary(CharRange(rangeStart, rangeEnd));
} else {
return CharRange(rangeStart, rangeEnd);
}
}
}
class ArbitraryChoiceBuilder implements ArbitraryBuilderMarker<Object> {
var parent;
List<Object> elements;
ArbitraryChoiceBuilder(this.elements, [this.parent = null]);
Arbitrary toArbitrary() {
if (parent !== null) {
return parent.toArbitrary(Choice(elements));
} else {
return Choice(elements);
}
}
}
class ForAll {
static ArbitraryIntBuilder get integers() =>
new ArbitraryIntBuilder();
static ArbitraryIntBuilder get positiveIntegers() =>
integers.greaterThan(0);
static ArbitraryIntBuilder get negativeIntegers() =>
integers.lessThan(0).greaterThan(-42);
static ArbitraryIntBuilder get nonNegativeIntegers() =>
integers.greaterOrEqual(0);
static ArbitraryIntBuilder get nonPositiveIntegers() =>
integers.lessOrEqual(0).greaterThan(-42);
static ArbitraryListBuilder get lists() =>
new ArbitraryListBuilder();
static ArbitraryCharBuilder get chars() =>
new ArbitraryCharBuilder();
static ArbitraryChoiceBuilder objectsIn(elements) =>
new ArbitraryChoiceBuilder(elements);
}
class ForAllProxy {
var parent;
ForAllProxy(this.parent);
get integers() {
if(parent !== null) {
return new ArbitraryIntBuilder.withParent(parent);
} else {
return ForAll.integers;
}
}
get lists() {
if (parent !== null) {
return new ArbitraryListBuilder.withParent(parent);
} else {
return ForAll.lists;
}
}
get chars() {
if (parent !== null) {
return new ArbitraryCharBuilder.withParent(parent);
} else {
return ForAll.chars;
}
}
ArbitraryChoiceBuilder objectsIn(elements) =>
new ArbitraryChoiceBuilder(elements, parent);
ArbitraryIntBuilder get positiveIntegers() =>
integers.greaterThan(0);
ArbitraryIntBuilder get negativeIntegers() =>
integers.lessThan(0).greaterThan(-42);
ArbitraryIntBuilder get nonNegativeIntegers() =>
integers.greaterOrEqual(0);
ArbitraryIntBuilder get nonPositiveIntegers() =>
integers.lessOrEqual(0).greaterThan(-42);
}