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

main.net.jqwik.kotlin.api.SequenceArbitrary.kt Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.kotlin.api

import net.jqwik.api.Arbitrary
import net.jqwik.api.RandomDistribution
import net.jqwik.api.arbitraries.ArbitraryDecorator
import net.jqwik.api.arbitraries.ListArbitrary
import net.jqwik.api.arbitraries.SizableArbitrary
import org.apiguardian.api.API
import org.apiguardian.api.API.Status.EXPERIMENTAL
import org.jspecify.annotations.Nullable
import java.util.function.Function

/**
 * Fluent interface to add functionality to arbitraries that generate instances
 * of type [Sequence]
 */
@API(status = EXPERIMENTAL, since = "1.6.0")
class SequenceArbitrary(elementArbitrary: Arbitrary) : ArbitraryDecorator>(),
    SizableArbitrary> {

    private var listArbitrary: ListArbitrary

    init {
        this.listArbitrary = elementArbitrary.list() as ListArbitrary
    }

    override fun arbitrary(): Arbitrary> {
        return listArbitrary.map { m -> m.asSequence() }
    }

    override fun ofSize(size: Int): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.ofSize(size)
        return clone
    }

    fun ofSize(range: IntRange): SequenceArbitrary {
        return ofMinSize(range.first).ofMaxSize(range.last)
    }

    override fun ofMinSize(minSize: Int): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.ofMinSize(minSize)
        return clone
    }

    override fun ofMaxSize(maxSize: Int): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.ofMaxSize(maxSize)
        return clone
    }

    override fun withSizeDistribution(distribution: RandomDistribution): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.withSizeDistribution(distribution)
        return clone
    }

    /**
     * Add the constraint that elements of the generated iterator must be unique,
     * i.e. no two elements must return true when being compared using [Object.equals].
     *
     * The constraint can be combined with other [.uniqueElements] constraints.
     *
     * @return new arbitrary instance
     */
    fun uniqueElements(): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.uniqueElements()
        return clone
    }

    /**
     * Add the constraint that elements of the generated iterator must be unique
     * relating to an element's "feature" being extracted using the
     * `by` function.
     * The extracted features are being compared using [Object.equals].
     *
     * The constraint can be combined with other [.uniqueElements] constraints.
     *
     * @return new arbitrary instance
     */
    fun uniqueElements(by: Function): SequenceArbitrary {
        val clone = typedClone>()
        clone.listArbitrary = listArbitrary.uniqueElements(by)
        return clone
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy