Package-level declarations
Kotest property functions for testing enums that represent an internal value. An example is as shown below:
// Enum
enum class Example {
One,
Two,
Three;
companion object {
fun fromValue(value: String) = when (value.lowercase()) {
"one" -> One
"example", "two" -> Two
else -> Three
}
}
}
// Test code
val valuesMap = mapOf(
"one" to Example.One,
"two" to Example.Two,
"example" to Example.Two,
"other value" to Example.Three
)
class ExampleEnumTest : DescribeSpec({
include(
enumTests(
describeName = "fromValue",
enumValuesMap = valuesMap,
fromValueOrNullFn = Example.Companion::fromValue,
invalidArb = Arb.string()
.filterNot { output -> output in enumValuesMap.map { it.key.value } }
)
)
})
Content copied to clipboard
The following methods are provided:
Types
Link copied to clipboard
An enum-map entry, with its property name and value representation.
Link copied to clipboard
annotation class ExperimentalEnumKotestApi
Denotes APIs that are experimental. Their implementation is likely to be changed, and no backwards compatibility is guaranteed.
Functions
Link copied to clipboard
fun <InternalValue, E : Enum<E>> enumKPropertyTests(describeName: String = "fromValueOrNull", dataNameFn: (Map.Entry<EnumMapEntry<InternalValue>, E>) -> String = { "${it.key.propertyName}: ${it.value}" }, enumValuesMap: Map<KProperty0<InternalValue>, E>, fromValueOrNullFn: (InternalValue) -> E?, invalidArb: Arb<InternalValue>): TestFactory
Test factory to create tests for the given enum.
Link copied to clipboard
fun <InternalValue, E : Enum<E>> enumTests(describeName: String = "fromValueOrNull", dataNameFn: (Map.Entry<EnumMapEntry<InternalValue>, E>) -> String = { "${it.key.propertyName}: ${it.value}" }, enumValuesMap: Map<EnumMapEntry<InternalValue>, E>, fromValueOrNullFn: (InternalValue) -> E?, invalidArb: Arb<InternalValue>): TestFactory
Test factory to create tests for the given enum.
Link copied to clipboard
fun <E : Enum<E>> intEnumTests(describeName: String = "fromValueOrNull", dataNameFn: (Map.Entry<EnumMapEntry<Int>, E>) -> String = { "${it.key.propertyName}: ${it.value}" }, enumValuesMap: Map<EnumMapEntry<Int>, E>, fromValueOrNullFn: (Int) -> E?, invalidArb: Arb<Int> = Arb.int()
.filterNot { output -> output in enumValuesMap.map { it.key.value } }): TestFactory
fun <E : Enum<E>> intEnumTests(describeName: String = "fromValueOrNull", dataNameFn: (Map.Entry<EnumMapEntry<Int>, E>) -> String = { "${it.key.propertyName}: ${it.value}" }, enumValuesMap: Map<KProperty0<Int>, E>, fromValueOrNullFn: (Int) -> E?, invalidArb: Arb<Int> = Arb.int()
.filterNot { output -> output in enumValuesMap.map { it.key.get() } }): TestFactory
Link copied to clipboard
Converts a Kotlin property (without a receiver) to its EnumMapEntry equivalent.
Converts a Kotlin property to its EnumMapEntry equivalent.