Kotlin
proptastic is a Java library, and Kotlin calls it as Java does: the same functions, the same failures, run by the same test frameworks. This page is what’s worth knowing when the tests are Kotlin’s.
A property in Kotlin
Section titled “A property in Kotlin”A Kotlin lambda is the check, and its parameter’s name is the one the failure prints:
import io.proptastic.Proptastic.forAllimport io.proptastic.Proptastic.integersimport io.proptastic.Proptastic.listsimport org.junit.jupiter.api.Assertions.assertEqualsimport org.junit.jupiter.api.Test
class SortTest { @Test fun sortKeepsSize() { forAll(lists(integers())).check { xs -> assertEquals(xs.size, sort(xs).size) } }}
/** A sort with a bug: a set drops repeated values. */fun sort(xs: List<Int>): List<Int> = xs.toSortedSet().toList()SortTest > sortKeepsSize() FAILED io.proptastic.core.PropertyFailure: expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'SortTest.sortKeepsSize'
Caused by: org.opentest4j.AssertionFailedError: expected: <2> but was: <1>The pin it line is Java source, .example(List.of(0, 0)), and it compiles in Kotlin as it
stands, as Kotlin calls Java’s List.of. With your test framework
has the setup for Gradle and Maven.
Data classes
Section titled “Data classes”any(…) draws a data class through its primary constructor, each property drawn by its type, as
it draws a Java record. To give a property its own domain, name it with .with(…), from
proptastic-kotlin:
fun orders(): Domain<Order> = any(Order::class.java) .with(Order::customer, elements("ada", "grace")) .with(Line::quantity, longs().atLeast(1).atMost(9)).with(…) reaches the data class wherever it’s drawn, however deep: here every line of every
order. It refuses a property that isn’t in the primary constructor, or a data class the type never
holds, naming it.
proptastic-kotlin is a jar of its own, beside proptastic, as the library itself needs no Kotlin.
It’s on Maven Central beside the library (Install).
Kotest
Section titled “Kotest”Kotest runs on the JUnit Platform, and a property inside a Kotest spec works as it does in JUnit.
proptastic names it by its spec’s file and line, as a Kotest test’s name isn’t on the stack, and
its rerun line runs the whole spec. With your test framework
shows a failure in each of Kotest’s styles.