Created
February 27, 2022 14:28
-
-
Save BjornvdLaan/d88d30ee69e32086aeaae117d49ab921 to your computer and use it in GitHub Desktop.
[Kotest] How to add shrinking to custom generators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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