Skip to content

Instantly share code, notes, and snippets.

@BjornvdLaan
Created February 27, 2022 14:28
Show Gist options
  • Save BjornvdLaan/d88d30ee69e32086aeaae117d49ab921 to your computer and use it in GitHub Desktop.
Save BjornvdLaan/d88d30ee69e32086aeaae117d49ab921 to your computer and use it in GitHub Desktop.
[Kotest] How to add shrinking to custom generators
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.comparables.shouldBeGreaterThanOrEqualTo
import io.kotest.property.Arb
import io.kotest.property.Shrinker
import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.arbitrary.nonNegativeInt
import io.kotest.property.checkAll
data class Coordinate(val x: Int, val y: Int)
class CoordinateTest : FunSpec({
context("Coordinate Transformations") {
val coordinateShrinker = Shrinker<Coordinate> { c ->
listOf(
Coordinate(c.x - 1, c.y),
Coordinate(c.x, c.y - 1),
Coordinate(c.x + 1, c.y),
Coordinate(c.x, c.y + 1),
)
}
val coordinateArb = arbitrary(coordinateShrinker) {
Coordinate(Arb.nonNegativeInt().bind(), Arb.nonNegativeInt().bind())
}
test("Coordinates are always positive after transformation") {
coordinateArb.checkAll {
transform(it).x shouldBeGreaterThanOrEqualTo 0
transform(it).y shouldBeGreaterThanOrEqualTo 0
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment