Kotest Reflective Arbs
When running tests on the JVM, Kotest supports automatically generating more complex Arbs.
Reflective Arbs
When running tests on the JVM, Kotest supports automatically generating more complex Arb instances. The generated Arb fills class parameters using built-in primitive Arbs and additional reflective Arbs. If you only need generation and instantiation and do not need filtering, you can use the class type directly in a checkAll or forAll call. If you want to obtain an Arb for further manipulation or to filter invalid values, you can use Arb.bind with a type argument to get the Arb. If the required type depends on a type that is not supported by default, you can provide an Arb for that type when calling Arb.bind.
Example:
enum class Currency {
USD, GBP, EUR
}
class CurrencyAmount(
val amount: Long,
val currency: Currency
)
context("Currencies converts to EUR") { // In some spec
checkAll(Arb.bind<CurrencyAmount>().filter { it.currency != EUR }) { currencyAmount ->
val converted = currencyAmount.convertTo(EUR)
converted.currency shouldBe EUR
}
}
context("Converting to a currency and back yields the same amount") { // In some spec
checkAll<CurrencyAmount, Currency>() { currencyAmount, currency ->
val converted = currencyAmount.convertTo(currency).convertTo(currencyAmount.currency)
converted.currency shouldBe currencyAmount.currency
}
}
Reflective binding is supported for:
- Non-private classes or data classes, when the primary constructor is not private and the constructor parameters are also supported types.
Pair, when items 1 and 2 are in this category.- Primitives.
- Enums.
- Sealed classes, when the subtypes and their primary constructors are not private.
LocalDate,LocalDateTime,LocalTime,Period, andInstantfromjava.time.BigDecimalandBigInteger.- Collections:
Set,List, andMap. - Classes whose Arb is provided through supplied Arbs.