Sergio Pro
08/12/2024, 4:32 PMCLOVIS
08/13/2024, 1:29 PMArb
and Exhaustive
directly?
I see there is https://serpro69.github.io/kotlin-faker/extensions/kotest-property-extension/, but I'd rather not add KSP etc just to generate test data.Sergio Pro
08/13/2024, 1:34 PMSergio Pro
08/13/2024, 1:39 PMArb
extension for each data generator... I think that's too much code maintenance π hence why I thought initially to just generate these things.
But I'd like to hear more about why you think the current solution via KSP isn't good enough. It can certainly be changed if something else would improve the user experience.CLOVIS
08/13/2024, 1:41 PMSergio Pro
08/13/2024, 1:41 PMSergio Pro
08/13/2024, 1:42 PMpublic val Faker.arb: ArbFaker // β
get() = ArbFaker(this)
public val Arb.Companion.faker: ArbFaker // β‘
get() = io.github.serpro69.kfaker.ArbFaker(Faker())
public val BooksFaker.arb: ArbBooksFaker // β
get() = ArbBooksFaker(this)
public val Arb.Companion.booksFaker: ArbBooksFaker // β‘
get() = io.github.serpro69.kfaker.books.ArbBooksFaker(BooksFaker())
public class ArbFaker(private val faker: Faker) { // β’
public val address: ArbAddress by lazy { ArbAddress(faker.address) }
public val color: ArbColor by lazy { ArbColor(faker.color) }
public val currency: ArbCurrency by lazy { ArbCurrency(faker.currency) }
// ...
}
public class ArbAddress internal constructor(private val address: Address) { // β£
public fun city(): Arb<String> = arbitrary { address.city() }
public fun country(): Arb<String> = arbitrary { address.country() }
// ...
}
CLOVIS
08/13/2024, 1:44 PMpublic val Faker.arb: ArbFaker // β
get() = ArbFaker(this)
public val Arb.Companion.faker: ArbFaker // β‘
get() = io.github.serpro69.kfaker.ArbFaker(Faker())
Then we can just use Arb.faker.bank.name()
no?Sergio Pro
08/13/2024, 1:48 PMname()
function itself must return an Arb
instance, right? So then it needs to be wrapped e.g. in an arbitrary { }
public class ArbAddress internal constructor(private val address: Address) {
public fun city(): Arb<String> = arbitrary { address.city() }
public fun country(): Arb<String> = arbitrary { address.country() }
}
Then you can do e.g.
forAll(f.arb.address.city()) { q ->
q.isNotBlank()
}
Sergio Pro
08/13/2024, 1:50 PMAddress
here is part of the core functionality (no KSP)
The ArbAddress
class from above is generated by the extension lib via KSPCLOVIS
08/13/2024, 1:52 PMCLOVIS
08/13/2024, 1:57 PMGenerator
type, you could have something like Arb.of(faker.bank.name)
be the conversion function.Sergio Pro
08/13/2024, 1:58 PMSergio Pro
08/13/2024, 2:00 PMSergio Pro
08/13/2024, 2:02 PMSergio Pro
08/13/2024, 2:03 PMCLOVIS
08/13/2024, 2:06 PMCLOVIS
08/13/2024, 2:07 PMSergio Pro
08/13/2024, 2:35 PMCLOVIS
08/13/2024, 2:36 PMSergio Pro
08/13/2024, 2:37 PMCLOVIS
08/13/2024, 2:38 PMfun <T> Arb.Companion.of(block: () -> T) = arbitrary { block() }
usage:
Arb.of(fake.bank::name)
?Sergio Pro
08/13/2024, 2:39 PMSergio Pro
08/13/2024, 4:14 PM