Skip to content
Permalink
Browse files

#973 Adds a samples type Gen which repeatably gives given samples

  • Loading branch information...
ashishkujoy committed Sep 7, 2019
1 parent 7bc052d commit 794297855efbe15a51caab5ee383c18fbf24a8ba
@@ -1,12 +1,6 @@
package io.kotlintest.properties

import io.kotlintest.properties.shrinking.ChooseShrinker
import io.kotlintest.properties.shrinking.DoubleShrinker
import io.kotlintest.properties.shrinking.FloatShrinker
import io.kotlintest.properties.shrinking.IntShrinker
import io.kotlintest.properties.shrinking.ListShrinker
import io.kotlintest.properties.shrinking.Shrinker
import io.kotlintest.properties.shrinking.StringShrinker
import io.kotlintest.properties.shrinking.*
import kotlin.jvm.JvmOverloads
import kotlin.math.abs
import kotlin.random.Random
@@ -591,3 +585,22 @@ fun Gen.Companion.factors(k: Int): Gen<Int> = object : Gen<Int> {
.filter { k % it == 0 }
}
}

fun <T:Any> Gen.Companion.samples(vararg sampleValues: T) = object : Gen<T> {
private fun nextNumberGenerator(): () -> T {
var currentIndex = 0;
return {
val nextIndex = currentIndex % sampleValues.size
val nextValue = sampleValues[nextIndex]
currentIndex += 1
nextValue
}
}

override fun random(seed: Long?): Sequence<T> {
val a = nextNumberGenerator()
return generateSequence(a)
}

override fun constants(): Iterable<T> = sampleValues.asIterable()
}
@@ -2,67 +2,33 @@

package com.sksamuel.kotlintest.properties

import io.kotlintest.*
import io.kotlintest.inspectors.forAll
import io.kotlintest.matchers.comparables.beGreaterThan
import io.kotlintest.matchers.booleans.shouldBeTrue
import io.kotlintest.matchers.collections.contain
import io.kotlintest.matchers.collections.shouldContain
import io.kotlintest.matchers.collections.shouldContainAll
import io.kotlintest.matchers.collections.shouldHaveAtMostSize
import io.kotlintest.matchers.comparables.beGreaterThan
import io.kotlintest.matchers.comparables.gte
import io.kotlintest.matchers.comparables.lt
import io.kotlintest.matchers.doubles.shouldBeGreaterThanOrEqual
import io.kotlintest.matchers.doubles.shouldBeLessThanOrEqual
import io.kotlintest.matchers.floats.shouldBeGreaterThanOrEqual
import io.kotlintest.matchers.floats.shouldBeLessThanOrEqual
import io.kotlintest.matchers.comparables.gte
import io.kotlintest.matchers.comparables.lt
import io.kotlintest.matchers.ints.shouldBeLessThanOrEqual
import io.kotlintest.matchers.string.include
import io.kotlintest.properties.Gen
import io.kotlintest.properties.assertAll
import io.kotlintest.properties.bind
import io.kotlintest.properties.choose
import io.kotlintest.properties.constant
import io.kotlintest.properties.create
import io.kotlintest.properties.default
import io.kotlintest.properties.double
import io.kotlintest.properties.duration
import io.kotlintest.properties.forAll
import io.kotlintest.properties.forClassName
import io.kotlintest.properties.from
import io.kotlintest.properties.generateInfiniteSequence
import io.kotlintest.properties.int
import io.kotlintest.properties.list
import io.kotlintest.properties.localDate
import io.kotlintest.properties.localDateTime
import io.kotlintest.properties.localTime
import io.kotlintest.properties.map
import io.kotlintest.properties.negativeIntegers
import io.kotlintest.properties.next
import io.kotlintest.properties.nextPrintableString
import io.kotlintest.properties.numericDoubles
import io.kotlintest.properties.numericFloats
import io.kotlintest.properties.oneOf
import io.kotlintest.properties.period
import io.kotlintest.properties.positiveIntegers
import io.kotlintest.properties.set
import io.kotlintest.properties.take
import io.kotlintest.properties.*
import io.kotlintest.shouldBe
import io.kotlintest.shouldHave
import io.kotlintest.shouldNotBe
import io.kotlintest.shouldThrow
import io.kotlintest.specs.WordSpec
import io.kotlintest.tables.headers
import io.kotlintest.tables.row
import io.kotlintest.tables.table
import java.time.*
import java.util.Random
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.List
import kotlin.collections.Set
import kotlin.collections.last
import kotlin.collections.listOf
import kotlin.collections.mutableSetOf
import kotlin.collections.plusAssign
import kotlin.collections.setOf
import kotlin.collections.toList
import kotlin.collections.toSet

class GenTest : WordSpec() {
init {
@@ -636,6 +602,12 @@ class GenTest : WordSpec() {
}
}
}

"Gen.samples(sampleValues)" should {
Gen.samples(1, 2).random().take(1).last() shouldBe 1
Gen.samples(1, 2).random().take(2).last() shouldBe 2
Gen.samples(1, 2).random().take(3).last() shouldBe 1
}
}
}

0 comments on commit 7942978

Please sign in to comment.
You can’t perform that action at this time.