https://kotlinlang.org logo
Title
t

tieskedh

11/11/2020, 1:15 PM
Great work on Kotest! I have a little question:
data class Person(
    val name : String,
    val age : Int
)

fun Arb.persons() = bind(
    string(), int()
) { name, age -> Person(name, age) }

@Test
fun test() = forAll(
    config = PropTestConfig(
        shrinkingMode = ShrinkingMode.Bounded(10)
    ), 
    Arb.persons()
){ it.age > 18 }
How can I use//create a shrinker for Person?
s

sam

11/11/2020, 1:19 PM
This compiles:
data class Person(
   val name: String,
   val age: Int
)

val persons = Arb.bind(
   Arb.string(), <http://Arb.int|Arb.int>()
) { name, age -> Person(name, age) }

suspend fun test() {

   forAll(
      config = PropTestConfig(
         shrinkingMode = ShrinkingMode.Bounded(10)
      ),
      persons
   ) { it.age > 18 }
}
Bear in mind, the shrinking won't work for a binded type.
t

tieskedh

11/11/2020, 1:31 PM
Thanks for the quick response! Is there a way to compose StringShrinker and IntShrinker to get Person to shrink?
s

sam

11/11/2020, 3:44 PM
Not currently. Could be added though. If you want to do a pr.
t

tieskedh

11/11/2020, 8:49 PM
Do you mean that you explicitly should use shrink? I tried with
forAll(
    config = PropTestConfig(shrinkingMode = ShrinkingMode.Unbounded),
    iterations = 1000,
    genA = Arb.string()
){
    println(it)
     "#" !in it
}
And it doesn't shrink either... I think I'm still understanding the framework wrong, but if it should work, maybe it's because I'm using multiplatform?