Best Kotest code snippet using io.kotest.property.arbitrary.bind.Arb.Companion.bind
predef-test.kt
Source:predef-test.kt
1package arrow.fx.coroutines2import arrow.core.Either3import arrow.core.Validated4import arrow.core.ValidatedNel5import arrow.core.identity6import arrow.core.invalid7import arrow.core.invalidNel8import arrow.core.left9import arrow.core.right10import arrow.core.valid11import arrow.core.validNel12import io.kotest.assertions.fail13import io.kotest.matchers.Matcher14import io.kotest.matchers.MatcherResult15import io.kotest.matchers.equalityMatcher16import io.kotest.property.Arb17import io.kotest.property.arbitrary.bind18import io.kotest.property.arbitrary.char19import io.kotest.property.arbitrary.choice20import io.kotest.property.arbitrary.choose21import io.kotest.property.arbitrary.constant22import io.kotest.property.arbitrary.int23import io.kotest.property.arbitrary.list24import io.kotest.property.arbitrary.long25import io.kotest.property.arbitrary.map26import io.kotest.property.arbitrary.string27import kotlinx.coroutines.Dispatchers28import kotlinx.coroutines.flow.Flow29import kotlinx.coroutines.flow.asFlow30import kotlin.coroutines.Continuation31import kotlin.coroutines.CoroutineContext32import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED33import kotlin.coroutines.intrinsics.intercepted34import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn35import kotlin.coroutines.resume36import kotlin.coroutines.startCoroutine37import kotlinx.coroutines.channels.Channel38import kotlinx.coroutines.flow.buffer39import kotlinx.coroutines.flow.channelFlow40import kotlinx.coroutines.flow.emptyFlow41public data class SideEffect(var counter: Int = 0) {42 public fun increment() {43 counter++44 }45}46public fun <A> Arb.Companion.flow(arbA: Arb<A>): Arb<Flow<A>> =47 Arb.choose(48 10 to Arb.list(arbA).map { it.asFlow() },49 10 to Arb.list(arbA).map { channelFlow { it.forEach { send(it) } }.buffer(Channel.RENDEZVOUS) },50 1 to Arb.constant(emptyFlow()),51 )52public fun Arb.Companion.throwable(): Arb<Throwable> =53 Arb.string().map(::RuntimeException)54public fun <L, R> Arb.Companion.either(left: Arb<L>, right: Arb<R>): Arb<Either<L, R>> {55 val failure: Arb<Either<L, R>> = left.map { l -> l.left() }56 val success: Arb<Either<L, R>> = right.map { r -> r.right() }57 return Arb.choice(failure, success)58}59public fun <L, R> Arb.Companion.validated(left: Arb<L>, right: Arb<R>): Arb<Validated<L, R>> {60 val failure: Arb<Validated<L, R>> = left.map { l -> l.invalid() }61 val success: Arb<Validated<L, R>> = right.map { r -> r.valid() }62 return Arb.choice(failure, success)63}64public fun <L, R> Arb.Companion.validatedNel(left: Arb<L>, right: Arb<R>): Arb<ValidatedNel<L, R>> {65 val failure: Arb<ValidatedNel<L, R>> = left.map { l -> l.invalidNel() }66 val success: Arb<ValidatedNel<L, R>> = right.map { r -> r.validNel() }67 return Arb.choice(failure, success)68}69public fun Arb.Companion.intRange(min: Int = Int.MIN_VALUE, max: Int = Int.MAX_VALUE): Arb<IntRange> =70 Arb.bind(Arb.int(min, max), Arb.int(min, max)) { a, b ->71 if (a < b) a..b else b..a72 }73public fun Arb.Companion.longRange(min: Long = Long.MIN_VALUE, max: Long = Long.MAX_VALUE): Arb<LongRange> =74 Arb.bind(Arb.long(min, max), Arb.long(min, max)) { a, b ->75 if (a < b) a..b else b..a76 }77public fun Arb.Companion.charRange(): Arb<CharRange> =78 Arb.bind(Arb.char(), Arb.char()) { a, b ->79 if (a < b) a..b else b..a80 }81public fun <O> Arb.Companion.function(arb: Arb<O>): Arb<() -> O> =82 arb.map { { it } }83public fun Arb.Companion.unit(): Arb<Unit> =84 Arb.constant(Unit)85public fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> =86 arb.map { b: B -> { _: A -> b } }87public fun <A> Arb.Companion.nullable(arb: Arb<A>): Arb<A?> =88 Arb.Companion.choice(arb, arb.map { null })89/** Useful for testing success & error scenarios with an `Either` generator **/90public fun <A> Either<Throwable, A>.rethrow(): A =91 fold({ throw it }, ::identity)92public fun <A> Result<A>.toEither(): Either<Throwable, A> =93 fold({ a -> Either.Right(a) }, { e -> Either.Left(e) })94public suspend fun Throwable.suspend(): Nothing =95 suspendCoroutineUninterceptedOrReturn { cont ->96 suspend { throw this }.startCoroutine(97 Continuation(Dispatchers.Default) {98 cont.intercepted().resumeWith(it)99 }100 )101 COROUTINE_SUSPENDED102 }103public suspend fun <A> A.suspend(): A =104 suspendCoroutineUninterceptedOrReturn { cont ->105 suspend { this }.startCoroutine(106 Continuation(Dispatchers.Default) {107 cont.intercepted().resumeWith(it)108 }109 )110 COROUTINE_SUSPENDED111 }112public fun <A> A.suspended(): suspend () -> A =113 suspend { suspend() }114/**115 * Example usage:116 * ```kotlin117 * import arrow.fx.coroutines.assertThrowable118 *119 * fun main() {120 * val exception = assertThrowable<IllegalArgumentException> {121 * throw IllegalArgumentException("Talk to a duck")122 * }123 * require("Talk to a duck" == exception.message)124 * }125 * ```126 * <!--- KNIT example-predef-test-01.kt -->127 * @see Assertions.assertThrows128 */129public inline fun <A> assertThrowable(executable: () -> A): Throwable {130 val a = try {131 executable.invoke()132 } catch (e: Throwable) {133 e134 }135 return if (a is Throwable) a else fail("Expected an exception but found: $a")136}137public suspend fun CoroutineContext.shift(): Unit =138 suspendCoroutineUninterceptedOrReturn { cont ->139 suspend { this }.startCoroutine(140 Continuation(this) {141 cont.resume(Unit)142 }143 )144 COROUTINE_SUSPENDED145 }146public fun leftException(e: Throwable): Matcher<Either<Throwable, *>> =147 object : Matcher<Either<Throwable, *>> {148 override fun test(value: Either<Throwable, *>): MatcherResult =149 when (value) {150 is Either.Left -> when {151 value.value::class != e::class -> MatcherResult(152 false,153 "Expected exception of type ${e::class} but found ${value.value::class}",154 "Should not be exception of type ${e::class}"155 )156 value.value.message != e.message -> MatcherResult(157 false,158 "Expected exception with message ${e.message} but found ${value.value.message}",159 "Should not be exception with message ${e.message}"160 )161 else -> MatcherResult(162 true,163 "Expected exception of type ${e::class} and found ${value.value::class}",164 "Expected exception of type ${e::class} and found ${value.value::class}"165 )166 }167 is Either.Right -> MatcherResult(168 false,169 "Expected Either.Left with exception of type ${e::class} and found Right with ${value.value}",170 "Should not be Either.Left with exception"171 )172 }173 }174public fun <A> either(e: Either<Throwable, A>): Matcher<Either<Throwable, A>> =175 object : Matcher<Either<Throwable, A>> {176 override fun test(value: Either<Throwable, A>): MatcherResult =177 when (value) {178 is Either.Left -> when {179 value.value::class != (e.swap().orNull() ?: Int)::class -> MatcherResult(180 false,181 "Expected $e but found $value",182 "Should not be $e"183 )184 value.value.message != (e.swap().orNull()?.message ?: -1) -> MatcherResult(185 false,186 "Expected $e but found $value",187 "Should not be $e"188 )189 else -> MatcherResult(190 true,191 "Expected exception of type ${e::class} and found ${value.value::class}",192 "Expected exception of type ${e::class} and found ${value.value::class}"193 )194 }195 is Either.Right -> equalityMatcher(e).test(value)196 }197 }...
Generators.kt
Source:Generators.kt
1package arrow.core.test.generators2import arrow.core.Const3import arrow.core.Either4import arrow.core.Endo5import arrow.core.Eval6import arrow.core.Ior7import arrow.core.Option8import arrow.core.Tuple109import arrow.core.Tuple410import arrow.core.Tuple511import arrow.core.Tuple612import arrow.core.Tuple713import arrow.core.Tuple814import arrow.core.Tuple915import arrow.core.Validated16import arrow.core.left17import arrow.core.right18import arrow.core.toOption19import io.kotest.property.Arb20import io.kotest.property.arbitrary.bind21import io.kotest.property.arbitrary.boolean22import io.kotest.property.arbitrary.byte23import io.kotest.property.arbitrary.choice24import io.kotest.property.arbitrary.constant25import io.kotest.property.arbitrary.filter26import io.kotest.property.arbitrary.flatMap27import io.kotest.property.arbitrary.int28import io.kotest.property.arbitrary.long29import io.kotest.property.arbitrary.map30import io.kotest.property.arbitrary.numericDoubles31import io.kotest.property.arbitrary.numericFloats32import io.kotest.property.arbitrary.of33import io.kotest.property.arbitrary.orNull34import io.kotest.property.arbitrary.short35import io.kotest.property.arbitrary.string36import kotlin.Result.Companion.failure37import kotlin.Result.Companion.success38import kotlin.math.abs39public fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> =40 arb.map { b: B -> { _: A -> b } }41public fun <A> Arb.Companion.functionAAToA(arb: Arb<A>): Arb<(A, A) -> A> =42 arb.map { a: A -> { _: A, _: A -> a } }43public fun <A, B> Arb.Companion.functionBAToB(arb: Arb<B>): Arb<(B, A) -> B> =44 arb.map { b: B -> { _: B, _: A -> b } }45public fun <A, B> Arb.Companion.functionABToB(arb: Arb<B>): Arb<(A, B) -> B> =46 arb.map { b: B -> { _: A, _: B -> b } }47public fun <A> Arb.Companion.functionToA(arb: Arb<A>): Arb<() -> A> =48 arb.map { a: A -> { a } }49public fun Arb.Companion.throwable(): Arb<Throwable> =50 Arb.of(listOf(RuntimeException(), NoSuchElementException(), IllegalArgumentException()))51public fun <A> Arb.Companion.result(arbA: Arb<A>): Arb<Result<A>> =52 Arb.choice(arbA.map(::success), throwable().map(::failure))53public fun Arb.Companion.doubleSmall(): Arb<Double> =54 Arb.numericDoubles(from = 0.0, to = 100.0)55public fun Arb.Companion.floatSmall(): Arb<Float> =56 Arb.numericFloats(from = 0F, to = 100F)57public fun Arb.Companion.intSmall(factor: Int = 10000): Arb<Int> =58 Arb.int((Int.MIN_VALUE / factor)..(Int.MAX_VALUE / factor))59public fun Arb.Companion.byteSmall(): Arb<Byte> =60 Arb.byte(min = (Byte.MIN_VALUE / 10).toByte(), max = (Byte.MAX_VALUE / 10).toByte())61public fun Arb.Companion.shortSmall(): Arb<Short> {62 val range = (Short.MIN_VALUE / 1000)..(Short.MAX_VALUE / 1000)63 return Arb.short().filter { it in range }64}65public fun Arb.Companion.longSmall(): Arb<Long> =66 Arb.long((Long.MIN_VALUE / 100000L)..(Long.MAX_VALUE / 100000L))67public fun <A, B, C, D> Arb.Companion.tuple4(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>): Arb<Tuple4<A, B, C, D>> =68 Arb.bind(arbA, arbB, arbC, arbD, ::Tuple4)69public fun <A, B, C, D, E> Arb.Companion.tuple5(70 arbA: Arb<A>,71 arbB: Arb<B>,72 arbC: Arb<C>,73 arbD: Arb<D>,74 arbE: Arb<E>75): Arb<Tuple5<A, B, C, D, E>> =76 Arb.bind(arbA, arbB, arbC, arbD, arbE, ::Tuple5)77public fun <A, B, C, D, E, F> Arb.Companion.tuple6(78 arbA: Arb<A>,79 arbB: Arb<B>,80 arbC: Arb<C>,81 arbD: Arb<D>,82 arbE: Arb<E>,83 arbF: Arb<F>84): Arb<Tuple6<A, B, C, D, E, F>> =85 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, ::Tuple6)86public fun <A, B, C, D, E, F, G> Arb.Companion.tuple7(87 arbA: Arb<A>,88 arbB: Arb<B>,89 arbC: Arb<C>,90 arbD: Arb<D>,91 arbE: Arb<E>,92 arbF: Arb<F>,93 arbG: Arb<G>94): Arb<Tuple7<A, B, C, D, E, F, G>> =95 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, arbG, ::Tuple7)96public fun <A, B, C, D, E, F, G, H> Arb.Companion.tuple8(97 arbA: Arb<A>,98 arbB: Arb<B>,99 arbC: Arb<C>,100 arbD: Arb<D>,101 arbE: Arb<E>,102 arbF: Arb<F>,103 arbG: Arb<G>,104 arbH: Arb<H>105): Arb<Tuple8<A, B, C, D, E, F, G, H>> =106 Arb.bind(107 Arb.tuple7(arbA, arbB, arbC, arbD, arbE, arbF, arbG),108 arbH109 ) { (a, b, c, d, e, f, g), h ->110 Tuple8(a, b, c, d, e, f, g, h)111 }112public fun <A, B, C, D, E, F, G, H, I> Arb.Companion.tuple9(113 arbA: Arb<A>,114 arbB: Arb<B>,115 arbC: Arb<C>,116 arbD: Arb<D>,117 arbE: Arb<E>,118 arbF: Arb<F>,119 arbG: Arb<G>,120 arbH: Arb<H>,121 arbI: Arb<I>122): Arb<Tuple9<A, B, C, D, E, F, G, H, I>> =123 Arb.bind(124 Arb.tuple8(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH),125 arbI126 ) { (a, b, c, d, e, f, g, h), i ->127 Tuple9(a, b, c, d, e, f, g, h, i)128 }129public fun <A, B, C, D, E, F, G, H, I, J> Arb.Companion.tuple10(130 arbA: Arb<A>,131 arbB: Arb<B>,132 arbC: Arb<C>,133 arbD: Arb<D>,134 arbE: Arb<E>,135 arbF: Arb<F>,136 arbG: Arb<G>,137 arbH: Arb<H>,138 arbI: Arb<I>,139 arbJ: Arb<J>140): Arb<Tuple10<A, B, C, D, E, F, G, H, I, J>> =141 Arb.bind(142 Arb.tuple9(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH, arbI),143 arbJ144 ) { (a, b, c, d, e, f, g, h, i), j ->145 Tuple10(a, b, c, d, e, f, g, h, i, j)146 }147public fun Arb.Companion.nonZeroInt(): Arb<Int> = Arb.int().filter { it != 0 }148public fun Arb.Companion.intPredicate(): Arb<(Int) -> Boolean> =149 Arb.nonZeroInt().flatMap { num ->150 val absNum = abs(num)151 Arb.of(152 listOf<(Int) -> Boolean>(153 { it > num },154 { it <= num },155 { it % absNum == 0 },156 { it % absNum == absNum - 1 }157 )158 )159 }160public fun <A> Arb.Companion.endo(arb: Arb<A>): Arb<Endo<A>> = arb.map { a: A -> Endo<A> { a } }161public fun <B> Arb.Companion.option(arb: Arb<B>): Arb<Option<B>> =162 arb.orNull().map { it.toOption() }163public fun <E, A> Arb.Companion.either(arbE: Arb<E>, arbA: Arb<A>): Arb<Either<E, A>> {164 val arbLeft = arbE.map { Either.Left(it) }165 val arbRight = arbA.map { Either.Right(it) }166 return Arb.choice(arbLeft, arbRight)167}168public fun <E, A> Arb<E>.or(arbA: Arb<A>): Arb<Either<E, A>> = Arb.either(this, arbA)169public fun <E, A> Arb.Companion.validated(arbE: Arb<E>, arbA: Arb<A>): Arb<Validated<E, A>> =170 Arb.either(arbE, arbA).map { Validated.fromEither(it) }171public fun Arb.Companion.unit(): Arb<Unit> =172 Arb.constant(Unit)173public fun <A, B> Arb.Companion.ior(arbA: Arb<A>, arbB: Arb<B>): Arb<Ior<A, B>> =174 arbA.alignWith(arbB) { it }175public fun <A, B> Arb.Companion.arbConst(arb: Arb<A>): Arb<Const<A, B>> =176 arb.map { Const<A, B>(it) }177public fun <A> Arb<A>.eval(): Arb<Eval<A>> =178 map { Eval.now(it) }179private fun <A, B, R> Arb<A>.alignWith(arbB: Arb<B>, transform: (Ior<A, B>) -> R): Arb<R> =180 Arb.bind(this, arbB) { a, b -> transform(Ior.Both(a, b)) }181public fun Arb.Companion.suspendFunThatReturnsEitherAnyOrAnyOrThrows(): Arb<suspend () -> Either<Any, Any>> =182 choice(183 suspendFunThatReturnsAnyRight(),184 suspendFunThatReturnsAnyLeft(),185 suspendFunThatThrows()186 )187public fun Arb.Companion.suspendFunThatReturnsAnyRight(): Arb<suspend () -> Either<Any, Any>> =188 any().map { suspend { it.right() } }189public fun Arb.Companion.suspendFunThatReturnsAnyLeft(): Arb<suspend () -> Either<Any, Any>> =190 any().map { suspend { it.left() } }191public fun Arb.Companion.suspendFunThatThrows(): Arb<suspend () -> Either<Any, Any>> =192 throwable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>193public fun Arb.Companion.any(): Arb<Any> =194 choice(195 Arb.string() as Arb<Any>,196 Arb.int() as Arb<Any>,197 Arb.long() as Arb<Any>,198 Arb.boolean() as Arb<Any>,199 Arb.throwable() as Arb<Any>,200 Arb.unit() as Arb<Any>201 )...
UnitSpec.kt
Source:UnitSpec.kt
1package arrow.core.test2import arrow.core.NonEmptyList3import arrow.core.Tuple44import arrow.core.Tuple55import arrow.core.test.generators.unit6import arrow.core.test.laws.Law7import io.kotest.core.names.TestName8import io.kotest.core.spec.style.StringSpec9import io.kotest.property.Arb10import io.kotest.property.Gen11import io.kotest.property.PropertyContext12import io.kotest.property.arbitrary.bind13import io.kotest.property.arbitrary.filter14import io.kotest.property.arbitrary.map15import io.kotest.property.arbitrary.list as KList16import io.kotest.property.arbitrary.map as KMap17import io.kotest.property.checkAll18import kotlin.jvm.JvmOverloads19import kotlin.math.max20/**21 * Base class for unit tests22 */23public abstract class UnitSpec(24 public val iterations: Int = 250,25 public val maxDepth: Int = 15,26 spec: UnitSpec.() -> Unit = {}27) : StringSpec() {28 public constructor(spec: UnitSpec.() -> Unit) : this(250, 15, spec)29 public fun <A> Arb.Companion.list(gen: Gen<A>, range: IntRange = 0..maxDepth): Arb<List<A>> =30 Arb.KList(gen, range)31 public fun <A> Arb.Companion.nonEmptyList(arb: Arb<A>, depth: Int = maxDepth): Arb<NonEmptyList<A>> =32 Arb.list(arb, 1..max(1, depth)).filter(List<A>::isNotEmpty).map(NonEmptyList.Companion::fromListUnsafe)33 public fun <A> Arb.Companion.sequence(arbA: Arb<A>, range: IntRange = 0..maxDepth): Arb<Sequence<A>> =34 Arb.list(arbA, range).map { it.asSequence() }35 @JvmOverloads36 public inline fun <reified A> Arb.Companion.array(gen: Arb<A>, range: IntRange = 0..maxDepth): Arb<Array<A>> =37 Arb.list(gen, range).map { it.toTypedArray() }38 public fun <K, V> Arb.Companion.map(39 keyArb: Arb<K>,40 valueArb: Arb<V>,41 minSize: Int = 1,42 maxSize: Int = 1543 ): Arb<Map<K, V>> =44 Arb.KMap(keyArb, valueArb, minSize = minSize, maxSize = maxSize)45 init {46 spec()47 }48 public fun testLaws(vararg laws: List<Law>): Unit = laws49 .flatMap { list: List<Law> -> list.asIterable() }50 .distinctBy { law: Law -> law.name }51 .forEach { law: Law ->52 registration().addTest(TestName(law.name), xdisabled = false, law.test)53 }54 public fun testLaws(prefix: String, vararg laws: List<Law>): Unit = laws55 .flatMap { list: List<Law> -> list.asIterable() }56 .distinctBy { law: Law -> law.name }57 .forEach { law: Law ->58 registration().addTest(TestName(prefix, law.name, true), xdisabled = false, law.test)59 }60 public suspend fun checkAll(property: suspend PropertyContext.() -> Unit): PropertyContext =61 checkAll(iterations, Arb.unit()) { property() }62 public suspend fun <A> checkAll(63 genA: Arb<A>,64 property: suspend PropertyContext.(A) -> Unit65 ): PropertyContext =66 checkAll(67 iterations,68 genA,69 property70 )71 public suspend fun <A, B> checkAll(72 genA: Arb<A>,73 genB: Arb<B>,74 property: suspend PropertyContext.(A, B) -> Unit75 ): PropertyContext =76 checkAll(77 iterations,78 genA,79 genB,80 property81 )82 public suspend fun <A, B, C> checkAll(83 genA: Arb<A>,84 genB: Arb<B>,85 genC: Arb<C>,86 property: suspend PropertyContext.(A, B, C) -> Unit87 ): PropertyContext =88 checkAll(89 iterations,90 genA,91 genB,92 genC,93 property94 )95 public suspend fun <A, B, C, D> checkAll(96 genA: Arb<A>,97 genB: Arb<B>,98 genC: Arb<C>,99 genD: Arb<D>,100 property: suspend PropertyContext.(A, B, C, D) -> Unit101 ): PropertyContext =102 checkAll(103 iterations,104 genA,105 genB,106 genC,107 genD,108 property109 )110 public suspend fun <A, B, C, D, E> checkAll(111 genA: Arb<A>,112 genB: Arb<B>,113 genC: Arb<C>,114 genD: Arb<D>,115 genE: Arb<E>,116 property: suspend PropertyContext.(A, B, C, D, E) -> Unit117 ): PropertyContext =118 checkAll(119 iterations,120 genA,121 genB,122 genC,123 genD,124 genE,125 property126 )127 public suspend fun <A, B, C, D, E, F> checkAll(128 genA: Arb<A>,129 genB: Arb<B>,130 genC: Arb<C>,131 genD: Arb<D>,132 genE: Arb<E>,133 genF: Arb<F>,134 property: suspend PropertyContext.(A, B, C, D, E, F) -> Unit135 ): PropertyContext =136 checkAll(137 iterations,138 genA,139 genB,140 genC,141 genD,142 genE,143 genF,144 property145 )146 public suspend fun <A, B, C, D, E, F, G> checkAll(147 gena: Arb<A>,148 genb: Arb<B>,149 genc: Arb<C>,150 gend: Arb<D>,151 gene: Arb<E>,152 genf: Arb<F>,153 geng: Arb<G>,154 fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G) -> Unit155 ) {156 checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, ::Pair)) { a, b, c, d, e, (f, g) ->157 fn(a, b, c, d, e, f, g)158 }159 }160 public suspend fun <A, B, C, D, E, F, G, H> checkAll(161 gena: Arb<A>,162 genb: Arb<B>,163 genc: Arb<C>,164 gend: Arb<D>,165 gene: Arb<E>,166 genf: Arb<F>,167 geng: Arb<G>,168 genh: Arb<H>,169 fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) -> Unit170 ) {171 checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, genh, ::Triple)) { a, b, c, d, e, (f, g, h) ->172 fn(a, b, c, d, e, f, g, h)173 }174 }175 public suspend fun <A, B, C, D, E, F, G, H, I> checkAll(176 gena: Arb<A>,177 genb: Arb<B>,178 genc: Arb<C>,179 gend: Arb<D>,180 gene: Arb<E>,181 genf: Arb<F>,182 geng: Arb<G>,183 genh: Arb<H>,184 geni: Arb<I>,185 fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) -> Unit186 ) {187 checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, genh, geni, ::Tuple4)) { a, b, c, d, e, (f, g, h, i) ->188 fn(a, b, c, d, e, f, g, h, i)189 }190 }191 public suspend fun <A, B, C, D, E, F, G, H, I, J> checkAll(192 gena: Arb<A>,193 genb: Arb<B>,194 genc: Arb<C>,195 gend: Arb<D>,196 gene: Arb<E>,197 genf: Arb<F>,198 geng: Arb<G>,199 genh: Arb<H>,200 geni: Arb<I>,201 genj: Arb<J>,202 fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) -> Unit203 ) {204 checkAll(205 gena,206 genb,207 genc,208 gend,209 gene,210 Arb.bind(genf, geng, genh, geni, genj, ::Tuple5)211 ) { a, b, c, d, e, (f, g, h, i, j) ->212 fn(a, b, c, d, e, f, g, h, i, j)213 }214 }215 public suspend fun forFew(216 iterations: Int,217 property: suspend PropertyContext.(Unit) -> Unit218 ): PropertyContext =219 checkAll(220 iterations,221 Arb.unit(),222 property223 )224}...
CashRegisterTest.kt
Source:CashRegisterTest.kt
1package exercies.chapter12import io.kotest.core.spec.style.FunSpec3import io.kotest.matchers.shouldBe4import io.kotest.property.Arb5import io.kotest.property.Exhaustive6import io.kotest.property.arbitrary.arbitrary7import io.kotest.property.arbitrary.bind8import io.kotest.property.arbitrary.double9import io.kotest.property.arbitrary.map10import io.kotest.property.arbitrary.of11import io.kotest.property.checkAll12import io.kotest.property.exhaustive.boolean13class CashRegisterTest : FunSpec(14 {15 context("No item purchased, costs nothing") {16 checkAll(Arb.itemConfiguration()) { config ->17 CashRegister.of(config).checkout(emptyList()) shouldBe PayableAmount(0.0)18 }19 }20 context("Uses discount when purchasing the amount to get") {21 checkAll(22 Arb.itemConfiguration(),23 Exhaustive.boolean()24 ) { (item, price, discount), randomItemIncluded ->25 val randomItem = Item("jukebox")26 val register = CashRegister(item, price, discount) + ItemConfiguration(27 randomItem,28 PayableAmount(0.1),29 Discount(1, 1)30 )31 val purchasesToUsePromotion = (1..discount.get).map { item }32 // Non-promotion item in the middle of things should not affect other promotions33 val purchases =34 if (!randomItemIncluded) purchasesToUsePromotion35 else listOf(item, randomItem) + item * (discount.get - 1)36 register.checkout(purchases) shouldBe PayableAmount(discount.payFor * price.value + if (randomItemIncluded) 0.1 else 0.0)37 }38 }39 context("Buy less than promotion, pay full price") {40 checkAll(Arb.itemConfiguration()) { (item, price, discount) ->41 CashRegister(item, price, discount)42 .checkout(item * (discount.get - 1)) shouldBe PayableAmount((discount.get - 1) * price.value)43 }44 }45 context("Use promotion and then buy one extra") {46 checkAll(Arb.itemConfiguration()) { (item, price, discount) ->47 CashRegister(item, price, discount)48 .checkout(item * (discount.get + 1)) shouldBe PayableAmount((discount.payFor + 1) * price.value)49 }50 }51 }52)53val items =54 listOf(55 Item("milk"),56 Item("eggs"),57 Item("cheese"),58 Item("coffee"),59 Item("mustard"),60 Item("ham"),61 )62fun Arb.Companion.item(excludedItems: Set<Item> = emptySet()) = Arb.of(items - excludedItems)63fun Arb.Companion.price() = Arb.double(0.1, 100.0).map { PayableAmount(it) }64fun Arb.Companion.discount() = arbitrary { rs ->65 val payFor = rs.random.nextInt(1, 20)66 val get = rs.random.nextInt(payFor + 1, 40)67 Discount(get, payFor)68}69fun Arb.Companion.itemConfiguration() =70 Arb.bind(Arb.item(), Arb.price(), Arb.discount()) { item, price, discount ->71 ItemConfiguration(item, price, discount)72 }...
StreamSpec.kt
Source:StreamSpec.kt
1package arrow.fx.coroutines.stream2import arrow.fx.coroutines.ArrowFxSpec3import arrow.fx.coroutines.suspend4import io.kotest.property.Arb5import io.kotest.property.Shrinker6import io.kotest.property.arbitrary.arb7import io.kotest.property.arbitrary.bind8import io.kotest.property.arbitrary.choice9import io.kotest.property.arbitrary.choose10import io.kotest.property.arbitrary.constant11import io.kotest.property.arbitrary.list12import io.kotest.property.arbitrary.map13import kotlin.math.abs14/**15 * A Spec that allows you to specify depth for all `Arb` used inside the spec.16 *17 * A `Int.Range` of `0..10` is equivalent to `Arb.list` as where it generates `0..100` by default.18 *19 * `Stream` is randomly generated among it's constructors, but it guarantees a depth of maxmimum `10x range.last`.20 * So for `0..10` it will generate at most a `Stream` with `10` `Chunk`s of `10` elements.21 */22abstract class StreamSpec(23 iterations: Int = 350,24 val depth: IntRange = 0..100,25 spec: StreamSpec.() -> Unit = {}26) : ArrowFxSpec(iterations) {27 init {28 spec()29 }30 fun Arb.Companion.long(range: LongRange = Long.MIN_VALUE..Long.MAX_VALUE): Arb<Long> {31 val edgecases = listOf(0L, 1, -1, Long.MAX_VALUE, Long.MIN_VALUE).filter { it in range }32 return arb(LongShrinker(range), edgecases) { it.random.nextLong(range.first, range.last) }33 }34 class LongShrinker(private val range: LongRange) : Shrinker<Long> {35 override fun shrink(value: Long): List<Long> =36 when (value) {37 0L -> emptyList()38 1L, -1L -> listOf(0)39 else -> {40 val a = listOf(abs(value), value / 3, value / 2, value * 2 / 3)41 val b = (1..5L).map { value - it }.reversed().filter { it > 0 }42 (a + b).distinct().filter { it in range && it != value }43 }44 }45 }46 inline fun <reified O, R> Arb.Companion.pull(47 arbO: Arb<O>,48 arbR: Arb<R>,49 range: IntRange = depth50 ): Arb<Pull<O, R>> =51 Arb.choice<Pull<O, R>>(52 Arb.bind(Arb.stream(arbO, range), arbR) { s, r ->53 s.asPull().map { r }54 },55 arbR.map { Pull.just(it) } as Arb<Pull<O, R>>,56 arbR.map { Pull.effect { it } }57 )58 fun <O> Arb.Companion.stream(59 arb: Arb<O>,60 range: IntRange = depth61 ): Arb<Stream<O>> =62 Arb.choose(63 10 to Arb.list(arb, range).map { os ->64 Stream.iterable(os)65 },66 10 to Arb.list(arb, range).map { os ->67 Stream.iterable(os).unchunk()68 },69 5 to arb.map { fo -> Stream.effect { fo } },70 1 to Arb.bind(Arb.suspended(arb), Arb.list(arb, range), Arb.suspended(Arb.constant(Unit))) { acquire, use, release ->71 Stream.bracketCase(acquire, { _, _ -> release.invoke() }).flatMap { Stream.iterable(use) }72 }73 )74 fun <O> Arb.Companion.suspended(arb: Arb<O>): Arb<suspend () -> O> =75 arb.map { suspend { it.suspend() } }76}...
addresses.kt
Source:addresses.kt
1package io.kotest.property.arbs.places2import io.kotest.property.Arb3import io.kotest.property.arbitrary.bind4import io.kotest.property.arbitrary.element5import io.kotest.property.arbitrary.int6import io.kotest.property.arbs.colours7private val endings = listOf(8 "Avenue",9 "Road",10 "Street",11 "Place",12 "Terrace",13 "Drive",14 "Court",15 "Way",16 "Grove",17 "Walk",18 "Crescent",19 "Mews",20 "Close",21 "Rise",22 "Row",23 "Wynd",24 "Meadow",25 "Park",26 "End",27 "Cross",28)29private val trees = listOf(30 "Chestnut",31 "Locust",32 "Cherry",33 "Oak",34 "Elm",35 "Birch",36 "River Birch",37 "Pinoak",38 "Crabapple",39 "Vine",40 "Spruce",41 "Horse Chestnut",42 "Pine",43 "Sycamore",44 "Walnut",45 "Fir",46)47private fun Arb.Companion.trees(): Arb<String> = Arb.element(trees)48private fun Arb.Companion.endings(): Arb<String> = Arb.element(endings)49private fun Arb.Companion.cols(): Arb<String> = Arb.element(colours)50fun Arb.Companion.addresses() = Arb.bind(51 Arb.cols(),52 Arb.trees(),53 Arb.endings(),54 Arb.int(1..9999)55) { color, name, ending, number ->56 "$number $color $name $ending".uppercase()57}...
PersonGenerator.kt
Source:PersonGenerator.kt
1package property.based.testing2import io.kotest.property.Arb3import io.kotest.property.arbitrary.Codepoint4import io.kotest.property.arbitrary.ascii5import io.kotest.property.arbitrary.bind6import io.kotest.property.arbitrary.cyrillic7import io.kotest.property.arbitrary.hebrew8import io.kotest.property.arbitrary.int9import io.kotest.property.arbitrary.katakana10import io.kotest.property.arbitrary.merge11import io.kotest.property.arbitrary.string12fun Arb.Companion.person(): Arb<Person> =13 Arb.bind(14 Arb.string(15 minSize = 1, codepoints = Codepoint.ascii()16 .merge(Codepoint.katakana())17 .merge(Codepoint.hebrew())18 .merge(Codepoint.cyrillic())19 ),20 Arb.address()21 ) { name, address ->22 Person(name, address)23 }24fun Arb.Companion.address(): Arb<Address> =25 Arb.bind(26 Arb.string(minSize = 1),27 Arb.string(minSize = 1),28 Arb.int(0..20000)29 ) { street, town, zip ->30 Address(street, town, zip)31 }...
chess.kt
Source:chess.kt
1package io.kotest.property.arbs.games2import io.kotest.property.Arb3import io.kotest.property.arbitrary.arbitrary4import io.kotest.property.arbitrary.bind5import io.kotest.property.arbitrary.of6import io.kotest.property.arbitrary.orNull7data class ChessPiece(val name: String, val points: Int) {8 companion object {9 val all = listOf(10 ChessPiece("Pawn", 1),11 ChessPiece("Bishop", 5),12 ChessPiece("Knight", 5),13 ChessPiece("Rook", 5),14 ChessPiece("Queen", 9),15 ChessPiece("King", 4),16 )17 }18}19fun Arb.Companion.chessSquare(): Arb<String> = arbitrary {20 ('A' + it.random.nextInt(0, 8)).toString() + it.random.nextInt(1, 9)21}22fun Arb.Companion.chessPiece() = Arb.of(ChessPiece.all)23data class ChessMove(val from: String, val to: String, val capture: ChessPiece?)24fun Arb.Companion.chessMove(): Arb<ChessMove> = Arb.bind(25 Arb.chessSquare(),26 Arb.chessSquare(),27 Arb.chessPiece().orNull(0.9)28) { from, to, capture ->29 ChessMove(from, to, capture)30}...
Arb.Companion.bind
Using AI Code Generation
1import io.kotest.property.arbitrary.bind2import io.kotest.property.arbitrary.int3import io.kotest.property.arbitrary.string4import io.kotest.property.arbitrary.withEdgecases5val arb = Arb.int().withEdgecases(1, 2, 3).bind { i -> Arb.string().withEdgecases(i.toString()) }6import io.kotest.property.arbitrary.bind7import io.kotest.property.arbitrary.int8import io.kotest.property.arbitrary.string9import io.kotest.property.arbitrary.withEdgecases10val arb = Arb.int().withEdgecases(1, 2, 3).bind { i -> Arb.string().withEdgecases(i.toString()) }11import io.kotest.property.arbitrary.bind12import io.kotest.property.arbitrary.int13import io.kotest.property.arbitrary.string14import io.kotest.property.arbitrary.withEdgecases15val arb = Arb.int().withEdgecases(1, 2, 3).bind { i -> Arb.string().withEdgecases(i.toString()) }16import io.kotest.property.arbitrary.bind17import io.kotest.property.arbitrary.int18import io.kotest.property.arbitrary.string19import io.kotest.property.arbitrary.withEdgecases20val arb = Arb.int().withEdgecases(1, 2, 3).bind { i -> Arb.string().withEdgecases(i.toString()) }
Arb.Companion.bind
Using AI Code Generation
1fun <A, B> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>): Arb<Pair<A, B>> =2 arbA.flatMap { a -> arbB.map { b -> Pair(a, b) } }3val arbPair = Arb.bind(arbInt, arbString)4val gen = arbPair.generator(1000)5for (i in 1..10) {6 println(gen.next().value)7}
Arb.Companion.bind
Using AI Code Generation
1 fun testArbBind() {2 checkAll<String, String, String> { a, b, c ->3 a + b + c == a.bind { a -> b.bind { b -> c.map { c -> a + b + c } } }4 }5 }6 fun testArbBind2() {7 checkAll<String, String, String> { a, b, c ->8 a + b + c == a.bind { a -> b.bind { b -> c.map { c -> a + b + c } } }9 }10 }11 fun testArbMap() {12 checkAll<String, String, String> { a, b, c ->13 a + b + c == a.map { a -> a + b + c }14 }15 }16 fun testArbMap2() {17 checkAll<String, String, String> { a, b, c ->18 a + b + c == a.map { a -> a + b + c }19 }20 }21 fun testArbMap3() {22 checkAll<String, String, String> { a, b, c ->23 a + b + c == a.map { a -> a + b + c }24 }25 }26}
Arb.Companion.bind
Using AI Code Generation
1val arbPair: Arb<Pair<Int, String>> = Arb.bind(Arb.int(), Arb.string())2val arbTriple: Arb<Triple<Int, String, Boolean>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool())3val arbQuadruple: Arb<Quadruple<Int, String, Boolean, Long>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool(), Arb.long())4val arbQuintuple: Arb<Quintuple<Int, String, Boolean, Long, Double>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool(), Arb.long(), Arb.double())5val arbSextuple: Arb<Sextuple<Int, String, Boolean, Long, Double, Float>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool(), Arb.long(), Arb.double(), Arb.float())6val arbSeptuple: Arb<Septuple<Int, String, Boolean, Long, Double, Float, Char>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool(), Arb.long(), Arb.double(), Arb.float(), Arb.char())7val arbOctuple: Arb<Octuple<Int, String, Boolean, Long, Double, Float, Char, Short>> = Arb.bind(Arb.int(), Arb.string(), Arb.bool(), Arb.long(), Arb.double(), Arb.float(), Arb.char(), Arb.short())
Arb.Companion.bind
Using AI Code Generation
1val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }2val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }3val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }4val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }5val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }6val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }7val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb . checkAll { sum -> sum > 0 }8val arb = Arb.bind( Arb.int( 1 .. 100 ) , Arb.int( 1 .. 100 ) ) { a , b -> a + b } arb
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!