spock.genesis.generators.values.RandomElementGenerator.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spock-genesis Show documentation
Show all versions of spock-genesis Show documentation
Mostly lazy data generators for property based testing using the Spock test framework
The newest version!
package spock.genesis.generators.values
import groovy.transform.CompileStatic
import spock.genesis.generators.InfiniteGenerator
import spock.genesis.generators.InfiniteIterator
/** A lazy infinite {@code Generator} that returns a random element from a source Collection
* @param < E > the generated type
*/
@CompileStatic
class RandomElementGenerator extends InfiniteGenerator {
final List elementSource
final WholeNumberGenerator indexSource
RandomElementGenerator(Collection elementSource) {
this.elementSource = elementSource.toList().asImmutable()
this.indexSource = new WholeNumberGenerator(this.elementSource.size() - 1)
}
@Override
InfiniteIterator iterator() {
new InfiniteIterator() {
private final Iterator indexIterator = indexSource.iterator()
@Override
E next() {
elementSource[indexIterator.next()]
}
}
}
@Override
RandomElementGenerator seed(Long seed) {
indexSource.seed(seed)
super.seed(seed)
this
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy