forked from Bijnagte/spock-genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGen.groovy
421 lines (385 loc) · 14 KB
/
Gen.groovy
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package spock.genesis
import groovy.transform.CompileStatic
import spock.genesis.extension.ExtensionMethods
import spock.genesis.generators.CyclicGenerator
import spock.genesis.generators.FactoryGenerator
import spock.genesis.generators.Generator
import spock.genesis.generators.IterableGenerator
import spock.genesis.generators.composites.DefinedMapGenerator
import spock.genesis.generators.composites.ListGenerator
import spock.genesis.generators.composites.PojoGenerator
import spock.genesis.generators.composites.RandomMapGenerator
import spock.genesis.generators.composites.TupleGenerator
import spock.genesis.generators.values.ByteArrayGenerator
import spock.genesis.generators.values.CharacterGenerator
import spock.genesis.generators.values.DateGenerator
import spock.genesis.generators.values.DoubleGenerator
import spock.genesis.generators.values.IntegerGenerator
import spock.genesis.generators.values.LongGenerator
import spock.genesis.generators.values.RandomElementGenerator
import spock.genesis.generators.values.StringGenerator
import spock.genesis.generators.values.ValueGenerator
import java.util.regex.Pattern
/**
* Static factory methods for Generators
*/
@SuppressWarnings(['MethodCount'])
@CompileStatic
class Gen {
/**
* Produces a {@link StringGenerator} capable of producing values
* of type {@link String}
*
* @return an infinite lazy String Generator
*/
static StringGenerator getString() {
new StringGenerator()
}
/**
* Produces a {@link StringGenerator} capable of producing values
* of type {@link String} with a maximum length passed as
* parameter
*
* @param maxLength
* @return an infinite lazy String Generator
*/
static StringGenerator string(int maxLength) {
new StringGenerator(maxLength)
}
/**
* Produces a {@link StringGenerator} capable of producing values
* of type {@link String}. These strings will take into account
* potential values passed as parameter.
*
* @param potentialCharacters A {@link String} with the potential
* characters that we would like to see in the generated values
* @return an infinite lazy String Generator {@link
* StringGenerator}
*/
static StringGenerator string(String potentialCharacters) {
new StringGenerator(potentialCharacters)
}
/**
* Produces a {@link StringGenerator} capable of producing values
* of type {@link String} with a maximum and minimum length passed
* as parameters
*
* @param minLength minimum length of the generated values
* @param maxLength maximum length of the generated values
* @return an infinite lazy String Generator {@link
* StringGenerator}
*/
static StringGenerator string(int minLength, int maxLength) {
new StringGenerator(minLength, maxLength)
}
/**
* Produces a {@link StringGenerator} capable of producing values
* of type {@link String} from a given {@link Pattern}
*
* @param regex the pattern expression used as template for
* generated values
* @return an infinite lazy String Generator {@link
* StringGenerator}
*/
static StringGenerator string(Pattern regex) {
new StringGenerator(regex)
}
/**
* Produces a {@link ByteArrayGenerator} capable of producing
* random values of type {@link Byte}
*
* @return an infinite lazy {@link ByteArrayGenerator}
*/
static ByteArrayGenerator getBytes() {
new ByteArrayGenerator()
}
/**
* Produces a {@link IntegerGenerator} capable of producing
* random values of type {@link Integer}
*
* @return an infinite lazy {@link IntegerGenerator}
*/
static IntegerGenerator getInteger() {
new IntegerGenerator()
}
/**
* Produces a {@link IntegerGenerator} capable of producing random
* values of type {@link Integer} from a minimum number to a
* maximum number.
*
* @param min minimum generated number allowed
* @param max maximum generated number allowed
* @return an infinite lazy {@link IntegerGenerator}
*/
static IntegerGenerator integer(int min, int max) {
new IntegerGenerator(min, max)
}
/**
* Produces a {@link IntegerGenerator} capable of producing random
* values of type {@link Integer} from a minimum number to a
* maximum number.
*
* @param range range representing the minimum and maximum
* possible number allowed
* @return an infinite lazy {@link IntegerGenerator}
*/
static IntegerGenerator integer(IntRange range) {
new IntegerGenerator(range.from, range.to)
}
/**
* Produces a {@link LongGenerator} capable of producing random
* values of type {@link Long}
*
* @return an infinite lazy {@link LongGenerator}
*/
static LongGenerator getLong() {
new LongGenerator()
}
/**
* Produces a {@link CharacterGenerator} capable of producing
* random values of type {@link Character}
*
* @return an infinite lazy {@link CharacterGenerator}
*/
static CharacterGenerator getCharacter() {
new CharacterGenerator()
}
/**
* Produces a {@link CharacterGenerator} capable of producing
* random values of type {@link Character} from a given
* set of potential characters
*
* @param potentialCharacters a {@link String} containing a set of
* potential characters that can be used in new generated values
* @return an infinite lazy {@link CharacterGenerator}
*/
static CharacterGenerator character(String potentialCharacters) {
new CharacterGenerator(potentialCharacters)
}
/**
* Produces a {@link DoubleGenerator} capable of producing random
* values of type {@link Double}
*
* @return an infinite lazy {@link DoubleGenerator}
*/
static DoubleGenerator getDouble() {
new DoubleGenerator()
}
/**
* Produces a {@link ValueGenerator} capable of producing values
* of the same kind passed as parameter
*
* @param value value you want to produce over an over again
* @return an infinite lazy of type {@link ValueGenerator}
*/
static <T> ValueGenerator<T> value(T value) {
new ValueGenerator(value)
}
/**
* Produces a lazy infinite generator that returns a random
* element from a source {@link Collection}
*
* @param source {@link Collection} of type {@link Object} to pick
* from
* @return a {@link RandomElementGenerator}
*/
static <T> RandomElementGenerator<T> any(Collection<T> source) {
new RandomElementGenerator(source)
}
/**
* Produces a lazy infinite generator that returns a random
* element from a source {@link Collection}
*
* @param source variable arguments of type {@link Object} to pick
* from
* @return a {@link RandomElementGenerator}
*/
static <T> RandomElementGenerator<T> any(T... source) {
new RandomElementGenerator(source.toList())
}
/**
* Produces a lazy infinite {@link CyclicGenerator} that repeats
* an {@link Iterable}.
*
* @param source {@link Iterable} to repeat over
* @return an instance of {@link CyclicGenerator}
*/
static <T> CyclicGenerator cycle(Iterable<T> source) {
new IterableGenerator<T>(source).repeat()
}
/**
* Produces a lazy infinite {@link PojoGenerator} that creates
* instances of objects of a given type
*
* @param keysToValueGenerators generators per each field
* @param target type of the object we would like to generate
* @return an instance of {@link PojoGenerator}
*/
static <T> PojoGenerator<T,Map> type(Map<String, Object> keysToValueGenerators, Class<T> target) {
new PojoGenerator(target, map(keysToValueGenerators))
}
/**
* Produces a lazy infinite {@link PojoGenerator} that creates
* instances of objects of a given type
*
* @param target type of the object we would like to generate
* @param argGenerators generators per each field
* @return an instance of {@link PojoGenerator}
*/
static <T> PojoGenerator<T, List> type(Class<T> target, Iterable... argGenerators) {
new PojoGenerator(target, tuple(argGenerators))
}
/**
* Produces a lazy infinite {@link PojoGenerator} that creates
* instances of {@link Map}
*
* @param keysToValueGenerators generators per each map field
* @return an instance of {@link DefinedMapGenerator}
*/
static DefinedMapGenerator map(Map keysToValueGenerators) {
new DefinedMapGenerator(keysToValueGenerators)
}
/**
* Produces a lazy infinite {@link PojoGenerator} that creates
* instances of {@link Map}
*
* @param keyGenerator generators of map keys
* @param valueGenerator generators of map values
* @return an instance of {@link RandomMapGenerator}
*/
static <K,V> RandomMapGenerator map(Iterable<K> keyGenerator, Iterable<V> valueGenerator) {
new RandomMapGenerator<K,V>(keyGenerator, valueGenerator)
}
/**
* Produces a lazy infinite {@link ListGenerator} that creates
* instances of {@link List} based on a given value generator
*
* @param valueGenerator generates values of the produced list
* @return an instance of {@link ListGenerator}
*/
static <T> ListGenerator<T> list(Generator<T> valueGenerator) {
new ListGenerator(valueGenerator)
}
/**
* Produces a lazy infinite {@link ListGenerator} that creates
* instances of {@link List} of a given length and it's based on a
* given value generator
*
* @param valueGenerator generates values of the produced list
* @param maxLength maximum length of generated lists
* @return an instance of {@link ListGenerator}
*/
static <T> ListGenerator<T> list(Generator<T> valueGenerator, int maxLength) {
new ListGenerator(valueGenerator, maxLength)
}
/**
* Produces a lazy infinite {@link ListGenerator} that creates
* instances of {@link List} of a given max and min length and
* it's based on a given value generator
*
* @param valueGenerator generates values of the produced list
* @param minLength minimum length of generated lists
* @param maxLength maximum length of generated lists
* @return an instance of {@link ListGenerator}
*/
static ListGenerator list(Generator valueGenerator, int minLength, int maxLength) {
new ListGenerator(valueGenerator, minLength, maxLength)
}
/**
* Produces a lazy infinite {@link TupleGenerator} that creates
* tuples with values based on the generators passed as parameter
*
* @param generators generators for each tuple element
* @return an instance of {@link TupleGenerator}
*/
static <T> TupleGenerator<T> tuple(Iterable<T>... generators) {
new TupleGenerator(generators)
}
/**
* Produces a lazy infinite {@link DateGenerator} that generates
* random instances of {@link Date}
*
* @return an instance of {@link DateGenerator}
*/
static DateGenerator getDate() {
new DateGenerator()
}
/**
* Produces a lazy infinite {@link DateGenerator}. This generator
* will create random instances of {@link Date} from a minimum
* date to a maximum date.
*
* @param minDate minimum possible date
* @param maxDate maximum possible date
* @return an instance of {@link DateGenerator}
*/
static DateGenerator date(Date minDate, Date maxDate) {
new DateGenerator(minDate, maxDate)
}
/**
* Produces a lazy infinite {@link FactoryGenerator}. This
* generator will produce random instances of the values returned
* by a given {@link Closure}
*
* @param factory the closure that defines the generated value
* @return an instance of {@link FactoryGenerator}
*/
static <T> FactoryGenerator<T> using(Closure<T> factory) {
new FactoryGenerator(factory)
}
/**
* Produces a lazy infinite {@link Generator}. This
* generator will produce the values taken from a given {@link
* Iterable} in the order they were defined
*
* @param iterable {@link Iterable} declaring values to produce
* @param finite sets the generator as finite or not
* @return an instance of {@link Generator}
*/
static <T> Generator<T> these(Iterable<T> iterable, boolean finite = false) {
ExtensionMethods.toGenerator(iterable, finite)
}
/**
* Produces a lazy infinite {@link Generator}. This
* generator will produce classes of the type passed as parameter
*
* @param clazz the type of class you want to produce
* @return an instance of {@link Generator}
*/
static Generator these(Class clazz) {
ExtensionMethods.toGenerator(clazz)
}
/**
* Produces a lazy infinite {@link Generator}. This
* generator will produce the values taken from the values passed
* as arguments in the order they were defined
*
* @param values variable arguments to get values from
* @return an instance of {@link Generator}
*/
static <T> Generator<T> these(T... values) {
ExtensionMethods.toGenerator(values)
}
/**
* Produces a lazy infinite {@link Generator}. This
* generator will produce the values taken from the values passed
* as arguments in the order they were defined
*
* @param values collection to get values from
* @return an instance of {@link Generator}
*/
static <T> Generator<T> these(Collection<T> values) {
ExtensionMethods.toGenerator(values)
}
/**
* Produces a lazy infinite {@link Generator}. This
* generator will produce copies of the value passed as parameter
*
* @param value sample value
* @return an instance of {@link Generator} that produces copies
* of the sample value
*/
static <T> Generator<T> once(T value) {
these([value])
}
}