https://kotlinlang.org logo
#kotest
Title
# kotest
a

André Martins

04/21/2022, 9:37 AM
Hey suppose I have a Student like
data class Student(val id: Int, val name: String)
and I’m trying to build a Arb of Student like
Copy code
fun Arb.Companion.student(
    id: Arb<Int> = positiveInt(),
    name: Arb<String> = string()
): Arb<Student> = arbitrary { rs ->
    Student(
        id.next(rs),
        name.next(rs) // or name.bind() and ignore rs?
    )
}
Should I call
bind
or
next
with the current randomSource?
e

Emil Kantis

04/21/2022, 11:43 AM
I think you can do
Arb.bind(id, name, ::Student)
a

André Martins

04/21/2022, 11:45 AM
?
m

mitch

04/21/2022, 11:48 AM
@André Martins I'd recommend using .bind. that will make sure all edgecases from all arbs are incorporated
a

André Martins

04/21/2022, 11:49 AM
Ok, but then when I do
Arb.student().next(myRandomSource)
will it pass the random source to all arbs?
m

mitch

04/21/2022, 11:49 AM
yep that’s right. next will call the random sample and discards all edgecases
a

André Martins

04/21/2022, 11:50 AM
ok thx
m

mitch

04/21/2022, 11:50 AM
hence if i were you, i’d do your second approach as in
Copy code
fun Arb.Companion.student(
    id: Arb<Int> = positiveInt(),
    name: Arb<String> = string()
): Arb<Student> = arbitrary { 
    Student(
        id.bind(),
        name.bind()
    )
}
👍 1
🔝 1
internally that will basically do all the correct things including the propagation of random source, as well as settings for edgecases - both for id-arb and name arb, and all other composed arbs therein
a

André Martins

04/21/2022, 11:53 AM
Basically we should only use next outside and bind for composition right?
e

Emil Kantis

04/21/2022, 11:54 AM
My example was shorthand of the above.
Arb.bind(id, name, ::Student)
takes the id and name arb and binds them using the Student constructor. 🙂
👍 2
m

mitch

04/21/2022, 11:54 AM
yep that’s right. outside of the
arbitrary { }
scope, that’s about the only thing possible. within the builder you can compose a lot more complex arbitraries
👍 1
a

André Martins

04/21/2022, 11:55 AM
Oh ok I rather call constructor explicitly
thanks guys!
👌 1