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

spock.genesis.generators.composites.RandomMapGenerator.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.extension.ExtensionMethods
import spock.genesis.generators.Generator
import spock.genesis.generators.UnmodifiableIterator
import spock.genesis.generators.values.WholeNumberGenerator

@CompileStatic
class RandomMapGenerator extends Generator> implements Closeable {

    static final int DEFAULT_ENTRY_LIMIT = 100
    final WholeNumberGenerator sizeSource
    final Generator keyGenerator
    final Generator valueGenerator

    RandomMapGenerator(Iterable keyGenerator, Iterable valueGenerator) {
        this(keyGenerator, valueGenerator, new WholeNumberGenerator(DEFAULT_ENTRY_LIMIT))
    }

    RandomMapGenerator(Iterable keyGenerator, Iterable valueGenerator, int maxSize) {
        this(keyGenerator, valueGenerator, new WholeNumberGenerator(maxSize))
    }

    RandomMapGenerator(Iterable keyGenerator, Iterable valueGenerator, int minSize, int maxSize) {
        this(keyGenerator, valueGenerator, new WholeNumberGenerator(minSize, maxSize))
    }

    RandomMapGenerator(Iterable keyGenerator, Iterable valueGenerator, IntRange sizeRange) {
        this(keyGenerator, valueGenerator, new WholeNumberGenerator(sizeRange))
    }

    RandomMapGenerator(Iterable keyGenerator, Iterable valueGenerator, WholeNumberGenerator sizeSource) {
        this.sizeSource = sizeSource
        this.keyGenerator = ExtensionMethods.toGenerator(keyGenerator)
        this.valueGenerator = ExtensionMethods.toGenerator(valueGenerator)
    }

    UnmodifiableIterator> iterator() {
        new UnmodifiableIterator>() {
            Iterator keys = keyGenerator.iterator()
            Iterator values = valueGenerator.iterator()
            Iterator sizes = sizeSource.iterator()

            @Override
            boolean hasNext() {
                keys.hasNext() && values.hasNext()
            }

            @Override
            Map next() {
                int targetSize = sizes.next()
                int i = 0
                Map result = [:]
                while (result.size() < targetSize && hasNext()) {
                    result[keys.next()] = values.next()
                    i++
                }
                result
            }
        }
    }

    @Override
    void close() {
        keyGenerator.close()
        valueGenerator.close()
    }

    @Override
    boolean isFinite() {
        keyGenerator.finite || valueGenerator.finite
    }

    @Override
    RandomMapGenerator seed(Long seed) {
        keyGenerator.seed(seed)
        valueGenerator.seed(seed)
        sizeSource.seed(seed)
        super.seed(seed)
        this
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy