All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spock.genesis.generators.composites.ListGenerator.groovy Maven / Gradle / Ivy

Go to download

Mostly lazy data generators for property based testing using the Spock test framework

The newest version!
package spock.genesis.generators.composites

import groovy.transform.CompileStatic
import spock.genesis.generators.Generator
import spock.genesis.generators.UnmodifiableIterator
import spock.genesis.generators.values.WholeNumberGenerator

@CompileStatic
class ListGenerator extends Generator> {

    static final int DEFAULT_LENGTH_LIMIT = 1000

    private final Generator valueSource
    private final WholeNumberGenerator lengthSource

    ListGenerator(Generator generator) {
        this.valueSource = generator
        this.lengthSource = new WholeNumberGenerator(DEFAULT_LENGTH_LIMIT)
    }

    ListGenerator(Generator generator, int maxLength) {
        this.valueSource = generator
        this.lengthSource = new WholeNumberGenerator(maxLength)
    }

    ListGenerator(Generator generator, int minLength, int maxLength) {
        this.valueSource = generator
        this.lengthSource = new WholeNumberGenerator(minLength, maxLength)
    }

    ListGenerator(Generator generator, IntRange range) {
        this.valueSource = generator
        this.lengthSource = new WholeNumberGenerator(range)
    }

    @Override
    UnmodifiableIterator> iterator() {
        new UnmodifiableIterator>() {
            private final Iterator source = valueSource.iterator()
            private final Iterator length = lengthSource.iterator()

            @Override
            boolean hasNext() {
                source.hasNext()
            }

            @Override
            List next() {
                Integer size = length.next()
                source.take(size).toList()
            }
        }
    }

    @Override
    ListGenerator seed(Long seed) {
        super.seed(seed)
        lengthSource.seed(seed)
        valueSource.seed(seed)
        this
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy